-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathz_pwm.py
60 lines (47 loc) · 1.26 KB
/
z_pwm.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import division
import RPi.GPIO as GPIO
import time
from UDPComms import Subscriber, timeout
import math
target_vel = Subscriber(8410, timeout = 1)
pwm_pin = 14 #TODO CAHNGE
dir_pin = 15
GPIO.setmode(GPIO.BCM)
GPIO.setup(pwm_pin, GPIO.OUT)
GPIO.setup(dir_pin, GPIO.OUT)
pwm = GPIO.PWM(pwm_pin, 50)
GPIO.output(dir_pin, True)
pwm.start(0)
time.sleep(1)
try:
while True:
try:
cmd = target_vel.get()['z']
except timeout:
print "TIMEOUT No commands received"
print('driving z at', 0)
pwm.ChangeDutyCycle(0)
except:
print('driving z at', 0)
pwm.ChangeDutyCycle(0)
raise
else:
if cmd < 0:
GPIO.output(dir_pin, False)
print('down')
else:
GPIO.output(dir_pin, True)
print('up')
# pwm_duty is in % so 0 - 100
# but motor is 12V and we are using 40V
# so don't got above 25%
pwm_duty = 14*math.fabs(cmd)
print('Driving z at',pwm_duty, cmd)
pwm.ChangeDutyCycle(pwm_duty)
except:
pass
finally:
pwm.ChangeDutyCycle(0)
pwm.stop()
GPIO.output(pwm_pin, False)
GPIO.cleanup()