TMotorCANControl.mit_can module

A module for controlling the motor in MIT mode over the CAN bus.

class TMotorCANControl.mit_can.CAN_Manager[source]

Bases: object

A class to manage the low level CAN communication protocols

MIT_controller(motor_id, motor_type, position, velocity, Kp, Kd, I)[source]

Sends an MIT style control signal to the motor. This signal will be used to generate a current for the field-oriented controller on the motor control chip, given by this expression:

q_control = Kp*(position - current_position) + Kd*(velocity - current_velocity) + I

Parameters
  • motor_id – The CAN ID of the motor to send the message to

  • motor_type – A string noting the type of motor, ie ‘AK80-9’

  • position – The desired position in rad

  • velocity – The desired velocity in rad/s

  • Kp – The position gain

  • Kd – The velocity gain

  • I – The additional current

__init__()[source]

ALl initialization happens in __new__

static __new__(cls)[source]

Makes a singleton object to manage a socketcan_native CAN bus.

_instance = None

Used to keep track of one instantation of the class to make a singleton object

add_motor(motor)[source]

Subscribe a motor object to the CAN bus to be updated upon message reception

Parameters

motor – The TMotorManager object to be subscribed to the notifier

debug = False

Set to true to display every message sent and recieved for debugging.

static float_to_uint(x, x_min, x_max, num_bits)[source]

Interpolates a floating point number to an unsigned integer of num_bits length. A number of x_max will be the largest integer of num_bits, and x_min would be 0.

Parameters
  • x – The floating point number to convert

  • x_min – The minimum value for the floating point number

  • x_max – The maximum value for the floating point number

  • num_bits – The number of bits for the unsigned integer

static limit_value(value, min, max)[source]

Limits value to be between min and max

Parameters
  • value – The value to be limited.

  • min – The lowest number allowed (inclusive) for value

  • max – The highest number allowed (inclusive) for value

parse_MIT_message(data, motor_type)[source]

Takes a RAW MIT message and formats it into readable floating point numbers.

Parameters
  • data – the bytes of data from a python-can message object to be parsed

  • motor_type – A string noting the type of motor, ie ‘AK80-9’

Returns

An MIT_Motor_State namedtuple that contains floating point values for the position, velocity, current, temperature, and error in rad, rad/s, amps, and *C. 0 means no error.

Notably, the current is converted to amps from the reported ‘torque’ value, which is i*Kt. This allows control based on actual q-axis current, rather than estimated torque, which doesn’t account for friction losses.

power_off(motor_id)[source]

Sends the power off code to motor_id.

Parameters

motor_id – The CAN ID of the motor to send the message to.

power_on(motor_id)[source]

Sends the power on code to motor_id.

Parameters

motor_id – The CAN ID of the motor to send the message to.

send_MIT_message(motor_id, data)[source]

Sends an MIT Mode message to the motor, with a header of motor_id and data array of data

Parameters
  • motor_id – The CAN ID of the motor to send to.

  • data – An array of integers or bytes of data to send.

static uint_to_float(x, x_min, x_max, num_bits)[source]

Interpolates an unsigned integer of num_bits length to a floating point number between x_min and x_max.

Parameters
  • x – The floating point number to convert

  • x_min – The minimum value for the floating point number

  • x_max – The maximum value for the floating point number

  • num_bits – The number of bits for the unsigned integer

zero(motor_id)[source]

Sends the zeroing code to motor_id. This code will shut off communication with the motor for about a second.

Parameters

motor_id – The CAN ID of the motor to send the message to.

