RoboCatz.com

Our Function Library (Updated Spring 2013)

EV3 - Motor Commands

There are four motor ports on the EV3 labeled A, B, C, and D. Each motor is equipped with an integrated encoder that can be used to determine the current position of the motor. The encoders have 360 counts per single revolution of the motor. A count of 180 indicates that the motor has traveled half of a rotation.

 

Command List

 

setMotorSpeed


voidsetMotorSpeed(tMotor nMotorIndex, int nSpeed)
Parameter Explanation Data Type
Return Type The function returns no value void
nMotorIndex Port number that the motor is plugged into. tMotor
nSpeed Speed value that will be passed to the motor (-100 to +100). Zero is the value for stopping the motors int
Usage: setMotorSpeed(nMotorIndex, speed);

 

 

Code Example:

//Sets the speed value of motor motor port A to full forward (+100)
 setMotorSpeed(motorA, 100);
 //Halt program flow for 1000ms (1 second)
 sleep(1000);
 
 //Sets the speed value of motor motor port A to half-speed backwards (-50)
 setMotorSpeed(motorA, -50);
 //Halt program flow for 1000ms (1 second)
 sleep(1000);
 
 //Sets the speed value of motor motor port A to stopped (0)
 setMotorSpeed(motorA, 0);
 //Halt program flow for 1000ms (1 second)
 sleep(1000);

 

waitUntilMotorStop


