Changeset 445
- Timestamp:
- 03/07/09 02:48:26 (3 years ago)
- Location:
- trunk/software
- Files:
-
- 1 added
- 10 modified
-
rb/auto.py (modified) (5 diffs)
-
rb/controller.py (modified) (1 diff)
-
rb/gui/compass.py (modified) (3 diffs)
-
rb/gui/gui.glade (modified) (7 diffs)
-
rb/gui/gui.py (modified) (7 diffs)
-
rb/gui/gui.xml (modified) (5 diffs)
-
rb/gui/mapwidget.py (modified) (3 diffs)
-
rb/shell.py (modified) (2 diffs)
-
rb/transcript.py (added)
-
rb/wiimote.py (modified) (1 diff)
-
rbconfig.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/rb/auto.py
r377 r445 23 23 from math import pi 24 24 25 from cwiid import (Wiimote, LED1_ON, LED2_ON, LED3_ON, LED4_ON, BTN_A,26 FLAG_MESG_IFC, RPT_BTN, RPT_STATUS, RPT_NUNCHUK,27 MESG_ERROR, MESG_NUNCHUK, MESG_BTN,28 ERROR_DISCONNECT, ERROR_COMM)29 25 30 26 from rb.core.logging import log_debug, log_info, log_warn, log_error, log_die … … 43 39 TARGET_POINT = (39.4843167, -87.3222833) 44 40 45 '''46 The zero point of the Wiimote nunchuk.47 '''48 STICK_CENTER = (124, 136)49 41 50 42 … … 99 91 self.stop_wiimote() 100 92 93 def stop(self): 94 self.running = False 95 101 96 def init_sensors(self): 102 97 self.gps = GPS(rbconfig.gps_port) … … 115 110 self.drive.running = False 116 111 117 def init_wiimote(self):118 '''119 Initialize the Wiimote. This needs to be done here because120 cwiid doesn't line running in anything other than the main121 thread.122 '''123 for i in range(10):124 try:125 log_info("auto: Connecting to the Wiimote: Press 1+2")126 self.wiimote = Wiimote(rbconfig.wiimote_addr)127 except RuntimeError:128 pass129 else:130 log_info("auto: Connected to the Wiimote")131 break132 133 if self.wiimote is None:134 log_die("auto: Unable to contact the wiimote")135 sys.exit(1)136 137 # Signal that the connection has been made138 self.wiimote.rumble = True139 time.sleep(.25)140 self.wiimote.rumble = False141 self.wiimote.rpt_mode = RPT_BTN | RPT_STATUS | RPT_NUNCHUK142 143 self.wiimote.enable(FLAG_MESG_IFC)144 self.wiimote.mesg_callback = self.wiimote_mesg145 112 146 def stop_wiimote(self):147 self.wiimote.close()148 self.wiimote = None149 150 def wiimote_control(self):151 '''152 Set the motors based on the Wiimote's Nunchuk's analog stick.153 '''154 x = float(self.stick[0] - STICK_CENTER[0])155 y = float(self.stick[1] - STICK_CENTER[1])156 157 lspeed = x / 127.0 if (abs(x) > 5) else 0158 rspeed = y / 127.0 if (abs(y) > 5) else 0159 160 self.drive.set_speeds(lspeed, rspeed)161 162 def wiimote_mesg(self, messages):163 '''164 Handle messages from the Wiimote. Be careful here, as cwiid165 swallows exceptions.166 '''167 try:168 for type, data in messages:169 if type == MESG_ERROR:170 if data == ERROR_DISCONNECT:171 self.running = False172 log_info("auto: Wiimote disconnected; exiting")173 elif data == ERROR_COMM:174 log_info("auto: ERROR_COMM encountered")175 elif type == MESG_BTN:176 if data & BTN_A:177 self.autonomous = not self.autonomous178 log_info('auto: Toggled autonomous: %s' % self.autonomous)179 elif type == MESG_NUNCHUK:180 self.stick = data['stick']181 except Exception, e:182 print e183 self.running = False184 113 185 114 def get_inputs(self): … … 248 177 self.wiimote.rumble = False 249 178 250 def set_leds(self):251 '''252 Set Wiimote LEDs based on current status.253 '''254 led = LED1_ON255 if self.autonomous:256 led |= LED2_ON257 if self.accel is not None:258 led |= LED3_ON259 if self.coords is not None:260 led |= LED4_ON261 if led != self.led:262 self.led = led263 self.wiimote.led = led264 179 265 180 -
trunk/software/rb/controller.py
r431 r445 1 1 import rbconfig 2 2 import rb.drive 3 import rb.gps 4 import rb.microstrain 5 import rb.wiimote 3 6 4 class Controller( ):7 class Controller(object): 5 8 def __init__(self): 6 self. mode= None7 self.drive = rb.drive.FakeDrive(rbconfig.left_motor_port,8 rbconfig.right_motor_port)9 #self.drive = rb.drive.Drive(rbconfig.left_motor_port,10 # rbconfig.right_motor_port)9 self.navigator = None 10 self.drive = None 11 self.gps = None 12 self.microstrain = None 13 self.wiimote = None 11 14 12 def set_ mode(self, mode):15 def set_navigator(self, nav): 13 16 ''' 14 Set the current mode. Override this method in a subclass15 to do something useful. 17 Set the current navigator. Override this method in a subclass 18 to do something useful. Return True on success, else False. 16 19 ''' 17 self.mode = mode 20 self.navigator = nav 21 return True 22 23 def init_drive(self): 24 if rbconfig.fake_drive: 25 self.drive = rb.drive.FakeDrive(rbconfig.left_motor_port, 26 rbconfig.right_motor_port) 27 else: 28 self.drive = rb.drive.Drive(rbconfig.left_motor_port, 29 rbconfig.right_motor_port) 30 31 def init_gps(self): 32 self.gps = rb.gps.GPS(rbconfig.gps_port) 33 34 def init_microstrain(self): 35 self.microstrain = rb.microstrain.MicroStrain(rbconfig.microstrain_port) 36 37 def init_wiimote(self): 38 self.wiimote = rb.wiimote.Wiimote(rbconfig.wiimote_addr, self) -
trunk/software/rb/gui/compass.py
r425 r445 20 20 21 21 import gtk 22 import cairo 22 23 23 24 … … 55 56 context.set_line_width(line_width) 56 57 context.arc(center_x, center_y, radius, 0, 2 * pi) 57 context.set_source_rgb (1.0, 1.0, 1.0)58 context.set_source_rgba(1.0, 1.0, 1.0, 1.0) 58 59 context.fill_preserve() 59 60 context.set_source_rgb(0, 0, 0) … … 68 69 context.stroke() 69 70 71 # Draw a shiny bit 72 #gleam = cairo.LinearGradient(center_x, center_y + .2 * radius, 73 # center_x - .2 * radius, center_y - radius) 74 #gleam.add_color_stop_rgba(0.0, 1.0, 1.0, 1.0, 0.0) 75 #gleam.add_color_stop_rgba(0.4, 1.0, 1.0, 1.0, 0.3) 76 #gleam.add_color_stop_rgba(1.0, 1.0, 1.0, 1.0, 0.1) 77 #context.arc(center_x, center_y, radius * .96, pi - .1, 0.0) 78 #context.arc_negative(center_x, center_y, radius * .4, .05, pi - .05) 79 #context.set_source(gleam) 80 #context.fill() 81 70 82 # Draw the bob in the center 71 83 context.arc(center_x, center_y, max(3, .01 * radius), 0, 2 * pi) -
trunk/software/rb/gui/gui.glade
r430 r445 1 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 2 <!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd"> 3 <!--Generated with glade3 3.4.5 on Wed Feb 25 23:31:382009 -->3 <!--Generated with glade3 3.4.5 on Sat Mar 7 02:00:15 2009 --> 4 4 <glade-interface> 5 5 <widget class="GtkWindow" id="window"> … … 134 134 <property name="label" translatable="yes">k_d</property> 135 135 <property name="use_underline">True</property> 136 <property name="mnemonic_widget">gain_value_k p</property>136 <property name="mnemonic_widget">gain_value_kd</property> 137 137 </widget> 138 138 <packing> … … 237 237 </child> 238 238 <child> 239 <widget class="GtkHBox" id=" mode">239 <widget class="GtkHBox" id="navigator"> 240 240 <property name="visible">True</property> 241 241 <property name="homogeneous">True</property> 242 242 <child> 243 <widget class="GtkRadioButton" id=" mode_manual">243 <widget class="GtkRadioButton" id="nav_manual"> 244 244 <property name="visible">True</property> 245 245 <property name="can_focus">True</property> … … 247 247 <property name="response_id">0</property> 248 248 <property name="active">True</property> 249 <signal name="toggled" handler="mode_cb"/> 250 </widget> 251 </child> 252 <child> 253 <widget class="GtkRadioButton" id="mode_auto"> 249 <signal name="toggled" handler="nav_cb"/> 250 </widget> 251 </child> 252 <child> 253 <widget class="GtkRadioButton" id="nav_wiimote"> 254 <property name="visible">True</property> 255 <property name="can_focus">True</property> 256 <property name="label" translatable="yes">Wiimote</property> 257 <property name="response_id">0</property> 258 <property name="active">True</property> 259 <property name="group">nav_manual</property> 260 <signal name="toggled" handler="nav_cb"/> 261 </widget> 262 <packing> 263 <property name="position">1</property> 264 </packing> 265 </child> 266 <child> 267 <widget class="GtkRadioButton" id="nav_auto"> 254 268 <property name="visible">True</property> 255 269 <property name="can_focus">True</property> … … 257 271 <property name="response_id">0</property> 258 272 <property name="active">True</property> 259 <property name="group"> mode_manual</property>260 <signal name="toggled" handler=" mode_cb"/>261 </widget> 262 <packing> 263 <property name="position"> 1</property>264 </packing> 265 </child> 266 <child> 267 <widget class="GtkRadioButton" id=" mode_stop">273 <property name="group">nav_manual</property> 274 <signal name="toggled" handler="nav_cb"/> 275 </widget> 276 <packing> 277 <property name="position">2</property> 278 </packing> 279 </child> 280 <child> 281 <widget class="GtkRadioButton" id="nav_stop"> 268 282 <property name="visible">True</property> 269 283 <property name="can_focus">True</property> … … 273 287 <property name="response_id">0</property> 274 288 <property name="active">True</property> 275 <property name="group"> mode_manual</property>276 <signal name="toggled" handler=" mode_cb"/>277 </widget> 278 <packing> 279 <property name="position"> 2</property>289 <property name="group">nav_manual</property> 290 <signal name="toggled" handler="nav_cb"/> 291 </widget> 292 <packing> 293 <property name="position">3</property> 280 294 </packing> 281 295 </child> … … 283 297 <packing> 284 298 <property name="position">1</property> 299 </packing> 300 </child> 301 <child> 302 <widget class="GtkStatusbar" id="statusbar"> 303 <property name="visible">True</property> 304 <property name="spacing">2</property> 305 </widget> 306 <packing> 307 <property name="expand">False</property> 308 <property name="position">2</property> 285 309 </packing> 286 310 </child> -
trunk/software/rb/gui/gui.py
r431 r445 3 3 import gtk 4 4 5 import rbconfig 5 6 from rb.core.event import connect 6 7 from rb.core.logging import log_debug, log_info … … 9 10 from rb.gui.analog import AnalogWidget 10 11 from rb.gui.speedgraph import SpeedGraph 12 from rb.transcript import TranscriptLogger 11 13 12 14 import os.path … … 14 16 # Full path to the GUI definition file 15 17 GTKBUILDER_FILE = os.path.join(os.path.dirname(__file__), 'gui.xml') 18 19 # Map widget names to navigator names 20 WIDGET_NAV_MAP = { 21 'nav_manual': 'analog', 22 'nav_wiimote': 'wiimote', 23 'nav_auto': 'autonomous', 24 'nav_stop': 'stop', 25 } 26 16 27 17 28 class GUI(Controller): … … 23 34 Controller.__init__(self) 24 35 gobject.threads_init() 25 36 37 self.init_drive() 38 self.init_gps() 39 self.init_microstrain() 40 self.init_wiimote() 41 self.transcript = TranscriptLogger('/tmp/rblog.txt', 42 self.drive, self.gps, self.microstrain) 43 26 44 self.arrow_keys_map = { 27 45 gtk.keysyms.Up : ( 0.01, 0.01), … … 38 56 39 57 # Glade-3 -> GtkBuilder fix 40 for btn in [' mode_manual', 'mode_auto', 'mode_stop']:58 for btn in ['nav_manual', 'nav_wiimote', 'nav_auto', 'nav_stop']: 41 59 self.builder.get_object(btn).set_mode(False) 42 60 log_debug("gui: Done creating window") … … 51 69 52 70 self.window.show_all() 53 self.set_mode('manual') 71 self.set_navigator('analog') 72 54 73 gtk.main() 55 74 56 def set_mode(self, mode): 57 Controller.set_mode(self, mode) # set the mode attribute 58 # TODO: disable manual motor control when in autonomous mode 59 if mode == 'stop': 60 self.set_speed(0, 0) 61 elif mode == 'autonomous': 62 print "Autonomous mode not yet implemented" 63 elif mode == 'manual': 64 print "Switching to manual mode" 65 66 self.analog.enabled = (mode == 'manual') 67 75 def wiimote_connect(self): 76 sb = self.builder.get_object('statusbar') 77 context_id = sb.get_context_id('wiimote_connect') 78 # FIXME: Make the first message show up before we do the blocking start() call 79 msg = 'Connecting to the Wiimote at %s' % rbconfig.wiimote_addr 80 print msg 81 sb.push(context_id, msg) 82 try: 83 self.wiimote.start() 84 except IOError: 85 success = False 86 msg = 'Wiimote connection failed' 87 else: 88 success = True 89 msg = 'Connected to the Wiimote at %s' % rbconfig.wiimote_addr 90 sb.pop(context_id) 91 message_id = sb.push(context_id, msg) 92 gobject.timeout_add(3000, lambda: sb.remove(context_id, message_id)) 93 return success 94 95 def set_navigator(self, nav): 96 ''' 97 Set the navigator. Return True if setting it is successful. 98 ''' 99 try: 100 # We need to try switching to the wiimote navigator first since it can fail 101 if nav == 'wiimote': 102 if not self.wiimote_connect(): 103 return False 104 elif nav == 'autonomous': 105 print "Autonomous navigator not yet implemented" 106 return False 107 108 if not Controller.set_navigator(self, nav): # set the navigator attribute 109 return False 110 111 if nav == 'stop': 112 print "Switching to 'stop' navigator" 113 self.set_speed(0, 0) 114 elif nav == 'analog': 115 print "Switching to analog navigator" 116 elif nav == 'wiimote': 117 print "Switching to wiimote navigator" 118 return True 119 finally: 120 self.analog.enabled = (self.navigator == 'analog') 121 self.update_nav_toggles() 68 122 69 123 def key_press_cb(self, widget, event): … … 120 174 log_debug("gui: Window closed") 121 175 122 def mode_cb(self, widget=None, data=None):176 def nav_cb(self, widget=None, data=None): 123 177 ''' 124 Callback for modeselection toggle buttons.178 Callback for navigator selection toggle buttons. 125 179 ''' 126 new_mode = { 127 'mode_stop': 'stop', 128 'mode_manual': 'manual', 129 'mode_auto': 'autonomous', 130 }[widget.get_name()] 131 self.set_mode(new_mode) 180 if widget.get_active(): 181 new_nav = WIDGET_NAV_MAP[widget.get_name()] 182 self.set_navigator(new_nav) 183 184 def update_nav_toggles(self): 185 ''' 186 Update the nav widgets to reflect the current 187 ''' 188 for name, nav in WIDGET_NAV_MAP.items(): 189 active = self.navigator == nav 190 self.builder.get_object(name).set_active(active) -
trunk/software/rb/gui/gui.xml
r430 r445 1 1 <?xml version="1.0"?> 2 <!--Generated with glade3 3.4.5 on Wed Feb 25 23:31:382009 -->2 <!--Generated with glade3 3.4.5 on Sat Mar 7 02:00:15 2009 --> 3 3 <interface> 4 4 <object class="GtkAdjustment" id="adjustment1"> … … 152 152 <property name="label" translatable="yes">k_d</property> 153 153 <property name="use_underline">True</property> 154 <property name="mnemonic_widget">gain_value_k p</property>154 <property name="mnemonic_widget">gain_value_kd</property> 155 155 </object> 156 156 <packing> … … 247 247 </child> 248 248 <child> 249 <object class="GtkHBox" id=" mode">249 <object class="GtkHBox" id="navigator"> 250 250 <property name="visible">True</property> 251 251 <property name="homogeneous">True</property> 252 252 <child> 253 <object class="GtkRadioButton" id=" mode_manual">253 <object class="GtkRadioButton" id="nav_manual"> 254 254 <property name="visible">True</property> 255 255 <property name="can_focus">True</property> 256 256 <property name="label" translatable="yes">Manual</property> 257 257 <property name="active">True</property> 258 <signal handler="mode_cb" name="toggled"/> 259 </object> 260 </child> 261 <child> 262 <object class="GtkRadioButton" id="mode_auto"> 258 <signal handler="nav_cb" name="toggled"/> 259 </object> 260 </child> 261 <child> 262 <object class="GtkRadioButton" id="nav_wiimote"> 263 <property name="visible">True</property> 264 <property name="can_focus">True</property> 265 <property name="label" translatable="yes">Wiimote</property> 266 <property name="active">True</property> 267 <property name="group">nav_manual</property> 268 <signal handler="nav_cb" name="toggled"/> 269 </object> 270 <packing> 271 <property name="position">1</property> 272 </packing> 273 </child> 274 <child> 275 <object class="GtkRadioButton" id="nav_auto"> 263 276 <property name="visible">True</property> 264 277 <property name="can_focus">True</property> 265 278 <property name="label" translatable="yes">Autonomous</property> 266 279 <property name="active">True</property> 267 <property name="group"> mode_manual</property>268 <signal handler=" mode_cb" name="toggled"/>269 </object> 270 <packing> 271 <property name="position"> 1</property>272 </packing> 273 </child> 274 <child> 275 <object class="GtkRadioButton" id=" mode_stop">280 <property name="group">nav_manual</property> 281 <signal handler="nav_cb" name="toggled"/> 282 </object> 283 <packing> 284 <property name="position">2</property> 285 </packing> 286 </child> 287 <child> 288 <object class="GtkRadioButton" id="nav_stop"> 276 289 <property name="visible">True</property> 277 290 <property name="can_focus">True</property> … … 280 293 <property name="image_position">GTK_POS_TOP</property> 281 294 <property name="active">True</property> 282 <property name="group"> mode_manual</property>283 <signal handler=" mode_cb" name="toggled"/>284 </object> 285 <packing> 286 <property name="position"> 2</property>295 <property name="group">nav_manual</property> 296 <signal handler="nav_cb" name="toggled"/> 297 </object> 298 <packing> 299 <property name="position">3</property> 287 300 </packing> 288 301 </child> … … 290 303 <packing> 291 304 <property name="position">1</property> 305 </packing> 306 </child> 307 <child> 308 <object class="GtkStatusbar" id="statusbar"> 309 <property name="visible">True</property> 310 <property name="spacing">2</property> 311 </object> 312 <packing> 313 <property name="expand">False</property> 314 <property name="position">2</property> 292 315 </packing> 293 316 </child> -
trunk/software/rb/gui/mapwidget.py
r332 r445 27 27 'location_clicked': (gobject.SIGNAL_RUN_FIRST, 28 28 gobject.TYPE_NONE, 29 ( gobject.TYPE_DOUBLE, gobject.TYPE_DOUBLE))29 (object,)) 30 30 } 31 31 … … 117 117 # We cannot figure out how to return a tuple here without gtk complaining 118 118 lat, lon = self.convert_to_degrees((event.x, event.y)) 119 self.emit("location_clicked", lat, lon)119 self.emit("location_clicked", (lat, lon)) 120 120 121 121 … … 164 164 Test the `MapWidget`. 165 165 ''' 166 def location_clicked_cb(widget, lat, lon):166 def location_clicked_cb(widget, (lat, lon)): 167 167 widget.draw_point((lat, lon), "green") 168 168 print "location clicked %s %s" % (lat, lon) -
trunk/software/rb/shell.py
r431 r445 26 26 import rbconfig 27 27 from rb.controller import Controller 28 from rb.transcript import TranscriptLogger 28 29 29 30 class Shell(cmd.Cmd, Controller): … … 37 38 cmd.Cmd.__init__(self) 38 39 Controller.__init__(self) 39 self.set_ mode('manual')40 self.set_navigator('shell') 40 41 try: 41 42 self.cmdloop() -
trunk/software/rb/wiimote.py
r370 r445 19 19 20 20 import time 21 from thread import start_new_thread21 import thread 22 22 23 23 import cwiid 24 from cwiid import (LED1_ON, LED4_ON, FLAG_MESG_IFC, RPT_BTN, 25 RPT_STATUS, RPT_NUNCHUK, MESG_ERROR, 26 ERROR_DISCONNECT, ERROR_COMM, MESG_BTN) 24 from cwiid import (LED1_ON, LED2_ON, LED3_ON, LED4_ON, BTN_A, 25 FLAG_MESG_IFC, RPT_BTN, RPT_STATUS, RPT_NUNCHUK, 26 MESG_ERROR, MESG_NUNCHUK, MESG_BTN, 27 ERROR_DISCONNECT, ERROR_COMM) 27 28 28 29 import rbconfig 29 from rb.core.logging import log_debug, log_error, log_info 30 from rb.core.logging import log_debug, log_error, log_info, log_die 31 32 33 ''' 34 The zero point of the Wiimote nunchuk. 35 ''' 36 STICK_CENTER = (124, 136) 30 37 31 38 32 39 class Wiimote(object): 33 40 ''' 34 Controller for Wiimote control of the robot. Some terrible contortions 35 are used here to get the cwiid stuff to run in the main thread --- without 36 that it 41 Controller for Wiimote control of the robot. Because cwiid is 42 sensitive to what thread things are run in (in particular, the 43 ``cwiid.Wiimote`` class must be instantiated in the main 44 thread) some contortions are necessary to accomodate it. 37 45 ''' 38 def __init__(self, addr=''): 46 47 def __init__(self, addr, controller): 48 ''' 49 Create a new Wiimote object. 50 ''' 51 self.running = False 52 self.addr = addr 53 self.controller = controller 54 self.wm = None 55 self.stick = STICK_CENTER # Latest stick data 56 57 58 def start(self): 59 ''' 60 Commence communications with the Wiimote if they have not already 61 been started. Raise an IOError exception if the Wiimote cannot be 62 contacted. 63 ''' 64 if not self.running: 65 self.connect() 66 thread.start_new_thread(self._main, ()) 67 68 def connect(self): 69 ''' 70 Attempt to connect to the Wiimote. Raise an IOError exception 71 on failure. This method may block for a significant period (30 seconds 72 or so) and must be run in the main thread. 73 ''' 74 try: 75 self.wm = cwiid.Wiimote(self.addr) 76 except RuntimeError, e: 77 log_die('Wiimote: Unable to contact the wiimote') 78 raise IOError('Wiimote not found') 39 79 self.running = True 40 self.killed = False # Set to True when the wiimote is powered off41 start_new_thread(self._main, (addr,))42 self._main(addr)43 80 44 def _main(self, addr): 81 # Signal that the connection has been made 82 self.wm.rumble = True 83 time.sleep(.25) 84 self.wm.rumble = False 85 self.wm.rpt_mode = RPT_BTN | RPT_STATUS | RPT_NUNCHUK 86 87 self.wm.enable(FLAG_MESG_IFC) 88 self.wm.mesg_callback = self.handle_mesg 89 90 def stop(self): 91 if self.controller.navigator == 'wiimote': 92 log_info('Wiimote: Switched navigator to manual') 93 self.controller.set_navigator('manual') 94 self.wm.close() 95 self.wm = None 96 self.running = False 97 98 def _main(self): 99 while self.running: 100 if self.controller.navigator == 'wiimote': 101 self._control() 102 time.sleep(.05) 103 else: 104 time.sleep(.1) 105 106 # FIXME: Integrate this somehow 107 def _control(self): 45 108 ''' 46 Continually try to connect to the Wiimote.109 Set the motors based on the Wiimote's Nunchuk's analog stick. 47 110 ''' 48 while self.running: 49 try: 50 self.wm = cwiid.Wiimote(addr) 51 52 # Signal that the connection has been made 53 self.wm.led = LED1_ON | LED4_ON 54 self.wm.rumble = True 55 time.sleep(.25) 56 self.wm.rumble = False 57 self.wm.enable(FLAG_MESG_IFC) 58 self.wm.rpt_mode = RPT_BTN | RPT_STATUS | RPT_NUNCHUK 59 60 self._communicate() 61 except RuntimeError, e: # No wiimote found, or connection lost 62 time.sleep(.1) 63 print e 64 continue 65 66 def _communicate(self): 111 cx, cy = STICK_CENTER 112 x = float(self.stick[0] - cx) 113 y = float(self.stick[1] - cy) 114 115 lspeed = x / 127.0 if (abs(x) > 5) else 0 116 rspeed = y / 127.0 if (abs(y) > 5) else 0 117 118 self.controller.set_speed(lspeed, rspeed) 119 120 def handle_mesg(self, messages): 67 121 ''' 68 Primary wiimote communication loop. 122 Handle messages from the Wiimote. Be careful here, as cwiid 123 swallows exceptions. 69 124 ''' 70 while True:71 for type, data in self.wm.get_mesg():125 try: 126 for type, data in messages: 72 127 if type == MESG_ERROR: 73 128 if data == ERROR_DISCONNECT: 74 self.killed = True75 self. running = False129 log_info("Wiimote: Disconnected") 130 self.stop() 76 131 elif data == ERROR_COMM: 77 132 log_info("Wiimote: ERROR_COMM encountered") 78 133 elif type == MESG_BTN: 79 print "Button(s) pressed: ", data 134 if data & BTN_A: 135 if self.controller.navigator != 'autonomous': 136 self.controller.set_navigator('autonomous') 137 log_info('Wiimote: Switched to autonomous navigator') 80 138 elif type == MESG_NUNCHUK: 81 print "Message:", (type, data) 139 self.stick = data['stick'] 140 except Exception, e: 141 print e 142 self.running = False 143 82 144 83 145 if __name__ == '__main__': -
trunk/software/rbconfig.py
r400 r445 15 15 16 16 # Serial port for the motor controller 17 fake_drive = True 17 18 left_motor_port = '/dev/ttyUSB0' 18 19 right_motor_port = '/dev/ttyUSB1' … … 23 24 24 25 25 # Wiimote control 26 # Wiimote controller to use 27 # Set this to '' to try to connect to any Wiimote 26 28 wiimote_addr = '00:19:1D:2C:F2:37' 27 29

