I need help understanding the code in the turn.py file, which is a part of the this project, please look

# File name   : car_dir.py
# Description : By controlling Servo,the camera can move Up and down,left and right and the Ultrasonic wave can move to left and right.
# Website     : www.adeept.com
# E-mail      : support@adeept.com
# Author      : William
# Date        : 2018/10/12
from __future__ import division
import time

import Adafruit_PCA9685

def replace_num(initial,new_num):   #Call this function to replace data in '.txt' file
    with open("set.txt","r") as f:
        for line in f.readlines():
            if(line.find(initial) == 0):
                line = initial+"%s" %(str_num+"\n")
            newline += line
    with open("set.txt","w") as f:

def num_import_int(initial):        #Call this function to import data from '.txt' file
    with open("set.txt") as f:
        for line in f.readlines():
            if(line.find(initial) == 0):
    return n

#import the settings for servos
vtr_mid_orig    = num_import_int('E_C1:')
hoz_mid_orig    = num_import_int('E_C2:')

turn_right_max  = num_import_int('turn_right_max:')
turn_left_max   = num_import_int('turn_left_max:')
turn_middle     = num_import_int('turn_middle:')

pwm = Adafruit_PCA9685.PCA9685()

def turn_ang(ang):
    if ang < turn_right_max:
        ang = turn_right_max
    elif ang > turn_left_max:
        ang = turn_left_max

def right():
    pwm.set_pwm(2, 0, turn_right_max)

def left():
    pwm.set_pwm(2, 0, turn_left_max)

def middle():
    pwm.set_pwm(2, 0, turn_middle)

def ultra_turn(hoz_mid):
    pwm.set_pwm(1, 0, hoz_mid)

def camera_turn(vtr_mid):
    pwm.set_pwm(0, 0, vtr_mid)

def ahead():
    pwm.set_pwm(1, 0, hoz_mid_orig)
    pwm.set_pwm(0, 0, vtr_mid_orig)

What is pwm.set_pwm() doing in the methods:

  • right()
  • left()
  • middle()
  • ultra_turn()
  • camera_turn()
  • ahead()

Also, what does pwm.set_freq(60) mean? Please refer this link


You need to look at a wiki to see how hobby servos are controlled. Also look up the meaning of PWM.

Basically they expect a series of pulses at about 50 Hz. That means they expect a series of pulses at about 50 per second.

Each pulse has a length. The length determines the angle the servo is meant to go to.

Generally a pulse of length 1500 µs means go to the centre angle. For each 10 µs increase in pulse length it means go another 1 degree clockwise. For each 10 µs decrease in pulse length it means go another degree counterclockwise. All these figures are approximate and need to be calibrated against each servo.

The pwm.set_freq(60) is setting the servo update frequency to 60 times per second. Personally I would use 50.

pwm.set_pwm(channel, 0, angle) is setting the angle of the servo connected to the channel (presumably 0-2 are connected to the pan tilt servos).

I have no idea what the middle 0 parameter is for. Neither do I know how angle is internally converted to a pulse width. You will have to look at the method to see the mapping.

| improve this answer | |
  • 1
    I have no idea. I will only answer the question as given. – joan Mar 1 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.