Skip to content

MotorDriver

This class allows to define robot wheel configuration and make required calculations in order to move or turn to a certain direction. It makes robot driving more predictable and hides inconveniences of controlling raw motor channels.
Each robot has a different wheel configuration and motors specifications. Driver should be configured with these parameters before use.
Supports up to 4 DC motors and 3 servo motors.

Functions list

Result Function Description
MotorDriver() Create object
MotorDriver(singleCmd) Create object with settings
none setTurnIntensity(intensity) Set turn intensity
none addFrontLeft(command, min, max, inv) Configure front left wheel
none addFrontRight(command, min, max, inv) Configure front right wheel
none addRearLeft(command, min, max, inv) Configure rear left wheel
none addRearRight(command, min, max, inv) Configure rear right wheel
none addServo(channel, cmd, min, center, max, inv) Configure servo motor
none moveServo(channel, position) Move servo arm to position
none move(drive, turn) Set drive and turn power to wheels
none brake(fl, fr, rl, rr) Set braking power to certain wheels
none brakeAll(power) Set braking power to all wheels
none brakeRear(power) Set braking power to rear wheels
none brakeFront(power) Set braking power to front wheels
none setModule(number, serial) Set module number and serial
TotemModule getModule() Get TotemModule object

API

Constructor

MotorDriver()

MotorDriver(singleCommand)

Create driver object.
MotorDriver internally writes motorABCD command. If individual motor updates are required (motorA), pass parameter false MotorDriver(false) to disable single command mode.
Disabling single command mode would be required if using one of DC motor channel for other purposes rather than robot wheels.
Parameter:
singleCommand - write command mode [true:false].
MotorDriver driver; // Create driver object
MotorDriver driver(false); // Create driver with individual command mode

Functions

setTurnIntensity(intensity)

Limit turn sensitivity if it's too high. This can be useful when turning (steering) affects driving too much.
It modifies internal computation and does not change parameters of move() function.
It only affects turning with 2 or 4 static wheels. Does not affect servo motors.
Parameter:
turn - turn sensitivity [0:100]%. Default: 100.
driver.turnIntensity(50); // Set to 50%

addFrontLeft(command, minPower, maxPower, inverted)

addFrontRight(command, minPower, maxPower, inverted)

addRearLeft(command, minPower, maxPower, inverted)

addRearRight(command, minPower, maxPower, inverted)

Configure robot wheel motors.
By providing settings for each motor, MotorDriver will adjust output power to make predictable movements. Functions can be called multiple times to reconfigure parameters during runtime (e.g. limiting max power).
minPower indicates minimum amount of power required to spin a wheel. This is used to compensate motor dead-zone.
maxPower by default should be set to 100. It may be lowered if there is a need to limit power of the robot or using lower voltage motors.
inverted used to change motor spin direction by providing [true:false].
Parameter:
command - command of the motor channel (e.g. motorA).
minPower - minimum power required to spin a motor [0:100]%.
maxPower - maximum allowed power for a motor [0:100]%.
inverted - invert motor pins [true:false]. Default: false.
// Set front left motor to use with channel motorA and start with power from 20%
driver.addFrontLeft("motorA", 20, 100);
// Set front left right to use with channel motorB and start with power from 20% + invert direction
driver.addFrontRight("motorB", 20, 100, true);

addServo(channel, command, minPos, centerPos, maxPos, inverted)

