A driver for the CyberGear micromotor
Project description
Python CyberGear Motor Driver
A python driver for the CyberGear motor.
Getting started
Install the package from pip:
pip install CyberGearDriver
CAN Interface
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])
Moving the motor
The motor has 4 different drive modes:
- Operation control mode (sometimes call MIT control mode)
- Position mode
- Velocity mode
- Torque mode (or current mode)
Operation Control 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)
Position control mode
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)
Velocity mode
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)
Torque mode (i.e. Current Mode)
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)
Get the motor state
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()
Reading and writing motor config and parameters
The motor has two lists of values that you can read and write to. Config values are saved between power cycles and parameter values control motor activity and are reset on power down.
NOTE: CyberGear calls both of these sets of values "parameters". However, in the driver we differentiate them as "config" and "parameters" because they are read and saved slightly differently. There are also some value names that exist in both lists.
To see the full tables of values, goto the motor docs
- Config values: section 3.3.3 (CyberGear also calls them parameters)
- Parameter values: section 4.1.12
Read a config value
To read a value, you have to request it from the motor and wait for it to come back. Then you can read it from the config dict.
motor.request_config("MechOffset")
time.sleep(1)
print(motor.config.get("MechOffset"))
You can also use an event listener to be notified when config value is retrieved
def config_value_received(name: str, value: str):
print(f"{name}: {value}")
motor.on("config_received", config_value_received)
motor.request_config("MechOffset")
Set config values
Not all config values can be written to (refer to the table in the docs).
Be careful when setting config values. Many are core to how the motor works and you could break your motor if you don't know what you're doing.
motor.set_config("overTempTime", 20001)
Read parameter values
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 parameters 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 values are 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")
Set parameter values
motor.set_parameter("loc_ref", 5)
Motor errors
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()
Other useful things
Change the CAN bus ID
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 zero position
Set the current motor position as the zero position. NOTE: this position is lost on power down.
motor.set_zero_position()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cybergeardriver-0.1.1.tar.gz.
File metadata
- Download URL: cybergeardriver-0.1.1.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2334b561cf1ac5fe5f8209bec235f6358346ad8ef826d723df521d65dd52862
|
|
| MD5 |
7755567d3a032d2f7e4edbfe0d3677ef
|
|
| BLAKE2b-256 |
b78f943b1fa25d15607bfcd229f441ecd3e2fd0de74fc4a034c6163ca616607d
|
File details
Details for the file cybergeardriver-0.1.1-py3-none-any.whl.
File metadata
- Download URL: cybergeardriver-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14bd3beedf45b8bbd063e708377b6b72f2efa7711b75e729b7009aeb5ab2cf84
|
|
| MD5 |
1d0ab272090ab4cbed9ea5b88c9624e5
|
|
| BLAKE2b-256 |
f71e0bc063f6f8d6723ae57a7877f76afbb2376b939559762f4518ea91ce67b4
|