This article is an example of the implementation of the servo motor driven robot car mentioned in the previous article. Motion is used to define the motion rules under the factor of distance read from a distance sensor such as an ultrasonic sensor.
Equipment
The equipment in this experiment consisted of
- ESP8266
- ETT’s R-Base, which is a car body kit made of ALUMINUM 3 mm thick.
- Two servos that can rotate 360 degrees.
- The wheeled servo. Or use a crawler set (R-TANK V1 KIT)
- Support wheels
- Power supply (we use a Power Bank)
- Ultrasonic sensor (HC-SR04 SENSOR)
- A Servo
Distance sensor
Using ultrasonic distance sensors or HC-SR04 SENSOR, the principle is to send pulses to trig pin then wait for the echo pin to receive echo, then calculate the distance from echo starting time to echo receiving time.
def distance():
echoPin.value()
trigPin.on()
trigPin.off()
usec = mc.time_pulse_us( echoPin, 1 )
echoTime = usec/1000000
distanceCm = (echoTime/2)*34300
return distanceCm
Connection
The connection between the ESP8266 and the modules is as follows.
ESP8266 | Module | Pin |
---|---|---|
GND | Servo Left | GND |
5V | Servo Left | Vcc |
GPIO5 | Servo Left | Signal |
GND | Servo Right | GND |
5V | Servo Right | Vcc |
GPIO4 | Servo Right | Signal |
GND | Servo Ultrasonic | GND |
5V | Servo Ultrasonic | Vcc |
GPIO2 | Servo Ultrasonic | Signal |
GND | Ultrasonic Sensor | GND |
GPIO12 | Ultrasonic Sensor | Echo |
GPIO14 | Ultrasonic Sensor | Trig |
5V | Ultrasonic Sensor | Vcc |
Example code
The example code12-1 is reading distance from a sensor. If the sensor finds that the distance to the left, right or center is less than 13 cm, it will move backward. But if the distance to the left is the greatest, it will turn left and move forward, if the distance to the right is greatest, it will turn right and move forward.
# code12-1
import esp
import time
import gc
import machine as mc
gc.enable()
gc.collect()
mc.freq(160000000)
servoL = mc.PWM(mc.Pin(5,mc.Pin.OUT),freq=50) # D1 คุมมอเตอร์ซ้าย
servoR = mc.PWM(mc.Pin(4,mc.Pin.OUT),freq=50) # D2 คุมมอเตอร์ขวา
servoU = mc.PWM(mc.Pin(2,mc.Pin.OUT),freq=50) # D4 คุม Ultrasonic
trigPin = mc.Pin(14,mc.Pin.OUT) # D5
echoPin = mc.Pin(12,mc.Pin.IN) # D6
trigPin.value(0)
waitTime = 350
time.sleep_ms(3000) # wait
def distance():
echoPin.value()
trigPin.on()
trigPin.off()
usec = mc.time_pulse_us( echoPin, 1 )
echoTime = usec/1000000
distanceCm = (echoTime/2)*34300
return distanceCm
def sensorMoveCenter():
global servoU
servoU.duty(70)
time.sleep_ms(waitTime)
def sensorMoveLeft():
global servoU
servoU.duty(90)
time.sleep_ms(waitTime)
def sensorMoveRight():
global servoU
servoU.duty(50)
time.sleep_ms(waitTime)
def robotForward():
global servoL,servoR
servoL.duty(120)
servoR.duty(40)
def robotStop():
global servoL,servoR
servoL.duty(0)
servoR.duty(0)
def robotBackward():
global servoL,servoR
servoL.duty(40)
servoR.duty(120)
def robotTurnRight():
global servoL,servoR
servoL.duty(120)
servoR.duty(120)
def robotTurnLeft():
global servoL,servoR
servoL.duty(40)
servoR.duty(40)
while True:
robotStop()
time.sleep_ms(100)
sensorMoveLeft()
distLeft = distance()
sensorMoveRight()
time.sleep_ms(waitTime)
distRight = distance()
sensorMoveCenter()
distCenter = distance()
print("distance at left/right/center = {}/{}/{} cm.".format(distLeft,distRight,distCenter))
if (distCenter < 13.0) or (distLeft < 13.0) or (distRight < 13.0):
robotBackward()
print("backward")
time.sleep_ms(2*waitTime)
else:
if (distLeft > distRight) and (distLeft > distCenter):
print("Left")
robotTurnLeft()
time.sleep_ms(waitTime)
robotStop()
if (distRight > distLeft) and (distRight > distCenter):
print("right")
robotTurnRight()
time.sleep_ms(waitTime)
robotStop()
robotForward()
print("Forward")
time.sleep_ms(waitTime)
Conclusion
From this article, readers can write a python program to read and calculate distances from the HC-SR04 sensor, which uses the principle of calculating the distance from the resonance of sound waves. And apply this knowledge to the wheeled robot to make their decisions and work on the conditions specified which is a simple use of artificial intelligence.
Our team hopes readers will benefit from this article and can be applied to your applications, create a variety of working conditions, be more accurate, create artificial intelligence for car robots to be more subtle.
Finally, have fun with programming.
(C) 2020, Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-05-14