EV3RT CXX API Reference [English]
An RTOS-based development platform for LEGO Mindstorms EV3.
MotorTank Class Reference

Class MotorTank. API for working with two motors together. More...

#include <ev3cxx_motor_tank.h>

Inheritance diagram for MotorTank:
MotorConstants

Public Member Functions

 MotorTank (MotorPort left_motor_port, MotorPort right_motor_port, MotorType type=MotorType::LARGE)
 Constructor of class MotorTank. More...
 
void off (bool brake=true)
 Stop motors. More...
 
void on (int left_speed=50, int right_speed=50)
 Set speed on regulated motors. More...
 
void unregulated (int left_power=50, int right_power=50)
 Set power on unregulated motors. More...
 
void onForDegrees (int left_speed=50, int right_speed=50, int degrees=360, bool_t brake=true, bool_t blocking=true, unsigned int wait_after_ms=60)
 Set number of degrees to rotate with regulated motors. More...
 
void onForRotations (int left_speed=50, int right_speed=50, float rotations=1, bool_t brake=true, bool_t blocking=true, unsigned int wait_after_ms=60)
 Set number of rotations to rotate with regulated motors. More...
 
void onForSeconds (int left_speed=50, int right_speed=50, unsigned int time_ms=1000, bool_t brake=true)
 Rotates the motor for a given time. More...
 
void resetPosition ()
 Reset the angular position of the motors to zero. More...
 
MotorleftMotor ()
 Return reference to the left motor. More...
 
const MotorleftMotor () const
 Return const reference to the left motor. More...
 
MotorrightMotor ()
 Return reference to the right motor. More...
 
const MotorrightMotor () const
 Return const reference to the right motor. More...
 

Private Attributes

Motor m_left_motor
 
Motor m_right_motor
 

Additional Inherited Members

- Static Public Attributes inherited from MotorConstants
static const int POWER_MAX = 100
 Max power on motor.
 
static const int POWER_MIN = -100
 Min power on motor.
 
static constexpr float NUMBER_OF_DEGREES_PER_ROTATION = 360.0
 

Detailed Description

Class MotorTank. API for working with two motors together.

Definition at line 17 of file ev3cxx_motor_tank.h.

Constructor & Destructor Documentation

◆ MotorTank()

MotorTank ( MotorPort  left_motor_port,
MotorPort  right_motor_port,
MotorType  type = MotorType::LARGE 
)
inline

Constructor of class MotorTank.

Parameters
left_motor_portPort number
right_motor_portPort number
typeType of motor ports: motor_type_t

Definition at line 31 of file ev3cxx_motor_tank.h.

32  : m_left_motor(left_motor_port, type),
33  m_right_motor(right_motor_port, type)
34  {}

Member Function Documentation

◆ leftMotor() [1/2]

Motor& leftMotor ( )
inline

Return reference to the left motor.

Returns
Return Motor&.

Definition at line 148 of file ev3cxx_motor_tank.h.

148 { return m_left_motor; }

◆ leftMotor() [2/2]

const Motor& leftMotor ( ) const
inline

Return const reference to the left motor.

Returns
Return const Motor&.

Definition at line 155 of file ev3cxx_motor_tank.h.

155 { return m_left_motor; }

◆ off()

void off ( bool  brake = true)
inline

Stop motors.

Set the motors power or speed to 0 and depending on param brake will brake the motors.

Parameters
brakeStart braking the motors (true = braking, false = not braking). Default value is true.

Definition at line 42 of file ev3cxx_motor_tank.h.

References Motor::off().

Referenced by MotorTank::onForSeconds().

42  {
43  m_left_motor.off(brake);
44  m_right_motor.off(brake);
45  }
void off(bool brake=true)
Stop motor.
Definition: ev3cxx_motor.h:80

◆ on()

void on ( int  left_speed = 50,
int  right_speed = 50 
)
inline

Set speed on regulated motors.

Using regulated motors driver (motors should has constant speed) and param power is equivalent to speed. Motor controls real power to keep constant speed.

Parameters
left_speedLeft motor speed. Range: -100 to +100. A negative value turns the wheel backwards. Default value is 50. If a out-of-range value is given, it will be clipped to the minimum (-100) or maximum (100) value.
right_speedRight motor speed. Range: -100 to +100. A negative value turns the wheel backwards. Default value is 50. If a out-of-range value is given, it will be clipped to the minimum (-100) or maximum (100) value.

Definition at line 57 of file ev3cxx_motor_tank.h.

References Motor::on().

Referenced by MotorTank::onForSeconds().

57  {
58  m_left_motor.on(left_speed);
59  m_right_motor.on(right_speed);
60  }
void on(int power=50)
Set speed on regulated motor.
Definition: ev3cxx_motor.h:91

◆ onForDegrees()

void onForDegrees ( int  left_speed = 50,
int  right_speed = 50,
int  degrees = 360,
bool_t  brake = true,
bool_t  blocking = true,
unsigned int  wait_after_ms = 60 
)
inline

Set number of degrees to rotate with regulated motors.

Parameters
left_speedLeft motor speed. Range: -100 to +100. Default value is 50.
right_speedRight motor speed. Range: -100 to +100. Default value is 50.
degreesNumber of degrees for rotation of the forward or faster motor. Default value is 360.
brakeIf true, then motor start braking after reach the position (= degrees).
blockingtrue (The function will be blocked until the move is finished), or false (The function will not be blocked). Default value is false.
wait_after_msAdds wait after the function executes. Default wait is 60 ms. It is important because of some race condition when another motor rotation follows immediately