TMotorCANControl.mit_can.MIT_Params = {'AK10-9': {'Current_Factor': 0.59, 'GEAR_RATIO': 9.0, 'Kd_max': 5.0, 'Kd_min': 0.0, 'Kp_max': 500.0, 'Kp_min': 0.0, 'Kt_TMotor': 0.16, 'Kt_actual': 0.206, 'P_max': 12.5, 'P_min': -12.5, 'T_max': 65.0, 'T_min': -65.0, 'Use_derived_torque_constants': False, 'V_max': 50.0, 'V_min': -50.0}, 'AK60-6': {'Current_Factor': 0.59, 'GEAR_RATIO': 6.0, 'Kd_max': 5.0, 'Kd_min': 0.0, 'Kp_max': 500.0, 'Kp_min': 0.0, 'Kt_TMotor': 0.068, 'Kt_actual': 0.087, 'P_max': 12.5, 'P_min': -12.5, 'T_max': 15.0, 'T_min': -15.0, 'Use_derived_torque_constants': False, 'V_max': 50.0, 'V_min': -50.0}, 'AK70-10': {'Current_Factor': 0.59, 'GEAR_RATIO': 10.0, 'Kd_max': 5.0, 'Kd_min': 0.0, 'Kp_max': 500.0, 'Kp_min': 0.0, 'Kt_TMotor': 0.095, 'Kt_actual': 0.122, 'P_max': 12.5, 'P_min': -12.5, 'T_max': 25.0, 'T_min': -25.0, 'Use_derived_torque_constants': False, 'V_max': 50.0, 'V_min': -50.0}, 'AK80-6': {'Current_Factor': 0.59, 'GEAR_RATIO': 6.0, 'Kd_max': 5.0, 'Kd_min': 0.0, 'Kp_max': 500.0, 'Kp_min': 0.0, 'Kt_TMotor': 0.091, 'Kt_actual': 0.017, 'P_max': 12.5, 'P_min': -12.5, 'T_max': 12.0, 'T_min': -12.0, 'Use_derived_torque_constants': False, 'V_max': 76.0, 'V_min': -76.0}, 'AK80-64': {'Current_Factor': 0.59, 'GEAR_RATIO': 80.0, 'Kd_max': 5.0, 'Kd_min': 0.0, 'Kp_max': 500.0, 'Kp_min': 0.0, 'Kt_TMotor': 0.119, 'Kt_actual': 0.153, 'P_max': 12.5, 'P_min': -12.5, 'T_max': 144.0, 'T_min': -144.0, 'Use_derived_torque_constants': False, 'V_max': 8.0, 'V_min': -8.0}, 'AK80-9': {'Current_Factor': 0.59, 'GEAR_RATIO': 9.0, 'Kd_max': 5.0, 'Kd_min': 0.0, 'Kp_max': 500.0, 'Kp_min': 0.0, 'Kt_TMotor': 0.091, 'Kt_actual': 0.115, 'P_max': 12.5, 'P_min': -12.5, 'T_max': 18.0, 'T_min': -18.0, 'Use_derived_torque_constants': True, 'V_max': 50.0, 'V_min': -50.0, 'a_hat': [0.0, 1.15605006, 0.000417389589, 0.268556072, 0.049042414]}, 'ERROR_CODES': {0: 'No Error', 1: 'Over temperature fault', 2: 'Over current fault', 3: 'Over voltage fault', 4: 'Under voltage fault', 5: 'Encoder fault', 6: 'Phase current unbalance fault (The hardware may be damaged)'}}

A Dictionary containing the parameters of each type of motor, as well as the error code definitions for the AK-series TMotor actuators. You could use an optional torque model that accounts for friction losses if one is available. So far, such a model is only available for the AK80-9.

This model comes from a linear regression with the following constants:
  • a_hat[0] = bias

  • a_hat[1] = standard torque constant multiplier

  • a_hat[2] = nonlinear torque constant multiplier

  • a_hat[3] = coloumb friction

  • a_hat[4] = gearbox friction

The model has the form: τ = a_hat[0] + gr*(a_hat[1]*kt - a_hat[2]*abs(i))*i - (v/(ϵ + np.abs(v)) )*(a_hat[3] + a_hat[4]*np.abs(i))

with the following values:
  • τ = approximated torque

  • gr = gear ratio

  • kt = nominal torque constant

  • i = current

  • v = velocity

  • ϵ = signum velocity threshold

class TMotorCANControl.mit_can.MIT_command(position, velocity, kp, kd, current)[source]

Bases: object

Data structure to store MIT_command that will be sent upon update

__init__(position, velocity, kp, kd, current)[source]

Sets the motor state to the input.

Parameters
  • position – Position in rad

  • velocity – Velocity in rad/s

  • kp – Position gain

  • kd – Velocity gain

  • current – Current in amps

TMotorCANControl.mit_can.MIT_motor_state

Motor state from the controller, uneditable named tuple

class TMotorCANControl.mit_can.TMotorManager_mit_can(motor_type='AK80-9', motor_ID=1, max_mosfett_temp=50, CSV_file=None, log_vars=['output_angle', 'output_velocity', 'output_acceleration', 'current', 'output_torque'])[source]

Bases: object

The user-facing class that manages the motor. This class should be used in the context of a with as block, in order to safely enter/exit control of the motor.

TMotor_current_to_qaxis_current(iTM)[source]

Try to convert TMotor reported torque to q-axis current

__enter__()[source]

Used to safely power the motor on and begin the log file (if specified).

__exit__(etype, value, tb)[source]

Used to safely power the motor off and close the log file (if specified).

__init__(motor_type='AK80-9', motor_ID=1, max_mosfett_temp=50, CSV_file=None, log_vars=['output_angle', 'output_velocity', 'output_acceleration', 'current', 'output_torque'])[source]

