The design of smartcar based on ATMEGA2560

With the development of automotive electronics and robot intelligent technology, intelligent vehicle has become a research hotspot in the field of automatic control. Intelligent vehicle control use automatic control, pattern recognition, sensing technology, electronics, electrical, computer, mechanical and other disciplines. Sensor technology is a very important part in the process of intelligent vehicle automatic driving, Infrared alignment tube is a kind of sensor commonly used in navigation. In this paper, it will first introduce the background of smart car, then the design of smart car software and hardware. After that, the code structure will be introduced. Finally, an intelligent car with the functions of navigation, ranging, automatic parking and searching for beacons will be designed.


Introduction
With the rapid development of automobile industry, the intellectualization of automobile will be the development trend of automobile industry in the future [1]. In this context, the main tasks of the project include enabling the vehicle to move following the guide line, parking at terminal zone, completing pathless navigation, and measuring and displaying the length of the guide line in real time.

Driver module
The motor drive circuit of the robot is constructed by H-bridge which composed of four MOSFETs, four Diodes and two half-bridge driver chips IR2104. And as shown in Figure 2, the N-Channel MOSFET Transistor IRLR8726PBF is selected. In addition, the diode IN5819G are connected in parallel with each MOSFET, which play role to release the counter electromotive force and protect the MOSFETs.

Figure 2. H-bridge circuit
The photoelectric isolation module uses light as the medium to transmit electrical signals which achieves that the Arduino is indirectly connected with H Bridge Circuit. The H Bridge Circuit have relatively larger current, and the counter electromotive force generated during the operation of the motor could affect the normal operation of the Arduino, so it is necessary to add a photoelectric isolation circuit to protect the Microcomputer as shown in Figure 3.

Photoelectric Encoder Module
In this part, two Code Disks and two Photoelectric Encoder Module are used. The Code Disks with a series of slots are mounted on each motor shaft opposite the wheels, so they rotate at the same speed as the wheels. These code disks will be placed in the gap of the Photoelectric Encoder Module. As for the output signal when the infrared signal is blocked by the Code Disk is different from the output signal when it is not blocked, so when the two DC motor with reduction gears start rotating, the output signal of the Photoelectric Encoder Module will change constantly. Pulse counting can be realized by triggering external interrupt by the changing of the output signal. 3 goes through in time T can be obtained , and the average speed of the robot can be obtained . When the time T is small enough, the speed can be considered as instantaneous velocity. Usually, the average speed within T=20ms is taken as the instantaneous speed of the robot. Considering the resolution of the Code Disk is low, in this module the average speed within 5T=100ms is taken as the instantaneous speed of the wheels. The array is the number of pulses in T=20ms, so the real time speed calculation formula is as follows: Line tracking refers to that the robot moves along the white line according to the different reflection of infrared light on the black line and white ground. In this design, Five-channel Infrared Geminate Tubes Circuit is composed of 5 ST188 Infrared Geminate Tube and two LM324 voltage comparator to form Infrared Geminate Tube module and realize the line-tracking function. In order to make sure the robot does not lose the white line when tracking this line, there must at least one infrared geminate tube above the white line, so the distance between infrared geminate tubes is designed to be 15 cm, as for the white tape width is 25 cm.
In the process of line tracking, this module continuously emits infrared light to the ground. When the infrared light diffuses to the white line, the reflected light is received by the receiving tube then the phototransistor is turned on and the output of comparator LM324 is high level. In the same way, when the receiving tube cannot receive the light diffused from the blue floor (or black edge line), the phototransistor cannot conduct and the output of comparator LM324 is low level. The Arduino Mega 2560 can obtain five digital outputs from LM324 and judge the position of the robot relative to the white line according to these five digital outputs [2]. Then the robot can adjust its position by adjusting the speed difference between the two wheels. In different condition, the robot have different speed difference, the bigger curvature, the bigger speed difference; the smaller curvature, the smaller speed difference. Additionally, the speed can be improved when the driving route is in straight or lesser curvature.

