[1] | 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 = 1 |
---|
| 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 cycle_motor(self, conn, port): |
---|
| 47 | |
---|
| 48 | "Turn the motor one direction, then the other, then stop it" |
---|
| 49 | |
---|
| 50 | port = jaraco.nxt.routine.get_port(port, \ |
---|
| 51 | jaraco.nxt.messages.OutputPort) |
---|
| 52 | cmd = jaraco.nxt.messages.SetOutputState(port, \ |
---|
| 53 | motor_on=True, \ |
---|
| 54 | set_power=60, \ |
---|
| 55 | run_state=jaraco.nxt.messages.RunState.running) |
---|
| 56 | conn.send(cmd) |
---|
| 57 | |
---|
| 58 | time.sleep(2) |
---|
| 59 | |
---|
| 60 | cmd = jaraco.nxt.messages.SetOutputState(port, \ |
---|
| 61 | motor_on=True, \ |
---|
| 62 | set_power=-60, \ |
---|
| 63 | run_state=jaraco.nxt.messages.RunState.running) |
---|
| 64 | conn.send(cmd) |
---|
| 65 | |
---|
| 66 | time.sleep(2) |
---|
| 67 | |
---|
| 68 | cmd = jaraco.nxt.messages.SetOutputState(port) |
---|
| 69 | conn.send(cmd) |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | ################################################################## |
---|
| 73 | |
---|
| 74 | def run(self): |
---|
| 75 | |
---|
| 76 | conn = jaraco.nxt.Connection(self.device) |
---|
| 77 | #jaraco.nxt.routine.cycle_motor_a(conn) |
---|
| 78 | self.cycle_motor(conn, 'a') |
---|
| 79 | |
---|
| 80 | conn.close() |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | ##################################################################### |
---|
| 84 | # Functions |
---|
| 85 | ##################################################################### |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | ##################################################################### |
---|
| 89 | # Main |
---|
| 90 | ##################################################################### |
---|
| 91 | |
---|
| 92 | if __name__ == '__main__': |
---|
| 93 | |
---|
| 94 | # Collect default settings and command line parameters |
---|
| 95 | device = BLUETOOTH_DEVICE |
---|
| 96 | |
---|
| 97 | for each in sys.argv: |
---|
| 98 | |
---|
| 99 | if each.startswith("--device="): |
---|
| 100 | device = each[ len("--device="): ] |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | rc = puzzlebox_brainstorms_rc(device=device, DEBUG=DEBUG) |
---|
| 104 | |
---|
| 105 | rc.run() |
---|
| 106 | |
---|