Sets up the motor manager. Note the device will not be powered on by this method! You must call __enter__, mostly commonly by using a with block, before attempting to control the motor.

Parameters
  • motor_type – The type of motor being controlled, ie AK80-9.

  • motor_ID – The CAN ID of the motor.

  • max_mosfett_temp – temperature of the mosfett above which to throw an error, in Celsius

  • CSV_file – A CSV file to output log info to. If None, no log will be recorded.

  • log_vars – The variables to log as a python list. The full list of possibilities is - “output_angle” - “output_velocity” - “output_acceleration” - “current” - “output_torque” - “motor_angle” - “motor_velocity” - “motor_acceleration” - “motor_torque”

_send_command()[source]

Sends a command to the motor depending on whats controlm mode the motor is in. This method is called by update(), and should only be called on its own if you don’t want to update the motor state info.

Notably, the current is converted to amps from the reported ‘torque’ value, which is i*Kt. This allows control based on actual q-axis current, rather than estimated torque, which doesn’t account for friction losses.

_update_state_async(MIT_state)[source]

This method is called by the handler every time a message is recieved on the bus from this motor, to store the most recent state information for later

Parameters

MIT_state – The MIT_Motor_State namedtuple with the most recent motor state.

Raises

RuntimeError when device sends back an error code that is not 0 (0 meaning no error)

property acceleration

Output acceleration in rad/s/s

property acceleration_motorside

Motor-side acceleration in rad/s/s

check_can_connection()[source]

Checks the motor’s connection by attempting to send 10 startup messages. If it gets 10 replies, then the connection is confirmed.

Returns

True if a connection is established and False otherwise.

property current_qaxis

Q-axis current in amps

device_info_string()[source]

Prints the motor’s ID and device type.

property error

Motor error code. 0 means no error.

get_current_qaxis_amps()[source]

Returns: The most recently updated qaxis current in amps

get_motor_acceleration_radians_per_second_squared()[source]

Wrapper for get_output_acceleration that accounts for gear ratio to get motor-side acceleration

Returns

The most recently updated motor-side acceleration in rad/s/s.

get_motor_angle_radians()[source]

Wrapper for get_output_angle that accounts for gear ratio to get motor-side angle

Returns

The most recently updated motor-side angle in rad.

get_motor_error_code()[source]

Returns: The most recently updated motor error code. Note the program should throw a runtime error before you get a chance to read this value if it is ever anything besides 0.

Codes: - 0 : ‘No Error’, - 1 : ‘Over temperature fault’, - 2 : ‘Over current fault’, - 3 : ‘Over voltage fault’, - 4 : ‘Under voltage fault’, - 5 : ‘Encoder fault’, - 6 : ‘Phase current unbalance fault (The hardware may be damaged)’

get_motor_torque_newton_meters()[source]

Wrapper for get_output_torque that accounts for gear ratio to get motor-side torque

Returns

The most recently updated motor-side torque in Nm.

get_motor_velocity_radians_per_second()[source]

Wrapper for get_output_velocity that accounts for gear ratio to get motor-side velocity

Returns

The most recently updated motor-side velocity in rad/s.

get_output_acceleration_radians_per_second_squared()[source]
Returns

The most recently updated output acceleration in radians per second per second

get_output_angle_radians()[source]

Returns: The most recently updated output angle in radians

get_output_torque_newton_meters()[source]
Returns

the most recently updated output torque in Nm

get_output_velocity_radians_per_second()[source]
Returns

The most recently updated output velocity in radians per second

get_temperature_celsius()[source]

Returns: The most recently updated motor temperature in degrees C.

property position

Output angle in rad

property position_motorside

Motor-side angle in rad

power_off()[source]

Powers off the motor.

power_on()[source]

Powers on the motor. You may hear a faint hiss.

qaxis_current_to_TMotor_current(iq)[source]

Try to convert q-axis current to TMotor reported torque

set_current_gains(kp=40, ki=400, ff=128, spoof=False)[source]

Uses plain current mode, will send 0.0 for position gains in addition to requested current.

Parameters
  • kp – A dummy argument for backward compatibility with the dephy library.

  • ki – A dummy argument for backward compatibility with the dephy library.

  • ff – A dummy argument for backward compatibility with the dephy library.

  • spoof – A dummy argument for backward compatibility with the dephy library.

set_impedance_gains_real_unit(kp=0, ki=0, K=0.08922, B=0.003807, ff=0)[source]

Uses plain impedance mode, will send 0.0 for current command in addition to position request.