Parking Module.
In order to perform the parking function, the ultrasonic module is used to detect the distance from the robot to the wall. While parking this robot, the distance from the robot to the wall should select properly. The distance should make robot stop in the parking area without hitting the wall.

Beacon Searching Module.
There are two infrared receivers and two photosensitive resistors used in this module. The two Infrared Receivers will be adopted to detect the beacon and adjust the driving direction. After the robot stop at the parking area for 20s and beep for 1s, the robot starts to find the Beacon. At first, the robot rotates right, taking the right wheel as axis. When left or right Infrared Receivers detect the infrared signals, it stops rotating and starts to go toward the beacon. When the left infrared receiver receives the signal from the beacon, the robot will turn left. When the right infrared receiver receives the signal from the beacon, the robot will turn right. When both infrared receivers can receive signal, the robot go ahead. As for the Beacon flash at 10Hz, when there is no infrared signal received by this receiver, the robot should continue to go in last speed and direction.
When the distance from the robot to the beacon is small enough for photosensitive resistors to detect the light from Red LED, Arduino Mega 2560 starts to judge whether the analogy output of the photosensitive resistor module is less than 100. When the AO less than 100 that indicates the robot reach the BEACON, and stop the robot.

H-bridge Circuit
As shown in Figure 2, In order to drive the motor, a pair of MOSFETs on the diagonal must switch on at the same time [3]. When Q 1 and Q 4 are switched on, the current passes through the motor from left to right, and the motor turns clockwise. When Q 2 and Q 3 are switched on, the current passes through the motor from right to left, and the motor turns anticlockwise.
It is vital important to ensure that the two MOSFETs on the same side of the H-bridge do not conduct simultaneously, otherwise the +7.2V will connect with GND directly and the current on the line will increase a lot which may damage the MOSFETs. When PWM signal inputs to IR2104, HO port outputs the same signal as IN port, LO port outputs the opposite signal as IN port.The two MOSFETs on the same side are driven by the HO port and LO port of the IR2104 chip respectively, so that the two MOSFETs on one side will not be switched on at the same time.

Optocoupler Isolation Circuit
In order to prevent the counter electromotive force generated during the operation of the motor from affecting the normal operation of the single chip, in the design process, the single chip cannot control the motor drive circuit directly, an optical isolator TLP281-4 need to be added between the them, which use the optical signal to disconnect the electrical connection of the two sides. The two ends of the optocoupler are respectively powered and grounded by the different Power Supply Module.  In order to protect this circuit, and reduce the power consumed by the resistors, 1kΩ resistances are used in actual design.
Note: It can be seen from the Optocoupler Isolation Circuit in Figure 5 that the input signal on the left side is opposite to the output signal on the right side. Therefore, it should be noted that the duty ratio of the input signal and the output signal is also opposite. So the code should be change as bellow: analogWrite (PWM,255-PWM).

Five-channel Infrared Geminate Tubes.
The infrared geminate tubes ST188 has four pins, and a light-emitting diode is between port A and K and a photosensitive triode is between port C and E. The detected white line signals are transmitted to the voltage comparator integrated circuit LM324 by port C, while the reference voltage is provided by a potentiometer, and finally the output signal are sent to the Arduino.
In addition, considering the current of infrared transmitting tube is larger, in order to protect the Arduino Mega2560 and reduce interference, we will supply power to infrared transmitting tube and receiving tube respectively. The infrared transmitting tube is connected to +5V1 which is used to power the motor. The infrared receiving tube is connected to +5V which is used to power the Arduino Mega2560.

Figure 6. Five-channel Infrared sensor typical circuit
According to the datasheet, the maximum forward current of ST188 is 50mA and the typical voltage drop is 1.25 volts, so the current-limiting resistance of the input can be calculated: . In order to meet the demands of different modules, the existing 7.2V power supply needs to be raised to 12V and lowered to 5V .

Figure 7. Typical application of LM2940
Since 7.2-5 =2.2V> 0.8V, a three-terminal low dropout regulator LM2940, can be selected. According to the datasheet, 0.8V is the maximum voltage drop of the regulator and the typical application circuits are shown in Figure 7, in which the two capacitors act as filters to make the output signal more stable.

