Tutorial

Table Of Contents
85
16 Automatic Obstacle Avoidance
The ultrasonic module of this product can only move up and down with the camera, and the left and right
movement can only rotate with the body of the body, and can not move left and right relative to the body, so the
obstacle avoidance function of this robot is relatively simple, as long as there is an obstacle in front Turn left
and retreat if the obstacle is too close, and move forward if the obstacle is far away or there is no obstacle.
The automatic obstacle avoidance function is based on the ultrasonic ranging module, so before writing
your project, you must first put ultra.py RPIservo.py and copy move.py to the same file directory as your
project, and then use the following code to use the automatic obstacle avoidance function.
import ultra
import move
import time
import Adafruit_PCA9685
import RPIservo
# Get the initial data of pwm
def num_import_int(initial):
global r
with open(thisPath+"/RPIservo.py") as f:
for line in f.readlines():
if(line.find(initial) == 0):
r=line
begin=len(list(initial))
snum=r[begin:]
n=int(snum)
return n
scGear = RPIservo.ServoCtrl() # servo control
scanNum = 3 # The car will scan the three parts in front of it, namely left, middle and right, that is, 1
represents left, 2 represents middle, and 3 represents right
scanPos = 1 # The next position to be scanned by the car, the value can be 1, 2, and 3 corresponding to left,
middle and right
scanDir = 1 # The direction of the car scanning, 1 is the leftmost, -1 is the rightmost
scanList = [] # Record the distance of obstacles in three directions that are scanned
scanServo = 1 # Pan-tit servo number
scanRange = 100 # The range of the angle scanned by servo
rangeKeep = 0.7 # Threshold, if it is less than it, turns; if it is greater than it, go back