예제 #1
0
def node2_read_variable(ser, variable_id):
    can_cmd.can_transmit(ser, msg_id=variable_id, msg_len=0, msg_data=[])
    try:
        return comms.read_signed_32_from_node2(ser)
    except comms.ReadException:
        print("Trying to resync!")
        basic_cmd.synchronize(ser)
예제 #2
0
def test_regulator_init(ser):
    """ Tries to initialize the regulator

        If it works we should get a can mesage with ID 139, and the encoder
        value should be between 3000 and 4000
    """
    mcp2515_cmd.write_can_rx_flag(ser, 0)   # Disable node 1 can reception
    can_cmd.can_transmit(ser, 138, 1, [1])

    init_success = 0
    for i in range(0, 50):
        returncode = can_cmd.can_receive(ser)

        if returncode != 1:
            if returncode[0] == 139:
                init_success = 1
                break

        time.sleep(0.5)
    
    assert init_success == 1
    
    mcp2515_cmd.write_can_rx_flag(ser, 1)
    time.sleep(3)

    encoder_value = motor_read_encoder(ser)
    print("Encoder_value", motor_read_encoder(ser))

    assert encoder_value > 3000
    assert encoder_value < 4000
예제 #3
0
def test_can_loopback(ser):
    # Set the device in loopback mode
    assert mcp.mcp2515_init(ser, mcp.MCP_MODE.LOOPBACK) == mcp.MCP_MODE.LOOPBACK
    mcp.write_can_rx_flag(ser, 0)

    # After reset, we should not have a pending receive buffer
    result = can_cmd.can_pending_rx_buffer(ser)
    assert result[1] == 3   # The number 3 indicates no pending buffer

    # Construct a message to send
    msg_id = 10
    msg_data = [1, 2, 3, 4, 5]
    msg_len = len(msg_data)

    # Try to send the message. It returns 0 on success
    assert can_cmd.can_transmit(ser, msg_id, msg_len, msg_data) == 0

    time.sleep(0.5)

    # Check to see if we have a pending receive buffer
    result = can_cmd.can_pending_rx_buffer(ser)
    assert result[1] != 3   # 0 or 1 is a valid buffer, 3 is not
    mcp.write_can_rx_flag(ser, 1)

    # Read out the message
    received_msg = can_cmd.can_receive(ser)

    transmit_msg = [msg_id, msg_len]
    transmit_msg.extend(msg_data)

    print("Transmitted:", transmit_msg)
    print("Received   :", received_msg)
    assert received_msg == transmit_msg
예제 #4
0
def motor_set_output(ser, direction, power):
    data = [direction]
    data.extend(list(power.to_bytes(2, byteorder='big')))
    can_cmd.can_transmit(ser, msg_id=1000, msg_len=3, msg_data=data)
예제 #5
0
def regulator_set_setpoint(ser, setpoint):
    data = list(setpoint.to_bytes(4, byteorder='big', signed=True))
    print(data)
    can_cmd.can_transmit(ser, msg_id=903, msg_len=4, msg_data=data)
예제 #6
0
def regulator_set_p_gain(ser, p_gain):
    data = list(p_gain.to_bytes(2, byteorder='big'))
    can_cmd.can_transmit(ser, msg_id=901, msg_len=2, msg_data=data)
예제 #7
0
def test_can_transmit(ser):
    msg_id = 10
    msg_data = [1, 2, 3, 4, 5]
    msg_len = len(msg_data)
    assert can_cmd.can_transmit(ser, msg_id, msg_len, msg_data) == 0