Parameters
  • kp – A dummy argument for backward compatibility with the dephy library.

  • ki – A dummy argument for backward compatibility with the dephy library.

  • K – The stiffness in Nm/rad

  • B – The damping in Nm/(rad/s)

  • ff – A dummy argument for backward compatibility with the dephy library.

set_impedance_gains_real_unit_full_state_feedback(kp=0, ki=0, K=0.08922, B=0.003807, ff=0)[source]

” Uses full state feedback mode, will send whatever current command is set in addition to position request.

Parameters
  • kp – A dummy argument for backward compatibility with the dephy library.

  • ki – A dummy argument for backward compatibility with the dephy library.

  • K – The stiffness in Nm/rad

  • B – The damping in Nm/(rad/s)

  • ff – A dummy argument for backward compatibility with the dephy library.

set_motor_angle_radians(pos)[source]

Wrapper for set_output_angle that accounts for gear ratio to control motor-side angle

Parameters

pos – The desired motor-side position in rad.

set_motor_current_qaxis_amps(current)[source]

Used for either current or full state feedback mode to set current command. Note, this does not send a command, it updates the TMotorManager’s saved command, which will be sent when update() is called.

Parameters

current – the desired current in amps.

set_motor_torque_newton_meters(torque)[source]

Version of set_output_torque that accounts for gear ratio to control motor-side torque

Parameters

torque – The desired motor-side torque in Nm.

set_motor_velocity_radians_per_second(vel)[source]

Wrapper for set_output_velocity that accounts for gear ratio to control motor-side velocity

Parameters

vel – The desired motor-side velocity in rad/s.

set_output_angle_radians(pos)[source]

Used for either impedance or full state feedback mode to set output angle command. Note, this does not send a command, it updates the TMotorManager’s saved command, which will be sent when update() is called.

Parameters

pos – The desired output position in rads

set_output_torque_newton_meters(torque)[source]

Used for either current or MIT Mode to set current, based on desired torque. If a more complicated torque model is available for the motor, that will be used. Otherwise it will just use the motor’s torque constant.

Parameters

torque – The desired output torque in Nm.

set_output_velocity_radians_per_second(vel)[source]

Used for either speed or full state feedback mode to set output velocity command. Note, this does not send a command, it updates the TMotorManager’s saved command, which will be sent when update() is called.

Parameters

vel – The desired output speed in rad/s

set_speed_gains(kd=1.0)[source]

Uses plain speed mode, will send 0.0 for position gain and for feed forward current.

Parameters

kd – The gain for the speed controller. Control law will be (v_des - v_actual)*kd = iq

set_zero_position()[source]

Zeros the position–like a scale you have to wait about a second before you can use the motor again. This responsibility is on the user!!

property temperature

Temperature in Degrees Celsius

property torque

Output torque in Nm

property torque_motorside

Motor-side torque in Nm

update()[source]

This method is called by the user to synchronize the current state used by the controller with the most recent message recieved, as well as to send the current command.

property velocity

Output velocity in rad/s

property velocity_motorside

Motor-side velocity in rad/s

class TMotorCANControl.mit_can._TMotorManState(value)[source]

Bases: Enum

An Enum to keep track of different control states

CURRENT = 2
FULL_STATE = 3
IDLE = 0
IMPEDANCE = 1
SPEED = 4
class TMotorCANControl.mit_can.motorListener(canman, motor)[source]

Bases: Listener

Python-can listener object, with handler to be called upon reception of a message on the CAN bus

__init__(canman, motor)[source]

Sets stores can manager and motor object references

Parameters
  • canman – The CanManager object to get messages from

  • motor – The TMotorCANManager object to update

_abc_impl = <_abc_data object>
on_message_received(msg)[source]

Updates this listener’s motor with the info contained in msg, if that message was for this motor.

Parameters

msg – A python-can CAN message

class TMotorCANControl.mit_can.motor_state(position, velocity, current, temperature, error, acceleration)[source]

Bases: object

Data structure to store and update motor states

__init__(position, velocity, current, temperature, error, acceleration)[source]

Sets the motor state to the input.

Parameters
  • position – Position in rad

  • velocity – Velocity in rad/s

  • current – current in amps

  • temperature – temperature in degrees C

  • error – error code, 0 means no error

set_state(position, velocity, current, temperature, error, acceleration)[source]

Sets the motor state to the input.

Parameters
  • position – Position in rad

  • velocity – Velocity in rad/s

  • current – current in amps

  • temperature – temperature in degrees C

  • error – error code, 0 means no error

set_state_obj(other_motor_state)[source]

Sets this motor state object’s values to those of another motor state object.

Parameters

other_motor_state – The other motor state object with values to set this motor state object’s values to.