Wireless communication between PC and smart car based on Matlab GUI

Publisher:QingfangLatest update time:2013-12-14 Keywords:Matlab Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

introduction

  In the Fencar smart car competition where speed determines the superiority, any factor may affect the operation of the car. In order to meet the needs of accurate analysis of various states of the car on the track, the data collection and processing of the host computer requires high communication baud rate, stable signal transmission, no packet loss, large data processing volume, and timely data processing. In the serial communication based on the Matlab environment, the non-real-time serial communication technology under the query mode has great limitations for the communication of smart cars with high real-time requirements. Although the communication under the interrupt mechanism currently used can meet the needs of real-time, it requires modifying the instrcallback callback function that comes with Matlab. After the callback function is modified during program debugging, Matlab needs to be restarted to make the new callback function file effective. It can be seen from this that its operation is complex and error-prone, and the data received by the instrcallback function and saved in the workspace cannot be called in real time by the function of the GUI component. Therefore, this method often generates multiple scattered graphics windows when multiple sets of data need to be plotted and analyzed separately. In order to meet the real-time communication needs of smart cars and make the communication interface based on Matlab GUI more concise, this paper proposes a way to write event interrupt function into GUI component function.

  Matlab is a powerful software system developed by Mathworks, an American company, for theoretical analysis research, engineering design processing and drawing. Matlab is stable, reliable and easy to use, and is a powerful assistant for scientific researchers in scientific research. Starting from Matlab 6.0, Mathworks has added an instrument control toolbox to the software, providing official support for serial communication of RS-232/RS-485 communication standards. Matlab GUI (Graphic User Interface) is a module built into Matlab for graphical interface development. This article mainly introduces the method of real-time communication between the host computer and the smart car in the Mariab GUI environment. Compared with programming in VC/VB high-level languages, this method

  The development time is greatly reduced and the development efficiency is improved. In addition, compared with the traditional RS-232 interface, the Bluetooth interface can transmit and collect data in a wider range and in all directions. The experimental results show that after two handshakes, the data transmission is real-time and stable, which is very practical.

  1 Overall system design

  The system uses Fyncar MCA8HCS12G128 microcontroller as the controller of the smart car, and PC as the host computer. Matlab GUI is used to build a platform for real-time communication, data processing and drawing between the two. The principle block diagram of the whole system is shown in Figure 1, which introduces the method of establishing a platform for real-time communication and drawing between the host computer and the smart car based on Matlab GUI and serial communication mechanism.

  System block diagram

  The smart car is connected to the PC via Bluetooth. When the system is working, Matlab calls the serial class function in the device control toolbox to set the serial port properties and create a serial port object, thereby realizing Matlab's control and read and write operations on the serial port. During the debugging of the smart car, special instructions can be sent to Bluetooth through Matlab to start the smart car. After the smart car system is running, the collected position and speed equivalent data can be sent back to the PC via Bluetooth.

  Matlab receives data in real time through interruption, analyzes and processes the data, and draws corresponding graphics in real time.

  2 Introduction to Matlab GUI Design

  ① After running Matlab, click the GUI icon in the upper left corner of the main interface to run the Matlab GUI development tool.

  ②Click the Blank GUI (Default) option and click the OK button to complete the creation of the new Blank GUI.

  ③ Add the elements required for the design in the right menu of the newly created Blank GUI interface to complete the GUI interface design of serial port communication and real-time data display and other functions.

  ④ Write the corresponding response control code of the GUI. The communication simulation interface after running is shown in Figure 2.

  Communication simulation interface after running

  3 Serial communication between MCU and PC

  3.1 Design Concept

  The software design of the MCA8HCS12G128 microcontroller needs to implement functions such as program initialization, receiving and sending serial port signals, and speed and position data collection. In order to ensure the reliability and stability of the data collected by the host computer and prevent packet loss, the handshake method of both parties is agreed upon during the communication process. After the program is initialized, wait for the handshake signal 0xFF sent by the fwrite command. When the signal received by the microcontroller for the first time is 0xFF, the handshake is successful and the subroutines such as the motor and laser head are started. During the experiment, it was found that the first byte of the first group of data sent was lost. In order to enhance the reliability of the data, first determine whether the first two bytes of the data packet are 0xFF. If so, remove the corresponding bytes, separate the valid data, and then embed the two-byte handshake signal 0xFF in the returned speed and laser sensor data. Matlab receives the serial port data and performs corresponding processing. The overall program flow of the intelligent vehicle controller communication is shown in Figure 3.

  The overall program flow of smart car controller communication

  3.2 System Bluetooth serial communication main code

  

