TMotorCANControl.servo_can module

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

class TMotorCANControl.servo_can.CAN_Manager_servo[source]

Bases: object

A class to manage the low level CAN communication protocols

__init__()[source]

ALl initialization happens in __new__

_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

static buffer_append_int16(buffer, number)[source]

buffer size for int 16

Parameters
  • Buffer – memory allocated to store data.

  • number – value.

static buffer_append_int32(buffer, number)[source]

buffer size for int 32

Parameters
  • Buffer – memory allocated to store data.

  • number – value.

static buffer_append_int64(buffer, number)[source]

buffer size for int 64

Parameters
  • Buffer – memory allocated to store data.

  • number – value.

static buffer_append_uint16(buffer, number)[source]

buffer size for Uint 16

Parameters
  • Buffer – memory allocated to store data.

  • number – value.

static buffer_append_uint32(buffer, number)[source]

buffer size for uint 32

Parameters
  • Buffer – memory allocated to store data.

  • number – value.

static buffer_append_uint64(buffer, number)[source]

buffer size for uint 64

Parameters
  • Buffer – memory allocated to store data.

  • number – value.

comm_can_set_cb(controller_id, current)[source]

Send a servo control message for current brake mode

Parameters
  • controller_id – CAN ID of the motor to send the message to

  • current – current in Amps to use (0 to 60)

comm_can_set_current(controller_id, current)[source]

Send a servo control message for current loop mode

Parameters
  • controller_id – CAN ID of the motor to send the message to

  • current – current in Amps to use (-60 to 60)

comm_can_set_duty(controller_id, duty)[source]

Send a servo control message for duty cycle mode

Parameters
  • controller_id – CAN ID of the motor to send the message to

  • duty – duty cycle (-1 to 1) to use

comm_can_set_origin(controller_id, set_origin_mode)[source]

set the origin

Parameters
  • controller_id – CAN ID of the motor to send the message to

  • set_origin_mode – 0 means setting the temporary origin (power failure elimination), 1 means setting the permanent zero point (automatic parameter saving), 2means restoring the default zero point (automatic parameter saving)

comm_can_set_pos(controller_id, pos)[source]

Send a servo control message for position control mode

Parameters
  • controller_id – CAN ID of the motor to send the message to

  • pos – desired position in degrees

comm_can_set_pos_spd(controller_id, pos, spd, RPA)[source]

Send a servo control message for position control mode, with specified velocity and acceleration This will be a trapezoidal speed profile.

Parameters
  • controller_id – CAN ID of the motor to send the message to

  • pos – desired position in

  • spd – desired max speed in ERPM

  • RPA – desired acceleration

comm_can_set_rpm(controller_id, rpm)[source]

Send a servo control message for velocity control mode

Parameters
  • controller_id – CAN ID of the motor to send the message to

  • rpm – velocity in ERPM (-100000 to 100000)

debug = False

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

parse_servo_message(data)[source]

Unpack the servo message into a servo_motor_state object

Parameters

data – bytes of the message to be processed

Returns

A servo_motor_state object representing the state based on the data recieved.

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.

  • Data – This is obtained from the datasheet.

send_servo_message(motor_id, data, data_len)[source]

Sends a Servo 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.

TMotorCANControl.servo_can.LOG_VARIABLES = ['motor_position', 'motor_speed', 'motor_current', 'motor_temperature']

default variables to be logged

