Skip to content

Motors and Motor Controllers

FROGbots-4634 edited this page Jul 6, 2019 · 9 revisions

Instantiation

In order to use motors, you will first need to create an instance of the motor controller class and then attach instances of the motor class to it. For instance:

/*
 * NOTE: This assumes you have a few "using", "define" and "include" statements at the top of your sketch. See the Empty Template examples sketch.
 */

/* Assuming you'd like to declare these on the stack rather than the heap */
HiTechnicDcMotorController driveTrainMotorController(DaisyChainPosition::FIRST);
HiTechnicMotor leftDriveMotor(&driveTrainMotorController, MotorPort::PORT_1);
HiTechnicMotor rightDriveMotor(&driveTrainMotorController, MotorPort::PORT_2);

Using Motors

Once you have created a motor object, all of the standard methods you're familiar with from the SDK such as getCurrentPosition and setMode can be invoked upon it. Additionally, you can get/set the PID parameters for the closed loop modes with the following methods:

void setPIDCoeffs(uint8_t kP, uint8_t kI, uint8_t kD);
void getPIDCoeffs(uint8_t* out);

Important note on using PID mode: The motor controller's firmware PID implementation was written with the Tetrix encoder (1440CPR) in mind. If you set the power too high when using a Neverest motor (1120CPR encoder) in a PID mode the control loop will experience runaway integral windup because it thinks the motor should be going faster than it physically can. In order to prevent this, multiply the power you are setting the motor to by 0.77 (derived from 1120/1440). It is important to note that this ONLY affects PID mode. If you are running in open loop mode, there is no need to multiply the power. Also note that AFAIK, this issue is also present when using the HiTechnic controllers through the Legacy Module and the SDK.


Battery Voltage

The motor controller can report the battery voltage through the following methods:

uint16_t getBatteryVoltageMillivolts();
float getBatteryVoltage();

Timeout

By default, the motor controller will timeout and disable the motors if it does not receive any communications for a period of 2.5 seconds. If you wish, this timeout can be disabled by invoking the following method:

void setTimeoutEnabled(bool timeoutEnabled);