bool waitUntilMotorStop(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type Function returns a boolean value bool
nMotorIndex Port number that the motor is plugged into. tMotor
Usage: waitUntilMotorStop(nMotorIndex);

 

 

Code Example:

 //Sets the motorA's target to 1000 encoder counts at a speed value of 50
 setMotorTarget(motorA, 1000, 50);
 
 //Holds program flow until the motor on motor port A comes to a complete stop.
 waitUntilMotorStop(motorA);

 

moveMotorTarget


voidmoveMotorTarget(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type The function returns no value void
nMotorIndex Port number that the motor is plugged into. tMotor
nPosition Target incremental value that the motor will travel to. float
nSpeed Speed value that will be passed to the motor (-100 to +100). int
Usage: moveMoterTarget(nMotorIndex, nPosition, nSpeed);

 

 

Code Example:

 //Moves the motor on motor port A 1000 degrees (forward)
 //at full speed forward (+100)
 moveMotorTarget(motorA, 1000, 100);
 
 //Holds program flow until the motor on motor port A comes to a complete stop.
 waitUntilMotorStop(motorA);
 
 //Moves the motor on motorA -1000 encoder counts (backwards)
 //at a speed level of 50 backwards (-50)
 moveMotorTarget(motorA, -1000, -50);
 
 //Holds program flow until the motor on motor port A comes to a complete stop.
 waitUntilMotorStop(motorA);

 

setMotorTarget


voidsetMotorTarget(tMotor nMotorIndex, float nPosition, int nspeed)
Parameter Explanation Data Type
Return Type The function returns no value void
nMotorIndex Port number that the motor is plugged into. tMotor
nPosition Target absolute value that the motor will travel to. float
nSpeed Speed value that will be passed to the motor (-100 to +100). int
Usage: setMotorTarget(nMotorIndex, nPosition, nSpeed);

 

 

Code Example:

 //Sets the absolute position target of the motor plugged
 //into motor port A to 1000 degrees at a speed of 50
 setMotorTarget(motorA, 1000, 50);
 
 //Holds program flow until the motor on motor port A comes to a complete stop.
 waitUntilMotorStop(motorA);
 
 //Sets the absolute position target of the motor plugged
 //into port 2 to -2000 degrees at a speed of -75
 setMotorTarget(motor2, -2000, -75);
 
 //Holds program flow until the motor on motor port A comes to a complete stop.
 waitUntilMotorStop(motorA);

 

getMotorTarget


floatgetMotorTarget(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type Functions returns a floating point decimal value. float
nMotorIndex Port number that the motor is plugged into. tMotor
Usage: getMotorTarget(nMotorIndex);

 

 

Code Example:

//Reset the encoder count of motor A
resetMotorEncoder(motorA);
 
//Set motor A's target of 1000 degrees at speed 50
moveMotorTarget(motorA, 1000, 50);
 
//Repeat until the motor has stopped
repeatUntil(getMotorEncoder(motorA) == getMotorTarget(motorA))
{
	 
}

 

resetMotorEncoder


voidresetMotorEncoder(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type The function returns no value void
nMotorIndex Port number that the motor is plugged into. tMotor
Usage: resetMotorEncoder(nMotorIndex);

 

 

Code Example:

//Resets the encoder value of motor port A to 0
resetMotorEncoder(motorA);

 

getMotorEncoder


float getMotorEncoder(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type Function returns a floating point decimal value float
nMotorIndex Port number that the motor is plugged into. tMotor
Usage: getMotorEncoder(nMotorIndex);

 

 

Code Example:

//Reset the motor encoder value to zero
resetMotorEncoder(motorA);
 
//Set the target of motor A to 1000 encoder counts at speed 50
setMotorTarget(motorA, 1000, 50);
 
//Loop until the current encoder counts are equal to the target counts
repeatUntil(getMotorEncoder(motorA) == getMotorTarget(motorA))
{
	 
}

 

getMotorSpeed


intgetMotorSpeed(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type Functions returns an integer value. int
nMotorIndex Port number that the motor is plugged into. tMotor
Usage: getMotorSpeed(nMotorIndex);

 

 

Code Example:

//Creates 'current speed' variable
int currentSpeed;
 
//Sets the speed of motor A to 50
setMotorSpeed(motorA, 50);
 
//Stores the speed of motor A in currentSpeed
currentSpeed = getMotorSpeed(motorA);
 
//Changes the speed of motor A to 100
//currentSpeed still contains a value of 50
setMotorSpeed(motorA, 100);

 

setMotorReversed


voidsetMotorReversed(tMotor nMotorIndex, bool bReversed)
Parameter Explanation Data Type
Return Type The function returns no value. void
nMotorIndex Port number that the motor is plugged into. tMotor
bReversed Flag set to 1 if a motor is reversed, 0 if it is not bool
Usage: setMotorReversed(nMotorIndex, bReversed);

 

 

Code Example:

setMotorReversed(motorA, true); //Set motor to be reversed
setMotorSpeed(motorA, 50);      //The motor runs counter-clockwise with positive speeds (reversed)
 
setMotorReversed(motorA, false); //Set motor to not be reversed
setMotorSpeed(motorA, 50);      //The motor runs clockwise with positive speeds (normal)

 

getMotorRunning


boolgetServoEncoder(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type Functions returns a floating point decimal value. float
nMotorIndex Port number that the motor is plugged into. tMotor
Usage: getMotorMoving(nMotorIndex)

 

 

Code Example:

//Set the target of the motor on motor port A to 1000 counts at speed 50
moveMotorTarget(motorA, 1000, 50);
 
//Keep looping until the motor stops moving
repeatUntil(getMotorMoving(motorA) == 0);
{
	//Do nothing (idle loop)
}

 

getMotorBrakeMode


TMotorBrakeModes getMotorBrakeMode(tMotor nMotorIndex)
Parameter Explanation Data Type
Return Type Function returns the brake mode of the motor. TMotorsBrakeModes
nMotorIndex Port number that the motor is plugged into. tMotor
Usage: getMotorBrakeMode(nMotorIndex);

 

 

 

Code Example:

//If the brake mode of motor A is not set to 'Hold' mode,
//set it to Hold mode
if(getBrakeMode(motorA) != motorHold)
{
	setBrakeMode(motorA, motorHold);
}

 

setMotorBrakeMode


voidsetMotorBrakeMode(tMotor nMotorIndex, TMotorBrakeModes modeSetting)
Parameter Explanation Data Type
Return Type The function returns no value. void
nMotorIndex Port number that the motor is plugged into. tMotor
modeSetting Flag representing which mode the motor is in (Coast, Brake, or Hold). tMotorBrakeModes
Usage: setMotorBrakeMode(nMotorIndex, modeSetting);

 

 

 

Code Example:

setBrakeMode(motorA, motorCoast);
setMotorSpeed(motorA, 0);  //The motor will now Coast (does not resist movement)
sleep(1000);
 
setBrakeMode(motorA, motorHold);
setMotorSpeed(motorA, 0);  //The motor will now Hold  (resist movement and stay at position)
sleep(1000);
 
setBrakeMode(motorA, motorBrake);
setMotorSpeed(motorA, 0);  //The motor will now Brake (resist movement only)
sleep(1000);