Definition at line 88 of file ev3cxx_motor_tank.h.

References Motor::onForDegrees().

Referenced by MotorTank::onForRotations().

88  {
89  int labs = abs(left_speed);
90  int rabs = abs(right_speed);
91  if (labs > rabs) {
92  const float sr = float(rabs) / float(labs);
93  m_right_motor.onForDegrees(right_speed, degrees * sr, brake, false, 0);
94  m_left_motor.onForDegrees (left_speed, degrees, brake, blocking, wait_after_ms);
95  } else {
96  const float sr = rabs != 0 ? float(labs) / float(rabs) : 0;
97  m_left_motor.onForDegrees (left_speed, degrees * sr, brake, false, 0);
98  m_right_motor.onForDegrees(right_speed, degrees, brake, blocking, wait_after_ms);
99  }
100  }
void onForDegrees(int speed=50, int degrees=360, bool_t brake=true, bool_t blocking=true, unsigned int wait_after_ms=60)
Set number of degrees to rotate with regulated motor.
Definition: ev3cxx_motor.h:125

◆ onForRotations()

void onForRotations ( int  left_speed = 50,
int  right_speed = 50,
float  rotations = 1,
bool_t  brake = true,
bool_t  blocking = true,
unsigned int  wait_after_ms = 60 
)
inline

Set number of rotations to rotate with regulated motors.

Parameters
left_speedLeft motor speed. Range: -100 to +100. Default value is 50.
right_speedRight motor speed. Range: -100 to +100. Default value is 50.
rotationsNumber of rotations for rotation of the forward or faster motor. Default value is 1.
brakeIf true, then motor start braking after reach the position (= degrees).
blockingtrue (The function will be blocked until the move is finished), or false (The function will not be blocked). Default value is false.
wait_after_msAdds wait after the function executes. Default wait is 60 ms. It is important because of some race condition when another motor rotation follows immediately

Definition at line 114 of file ev3cxx_motor_tank.h.

References MotorTank::onForDegrees().

114  {
115  onForDegrees(left_speed, right_speed, rotations * NUMBER_OF_DEGREES_PER_ROTATION, brake, blocking, wait_after_ms);
116  }
void onForDegrees(int left_speed=50, int right_speed=50, int degrees=360, bool_t brake=true, bool_t blocking=true, unsigned int wait_after_ms=60)
Set number of degrees to rotate with regulated motors.

◆ onForSeconds()

void onForSeconds ( int  left_speed = 50,
int  right_speed = 50,
unsigned int  time_ms = 1000,
bool_t  brake = true 
)
inline

Rotates the motor for a given time.

Parameters
left_speedSpeed of left motor. Range: -100 to +100. Default value is 50.
right_speedSpeed of right motor. Range: -100 to +100. Default value is 50.
msNumber of miliseconds for rotate with motor. Default value is 1000.
brakeIf true, then motor start braking after reach the position (= degrees).

Definition at line 126 of file ev3cxx_motor_tank.h.

References MotorTank::off(), and MotorTank::on().

126  {
127  on(left_speed, right_speed);
128  tslp_tsk(time_ms);
129  off(brake);
130  }
void on(int left_speed=50, int right_speed=50)
Set speed on regulated motors.
void off(bool brake=true)
Stop motors.

◆ resetPosition()

void resetPosition ( )
inline

Reset the angular position of the motors to zero.

Setting the value of the angular position sensor of the motor does not affect the actual power and position of the motor.

Definition at line 137 of file ev3cxx_motor_tank.h.

References Motor::resetPosition().

138  {
139  m_left_motor.resetPosition();
140  m_right_motor.resetPosition();
141  }
void resetPosition()
Reset the angular position of the motor to zero.
Definition: ev3cxx_motor.h:202

◆ rightMotor() [1/2]

Motor& rightMotor ( )
inline

Return reference to the right motor.

Returns
Return Motor&.

Definition at line 162 of file ev3cxx_motor_tank.h.

162 { return m_right_motor; }

◆ rightMotor() [2/2]

const Motor& rightMotor ( ) const
inline

Return const reference to the right motor.

Returns
Return const Motor&.

Definition at line 169 of file ev3cxx_motor_tank.h.

169 { return m_right_motor; }

◆ unregulated()

void unregulated ( int  left_power = 50,
int  right_power = 50 
)
inline

Set power on unregulated motors.

Motors work with constant power, NOT speed. Speed changes depend on load.

Parameters
left_powerLeft motor power. Range: -100 to +100. A negative value turns the wheel backwards. Default value is 50. If a out-of-range value is given, it will be clipped to the minimum (-100) or maximum (100) value.
right_powerRight motor power. Range: -100 to +100. A negative value turns the wheel backwards. Default value is 50. If a out-of-range value is given, it will be clipped to the minimum (-100) or maximum (100) value.

Definition at line 71 of file ev3cxx_motor_tank.h.

References Motor::unregulated().

71  {
72  m_left_motor.unregulated(left_power);
73  m_right_motor.unregulated(right_power);
74  }
void unregulated(int power=50)
Set power on unregulated motor. [TODO: fix - same behavior as on() -> problem in EV3RT].
Definition: ev3cxx_motor.h:106