4 Communication between PC and smart car based on Matlab GUI

  4.1 Software design for serial communication based on Matlab GUI

  In the Matlab environment, there are two ways to read serial port data: query and interrupt. In the query mode, data can only be transmitted in batches, which is not very real-time and occupies a lot of system resources. The usual way to handle serial port communication implemented in interrupt mode is to modify the instrcallback callback function provided by Matlab and use an event-driven method to achieve the purpose of real-time processing of data transmitted by the lower computer. However, programming using Matlab's event and callback function mechanism is relatively complicated, and it is easy to make mistakes and is troublesome when modifying and backing up the instreallback function. In addition, Matlab must be restarted every time the instreallbaek function is modified, which may cause some unnecessary troubles such as data loss. Writing the event-driven function into the M file of the GUI component can reduce the complexity of the program, avoid some unnecessary troubles, and realize real-time communication between serial ports.

  4.2 Introduction to the Device Control Toolbox

  The Matlab device control toolbox mainly provides direct communication between Matlab software and various intelligent instruments. The toolbox provides the following functions:

  ①Support serial interface (RS-232/RS-422/RS-485), GPIB interface (IEEE 488) and other communication protocols.

  ②Supports IVI, VXI plug&play and Matlab instrument drivers, so you can directly connect to the instrument without having to learn how to write device-related instructions.

  ③The transmitted data can be text or binary (digital).

  ④Support synchronous and asynchronous (blocking or non-blocking) communication.

  ⑤Support event-based communication.

  ⑥The new TMTOOL graphical user interface can not only connect to instruments and receive/send data, but also automatically generate M-code files to be added to the written Matlab program.

  5 Asynchronous serial communication based on Matlab GUI query method

  Although the non-real-time serial communication based on Matlab query method is simple to program and easier to call and process data, it can only receive data of one process, which often cannot meet the needs of designers in systems with high real-time requirements. The following is part of the communication program code.

  

6 Realizing real-time serial communication based on Matlab interrupt method

  The advantage of the event-driven method is that the program responds promptly and has high reliability. The interrupt method for implementing real-time communication in the Matlab environment is actually implemented in an event-driven manner, which is similar to the implementation method of the OnComm event in the MSComm control of the VB language. When the serial port monitors that the buffer has a specified number of bytes of data available (bytes-available event), the data received by the serial port is inactive for a long time (break-interrupt event), the serial port pin status changes (pin-status event) or the output buffer is empty (output empty event), Matlab will automatically call the callback function to process the communication event. Therefore, event-driven is essentially an interrupt mechanism, and the callback function is essentially equivalent to an interrupt service subroutine. The program flow of real-time serial communication on the Matlab side is shown in Figure 4. The following are two different ways to implement the interrupt mechanism program for real-time communication.

  Program flow of real-time serial communication on Matlab side

  6.1 Write the interrupt service subroutine into the M file code of the GUI component

  The code for the main program to create a serial device object, set serial device properties, open the serial port, and other initialization operations is the same as the initialization code in the query mode:

  

  6.2 Implementing interruption by calling the modified instroallback.m file

  Main program:

  Modify the instrcallback (obj, event) callback function. The instrcallback.m file contains the callback function program template provided by Matlab. Designers can add corresponding service program codes as needed. There are two instrcallback.m files in the Matlab installation directory. You only need to modify the instrcallback.m file in the \MATLAB\toolbox\matlab\iofun\@instrument directory. In addition, before modifying the instrcallback.m file, it is best to make a backup of it. After modifying the instrcallback (obj, event) callback function, you need to save it and restart Matlab to configure the file so that the modified function can take effect.

  Real-time display of the speed of the smart car starting process

  FIG5 is a real-time display of the speed of the smart car startup process implemented in the following manner. The main program code of the modified instrcallback.m file is as follows:

  

  Experiments have shown that the real-time serial port communication between the smart car and the host computer based on Matlab event-driven can well meet the needs of the smart car debugging process, and both the communication methods of writing the interrupt service subroutine into the GUI component and modifying the instrcallback.m file are stable and reliable.

  Conclusion

  In the Matlab environment, the two interrupt mechanisms for achieving real-time communication between the smart car and the PC can both transmit data in real time and stably. However, the method of writing the interrupt service subroutine into the GUI component is simple to program, convenient to process data, easier to implement, and the development efficiency is relatively improved. Practice has proved that this method is feasible. It can not only be applied to the smart car system in this article, but also can be widely used in other instruments and equipment with serial communication capabilities.

