-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservo_servoblaster.py
executable file
·38 lines (36 loc) · 1.07 KB
/
servo_servoblaster.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
#!/usr/bin/env python
import json
import signal
import subprocess
import sys
def exit_handler(signal, frame):
print('\nExiting...')
node_script.terminate()
f = open('/dev/servoblaster','w') #turn off servo
f.write('0=0\n')
f.close()
servoblaster_daemon.terminate()
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGINT, exit_handler)
if len(sys.argv) > 1:
pin = sys.argv[1]
else:
pin = "22"
print "Servo pin n.22"
node_script = subprocess.Popen(["node","interface.js"],stdout=subprocess.PIPE) #launch nodejs
servoblaster_daemon = subprocess.Popen(["servod","--p1pins="+pin],stdout=subprocess.PIPE) #launch servoblaster
print "listening on port 3000"
position = "0"
while True:
try:
output = json.loads(node_script.stdout.readline()) #obtain a dict from JSON
if output['gamepad_x'] == 0 :
position = "50%"
else:
position = str((output['gamepad_x']+1)/0.02)+"%"
f = open('/dev/servoblaster','w')
f.write('0='+position+"\n")
f.close()
except ValueError:
node_script.stdout.readline() #discard unmeaningful data