Welcome to DOGZILLA-Lite repository
1. Color patrol
Color patrol
1. Purpose of the experiment
This tutorial will introduce in detail how to program and control the robot dog to realize the automatic line patrol function, but it is limited to identifying and following the lines of four specific colors: red, green, blue, and yellow. First, you need to equip the robot dog with a color sensor module, which can accurately distinguish the four preset colors of red (RGB value is about 255,0,0), green (0,255,0), blue (0,0,255) and yellow (255,255,0). When programming, we need to establish a color recognition threshold system. When the sensor detects the set target color (for example, red is selected as the line patrol color), the robot dog's control system will adjust the left and right wheel speed difference through the PID algorithm to keep it on the color line. It is particularly important to note that the method in this tutorial is not applicable to other colors because the system only presets the feature value library of these four colors. During the implementation process, it is also necessary to consider the impact of ambient light conditions on color recognition. It is recommended to test and adjust in an indoor environment with stable light.
2. Experimental path source code
First, enter the robot dog system, end the robot dog program, and then enter the path of cd ~/DOGZILLA_Lite_class/6.AI Visual Interaction Course/01.color_line in the terminal and run follow_line.py .
3. Experimental Phenomenon
xxxxxxxxxxpython3 follow_line.py
After executing the above command, the robot dog will start the color line patrol operation. By default, it patrols the blue line. You can switch the patrol color by pressing the button in the upper right corner of the robot dog screen or the lowercase 'k' key on the keyboard. As shown in the figure:

The upper left corner of the picture shows a yellow line, but there are only blue lines, so the robot dog will not move. You need to press the button to turn yellow into blue , and the robot dog will patrol the line.

Press the button in the lower left corner of the screen to exit this program.
4. Main source code analysis
line_speed = 10 #巡线的速度 Speed of patrol lineline_color = 'blue' #yellow blue green redline_color_step = 2 #蓝色线 blue line#初始化pid Init pidPx_line = 0.08 # move:0.25 trun 0.15Ix_line = 0Dx_line = 0 #.0001X_line_Middle_error = 160 #图像X轴中心 Image X-axis centerX_line_track_PID = PID.PositionalPID(Px_line, Ix_line, Dx_line) The above line patrol PID parameters, if you want to change the line patrol speed and PID value, just change these parameters in the source code.
xxxxxxxxxx#要识别的颜色阈值 Color threshold to be recognizedcolor_hsv = {"red" : ((0, 70, 72), (7, 255, 255)), "green" : ((54, 109, 78), (77, 255, 255)), "blue" : ((92, 100, 62), (121, 251, 255)), "yellow": ((26, 100, 91), (32, 255, 255))}Here are the hsv parameters of the color. If you want to add a color for the line patrol, you can add one here. Then, combined with the ability to understand the code, add the function of adding color switching by yourself