TMotorCANControl.servo_can.Servo_Params = {'AK10-9': {'Curr_max': 1500, 'Curr_min': -1500, 'Current_Factor': 0.59, 'GEAR_RATIO': 9.0, 'Kt_TMotor': 0.16, 'Kt_actual': 0.206, 'P_max': 32000, 'P_min': -32000, 'T_max': 15, 'T_min': -15, 'Use_derived_torque_constants': False, 'V_max': 100000, 'V_min': -100000}, 'AK80-9': {'Curr_max': 1500, 'Curr_min': -1500, 'Current_Factor': 0.59, 'GEAR_RATIO': 9.0, 'Kt_TMotor': 0.091, 'Kt_actual': 0.115, 'NUM_POLE_PAIRS': 21, 'P_max': 32000, 'P_min': -32000, 'T_max': 30, 'T_min': -30, 'Use_derived_torque_constants': False, 'V_max': 32000, 'V_min': -32000}, 'CAN_PACKET_ID': {'CAN_PACKET_SET_CURRENT': 1, 'CAN_PACKET_SET_CURRENT_BRAKE': 2, 'CAN_PACKET_SET_DUTY': 0, 'CAN_PACKET_SET_ORIGIN_HERE': 5, 'CAN_PACKET_SET_POS': 4, 'CAN_PACKET_SET_POS_SPD': 6, 'CAN_PACKET_SET_RPM': 3}, '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 with the parameters needed to control the motor

class TMotorCANControl.servo_can.TMotorManager_servo_can(motor_type='AK80-9', motor_ID=1, max_mosfett_temp=50, CSV_file=None, log_vars=['motor_position', 'motor_speed', 'motor_current', 'motor_temperature'])[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.

__enter__()[source]

Used to safely power the motor on and begin the log file.

__exit__(etype, value, tb)[source]

Used to safely power the motor off and close the log file.

__init__(motor_type='AK80-9', motor_ID=1, max_mosfett_temp=50, CSV_file=None, log_vars=['motor_position', 'motor_speed', 'motor_current', 'motor_temperature'])[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.

_update_state_async(servo_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

servo_state – the servo_state object with the updated 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

property angle_motorside

Motor-side angle in rad

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.

enter_current_brake_control()[source]

Must call this to enable sending current brake commands.

enter_current_control()[source]

Must call this to enable sending current commands.

enter_duty_cycle_control()[source]

Must call this to enable sending duty cycle commands.

enter_idle_mode()[source]

Enter the idle state, where duty cycle is set to 0. (This is the default state.)

enter_position_control()[source]

Must call this to enable position commands.

enter_position_velocity_control()[source]

Must call this to enable sending position commands with specified velocity and accleration limits.

enter_velocity_control()[source]

Must call this to enable sending velocity commands.

property error

Motor error code. 0 means no 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)’

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

power_off()[source]

Powers off the motor.

power_on()[source]

Powers on the motor.

qaxis_current_to_TMotor_current(iq)[source]
set_duty_cycle_percent(duty)[source]

Used for duty cycle mode, to set desired duty cycle. Note, this does not send a command, it updates the TMotorManager’s saved command, which will be sent when update() is called.

Parameters

duty – The desired duty cycle, (-1 to 1)

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 current 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]

Wrapper 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, vel, acc)[source]

Update the current command to the desired position, when in position or position-velocity mode. 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 angle in rad

  • vel – The desired speed to get there in rad/s (when in POSITION_VELOCITY mode)

  • acc – The desired acceleration to get there in rad/s/s, ish (when in POSITION_VELOCITY mode)

set_output_torque_newton_meters(torque)[source]

Used for current 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 velocity 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_zero_position()[source]

Zeros the position

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/logger 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.servo_can._TMotorManState_Servo(value)[source]

Bases: Enum

An Enum to keep track of different control states

CURRENT_BRAKE = 2
CURRENT_LOOP = 1
DUTY_CYCLE = 0
IDLE = 7
POSITION = 4
POSITION_VELOCITY = 6
SET_ORIGIN = 5
VELOCITY = 3
class TMotorCANControl.servo_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.servo_can.servo_command(position, velocity, current, duty, acceleration)[source]

Bases: object

Data structure to store Servo command that will be sent upon update

__init__(position, velocity, current, duty, acceleration)[source]

Sets the motor state to the input.

Parameters
  • position – Position in deg

  • velocity – Velocity in ERPM

  • current – Current in amps

  • duty – Duty cycle in percentage ratio (-1 to 1)

  • acceleration – acceleration in ERPMs

class TMotorCANControl.servo_can.servo_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

  • acceleration – acceleration in rad/s

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

  • acceleration – acceleration in rad/s

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.