The source code of robot car
This tutorial will briefly explain about robot car that the source code structure of the studio driver board at the request of many car fans who are not familiar with MCU Features. And give two examples of robot car to illustrate how to merge the new functions released by the studio and how to expand their own functions.
You need to install keil 4 to modify the source code of the driver board of the studio.
First, let's take a look at the architecture of the driver board source code:
The first-level directory of the source code released by the studio is probably like this. There are four files that we need to pay attention to, and the other files can be ignored.
- The lib folder at the top: This folder is the library file of the driver board, such as the manipulator driver function library is in it.
- Source folder: This folder is the place where all the C code of the driver board is placed. What we need to modify is the program inside.
- Motor_con.uvproj file: This file is the project file. After keil 4 is installed, the system will automatically associate it. You only need to double-click this icon to modify the source code in the future.
- motor_con.hex: This file is the firmware compiled by keil 4, which is the firmware to be burned into the driver board.
Let's take a look at the secondary directory. There is only one library file in the lib directory. Generally, you don't need to care about it, so we won't take a closer look. Mainly look at the Source folde.
Config.h: This file saves various initialization information of the robot, including pin definition, timer definition, initial angle of the servo, motor output port and other configurations. This file is also one of our most frequently modified files.
main.c: The main function file. There is a main() function in this file. The program execution of the MCU starts from this main() function.
motor.c Initialization of motor and pins.
timer.c The timer interrupt file contains the interrupt implementation of each timer.
uart.c The serial port interrupts the file, and the communication with the host computer is realized here.
Other files ending in .h: all header files, including declarations of various functions, and those who are interested can open them by themselves.
After the introduction of the basic file structure, we can see that the studio's driver board source code structure level is very clear, which is very convenient for code writing and maintenance.
Next, take an upgrade package "power on and turn on the water lamp" released by the studio as an example to explain how to merge the code. First download the upgrade package from the studio's lower computer source code release post, and unzip it to get three files: SelfTest.c, SelfTest.h, and main.c. According to the original source project comparison, it can be found that there are two more files, SelfTest.c and SelfTest.h. At the same time, the original code in the main.c file is:
1. You can modify this program arbitrarily and apply it to the smart car robots and other electronic products developed by yourself, but it is prohibited to use it for profiteering from others.
2.By WIFI Robot Network·Robot Creative Studio
14./* Timer 0 is dedicated to the servo, it is not recommended to change the configuration */
16./* Timer 1 is free, users can use it flexibly */
Now it becomes:
- You can modify this program arbitrarily and apply it to the smart car robots and other electronic products developed by yourself, but it is prohibited to use it for profiteering from others.
- By WIFI Robot Network·Robot Creative Studio
- void main(void)
18./* Timer 0 is dedicated to the servo, it is not recommended to change the configuration */
20./* Timer 1 is free, users can use it flexibly */
22.Motor_Init(); twenty three.
Please pay attention to the code on lines 10 and 15 above. This is modified where the original C program .
If it is a file that did not exist before, such as SelfTest.c and SelfTest.h, we can put these two files in the Source folder according to the prompts on the source code release post.
Then the original main.c file, we should refer to the difference between these two files for code migration.
Copy above of the code in the red part of the main.c file to the corresponding location of the original main.c file and save it.
At this point, the new function has been transplanted, referring to the tips in each source code release post. Actually, it is very simple. After the migration is completed, you need to regenerate a new firmware, use the compiling function of keil 4, click to generate the motor_con.hex file, and then refer to Tutorial 5, burn it to the driver board, and the new functions will be added!
So what should I do if I want to add new features myself? Let's take the addition of "P22 port controlled lights" as an example, suppose the instruction to turn on the lights is FF040100FF and turn off is FF040000FF.
First of all, you need to figure out which pin you want to add to the function, you can refer to the pin description diagram in the driver board release post to choose by yourself. Now, we are going to use the P22 port to control the lights, so the first step is to add pin definitions in the Config.h file:
This is the definition that we use the P22 interface to connect the headlights.
Then, in the motor.h file, define the macro definition of the headlight, so that we don't need to use the unintuitive way of MAINLIGHT_CON=1 in specific calls,and the code is convenient for reading and understanding .
1.#define MAINLIGHT_TURNON MAINLIGHT_CON=0;
2.#define MAINLIGHT_TURNOFF MAINLIGHT_CON=1;
From above of the macro definition , you can see that when the car lights are turned on and the macro is executed, the MAINLIGHT_CON pin that is the P22 pin, is low. At this time, if the outside is high, then the car lights are on, but when the macro definition of turning off the lights is executed, the MAINLIGHT_CON=1 is high level, and the lights are turned off. Of course, here is the sink current used , and the current source method also can be used, but the driving force is relatively weak and the car lights are not so bright.
In the third step, after finishing the macro definition of the headlights, we will implement the work of the headlights.
All actions of the studio driver board are executed in the serial port interrupt, so we need to modify the Uart.c file. Open the Uart.c file and find the void Communication_Decode(void) function. This function parses the data packet sent by the host computer. Our car light execution function will be added here.
- void Communication_Decode(void)
- case 0x01:MOTOR_GO_FORWARD; return;
- case 0x02:MOTOR_GO_BACK; return;
- case 0x03:MOTOR_GO_LEFT; return;
- case 0x04:MOTOR_GO_RIGHT; return;
- case 0x00:MOTOR_GO_STOP; return;
- default: return;
- else if(buffer==0x01)
- case 0x01:se_timer=buffer; return;
- case 0x02:se_timer=buffer; return;
- case 0x03:se_timer=buffer; return;
- case 0x04:se_timer=buffer; return;
- case 0x05:se_timer=buffer; return;
- case 0x06:se_timer=buffer; return;
- case 0x07:se_timer=buffer; return;
- case 0x08:se_timer=buffer; return;
- default : return;
- else if(buffer==0x04) //Lamp control used by sink current method
- case 0x01:
- case 0x00:
As can be seen from the code from line 32 to line 45 above, we have one more branch on the if branch. This branch is used to determine whether the light-on data packet of the host computer has arrived. If it is found that the type bit is 0x04 and the command bit is 0x01, then only MAINLIGHT_TURNON; This macro definition is to pull down the P22 pin, that is, to turn on the light for the command FF040100FF. On the contrary, the P22 pin is pulled up and the car lights go out. OK, execute the compilation in Keil, you can generate the driver firmware containing the P22 headlights, what are you waiting for? Hurry up and burn it into the driver board, and go to find something under the bed~~~