Tutorial

Table Of Contents
65
pwm.set_pwm (3, 0, 300) This method is used to control the rotation of a servo to a certain position,
where 3 is the servo port number, which corresponds to the number identified on the Motor HAT driver board,
but pay attention to the rudder When the machine is connected to the drive board, do not insert the reverse
direction of the ground wire, VCC and signal wire, brown to black, red to red, yellow to yellow; 0 is the deviation
of controlling the rotation of the servo Our program does not use this function to correct the deviation (the
reason for the error of the steering gear can refer to 4.2 Structural Assembly Note); 300 is the PWM duty
cycle value you want to set. According to the different servos, this value represents different servo angles.The
PWM duty cycle range of the servos we use is approximately 100 to 560, which corresponds to a rotation
range of approximately 0 ° to 180 °.
The above code to control the steering gear does not control the rotation speed of the steering gear. If
we want to make a certain steering gear swing back and forth slowly between two positions, we need to use an
increasing or decreasing variable method to control the steering gear.
10.2 Control The Slow Motion of The Steering Gear
import Adafruit_PCA9685 # Import the library used to communicate with PCA9685
import time
pwm = Adafruit_PCA9685.PCA9685()# Instantiate the object used to control the PWM
pwm.set_pwm_freq(50) # Set the frequency of the PWM signal
while 1:
for i in range(0,100): # Slowly move the servo from 300 to 400
pwm.set_pwm(3, 0, (300+i))
time.sleep(0.05)
for i in range(0,100): # Slowly move the servo from 400 to 300
pwm.set_pwm(3, 0, (400-i))
time.sleep(0.05)
The above code can make the steering gear rotate slowly back and forth between 300 and 400, but this
method of controlling the steering gear also has a lot of drawbacks. When the program is executed until the
slow movement of the steering gear will block, this will seriously affect the program Performance, so we
provide a multi-threaded solution in our robot product program to solve this problem.