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.
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.
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.
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.
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.
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.
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
- High signal-to-noise ratio MEMS microphone drives artificial intelligence interaction
- Advantages of using a differential-to-single-ended RF amplifier in a transmit signal chain design
- ON Semiconductor CEO Appears at Munich Electronica Show and Launches Treo Platform
- ON Semiconductor Launches Industry-Leading Analog and Mixed-Signal Platform
- Analog Devices ADAQ7767-1 μModule DAQ Solution for Rapid Development of Precision Data Acquisition Systems Now Available at Mouser
- Domestic high-precision, high-speed ADC chips are on the rise
- Microcontrollers that combine Hi-Fi, intelligence and USB multi-channel features – ushering in a new era of digital audio
- Using capacitive PGA, Naxin Micro launches high-precision multi-channel 24/16-bit Δ-Σ ADC
- Fully Differential Amplifier Provides High Voltage, Low Noise Signals for Precision Data Acquisition Signal Chain
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Application of “C2000+TMS570” dual-chip solution in automotive electric drive functional safety
- Solution to the error when downloading the CCS program
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board] Two-way counter implementation
- C2000 ADC sampling时序例程
- Showing goods (1) - Various development boards
- Designing for Low Quiescent Current in Small Battery-Powered Devices
- About foreign masks
- 6657Statically configure serial port general interrupt in sys/bios
- Smart LED Octahedron
- [Lazy self-care fish tank control system] BLE_MESH fish tank light peripheral production