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
-
My answer to the following question might help. raspberrypi.stackexchange.com/questions/105194/… – tlfong01 2 hours ago
-
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) raspberrypi.stackexchange.com/questions/102269/…, (2) raspberrypi.stackexchange.com/questions/104408/…, / to continue, … – tlfong01 7 mins ago
-
(3) raspberrypi.stackexchange.com/questions/99408/… (4) raspberrypi.stackexchange.com/questions/99315/… (5) raspberrypi.stackexchange.com/questions/98467/… (6) raspberrypi.stackexchange.com/questions/97999/… (7) raspberrypi.stackexchange.com/questions/96830/…. Happy reading. Cheers. – tlfong01 6 mins ago
-
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 just now Edit
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.
-
Respected Sir, Please have a look at motor.py : github.com/adeept/Adeept_PiCar-B/blob/master/server/motor.py Is that also connected to this ? – Vaibhav 1 hour ago
-
-
Categories: Uncategorized