Preface
I have a "deep" relationship with cars. From the four-wheel drive cars and remote control cars I played when I was a child to the "tracking car" I learned in college, I have an indissoluble bond with cars. Therefore, I had the idea of making my own "smart car" before, but I was too green and my knowledge of the microcontroller software was very weak. Although I could write some simple programs, they were all "relying on defense", so I had only ideas but no strength, and it has been put aside until now. It happened that the LiChuang development board opened this smart car training camp (it looks cool and the lectures are very useful). While learning the basic knowledge of microcontrollers, you can also make a smart car of your own, which is also the original intention of making this car. The car originated from the training camp, but to make it the smart car you want to make, you need to integrate your own ideas, and learn about microcontrollers while being able to make a car. Next, I will briefly analyze the reasons for using these modules or components.
Due to the "Professional Comprehensive Experiment" during this period, the Si24r1 and esp01 modules have not been adjusted yet. I will not introduce these two later. I will post them after they are made.
1. The appearance shows
the overall car. Since I want to make a slightly larger car, considering the circuit performance of the circuit board, the modules can be placed on each circuit board separately to save the mainboard space, so I chose to use PCB to assemble the overall frame of the car.
Front view of the car
Left side view of the car Front
and back of the car mainboard: connected to the LiChuang development board, and most of the functions are concentrated on this board
Front panel of the car: connected to the mainboard, place the screen and ws2812 colored lights, esp01 module, and connect the ultrasonic module and front lights of the lower bottom plate
Lower bottom plate of the car: fix the mainboard, fix the motor, and serve as the main frame of the car; place the front lights and ultrasonic module
Lower bottom plate connecting plate: the gaps between the holes in the lower bottom plate are matched with each other, and overlapping can be matched without the connecting plate, and connected with screws, but for the sake of aesthetics and the front and rear lower bottom plates The height is consistent, so I drew
the matching diagram of the lower base plate and the lower base plate connecting plate of this board
. Regarding the assembly problem:
Except for the marked part of the lower base plate, no processing and welding are required. The lower
base plate is roughly assembled like this
. After the motherboard is welded, it is assembled like this.
All the places on the front panel that can be welded need to be welded.
2. Purchase of components
Purchase list
Most of the components can be purchased on LiChuang Mall, such as resistors and capacitors, and Liangshanpai development board
Liangshanpai purchase link
The other part needs to be purchased on Taobao, and the purchase list is as follows
Note: The Tp5100 8.4V battery charging part was originally drawn on the PCB (refer to Ji Xiaoxin). It seems that there is a problem with this chip. I didn't adjust it, so I won't introduce it, and there is no need to weld this part of the circuit.
The following is a detailed introduction to the comprehensive price
of motor purchase
and the size of the car body, and after comparing it with the 130 reduction motor, the N20 was finally selected as the car motor. Since I bought the N20 motor and wheels twice, I think it is very important to buy a good motor. Either buy one with low speed and high torque, or buy one with good speed and torque (theoretically, speed and torque are opposite, high speed and low torque, but the speed and torque of the motor I bought the second time are very good, maybe the motor has enough power). Don't buy one with high speed and low torque, otherwise it may cause no problem in moving forward and backward, but can't turn. I have experienced this myself, and changing the wheel can't solve it. This is the purchase link of the N20 motor from the store I bought the second time. Although it is disassembled, the power is very strong. Compared with the
motor
, the wheel purchase is not so important. You can buy it from any store. At present, I know that there are three wheels that are paired with the N20 motor, as shown in the figure below: a thin wheel with a diameter of 34mm, a thick wheel with a diameter of 43mm, and a Mecanum wheel with a diameter of 58mm. In comparison, the first two are cheaper, and the third one is more playable. The first two models are easy to find on Taobao, so I won't post the link. You can buy them at will. There are relatively few Mecanum wheels that match N20. In order to facilitate your purchase, I will post a link for you. Mecanum wheel purchase link
N20 motor seat
In order to facilitate the fixation and disassembly of N20 motor, you need a motor seat. Of course, you can also fix it permanently and use glue directly (although it is possible, it is not recommended). Since this is also a lot of Taobao searches, there are few pitfalls, so I don't recommend it. However, in order to facilitate your purchase or reference, I will also post a link. The module used in the N20 motor seat
Bluetooth module
is the Hc05l Bluetooth module. There is no restriction on this. You can buy it at will. Here is a link for your reference. Bluetooth purchase link
SI24R1 module
This is not a must-have option. This is also used as a signal receiver to control the car like Bluetooth; but the remote control distance of this module will be farther, similar to Nrf2401, while Si24r1 is domestically produced. It can be used with another identical Si24r1 module (the remote control part of the handle has been drawn, but due to time constraints, it has not been adjusted yet. I will post it later). Since the pin connections of this module are different in different stores, I put a reference link. If you buy it in other stores, the pins need to be consistent with this one. Si24r1 reference purchase link
The picture below is a drawn handle (reference Ji Xiaoxin), still waiting for the chip...
The 0.96-inch
screen can display the power, car status, and mode. This screen can also be bought casually. It should be noted that this is an I2C interface, so don't buy an SPI screen. Reference link for purchasing screens
Other modules
Since there is basically nothing to pay attention to when purchasing esp01 modules, mpu6050 and other modules, I will not introduce them one by one. The following are reference links (for reference only)
Ultrasonic module: Ultrasonic purchase reference link
esp01 module: esp01 purchase reference link
mpu6050: mpu6050 purchase reference link
M3*6 screws: M3*6 screws purchase reference link
M3*26 copper pillars: M3*26 copper pillars purchase reference link
RZ7899: RZ7899 motor driver purchase reference link
There are many batteries and chargers, so I will not put the reference link.
3. Hardware circuit part
The main part
The main circuit connection is connected to the development board. Since there are too many pin distributions, the specific pin connection table will be listed later
Motor driver
Since the microcontroller cannot drive the motor directly, a motor driver chip is needed to drive the motor. There are many options for motor driver chips, such as RZ7899, TB6612, L298N, etc. Considering the price, cost performance and volume, the RZ7899 chip was finally selected. This chip can be used to drive ordinary DC motors and stepper motors. The circuit and program are relatively simple, the peripheral circuit is simple, the components are few, and only the microcontroller needs to control the high and low level changes of the two pins to realize the forward, reverse and braking of the motor. Input power supply voltage: 3V~25V; maximum drive current: 3A, which can meet the use of many small motors. The following is the circuit diagram. The purpose of connecting the pull-down resistor at BI and FI is to prevent the interference level when the main control is powered on from causing the motor to rotate, and the capacitors connected in parallel at BO and FO are to eliminate the sparks and high-frequency noise generated by the motor rotation.
Buzzer circuit
Having a buzzer that can play songs is a very cool thing, and the car naturally cannot be without this "speaker"; there are two types of buzzer circuits, one is the buzzer connected to the PNP circuit in the official case. According to group friends, it will make a sound inexplicably when powered on, and I have always used the buzzer with NPN circuit, so I did not use that solution. The figure below is the buzzer circuit with NPN connection. You only need to control the frequency of the mos switch to get different sounds (only one frequency is written in the car program, so there is only one tone. If you are interested in the buzzer "playing music", you can ask me for information)
ADC voltage acquisition circuit
If you want the battery life of the car to be longer, you need to pay attention to the voltage of the battery. You can't wait until the battery is exhausted before charging. Therefore, you need to design a voltage acquisition circuit, and then display the power information on the screen, so that you can pay attention to the battery power and make corresponding adjustments. The acquisition circuit is relatively simple, and only three resistors are needed for voltage division. The maximum voltage after voltage division is just within the measurement range of the microcontroller. The reason why the battery voltage cannot be measured directly with the microcontroller pin is that the voltage measured by the microcontroller pin cannot exceed 3.3V, otherwise the microcontroller may be burned out.
The button circuit
only needs to connect one end of the button to the microcontroller pin and one end to the ground. The microcontroller pin is pulled up internally. When the button is pressed, the microcontroller can detect the change in the pin level.
Bluetooth module circuit
Since the communication method between Bluetooth and the microcontroller is serial communication, the two serial port lines need to be connected; secondly, the power supply, its power supply voltage is 3.3V-6V, and the power supply here is 5V (the power supply was 3.3V during the previous debugging, which caused it to be unable to adjust for a long time. Later, it was discovered that the voltage pit was stepped on) The reason for adding this module to
the MPU6050 module
is that, when shunting cars, we would encounter the problem that the car could not go straight due to different speeds on both sides (given the same PWM value) and other reasons, or even "overtake on a curve", and the deflection was very serious. After studying some materials, we found that the yaw angle of the mpu6050 can be used to correct the deflection of the car, and combined with the PID algorithm, a good effect can be achieved. The circuit of this module is also relatively simple, and only 4 wires need to be connected for I2C communication. The negative pole of the LED
in the car light circuit
is grounded, and the positive pole is connected to the microcontroller pin. When the microcontroller gives a high level to the LED pin, the LED light is on, otherwise it is off.
WS2812 circuit
Since ws2812 is a single-line data transmission, only one signal line is needed for transmission, and 5V/3.3V power supply is available.
The screen circuit
is the same as mpu6050, and it also uses I2C communication.
Ultrasonic circuit
IV. Software part
Note: In the early stage of this project, I learned the basic knowledge of Liangshanpai development board and felt that I was "okay" (I personally think the knowledge in it is very basic and practical), so I prepared to "make a code" by myself. If I can't make it, I will use the official code. Fortunately, the code I made can be used, but because it is made by myself, it is difficult to understand. Some frameworks are made by myself, so they may not be as standardized as the official ones. (For your reference only)
Pin assignment (currently used)
Motor driver
To make the car move, you must first rub out the code of the driver chip. Without further ado, let's go directly to the code (the code of the car motor driver is placed in the motor folder)
Personal understanding: .c is used to write specific functions, and .h is used to put some definitions and function declarations, etc. Basically, every time a peripheral is used, the related library functions must be written in this way. One is standardization, and the other is to facilitate the reference
of the use of the microcontroller pins. They all need to be initialized first, and the drive motor is no exception. First, the pins are initialized in moto.c. Since pwm output will be used later, mode reuse is performed during initialization.
Then initialize the pwm channel corresponding to the pin. For easy understanding and reference, it is written in another function bsp_pwm.c, which has the configuration of different channels of two timers,
and then configure the forward and reverse rotation, brake, and flameout functions of each wheel.
Then nest and encapsulate the previous functions. It will be simpler to use in the main function. Reference the function, give a certain value in the brackets, and the car can turn. Bluetooth
module
can be configured in this software, mainly master-slave mode, module name, and baud rate. If the baud rate set by the Bluetooth module does not match the baud rate of the connected microcontroller, communication cannot be carried out, so its configuration is also very important; it needs to be set to slave mode with a baud rate of 9600. Before connecting to the computer, connect the Bluetooth module to ch340 first. There is a small button in the Bluetooth module. After connecting HC-05 to CH340, press and hold the button to power on the module, and the LED light flashes quickly.
Then click search in the software. After the connection is completed, follow
the configuration as follows. If you don’t understand, you can read the following article. Bluetooth module configuration.
Below is the program part. This part of the program is placed in bsp_usart.c. First, the serial port pin configuration
and then the data serial port reception .
Then it can be used in the main function.
The following is the Bluetooth app software interface used (it will be placed in the attachment below, and you can download it if you need it).
MPU6050 module.
This module is very easy to transplant according to the code in Mr. Li’s module transplantation manual. I have to say that the manual YYDS has many modules that can be used. There is also a dmp library in it, but I don’t know why the heading angle will drift zero when it is just powered on, while the other two angles will not. However, it is necessary to ensure that there is no problem with the heading angle when running in a straight line. I checked a lot of information and didn’t know how to solve it. When I was about to give up, I found that the heading angle would stabilize at a value after a certain data of zero drift, and it would be more stable afterwards. Therefore, wait until the heading angle data is stable before starting the straight-line driving program.
The most important thing when transplanting is to pay attention to the correct connection of the pins, otherwise
the pin initialization will not be used. After
initializing the dmp library in the main program,
it can be used in the main function.
Since the heading angle obtained above will produce zero drift, it cannot be used directly, and the data needs to be processed. After analyzing multiple sets of data, it is found that the heading angle will have zero drift for a period of time after power-on, and then stabilize at a value of about zero, and the zero drift time is uncertain. Therefore, the following processing method is obtained. First, the obtained heading angle is converted into an integer (because the value obtained without conversion is a floating point number, it is difficult to meet the if condition using the following processing method), and then the flag bit determines whether the car is initialized or started. When the flag bit is zero, it will enter the judgment loop all the time. If the heading angle of the second minus the first is zero, it means that the heading angle data is stable. At this time, the target heading angle (the reference angle of the car's straight-line driving) is changed to the stabilized heading angle, the flag position is 1, the loop is jumped out, and the car's straight-line driving function is executed.
The essence of the car's straight-line driving is the pid algorithm. If the car adjusts the angle quickly and stably, the pid algorithm needs to be used. Give the left wheel a stable speed to drive forward, and the right wheel a speed processed by pid; if the car does not deviate at an angle, pid will not produce any effect, that is, the speeds given on both sides are the same. If the car deviates to the right, it means that the speed of the left wheel is faster than that of the right. At this time, the pid algorithm increases the speed of the right wheel in proportion to the deviation. This is the role of Kp. Ki is the integral coefficient, which mainly makes the car drive more smoothly, and the role of Kd is to limit the amplitude to prevent the car from suddenly increasing too much and causing oscillation (of course, a large Kd will also cause oscillation). Err is the deviation, that is, the target value-actual value, E is the integral of the deviation, and D is the last deviation minus the current deviation. The principle of
ultrasonic following
is to allow the car to move forward, backward, stop, etc. according to the measured distance. It also needs to use pid. The principle is similar to the above. It should be noted that different cars and different voltages may have different effects after using the same pid. Therefore, to make it very stable, you may need to fine-tune some data yourself.
Pin, timer initialization
distance measurement principle: by calculating the ultrasonic wave following principle by the ultrasonic wave sending time and receiving time
, when the car is very close to the target, the car retreats, when the car is far away from the target, the car moves forward, and the PID algorithm is integrated to make the car more stable. The PID principle here is the same as the PID principle of straight-line driving above. There are a lot of OLED functions
for screen display
. As long as you can understand its general principle and use it, it will be fine. First, the pin is initialized. Two I2C output methods are given in the program. If the pin connected to the screen is not the hardware I2C pin of the microcontroller, you need to use software I2C. In oled.h, change the 0 of the macro definition USE_SOFTWARE to 1; if the hardware I2C pin of the microcontroller is connected, both can be used. The figure below uses the hardware I2C function.
As long as you understand the following functions, you will use them, and Mr. Li is also very considerate. Each function has annotations and usage methods for easy transplantation and use.
Clear screen function
Character display function
Buzzer
There are two types of buzzers: active and passive. For active buzzers, you don't need to adjust the frequency. You can use it by giving a voltage (just like lighting a lamp), so I won't describe it here in detail. I will mainly talk about the use of passive buzzers. Since passive buzzers are directly given voltage, they will not ring. They need to be given a certain frequency to ring. Of course, it will be more troublesome to use this way, but it also has advantages. This configuration means that the tone of the buzzer is adjustable, so there can be other ways to play, such as playing songs, but here the car only needs one tone.
It's still the old rules. Initialize the pins first and turn on the timer clock.
The following is to configure the timer and pwm. This function can be used directly. It is a little different from the previous car pwm configuration. The frequency of the car motor does not need to be changed, so this function is only used to configure a frequency; and here it is used to indirectly change its frequency by changing the pulse value and period to emit different tones. It took some time to figure out
the ADC voltage measurement
. I originally referred to the official information, but the writing methods were different in different places, so I followed the video to write the code. It didn't work at first, and the measured value was always around 1300. Later, it worked again (maybe it was not configured properly). I went to CSDN to find the voltage calculation formula and got the measured voltage.
Pin initialization, ADC sampling configuration
ADC voltage sampling: The voltage measurement range of the microcontroller used is 3.3V (cannot be exceeded), the sampled value is from 0 to 4095, and the internal reference voltage of the microcontroller is 3.3V. In layman's terms, the sampling value 0-4095 corresponds to the voltage 0-3.3V; therefore, the percentage of the sampling value in 4095 is obtained by dividing the sampling value by 4095, and then multiplying it by the reference voltage to get the measured voltage. The powervalue at the end is the calculated voltage percentage, which is convenient for screen display and viewing, and there is no need to calculate how much voltage is left without power. Theoretically, the maximum voltage measured after the resistor voltage division is 2.8V (i.e. 8.4/3), and the minimum battery voltage is 1.87 (5.6/3). Based on these two data, the battery voltage percentage can be calculated. 0.93=2.8-1.83, the reasonable range of the measured battery voltage, vdd-1.87 is the percentage of 0.93 (reasonable voltage range).
Then use timer 6 to update the voltage percentage every 5 seconds.
Key
detection There are two ways to detect keys, and two methods are also used here: external interrupt and polling. Since the switching mode requires a higher priority and reduces the CPU calculation, the key to switch the mode uses an external interrupt, while the start and stop keys of the car are only in the ultrasonic follow mode, so the priority required is lower, and only when entering the ultrasonic mode will this polling function be executed, which can effectively avoid the car running due to the accidental touch of the start button in other modes. Key pin, external interrupt initialization detects the key press through external interrupt, and switches the car mode after pressing it . The key
press is detected by polling. After the key is pressed, the pin voltage change can be detected. If the car is in ultrasonic mode, the button can be used to change the car's motion state (start/stop) . The use of WS2812 colored lights This light is relatively simple to use and easy to understand. You only need to understand the color sending function and the way to change the color. First, the pin is initialized. This data transmission method has no special requirements for the pin. Then understand this function. There are two parameters. The first parameter is the position of the light. If the control is the first light, then fill in 0 for the parameter here. If it is the second, fill in 1, and so on. The second parameter is the color of the light. The RGB format controls one color every 8 bits, such as 0xff0000 is red, 0x00ff00 is green, and 0x0000ff is blue. You can go directly to the Internet to find your favorite color and try it (just change the data after 0x). The following is a use case to define an array for color. Change the color of the first light every 0.1 seconds. Note: Due to the limited size of the uploaded file, the longest video file in the following demonstration video is compressed a lot, and the picture quality looks a bit poor. The high-definition demonstration video is posted on Station B. Smart car demonstration Note : There are four boards in total, and the fourth one is the main board! ! !