- Timestamp:
- 06/06/10 14:31:39 (20 months ago)
- Files:
-
- 1 modified
-
trunk/software/rb/can.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/software/rb/can.py
r739 r747 1 #!/usr/bin/python 1 #!/usr/bin/python -u 2 2 # Copyright (C) 2010 Thomas W. Most 3 3 # … … 64 64 escaped_bytes = jaguar_escape(bytes) 65 65 message = '\xff%c%s' % (len(bytes), bytes) 66 print "writing", hex_bytes(message) 66 67 try: 67 68 self.serial.write(message) … … 71 72 raise IOError('CAN error') 72 73 73 #def receive(self): 74 # ''' 75 # Receive a message from the can bus. Return a two-tuple of 76 # (can_id, bytes) if a valid message is received, or raises 77 # IOError if an invalid message is received. 78 # ''' 79 # message = '' 80 # while True: 81 # 74 def recieve(self): 75 ''' 76 Receive a message from the can bus. Return a two-tuple of 77 (can_id, bytes) if a valid message is received, or raises 78 IOError if an invalid message is received. 79 ''' 80 message = '' 81 while self.serial.inWaiting(): 82 message += self.serial.read(self.serial.inWaiting()) 83 print "read", hex_bytes(message) 84 return message 85 86 def send_heartbeat(self): 87 # Broadcast message on the system heartbeat API 88 can_id = build_can_id( 89 device_type=DEVICE_TYPE_BROADCAST, 90 manufacturer=MANF_BROADCAST, 91 api_class=0, 92 api_index=SYS_API_HEARTBEAT, 93 #api_index=0, 94 device_number=0 95 ) 96 self.send(can_id) 97 98 def hex_bytes(bytes): 99 return ' '.join('{0:02x}'.format(ord(b)) for b in bytes) 82 100 83 101 def jaguar_escape(plain_bytes): … … 111 129 api_class << 10 | 112 130 api_index << 6 | 113 device_number 131 device_number << 0 114 132 ) 115 133 … … 174 192 assert can_bus is not None 175 193 self.can_bus = can_bus 176 self.left_controller_nums = [2, 3 , 6]194 self.left_controller_nums = [2, 3] 177 195 self.right_controller_nums = [4, 5] 178 196 self.controller_nums = (self.left_controller_nums + 179 197 self.right_controller_nums) 180 198 if mode == 'speed': 181 self.init_speed_control( )182 183 def init_speed_control(self ):199 self.init_speed_control(p=0.35, i=0.003, d=0.001) 200 201 def init_speed_control(self, p, i, d): 184 202 # Configure encoder lines 185 203 for num in self.controller_nums: … … 198 216 device_number=num 199 217 ) 200 self.can_bus.send(id, float_to_fixed1616( 0.35))218 self.can_bus.send(id, float_to_fixed1616(p)) 201 219 for num in self.controller_nums: 202 220 id = build_can_id( … … 205 223 device_number=num 206 224 ) 207 self.can_bus.send(id, float_to_fixed1616( 0.003))225 self.can_bus.send(id, float_to_fixed1616(i)) 208 226 for num in self.controller_nums: 209 227 id = build_can_id( … … 212 230 device_number=num 213 231 ) 214 self.can_bus.send(id, float_to_fixed1616( 0.001))232 self.can_bus.send(id, float_to_fixed1616(d)) 215 233 216 234 # Enable speed control … … 294 312 api_class=API_VOLTAGE_CONTROL, 295 313 api_index=VOLTAGE_SET, 296 device_number=6) 314 device_number=3) 315 bus.send(heartbeat_can_id) 297 316 try: 298 317 print "Ramping up" 299 for dc in range(0, 32767, 10 ):300 bus.send(heartbeat_can_id, '')318 for dc in range(0, 32767, 100): 319 #bus.send(heartbeat_can_id) 301 320 bus.send(set_voltage_can_id, struct.pack('<h', dc)) 302 321 time.sleep(0.05) 303 322 print "At full speed (Ctrl-C to ramp down)" 304 323 while True: 324 #bus.send(heartbeat_can_id) 305 325 bus.send(set_voltage_can_id, struct.pack('<h', 32767)) 306 326 time.sleep(0.05) 307 327 except KeyboardInterrupt: 308 328 print "Ramping down" 309 for dc in range(32767, 0, -10 ):310 bus.send(heartbeat_can_id, '')329 for dc in range(32767, 0, -100): 330 #bus.send(heartbeat_can_id) 311 331 bus.send(set_voltage_can_id, struct.pack('<h', dc)) 312 332 time.sleep(0.05) 313 333 314 if __name__ == '__main__': 315 demo() 334 def demo2(): 316 335 import time 317 336 bus = CANBus('/dev/ttyS0') … … 333 352 time.sleep(0.05) 334 353 print "Duuuuh." 354 355 def demo_heartbeat(): 356 import time 357 bus = CANBus('/dev/ttyS0') 358 print "Sending heartbeat messages", 359 while True: 360 bus.send_heartbeat() 361 print ".", 362 time.sleep(1.0 / 150.0) 363 364 def color(s, color): 365 n = dict(red=31, green=32)[color] 366 return "\x1b[{0}m{1}\x1b[0m".format(n, s) 367 368 def demo_set_voltage(bus, controller_num): 369 import time 370 enable_voltage_control_can_id = build_can_id( 371 api_class=API_VOLTAGE_CONTROL, 372 api_index=VOLTAGE_MODE_ENABLE, 373 device_number=controller_num) 374 set_voltage_can_id = build_can_id( 375 api_class=API_VOLTAGE_CONTROL, 376 api_index=VOLTAGE_SET, 377 device_number=controller_num) 378 dc = 0x0800 379 380 bus.send_heartbeat() 381 382 print color("Enabling voltage control", "green") 383 bus.send(enable_voltage_control_can_id) 384 while True: 385 bus.send_heartbeat() 386 time.sleep(1.0 / 150.0) 387 if bus.recieve(): 388 break 389 390 print color("Setting voltage to 0x{0:04x}".format(dc), "green") 391 bus.send(set_voltage_can_id, struct.pack('<h', dc)) 392 while True: 393 bus.send_heartbeat() 394 time.sleep(1.0 / 150.0) 395 if bus.recieve(): 396 break 397 398 def demo_query_voltage(bus, controller_num): 399 import time 400 set_voltage_can_id = build_can_id( 401 api_class=API_VOLTAGE_CONTROL, 402 api_index=VOLTAGE_SET, 403 device_number=controller_num) 404 print color("Reading voltage", "green") 405 bus.send(set_voltage_can_id) 406 while True: 407 bus.send_heartbeat() 408 time.sleep(1.0 / 150.0) 409 if bus.recieve(): 410 break 411 412 if __name__ == '__main__': 413 demo_heartbeat() 414 bus = CANBus('/dev/ttyS0') 415 num = 2 416 demo_set_voltage(bus, num) 417 demo_query_voltage(bus, num) 418

