Rose-Hulman Robotics Team

Changeset 747 for trunk

Show
Ignore:
Timestamp:
06/06/10 14:31:39 (20 months ago)
Author:
mosttw
Message:

CAN debugging code from yesterday

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/software/rb/can.py

    r739 r747  
    1 #!/usr/bin/python 
     1#!/usr/bin/python -u 
    22# Copyright (C) 2010 Thomas W. Most 
    33# 
     
    6464        escaped_bytes = jaguar_escape(bytes) 
    6565        message = '\xff%c%s' % (len(bytes), bytes) 
     66        print "writing", hex_bytes(message) 
    6667        try: 
    6768            self.serial.write(message) 
     
    7172            raise IOError('CAN error') 
    7273 
    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 
     98def hex_bytes(bytes): 
     99    return ' '.join('{0:02x}'.format(ord(b)) for b in bytes) 
    82100 
    83101def jaguar_escape(plain_bytes): 
     
    111129        api_class << 10 | 
    112130        api_index << 6 | 
    113         device_number 
     131        device_number << 0 
    114132    ) 
    115133 
     
    174192        assert can_bus is not None 
    175193        self.can_bus = can_bus 
    176         self.left_controller_nums = [2, 3, 6] 
     194        self.left_controller_nums = [2, 3] 
    177195        self.right_controller_nums = [4, 5] 
    178196        self.controller_nums = (self.left_controller_nums + 
    179197                                self.right_controller_nums) 
    180198        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): 
    184202        # Configure encoder lines 
    185203        for num in self.controller_nums: 
     
    198216                device_number=num 
    199217            ) 
    200             self.can_bus.send(id, float_to_fixed1616(0.35)) 
     218            self.can_bus.send(id, float_to_fixed1616(p)) 
    201219        for num in self.controller_nums: 
    202220            id = build_can_id( 
     
    205223                device_number=num 
    206224            ) 
    207             self.can_bus.send(id, float_to_fixed1616(0.003)) 
     225            self.can_bus.send(id, float_to_fixed1616(i)) 
    208226        for num in self.controller_nums: 
    209227            id = build_can_id( 
     
    212230                device_number=num 
    213231            ) 
    214             self.can_bus.send(id, float_to_fixed1616(0.001)) 
     232            self.can_bus.send(id, float_to_fixed1616(d)) 
    215233     
    216234        # Enable speed control 
     
    294312            api_class=API_VOLTAGE_CONTROL, 
    295313            api_index=VOLTAGE_SET, 
    296             device_number=6) 
     314            device_number=3) 
     315    bus.send(heartbeat_can_id) 
    297316    try: 
    298317        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) 
    301320            bus.send(set_voltage_can_id, struct.pack('<h', dc)) 
    302321            time.sleep(0.05) 
    303322        print "At full speed (Ctrl-C to ramp down)" 
    304323        while True: 
     324            #bus.send(heartbeat_can_id) 
    305325            bus.send(set_voltage_can_id, struct.pack('<h', 32767)) 
    306326            time.sleep(0.05) 
    307327    except KeyboardInterrupt: 
    308328        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) 
    311331        bus.send(set_voltage_can_id, struct.pack('<h', dc)) 
    312332        time.sleep(0.05) 
    313333 
    314 if __name__ == '__main__': 
    315     demo() 
     334def demo2(): 
    316335    import time 
    317336    bus = CANBus('/dev/ttyS0') 
     
    333352        time.sleep(0.05) 
    334353    print "Duuuuh." 
     354 
     355def 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 
     364def color(s, color): 
     365    n = dict(red=31, green=32)[color] 
     366    return "\x1b[{0}m{1}\x1b[0m".format(n, s) 
     367 
     368def 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 
     398def 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 
     412if __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