A python driver for the CyberGear motor.
To see this API connected to a GUI, checkout the CyberGear Dashboard.
Install the package from pip:
pip install CyberGearDriver
Since there are so many CAN interfaces available, this library doesn't have a built-in CAN driver but provides a simple method to connect it to your CAN library of choice.
For example, using a Serial CAN USB adapter with python-can
would look something like this:
import can
from CyberGearDriver import CyberGearMotor, RunMode, CyberMotorMessage
# Connect to the bus with python-can
# @see https://python-can.readthedocs.io/en/stable/installation.html
bus = can.interface.Bus(
interface='slcan',
channel='/dev/cu.usbmodem101',
bitrate=1000000,
)
# Create function to pass the messages from CyberGear to the CAN bus
def send_message(message: CyberMotorMessage):
bus.send(
can.Message(
arbitration_id=message.arbitration_id,
data=message.data,
is_extended_id=message.is_extended_id,
)
)
# Create the motor controller
motor = CyberGearMotor(motor_id=127, send_message=send_message)
# Send the CyberGear driver messages received from the CAN bus
notifier = can.Notifier(bus, [motor.message_received])
The motor has 4 different drive modes:
- Operation control mode (sometimes call MIT control mode)
- Position mode
- Velocity mode
- Torque mode (or current mode)
This is sometimes called MIT mode and combines position, speed, and torque to move the motor in a single function:
motor.enable()
motor.mode(RunMode.OPERATION_CONTROL)
# Move the motor to position 6 (in radians from the zero position)
motor.control(position=6, velocity=0, torque=0, kp=0.1, kd=0.1)
In this mode the motor will move to the loc_ref
parameter value (see section 4.1.12 for a list of parameters).
motor.enable()
motor.mode(RunMode.POSITION)
# Limit the top speed
motor.set_parameter("limit_spd", 10)
# Move to position 8 rad
motor.set_parameter("loc_ref", 8)
This mode continuously spins the motor at a particular speed, controlled by the spd_ref
, spd_kp
, spd_ki
, and limit_cur
parameter values. (see section 4.1.12 for a full list of parameters).
motor.enable()
motor.mode(RunMode.VELOCITY)
# Turn at 5 rad/s
motor.set_parameter("spd_ref", 5)
This controls how much current is applied to the motor with the iq_ref
, cur_kp
, cur_ki
, and cur_filt_gain
parameter values. (see section 4.1.12 for a full list of parameters).
motor.enable()
motor.mode(RunMode.TORQUE)
# Apply 0.5A of maximum torque to the motor
motor.set_parameter("iq_ref", 0.5)
There's a single command to request the position, torque, velocity, and temperature of the motor. The controller will listen for the response and automatically add it to the state dict.
motor.request_motor_state()
time.sleep(1)
print(motor.state.get("position"))
You can also use an event listener to be notified when the state value has been retrieved
def state_updated():
print(f"{motor.state}")
motor.on("state_changed", state_updated)
motor.request_motor_state()
The motor has a list of params that you can read, and some of which you can write to. These control the motors calibration and current state. For example, in position mode, to move the motor to position 2 rad, you would set the loc_ref
param to 2.0
.
You can see all the available parameters in the documentation -- it's spread across two tables: section 3.3.3, and 4.1.12. Some values are duplicated in each table, in these cases, the function code in the first table is used.
NOTE: String values are not yet supported.
To read a parameter, you have to request it from the motor and wait for it to come back. Then you can read it from the params
dict.
motor.request_parameter("loc_ref")
time.sleep(1)
print(motor.params.get("loc_ref"))
You can also use an event listener to be notified when param value is retrieved
def param_value_received(name: str, value: str):
print(f"{name}: {value}")
motor.on("param_received", param_value_received)
motor.request_parameter("loc_ref")
motor.set_parameter("loc_ref", 5)
It's important to know when there is a motor fault. Here's how to request the current fault state of the motor:
motor.request_motor_fault_status()
time.sleep(1)
# All the faults might be defined, but only the ones set to True are actively faulting
active_faults = [motor.fault for fault, is_active in faults.items() if is_active]
print("Active motor faults:", motor.faults)
You can also use an event listeners to be notified when faults happen or are cleared.
def has_fault():
active_faults = [motor.fault for fault, is_active in faults.items() if is_active]
print("Active motor faults:", motor.faults)
def faults_clear():
print("No more faults")
# Define event listeners
motor.on("has_fault", has_fault)
motor.on("fault_cleared", has_fault)
# Check regularly
motor.request_motor_fault_status()
If you have multiple motors, you'll need to change their CAN bus IDs so they can all be on the same bus.
motor.change_motor_id(5)
Set the current motor position as the zero position. NOTE: this position is lost on power down.
motor.set_zero_position()