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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Explain separately:

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

      3.*/

     4.#include "stc_new_8051.h"

     5.#include "motor.h"

     6.#include "uart.h"

     7.#include <stdio.h>

     8.#include "type.h"

     9.#include "timer.h"

     10.

     11.void main(void)

     12.{

      13.UART_init();

      14./* Timer 0 is dedicated to the servo, it is not recommended to change the configuration */

      15.Timer0_Init();

       16./* Timer 1 is free, users can use it flexibly */

       17.Timer1_Init();

        18.Motor_Init();

        19. while(1)

        20.{

        21.  ;

        22. }

        23.}

Copy code

Now it becomes:

  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
  3. */

    4.#include "stc_new_8051.h"

     5.#include "motor.h"

     6.#include "uart.h"

     7.#include <stdio.h>

     8.#include "type.h"

     9.#include "timer.h"

    10.#include "SelfTest.h"

  1. void main(void)

    13.{

  1. TestApp_Init();
  2. UART_init();

    18./* Timer 0 is dedicated to the servo, it is not recommended to change the             configuration */

    19.Timer0_Init();

    20./* Timer 1 is free, users can use it flexibly */

    21.Timer1_Init();

    22.Motor_Init(); twenty three.

  1. while(1)
  2. {
  3. }
  4. }

Copy code

     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:

1.sbit MAINLIGHT_CON=P2^2;

Copy code

     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;

Copy code

    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.

  1. void Communication_Decode(void) 
  2. if(buffer[0]==0x00)
  3. {
  4. switch(buffer[1])
  5. {
  6. case 0x01:MOTOR_GO_FORWARD; return;
  7. case 0x02:MOTOR_GO_BACK; return;
  8. case 0x03:MOTOR_GO_LEFT; return;
  9. case 0x04:MOTOR_GO_RIGHT; return;
  10. case 0x00:MOTOR_GO_STOP; return;
  11. default: return;
  12. }
  13. else if(buffer[0]==0x01)
  14. {
  15. if(buffer[2]>180)
  16. return;
  17. switch(buffer[1])
  18. {
  19. case 0x01:se_timer[0]=buffer[2]; return;
  20. case 0x02:se_timer[1]=buffer[2]; return;
  21. case 0x03:se_timer[2]=buffer[2]; return;
  22. case 0x04:se_timer[3]=buffer[2]; return;
  23. case 0x05:se_timer[4]=buffer[2]; return;
  24. case 0x06:se_timer[5]=buffer[2]; return;
  25. case 0x07:se_timer[6]=buffer[2]; return;
  26. case 0x08:se_timer[7]=buffer[2]; return;
  27. default : return;
  28. }
  29. }
  30. else if(buffer[0]==0x04) //Lamp control used by sink current method
  31. {
  32. switch(buffer[1])
  33. {
  34. case 0x01:
  35. MAINLIGHT_TURNON;
  36. break;
  37. case 0x00:
  38. MAINLIGHT_TURNOFF;
  39. break;
  40. default:return;
  41. }
  42. }
  43. else
  44. {
  45. return;
  46. }
  47. }

Copy code

      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~~~

 

ArduinoRobot carRoboticsSmart robot car

Leave a comment

All comments are moderated before being published