for i in range(90,30,-60):
self.pwm_S.setServoPwm('0',i)
time.sleep(0.2)
if i==30:
L = self.get_distance()
elif i==90:
M = self.get_distance()
else:
R = self.get_distance()
self.run_motor(L,M,R)
for i in range(30,151,60):
self.pwm_S.setServoPwm('0',i)
time.sleep(0.2)
if i==30:
L = self.get_distance()
elif i==90:
M = self.get_distance()
else:
R = self.get_distance()
self.run_motor(L,M,R)
ultrasonic=Ultrasonic()
# Main program logic follows:
if __name__ == '__main__':
print ('Program is starting ... ')
try:
ultrasonic.run()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be
executed.
PWM.setMotorModel(0,0,0,0)
ultrasonic.pwm_S.setServoPwm('0',90)
Result analysis
Let servo0 rotate back and forth to 30 degrees, 90 degrees and 150 degrees respectively. And the ultrasonic
module also follows the movement to measure the obstacle distance of these three angles.
When distances detected on the left>30cm, middle >30cm, right>30cm. It means that there is no obstacle
within 30cm. So the car move forward.
When distances detected on the left<30cm, middle <30cm, right<30cm, it means that the car enters a dead
end, so the car move back and turned back.
When distances detected on the left<30cm, middle <30cm, right>30cm. It means that there is an obstacle on
the left side of the car, so the car turn right.
When distances detected on the left>30cm, middle <30cm, right<30cm. It means that there is an obstacle on
the right side of the car, so the car turn left.