1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Puzzlebox - Brainstorms - RC |
---|
5 | # |
---|
6 | # Copyright Steven M. Castellotti (2010) |
---|
7 | # |
---|
8 | # Portions of this code have been previously |
---|
9 | # released under the GNU Pulic License (GPL) version 2 |
---|
10 | # and is Copyright Steven M. Castellotti (2010) |
---|
11 | # For more information please refer to http://www.gnu.org/copyleft/gpl.html |
---|
12 | # |
---|
13 | # Last Update: 2010.01.25 |
---|
14 | # |
---|
15 | ##################################################################### |
---|
16 | |
---|
17 | import sys, time |
---|
18 | import serial |
---|
19 | import jaraco.nxt |
---|
20 | import jaraco.nxt.messages |
---|
21 | import jaraco.nxt.routine |
---|
22 | |
---|
23 | ##################################################################### |
---|
24 | # Globals |
---|
25 | ##################################################################### |
---|
26 | |
---|
27 | DEBUG = 2 |
---|
28 | |
---|
29 | BLUETOOTH_DEVICE='/dev/rfcomm0' |
---|
30 | |
---|
31 | ##################################################################### |
---|
32 | # Classes |
---|
33 | ##################################################################### |
---|
34 | |
---|
35 | class puzzlebox_brainstorms_rc: |
---|
36 | |
---|
37 | def __init__(self, device=BLUETOOTH_DEVICE, DEBUG=DEBUG): |
---|
38 | |
---|
39 | self.DEBUG = DEBUG |
---|
40 | |
---|
41 | self.device=device |
---|
42 | |
---|
43 | |
---|
44 | ################################################################## |
---|
45 | |
---|
46 | def connect_to_nxt(self, device): |
---|
47 | |
---|
48 | conn = jaraco.nxt.Connection(self.device) |
---|
49 | |
---|
50 | if self.DEBUG > 1: |
---|
51 | battery_voltage = jaraco.nxt.routine.get_voltage(conn) |
---|
52 | print "Battery voltage:", |
---|
53 | print battery_voltage |
---|
54 | |
---|
55 | |
---|
56 | return(conn) |
---|
57 | |
---|
58 | |
---|
59 | ################################################################## |
---|
60 | |
---|
61 | def cycle_motor(self, conn, port): |
---|
62 | |
---|
63 | "Turn the motor one direction, then the other, then stop it" |
---|
64 | |
---|
65 | port = jaraco.nxt.routine.get_port(port, \ |
---|
66 | jaraco.nxt.messages.OutputPort) |
---|
67 | cmd = jaraco.nxt.messages.SetOutputState(port, \ |
---|
68 | motor_on=True, \ |
---|
69 | set_power=60, \ |
---|
70 | run_state=jaraco.nxt.messages.RunState.running) |
---|
71 | conn.send(cmd) |
---|
72 | |
---|
73 | time.sleep(2) |
---|
74 | |
---|
75 | cmd = jaraco.nxt.messages.SetOutputState(port, \ |
---|
76 | motor_on=True, \ |
---|
77 | set_power=-60, \ |
---|
78 | run_state=jaraco.nxt.messages.RunState.running) |
---|
79 | conn.send(cmd) |
---|
80 | |
---|
81 | time.sleep(2) |
---|
82 | |
---|
83 | cmd = jaraco.nxt.messages.SetOutputState(port) |
---|
84 | conn.send(cmd) |
---|
85 | |
---|
86 | |
---|
87 | ################################################################## |
---|
88 | |
---|
89 | def run(self): |
---|
90 | |
---|
91 | conn = self.connect_to_nxt(self.device) |
---|
92 | |
---|
93 | self.cycle_motor(conn, 'a') |
---|
94 | |
---|
95 | conn.close() |
---|
96 | |
---|
97 | |
---|
98 | ##################################################################### |
---|
99 | # Functions |
---|
100 | ##################################################################### |
---|
101 | |
---|
102 | |
---|
103 | ##################################################################### |
---|
104 | # Main |
---|
105 | ##################################################################### |
---|
106 | |
---|
107 | if __name__ == '__main__': |
---|
108 | |
---|
109 | # Collect default settings and command line parameters |
---|
110 | device = BLUETOOTH_DEVICE |
---|
111 | |
---|
112 | for each in sys.argv: |
---|
113 | |
---|
114 | if each.startswith("--device="): |
---|
115 | device = each[ len("--device="): ] |
---|
116 | |
---|
117 | |
---|
118 | rc = puzzlebox_brainstorms_rc(device=device, DEBUG=DEBUG) |
---|
119 | |
---|
120 | rc.run() |
---|
121 | |
---|