PythonControlling Motors

Controlling Motors

Each motor channel can be controlled by it’s corresponding MotorChannel object in the Plink object. The MotorChannel objects can be accessed as:

python
plink.channel1
plink.channel2
plink.channel3
plink.channel4

You can save a reference to the MotorChannel objects as local variables for convenience. For example:

python
left_drive_wheel = plink.channel1
right_drive_wheel = plink.channel2

Voltage Limits

To protect the motors, each motor channel has a voltage limit that is set to 0.0 by default. The voltage limit for each channel must be set when initially connecting to the Plink to match the voltage limit of your motors. The example below shows how to modify the initialization code to set the motor voltage limits.

python
 
from motorgo import Plink
 
# Create a Plink object, the main interface to the MotorGo board
plink = Plink()
 
# Set the power supply voltage
plink.power_supply_voltage = 9.0
 
left_drive_wheel = plink.channel1
right_drive_wheel = plink.channel2
 
# Set the motor voltage limit for each motor channel
left_motor.motor_voltage_limit = 5.0
right_motor.motor_voltage_limit = 5.0
plink.channel3.motor_voltage_limit = 5.0
plink.channel4.motor_voltage_limit = 5.0
 
# Finally, connect to the MotorGo board and push the configuration
plink.connect()

Motor Control Modes

The Plink supports two motor control modes: ControlMode.POWER and ControlMode.VELOCITY.

Power Control Mode

In power control mode, the motor is controlled by directly setting the power level. The power level is a value between -1.0 and 1.0, where -1.0 is full reverse, 0.0 is stopped, and 1.0 is full forward. To configure a motor channel to power control mode, set the control_mode attribute of the MotorChannel object to ControlMode.POWER.

python
from motorgo import ControlMode
 
left_drive_wheel.control_mode = ControlMode.POWER

To set the power level of the motor, set the power attribute of the MotorChannel object to a value between -1.0 and 1.0.

python
left_drive_wheel.power = 0.5

Velocity Control Mode

In velocity control mode, the motor is controlled by setting the desired velocity in rad/s. To configure a motor channel to velocity control mode, set the control_mode attribute of the MotorChannel object to ControlMode.VELOCITY.

python
from motorgo import ControlMode
 
left_drive_wheel.control_mode = ControlMode.VELOCITY

Next, you must configure the velocity PID controller parameters for the motor:

python
left_drive_wheel.set_velocity_pid_gains(p = 1.0, i = 0.0, d = 0.0, output_ramp = None, lpf = None)

The output_ramp can be set to limit the rate of change of the motor power output. The lpf parameter can be set to apply a low-pass filter to the velocity setpoint. You can set any of the parameters to None to use the default values.

Finally, set the desired velocity of the motor by setting the velocity attribute of the MotorChannel object to the desired velocity in rad/s.

python
left_drive_wheel.velocity = 1.0

If the motor seems to be spinning at the top speed regardless of the velocity setpoint, you may need to flip the sign of the PID gains, as the motor may be spinning in the opposite direction of the encoder.