Figure 8.
Step-Up Converter of MC34063 The MC34063 can realize the function of dc-to-dc converters, which can be used in buck, boost or reverse transform, and the typical set-up converter circuit are shown in Figure 8[5].
The output voltage is determined by the resistance value of R1, R2, and potentiometer BP1 whose resistance value ranges. And the output voltage value conforms to the following formula:  Figure 9. The application circuit of TL1838 In the pathless navigation task, infrared signal detecting are needed to judge the general direction of the robot, where the Pulsed Infrared Receiver TL1838 can be used. And the application circuit are shown in Figure 9 [6]. Since the infrared receiving head is easily interfered by external signals, two filter capacitors are added to both ends of the power supply in parallel, and a pull-up resistor is added to the output pin to stabilizes the output signal at the high level when TL1838 unable to receive the infrared signal. In addition, a resistor is connected in series at the VCC end, which can form a RC filter with a capacitor to further stabilize the output signal of the receiver.

Photosensitive Resistor Module.
Under the irradiation of the light, the resistance value of 5516 decreases with the increases of light intensity. After connecting a 10kΩ resistor with 5516, the analogy output of this module is the voltage divided by photosensitive resistor. The light intensity can be reflected by this analog output value. In this module the buzzer is active and triggered at low levels [6]. The negative electrode of the buzzer is connected to a triode, which is a silicon tube of PNP type. When the input signal of port1 is at low level, the triode is on and the buzzer works. At normal stage the port1 remains high level and the triode is off. To facilitate the placement of the two battery packs, the Arduino Mega2560 is placed on a high level with two copper columns, near the rear of the robot.The PCB will be placed in the middle of the car, again using a copper column to stand on high.

Assemble
To stabilize the robot's center of gravity, two batteries are placed at the rear of the robot and secured under the MCU with nylon ropes.
To prevent interference from other sensors, the ultrasonic sensors will be mounted on top of a plastic column near the front of the robot.

Main procedure
In the main procedure, there are three main functions, external interrupt function, internal clock interrupt function and loop function. The external interrupt function is used to calculate the pulse number of the Photoelectric Encoder, while the internal clock interrupt function is used to perform the main functions of the project, such as scanning switch, tracking line, ranging, stop automatically, searching for beacons and finishing the task, besides these six functions are divided into different stages by six flag bits. And when the interrupt functions completes, the procedure will return to the loop function which is used to display OLED.

Speed Control procedure
Due to the limitations of intelligent vehicle photoelectric encoder, negative speed cannot be measured, so we have made some improvements on the output of PID control. The user only needs to input the expected speed in the speed control function, and the motors will output the corresponding speed. The user does not need to care about the calculation of PWM signal and pin assignment. In addition, the speed control function have current limiting control function.

Navigation procedure
When the infrared tube encounters the white line, the return value is 1, while the black line returns 0. Calculate a variable sum by reading the value of a five-way infrared pair tube, different sum values are used to replace different infrared tube states. In different condition, the robot have different speed difference, the bigger curvature, the bigger speed difference; the smaller curvature, the smaller speed difference. Additionally, the speed can be improved when the driving route is in straight or lesser curvature.

Searching for Beacon procedure
The searching for beacon task are divided into four stages by different flag bits. In the first stage, the robot is rotating right taking the right wheel as axis to search for the beacon. After the beacon is found and the AO output is larger than 300, it was navigated by IR signal. When the AO is less than 300 but larger than 100, the robot navigated by the red LED. While when the AO is less than 100 that indicates the robot reach the beacon, and then stop the robot.

Conclusion
This project realized the navigation function with the help of the five-channel infrared pair tube, realized the parking function with the ultrasonic module, realized the ranging function with the photoelectric encoder and the related algorithm, and realized the beacon searching function with the photosensitive resistor and the infrared receiver. In the aspect of speed control, the intelligent car adopts PID speed control algorithm, which makes the speed and steering of the car more stable. The hardware design of the intelligent car is complete, and the software algorithm has the advantages of strong stability and strong anti-interference ability.