0

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

 #!/usr/bin/python3
# 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
    newline=""
    str_num=str(new_num)
    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:
        f.writelines(newline)

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):
                r=line
    begin=len(list(initial))
    snum=r[begin:]
    n=int(snum)
    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()
pwm.set_pwm_freq(60)

def turn_ang(ang):
    if ang < turn_right_max:
        ang = turn_right_max
    elif ang > turn_left_max:
        ang = turn_left_max
    else:
        pass
    pwm.set_pwm(2,0,ang)

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

tlfong01
  • 4,665
  • 3
  • 10
  • 24
Vaibhav
  • 51
  • 1
  • 6
  • My answer to the following question might help. https://raspberrypi.stackexchange.com/questions/105194/adafruit-servokit-circuitpython-blinka-library-installation-problem – tlfong01 Mar 01 '20 at 08:09
  • Ah let me see. You seem to talk like a PWM newbie, not know even the basic idea. So I would advise be humble, and not to jump start to the ninja only PCA9685. Newbies should first drill the basic kung fu of using GPIO pin in PWM mode, before trying ninja stuff. Below are the references you might take a look: (1) https://raspberrypi.stackexchange.com/questions/102269/rpi4b-pca9685-pwm-controlling-servo-problem, (2) https://raspberrypi.stackexchange.com/questions/104408/how-to-use-rpi-python-to-read-write-convert-to-from-arduino-ppm-and-pwm-servo-c, / to continue, ... – tlfong01 Mar 01 '20 at 10:24
  • (3) https://raspberrypi.stackexchange.com/questions/99408/pin-powered-pi-crashes-when-servos-move (4) https://raspberrypi.stackexchange.com/questions/99315/run-the-program-in-the-laptop-and-use-the-raspberry-gpios-pwm-to-control-servos (5) https://raspberrypi.stackexchange.com/questions/98467/how-can-rpi-move-a-servo-motor-using-a-gpio-pin-in-pwm-mode (6) https://raspberrypi.stackexchange.com/questions/97999/sg-5010-servo-360-degree-modification-not-working (7) https://raspberrypi.stackexchange.com/questions/96830/control-function-for-a-dc-servomotor-angle. Happy reading. Cheers. – tlfong01 Mar 01 '20 at 10:24
  • I skimmed your pwm motor control code and find a weird function called "camera turn()" which seems to be senior level ninja stuff. If you can do this trick, your bad Arduino friends would be very jealous, and respect you more than you deserve, ... :) – tlfong01 Mar 01 '20 at 10:31
  • 1
    @tlfong01Thanks a lot for helping me, sir. Actually I do not have much time and I need to go through all this for school work. – Vaibhav Mar 01 '20 at 11:53
  • Hi @Vaibhav, You are welcome. Good luck. Cheers. – tlfong01 Mar 01 '20 at 12:50

1 Answers1

1

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.

joan
  • 71,024
  • 5
  • 73
  • 106