-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrobo.py
More file actions
87 lines (70 loc) · 2.05 KB
/
robo.py
File metadata and controls
87 lines (70 loc) · 2.05 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# by Carl Monk (@ForToffee)
# github.com/ForToffee/Robosapien
# based on work from http://playground.arduino.cc/Main/RoboSapienIR
# command codes originally from http://www.aibohack.com/robosap/ir_codes.htm
import time
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html
CODE_RSTurnRight = 0x80
CODE_RSRightArmUp = 0x81
CODE_RSRightArmOut = 0x82
CODE_RSTiltBodyRight = 0x83
CODE_RSRightArmDown = 0x84
CODE_RSRightArmIn = 0x85
CODE_RSWalkForward = 0x86
CODE_RSWalkBackward = 0x87
CODE_RSTurnLeft = 0x88
CODE_RSLeftArmUp = 0x89
CODE_RSLeftArmOut = 0x8A
CODE_RSTiltBodyLeft = 0x8B
CODE_RSLeftArmDown = 0x8C
CODE_RSLeftArmIn = 0x8D
CODE_RSStop = 0x8E
CODE_RSWakeUp = 0xB1
CODE_RSBurp = 0xC2
CODE_RSRightHandStrike = 0xC0
CODE_RSNoOp = 0xEF
CYCLE = 833
class Robo(object):
def __init__(self, pin):
self.pin = pin
self.pi = pigpio.pi() # Connect to Pi.
self.pi.set_mode(pin, pigpio.OUTPUT) # IR TX connected to this GPIO.
self.pi.write(self.pin, 1)
self.pi.wave_clear()
self.wf_head = self.add_wave(8, 8)
self.wf_hi = self.add_wave(4, 1)
self.wf_lo = self.add_wave(1, 1)
self.wf_tail = self.add_wave(8, 8)
self.keep_alive = self.create_code(CODE_RSNoOp)
def add_wave(self, hi, lo):
self.pi.wave_add_generic([pigpio.pulse(1<<self.pin, 0, hi * CYCLE), pigpio.pulse(0, 1<<self.pin, lo * CYCLE)])
return self.pi.wave_create()
def create_code(self, code):
data = code
print data
wave = []
wave.append(self.wf_head)
for x in range(8):
if (data & 128 != 0):
wave.append(self.wf_hi)
print 1
else:
wave.append(self.wf_lo)
print 0
data <<= 1
wave.append(self.wf_tail)
print wave
print "end"
return wave
def send_wave(self, wave):
#print wave
self.pi.wave_chain(wave)
while self.pi.wave_tx_busy():
time.sleep(0.002)
self.pi.write(self.pin, 1)
def send_code(self, code):
self.send_wave(self.create_code(code))
time.sleep(0.5)
def clean_up(self):
pi.wave_clear()
pi.stop() # Disconnect from Pi.