source: trunk/brainstorms/Puzzlebox/Brainstorms/Wheelchair_Control.py @ 228

Last change on this file since 228 was 228, checked in by sc, 12 years ago

trunk/brainstorms/Puzzlebox/Brainstorms/Wheelchair_Control.py:

  • minor tweaks to permit reverse-engineering of protocol
  • Property svn:executable set to *
File size: 2.8 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Puzzlebox - Brainstorms - Wheelchair Control
5#
6# Copyright Puzzlebox Productions, LLC (2010)
7#
8# This code is released under the GNU Pulic License (GPL) version 2
9# For more information please refer to http://www.gnu.org/copyleft/gpl.html
10
11__changelog__ = """
12Last Update: 2010.11.29
13
14"""
15
16import time
17import signal
18#import parallel
19
20#####################################################################
21# Globals
22#####################################################################
23
24# 'dir' : (low_pin, high_pin)
25CONTROLLS = {
26                'fwd'   : (1<<1, 1<<0),
27                'rev'   : (1<<3, 1<<2),
28                'right' : (1<<5, 1<<4),
29                'left'  : (1<<7, 1<<6),
30                }
31
32MOVE_COMANDS ={
33                ' ' : 'stop',
34                'i' : 'fwd',
35                'm' : 'rev',
36                'j' : 'left',
37                'k' : 'right',
38                }
39
40SPEED_COMMANDS = {
41                '1' : 'speed-1',
42                '2' : 'speed-2',
43                '3' : 'speed-3',
44                }
45
46STOP_TIME = 0
47STOP_INTERVAL = 0.2
48ALARM_INTERVAL = 0.1
49#PORT = parallel.Parallel()
50
51#####################################################################
52# Classes
53#####################################################################
54
55class Getch:
56        def __init__(self):
57                import tty, sys
58
59        def __call__(self):
60                import sys, tty, termios
61                fd = sys.stdin.fileno()
62                old_settings = termios.tcgetattr(fd)
63                try:
64                        tty.setraw(sys.stdin.fileno())
65                        ch = sys.stdin.read(1)
66                finally:
67                        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
68                return ch
69
70
71#####################################################################
72# Functions
73#####################################################################
74
75def alarmHandler(arg1, arg2):
76        print 'alarm!'
77        if STOP_TIME < time.time():
78                stopBot()
79
80def initAlarm():
81        signal.alarm(ALARM_INTERVAL)
82        signal.signal(signal.SIGALRM, alarmHandler)
83
84def setOutput(data):
85        output = data ^ int('00110011', 2)
86        #PORT.setData(output)
87        print 'Output set to: ', int2bin(output)
88        print 'commands: i j k m to move, SPACE = stop, x = quit'
89
90def moveBot(dir, speed):
91        output = 0
92        pins = CONTROLLS[dir]
93        if speed == 1:
94                output = pins[0]
95        elif speed == 2:
96                output = pins[1]
97        elif speed == 3:
98                output = pins[0] | pins[1]
99        setOutput(output)
100        time.sleep(STOP_INTERVAL)
101        stopBot()
102
103def stopBot():
104        setOutput(0)
105
106def int2bin(n, count=8):
107        return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)])
108
109
110#####################################################################
111# Main
112#####################################################################
113
114MYGETCH = Getch()
115
116#initAlarm()
117
118stopBot()
119
120speed = 1
121while True:
122        cmd = MYGETCH()
123        if cmd == 'x':
124                exit()
125        if cmd in SPEED_COMMANDS.keys():
126                speed = int(cmd)
127                print SPEED_COMMANDS[cmd]
128        elif cmd in MOVE_COMANDS.keys():
129                if MOVE_COMANDS[cmd] == 'stop':
130                        stopBot()
131                else:
132                        print MOVE_COMANDS[cmd]
133                        moveBot(MOVE_COMANDS[cmd], speed)
134                        STOP_TIME = time.time() + STOP_INTERVAL
Note: See TracBrowser for help on using the repository browser.