Changeset 239
- Timestamp:
- 12/02/10 04:12:37 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/brainstorms/Puzzlebox/Brainstorms/Wheelchair_Control.py
r233 r239 19 19 import serial 20 20 21 try: 22 import PySide as PyQt4 23 from PySide import QtCore 24 except: 25 print "Using PyQt4 module" 26 from PyQt4 import QtCore 27 else: 28 print "Using PySide module" 29 21 30 ##################################################################### 22 31 # Globals 23 32 ##################################################################### 33 34 DEBUG = 1 35 36 DEFAULT_COMMAND = 'stop' 37 DEFAULT_SERIAL_DEVICE = '/dev/ttyACM0' 38 39 COMMAND_CHARACTER = 'x' 40 41 WHEELCHAIR_COMMANDS = { 42 'stop': '00110011', 43 } 24 44 25 45 # 'dir' : (low_pin, high_pin) … … 52 72 ##################################################################### 53 73 # Classes 74 ##################################################################### 75 76 class puzzlebox_brainstorms_wheelchair_control(QtCore.QThread): 77 78 def __init__(self, \ 79 device_address=DEFAULT_SERIAL_DEVICE, \ 80 command=DEFAULT_COMMAND, \ 81 DEBUG=DEBUG, \ 82 parent=None): 83 84 QtCore.QThread.__init__(self, parent) 85 86 self.log = None 87 self.DEBUG = DEBUG 88 self.parent = parent 89 90 self.device_address = device_address 91 self.command = command 92 93 self.device = self.initializeSerial() 94 95 96 ################################################################## 97 98 def run(self): 99 100 pass 101 102 103 ################################################################## 104 105 def exitThread(self, callThreadQuit=True): 106 107 try: 108 self.device.stop() 109 except: 110 pass 111 112 #self.wait() 113 if callThreadQuit: 114 QtCore.QThread.quit(self) 115 116 117 ################################################################## 118 119 def initializeSerial(): 120 121 baudrate = 9600 122 bytesize = 8 123 parity = 'NONE' 124 stopbits = 1 125 software_flow_control = 'f' 126 rts_cts_flow_control = 't' 127 #timeout = 15 128 timeout = 5 129 130 # convert bytesize 131 if (bytesize == 5): 132 init_byte_size = serial.FIVEBITS 133 elif (bytesize == 6): 134 init_byte_size = serial.SIXBITS 135 elif (bytesize == 7): 136 init_byte_size = serial.SEVENBITS 137 elif (bytesize == 8): 138 init_byte_size = serial.EIGHTBITS 139 else: 140 #self.log.perror("Invalid value for %s modem byte size! Using default (8)" % modem_type) 141 init_byte_size = serial.EIGHTBITS 142 143 # convert parity 144 if (parity == 'NONE'): 145 init_parity = serial.PARITY_NONE 146 elif (parity == 'EVEN'): 147 init_parity = serial.PARITY_EVEN 148 elif (parity == 'ODD'): 149 init_parity = serial.PARITY_ODD 150 else: 151 #self.log.perror("Invalid value for %s modem parity! Using default (NONE)" % modem_type) 152 init_parity = serial.PARITY_NONE 153 154 # convert stopbits 155 if (stopbits == 1): 156 init_stopbits = serial.STOPBITS_ONE 157 elif (stopbits == 2): 158 init_stopbits = serial.STOPBITS_TWO 159 else: 160 #self.log.perror("Invalid value for %s modem stopbits! Using default (8)" % modem_type) 161 init_byte_size = serial.STOPBITS_ONE 162 163 # convert software flow control 164 if (software_flow_control == 't'): 165 init_software_flow_control = 1 166 else: 167 init_software_flow_control = 0 168 169 # convert rts cts flow control 170 if (rts_cts_flow_control == 't'): 171 init_rts_cts_flow_control = 1 172 else: 173 init_rts_cts_flow_control = 0 174 175 device = serial.Serial(port = self.device_address, \ 176 baudrate = baudrate, \ 177 bytesize = init_byte_size, \ 178 parity = init_parity, \ 179 stopbits = init_stopbits, \ 180 xonxoff = init_software_flow_control, \ 181 rtscts = init_rts_cts_flow_control, \ 182 timeout = timeout) 183 184 device.write('%s%s' % COMMAND_CHARACTER, DEFAULT_COMMAND) 185 if self.DEBUG: 186 print "--> Wheelchair Command:", 187 print DEFAULT_COMMAND 188 189 time.sleep(2) 190 191 return(device) 192 193 194 ##################################################################### 54 195 ##################################################################### 55 196 … … 169 310 else: 170 311 init_rts_cts_flow_control = 0 171 312 172 313 device = serial.Serial(port = '/dev/ttyACM0', \ 173 314 baudrate = baudrate, \ … … 178 319 rtscts = init_rts_cts_flow_control, \ 179 320 timeout = timeout) 180 321 181 322 device.write('x00110011') 182 323 print "sent stop"
Note: See TracChangeset
for help on using the changeset viewer.