SunFounder PiCar-S
The function for front wheels’ turning. The main program will call this function if applying the front wheels for
turning. The parameter is the turning angle.
bw.forward()
bw.set_speed(forward_speed)
Here we need two functions for rear wheels. One is to control the rotating direction as forward (for rotating backwards,
bw.backward()
). The second one is to set the rotating speed; the parameter is the speed value (range 0~100). The
bigger the parameter is, the faster the wheel rotates.
1.8.4 Combination
So, this smart car now is smart in three separate features. But, you think only one sensor module is not enough? Try to
combine those sensor modules in one! Here we can show you an experiment - light following with obstacle avoidance
for reference.
When the car runs with the light follower, sometimes it may crash into obstacles when following the light, and it’s
not quite convenient to let the car move back (though we’ve set the car to move backward if the array is [1,0,1], it’s
hard to acquire these values since the the car is moving and the light cannot be exactly as required sometimes). So we
consider Also, you can let the car move backwards by a paper board or your foot, which is quite easy.
Check below the program of this example.
Assemble the light follower module and ultrasonic obstacle avoidance module on the car first.
Log into the Raspberry Pi on your computer via ssh, and get into the directory
cd
~/
SunFounder_PiCar
-
S
/
example
Run the code.
python3 light_with_obsavoidance
.
py
How it works
Set the obstacle avoidance as a superior priority than light following: if there is an obstacle in front of the car, it walk
away from the obstacle and back to the track; if not, then the car will keep follow light.
Since the light following and obstacle avoidance of the car depend on the sensor modules, we set two functions to read
the status of two sensors separately, and assign values to flags to be returned from the functions:
state_light()
, and
state_sonic()
.
In the function
state_sonic()
, the return value is
avoid_flag
.
If the car is
close to
an obstacle, it will return
avoid_flag =2
;
if it is
too close to
the obstacle, it will return
avoid_flag =1
;
if ahead
no obstacle
is detected near, it will return
avoid_flag =0
.
In the function
state_light()
, the return value is
light_flag
.
If the light spot is
in front of
the car, it will return
light_flag = 0
;
if the spot is
at the right side
, it will return
light_flag = 1
;
if the spot is
at the left side
, it will return
light_flag = 2
;
if the spot is
at the back
, it will return
light_flag = 3
;
if
no light spot
is detected, it will return
light_flag = 4
.
68
Chapter 1. About the PiCar-S
Summary of Contents for PiCar-S
Page 1: ...SunFounder PiCar S www sunfounder com Nov 10 2021 ...
Page 2: ......
Page 4: ...ii ...
Page 8: ...SunFounder PiCar S 4 Chapter 1 About the PiCar S ...
Page 9: ...SunFounder PiCar S 1 1 3 Mechanical Fasteners 1 1 Components List 5 ...
Page 10: ...SunFounder PiCar S 1 1 4 Wires 6 Chapter 1 About the PiCar S ...
Page 11: ...SunFounder PiCar S 1 1 5 PCB 1 1 Components List 7 ...
Page 12: ...SunFounder PiCar S 1 1 6 Other Components 8 Chapter 1 About the PiCar S ...
Page 22: ...SunFounder PiCar S 18 Chapter 1 About the PiCar S ...
Page 26: ...SunFounder PiCar S 22 Chapter 1 About the PiCar S ...
Page 27: ...SunFounder PiCar S 1 4 Circuits Building 1 4 1 Connect the Power 1 4 Circuits Building 23 ...
Page 28: ...SunFounder PiCar S 1 4 2 Connect the Modules 24 Chapter 1 About the PiCar S ...
Page 29: ...SunFounder PiCar S 1 4 3 Connect the Servo 1 4 Circuits Building 25 ...
Page 69: ...SunFounder PiCar S Subflow of Line Following Function 1 8 Arming the Car 65 ...
Page 74: ...SunFounder PiCar S 70 Chapter 1 About the PiCar S ...