-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathservo_type_position.py
38 lines (31 loc) · 1.15 KB
/
servo_type_position.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from Raspi_MotorHAT.Raspi_PWM_Servo_Driver import PWM
import atexit
pwm = PWM(0x6f)
# This sets the timebase for it all
pwm_frequency = 60
pwm.setPWMFreq(pwm_frequency)
# Frequency is 1/period, but working ms, we can use 1000
period_in_ms = 1000.0 / pwm_frequency
# The chip has 4096 steps in each period.
pulse_steps = 4096.0
# Mid point of the servo pulse length in milliseconds.
servo_mid_point_ms = 1.5
# What a deflection of 90 degrees is in pulse length in milliseconds
deflect_90_in_ms = 0.9
# Steps for every millisecond.
steps_per_ms = pulse_steps / period_in_ms
# Steps for a degree
steps_per_degree = (deflect_90_in_ms * steps_per_ms) / 90.0
# Mid point of the servo in steps
servo_mid_point_steps = servo_mid_point_ms * steps_per_ms
def convert_degrees_to_pwm(position):
return int(servo_mid_point_steps + (position * steps_per_degree))
def stop():
# Set pin off flag
pwm.setPWM(0, 0, 4096)
atexit.register(stop)
import time
while (True):
position = int(raw_input("Type your position in degrees (90 to -90, 0 is middle): "))
end_step = convert_degrees_to_pwm(position)
pwm.setPWM(0, 0, end_step)