Configure servo motor.
Define parameters for servo motor, to use with this driver. Parameters are saved to specified channel. Same number must be used when calling moveServo(). It can be any digit in range [0:2].
Function can be called multiple times to reconfigure parameters during runtime (e.g. changing motor limits).
Position [-100:100]% set by moveServo() will be mapped to minPos, maxPos values.
Parameter:
channel - number of servo channel [0:2].
command - command of the motor channel (e.g. servoA).
minPos - minimum allowed position for servo motor to spin (limit) [-100:100]%.
centerPos - center position offset for servo motor on value 0 [-100:100]%.
maxPower - maximum allowed position for servo motor to spin (limit) [-100:100]%.
inverted - invert servo motor spin direction [true:false]. Default: false.
// Configure servo motor to channel 0
// With command "servoA"
// Min pos: 10, center pos: 55, max pos: 94
driver.addServo(0, "servoA", 10, 55, 94);
// Configure servo motor to channel 1
// With command "servoB"
// Min pos: -70, center pos: 50, max pos: 90
// Invert spin direction
driver.addServo(1, "servoB", -70, 50, 90, true);
// Spin motor 0 to position -100. Motor internally is set to value 10
driver.moveServo(0, -100);
// Spin motor 1 to position -100. Motor internally is set to value 90
driver.moveServo(1, -100);

moveServo(channel, position)

Move servo motor to desired position.
Parameter:
channel - number of configured servo channel [0:2].
position - motor position [-100:100]%. 0 - center.
driver.moveServo(0, 50); // Spin motor 0 to position 50
driver.moveServo(1, -6); // Spin motor 1 to position -6

move(drive, turn)

Set robot move parameters that will be converted to motor output.
This function call will write configured motor channels.
turn parameter will spin wheels in opposite direction.
Parameter:
drive - power of forward/backward direction [-100:100]%.
turn - power of turning left/right [-100:100]%. Default: 0.
driver.move(50); // Drive forward at 50% power
driver.move(-20); // Drive backward at 20% power
driver.move(0, 50); // Turn right at 50% power
driver.move(0, -30); // Turn left at 30% power
driver.move(100, -30); // Drive forward at 100% power while turning left at 30% power
driver.move(-80, 90); // Drive backward at 80% power while turning right at 90% power

brake(frontLeft, frontRight, rearLeft, rearRight)

This will apply electronic brake for modules supporting this feature. Braking power can be set per each wheel.
If move() and brake() functions parameters are non-zero, driver will cut power to motors only if braking power is higher than motor spin power. Otherwise - motor spin power is lowered as if real brake pads are pressing spinning wheel. Setting full braking power will always stop motor completely.
Parameter:
power - braking power [0:100]%.
driver.brake(100, 0, 0, 0); // Brake front left wheel at 100%
driver.brake(0, 0, 80, 80); // Brake rear wheels at 80%
driver.brake(0, 0, 0, 0); // None of wheels are braking
// Can be used with motor spinning power
driver.move(100); // Set wheels spin at 100% power
driver.brake(60, 60, 60, 60); // Will slow down wheels spin by applying 60% of brake power

brakeAll(power)

Apply electronic brake power to all wheels.
Parameter:
power - braking power [0:100]%.
driver.brakeAll(100); // Brake all wheels at 100%

brakeRear(power)

Apply electronic brake power to rear wheels.
Parameter:
power - braking power [0:100]%.
driver.brakeRear(100); // Brake only rear wheels at 100% (handbrake)

brakeFront(power)

Apply electronic brake power to front wheels.
Parameter:
power - braking power [0:100]%.
driver.brakeFront(100); // Brake only front wheels at 100%

setModule(number, serial)

MotorDriver contains TotemModule to write motor output values. This function allows to configure exact module to communicate with. By default it's set to 00 (all modules). Same as setNumber(number) and setSerial(serial).
Parameter:
number - module number [0:255]. 0 - all modules.
serial - module serial [0:32767]. 0 - ignore serial.
driver.setModule(04); // Send move() updates to 04 modules only
driver.setModule(04, 21015); // Send move() updates to 04 module with serial 21015 only

TotemModule getModule()

MotorDriver contains TotemModule to write motor output values. This function returns it to use for other purposes. Allows to reuse already existing TotemModule instance without creating a new one.
Returns: TotemModule.
int voltage = driver.getModule().readWait("battery").getInt(); // Read battery using returned TotemModule