Keywords:Matlab Reference address:Wireless communication between PC and smart car based on Matlab GUI

Previous article:ADI Lab Circuit: Flexible IF-to-Baseband Receiver Solution
Next article:ADI Lab Circuit: Complete HART-Compliant 4mA to 20mA Solution (Part 1)

Recommended ReadingLatest update time:2024-11-15 16:50

Serial communication programming between PC and 89C51 microcontroller
1. The serial communication programming process on the PC is as follows: 1. Create a project Open VC++6.0 and create a dialog-based MFC application SCommTest (which is consistent with my source code and will be more convenient for you later); 2. Insert the MSComm control into the project   Select the Components
[Microcontroller]
Serial communication programming between PC and 89C51 microcontroller
ARM abnormal return PC address analysis
Important basics: R15 (PC) always points to the instruction being fetched, and It does not refer to the instruction being "executed" or the instruction being "decoded". In general, People usually agree to use "the instructions being executed as the reference point", which is called the current The first instructio
[Microcontroller]
Gartner: Global AI PC shipments are expected to account for 43% of total PC shipments in 2025
By 2026, Al laptops will be the only choice for large enterprise laptops According to the latest forecast from Gartner, global shipments of artificial intelligence personal computers (AI PCs) will reach 114 million units in 2025, a 165.5% increase from 2024 . Gartner defines AI P
[Home Electronics]
Gartner: Global AI PC shipments are expected to account for 43% of total PC shipments in 2025
Gartner: Q2 global PC shipments saw the largest decline in nine years, with only Apple achieving positive growth
According to Gartner, global PC shipments totaled 72 million units in the second quarter of 2022, down 12.6% from the second quarter of 2021. At the same time, Apple's global Mac shipments in the second quarter increased by 9.3% year-on-year, and US Mac shipments increased by 19.5% during the same period.    Gartner
[Home Electronics]
Gartner: Q2 global PC shipments saw the largest decline in nine years, with only Apple achieving positive growth
Gartner: Global PC shipments fell 9% in the third quarter of 2023
After eight consecutive quarters of decline, the PC market is expected to start recovering in the fourth quarter of 2023 October 13, 2023 - Gartner's preliminary statistics show that the global personal computer (PC) market shipped 64.3 million units in the third quarter of 2023, a 9% decrease from the third quarter
[Home Electronics]
Gartner: Global PC shipments fell 9% in the third quarter of 2023
Single chip microcomputer implementation of temperature measurement system based on PC serial communication
  Temperature is one of the main controlled parameters in industrial control. Traditional methods of temperature detection and control mostly use thermal resistors and thermocouples as temperature sensing elements. However, the output of this analog temperature sensor is an analog signal, which must be converted into
[Microcontroller]
Single chip microcomputer implementation of temperature measurement system based on PC serial communication
Harmonic Analysis and Simulation of Dead Zone Effect of AC/DC/AC Power Supply Based on MATLAB
0Introduction At present, most of the control methods of various inverter power supplies and the analysis of SPWM signal modulation methods are based on the assumption that the power switch device is an ideal switch device, that is, the rise, fall and storage time of the switch device are not considered. But in fact
[Power Management]
MathWorks launches Simulink Fault Analyzer and Polyspace Test in MATLAB and Simulink Release 2023b
New products and updates simplify model-based design for engineers and researchers in the aerospace, automotive and wireless communications industries Beijing, China, September 21, 2023 - MathWorks, the world's leading developer of mathematical computing software, today announced the launch of MATLAB® and Simulink®
[Embedded]
MathWorks launches Simulink Fault Analyzer and Polyspace Test in MATLAB and Simulink Release 2023b
Latest Analog Electronics Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号