Uncategorized

PCA9685 PWM motor, camera turn etc

Asked 
Active today
Viewed 16 times
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

 New contributor
0

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.

.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: