- Tracking car is the current photoelectric learning/single-chip learning/photoelectric competition often involved in the project, most of the tracking car using a number of gray sensors + single-chip, in contrast, the camera +X3pi tracking car made more accurate and smooth.
- The author is currently a freshman, majoring in optoelectronic information science and engineering. At the beginning of the year, inspired by the senior students, he entered the X3pi development board. After 3 months of learning, he started from a verily layman and completed a trace-finding car with excellent effect.
-- – – – – – – – – – – – – – – – – -![] Advantages of camera tracking:
- Compared with the grayscale sensor, the camera does not need special position adjustment, and can be deployed in a reasonable position to take the image range according to demand.
- Camera accuracy is high, the CMOS of the camera is equivalent to mxn (hundreds/thousands of orders of magnitude) gray sensor, can directly take the pixel deviation as the offset.
- If it is a USB camera, you can call it directly with cv2.
Advantages of X3pi tracking car: The X3pi includes both a 40pin pin and a USB 3.0/2.0 port.
- Compared with the single chip microcomputer, X3pi can quickly complete the processing of the image and obtain the tracking offset.
- Hardware preparation
Rising Sun X3pi
X3pi only uses the USB camera function and 40pin function in the production of the tracking car, and the computing power of X3pi can process the camera image very quickly and pass it into the PID algorithm to correct the error in time. ! Sunrise X3pi
Car chassis
There are many kinds of car chassis: four-wheel drive type, two-wheel drive + one free wheel type, McNamm wheel type, omnidirectional free wheel, two-wheel balance type, etc., its difficulty, effect, error are also different.
After comprehensive consideration, we have chosen a two-wheel drive + a free-wheel type car with simple use and great steering flexibility! [two wheel drive + a free wheel car] (/ API/v1 / static/imgData / 1682168624406. PNG)
L298N motor drive module
L298N is a common motor drive module on the market, which is simple to use, stable drive, not easy to burn out, and the price is not high.
The method of use is as follows:
Power Supply: L298N generally uses the 5V enable port to supply power to the single chip computer, but the current requirement of X3pi is too high, so the 5V enable port is not needed here, and only the power supply with appropriate voltage is needed in VCC and GND.
Logical interface: The L298N has four input interfaces. IN1 and IN2 correspond to OUT1 and OUT2. IN3 and IN4 corresponding control OUT3 and OUT4 Connect the IN of L298N and the 40pin of X3pi to control the high and low level output to control the motor rotation. L298N truth table is as follows:! [L298N logic control] (/ API/v1 / static/imgData / 1682169895124. The PNG)
PWM control: Because the tracking car uses a two-wheel differential turning, it needs to use PWM speed regulation to change the DC voltage into a pulse voltage (equivalent to DC low voltage). To use, unplug the jumper cap of the EN pin and connect the PWM pin of the X3pi (only pins 32 and 33).
Power supply: 18650 battery (3C or 5C!!) +LM2596S stabilized power module The 18650 battery is also a commonly used power supply for making cars, but with the battery consumption, the voltage will change, so the voltage regulator power module is needed to stabilize the voltage, considering the capacity and current, the owner uses three 5C 18650 batteries. ! [18650 c] cell 5 (/ API/v1 / static/imgData / 1682170970727. The PNG)! ] [LM2596S regulated power supply module (/ API/v1 / static/imgData / 1682170986024. The PNG)
It should be noted that the X3pi needs 2A current to turn on normally, the 18650 battery of 3C or 5C to ensure its current supply, the power module also needs to pay attention to the maximum current that it can output (3A or more insurance), it is best to adjust the output, with the number of display 3 board, the students who are not confident in the wiring can also find a reverse connection protection.
Connect the positive and negative cables of the X3pi to the positive and negative cables of the Type-C cable to supply power to the X3pi.
USB camera
X3pi using the on-board camera involves the conversion of the stream, which is difficult, and the use of the USB camera can be directly called by cv2. [USB camera] (/ API/v1 / static/imgData / 1682171392986. The PNG)
USB cable can do appropriate diy, a simple understanding of the principle of USB can know that USB 2.0 transmission is only composed of two power cables and two data cables, want to wiring is very simple.
Type lsusb in the X3pi terminal to check the USB connection. If the USB connection of the camera is not checked, it may be that the X3pi is insufficient to supply power to the camera. You can try to pull out the USB power cord to supply power separately (not through practice, but feasible in theory).
Two, hardware connection The basic connection route is as follows! [circuit connection diagram] (/ API/v1 / static/imgData / 1682183827415. PNG) Regarding the pin connection of the X3pi, note: ! Synopsis] [sunrise X3pi 40 pin pin (/ API/v1 / static/imgData / 1682922435815. PNG) Set the pin mode to “BOARD”, but the function of each pin still persists, for example, pin 9,14,25,30,34,39 is a ground signal pin and cannot output a high level. In order to prevent interference from other pin functions, it is best to use GPIO model pins, such as: 11,13,15, etc.; Second, it should be noted that only pin 32,33 has PWM capability.
- Code writing The code part has 3 modules: control module, vision module, PID regulation module, we package these three modules in 3 classes. The following modules need to be referenced
```language import Hobot.GPIO as GPIO import time import cv2 `
Control module
```python class CTRL(): `
init Method Setting Pins (Connecting to L298N)
```python def init(self,in1,in2,in3,in4,pa,pb): GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False)
GPIO.setup(in1,GPIO.OUT) GPIO.setup(in2,GPIO.OUT) GPIO.setup(in3,GPIO.OUT) GPIO.setup(in4,GPIO.OUT)
self.in1 = in1 self.in2 = in2 self.in3 = in3 self.in4 = in4
self.PWMA = GPIO.PWM(pa,48000) self.PWMB = GPIO.PWM(pb,48000) ` A function that controls the difference between the left and right wheels
```python def drive(self,FL,FR): if FL >= 0: GPIO.output(self.in3,GPIO.HIGH) GPIO.output(self.in4,GPIO.LOW) elif FL < 0: GPIO.output(self.in4,GPIO.HIGH) GPIO.output(self.in3,GPIO.LOW)
if FR >= 0: GPIO.output(self.in1,GPIO.HIGH) GPIO.output(self.in2,GPIO.LOW) elif FR < 0: GPIO.output(self.in2,GPIO.HIGH) GPIO.output(self.in1,GPIO.LOW)
self.PWMA.ChangeDutyCycle(abs(FR)) self.PWMB.ChangeDutyCycle(abs(FL)) self.PWMA.start(abs(FR)) self.PWMB.start(abs(FL)) ` Pull up
```python def stop(self): GPIO.output(self.in1,GPIO.LOW) GPIO.output(self.in2,GPIO.LOW) GPIO.output(self.in3,GPIO.LOW) GPIO.output(self.in4,GPIO.LOW)
self.PWMA.ChangeDutyCycle(0) self.PWMB.ChangeDutyCycle(0) self.PWMA.start(0) self.PWMB.start(0) ` pwm deactivate and clean pins
```python def clean(self): self.PWMB.stop() self.PWMA.stop() GPIO.cleanup() `
Vision module
```python class EYE(): ` Call in init, turn on the camera, and complete the related Settings
```python def init(self): self.video = cv2.VideoCapture(8) ret = self.video.isOpened() if ret: print(“The video is opened.”) else: print(“No video.”)
codec = cv2.VideoWriter_fourcc( ‘M’, ‘J’, ‘P’, ‘G’ ) self.video.set(cv2.CAP_PROP_FOURCC, codec) self.video.set(cv2.CAP_PROP_FPS, 30) self.video.set(cv2.CAP_PROP_FRAME_WIDTH, 320) self.video.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) ` In the function “outmiss”, the camera image is read and converted into a grayscale image (considering the image processing speed, binary processing is not done here), a row of pixels at the most middle height is taken, binary by itself, the average value of the black track coordinates is taken, the image value is subtracted, and the error is obtained. If there are less than 20 pixels of the black track, It is regarded as a null reading (corresponding to right Angle or obtuse corner in tracking), the error is None, and the error is finally returned
```python def outmiss(self): _ , img = self.video.read() img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) img = img