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

Class Motor. API for working with motor. More...

#include <ev3cxx_motor.h>

Inheritance diagram for Motor:
MotorConstants

Public Member Functions

 Motor (MotorPort port, MotorType type=MotorType::LARGE)
 Constructor of class motor. More...
 
 ~Motor ()
 Destructor of class motor. More...
 
void off (bool brake=true)
 Stop motor. More...
 
void on (int power=50)
 Set speed on regulated motor. More...
 
void unregulated (int power=50)
 Set power on unregulated motor. [TODO: fix - same behavior as on() -> problem in EV3RT]. More...
 
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. More...
 
void onForRotations (int speed=50, float rotations=1, bool_t brake=true, bool_t blocking=true, unsigned int wait_after_ms=60)
 Set number of rotation to rotate with regulated motor. More...
 
void onForSeconds (int speed=50, unsigned int time_ms=1000, bool_t brake=true)
 Rotates the motor for a given time. More...
 
int degrees () const
 Get the actual position of a motor in degrees. More...
 
float rotations () const
 Get the actual position of a motor in rotations. More...
 
int currentPower () const
 Get the actual power (equivalent with speed) of a motor. [TODO: power/speed]. More...
 
void resetPosition ()
 Reset the angular position of the motor to zero. More...
 
motor_port_t getPort () const
 Get motor port. More...
 
ER_UINT getType () const
 Get curent type of motor which is set in EV3RT (not set by constructor). More...
 

Private Attributes

motor_port_t m_port
 
motor_type_t m_type
 

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 Motor. API for working with motor.

Definition at line 47 of file ev3cxx_motor.h.

Constructor & Destructor Documentation

◆ Motor()

Motor ( MotorPort  port,
MotorType  type = MotorType::LARGE 
)
inline

Constructor of class motor.

Parameters
portPort number
typeType of port: motor_type_t

Definition at line 58 of file ev3cxx_motor.h.

59  : m_port(static_cast<motor_port_t>(port)),
60  m_type(static_cast<motor_type_t>(type))
61  {
62  ev3_motor_config(m_port, m_type);
63  }

◆ ~Motor()

~Motor ( )
inline

Destructor of class motor.

Turns off the motor and lefts it coasts.

Definition at line 70 of file ev3cxx_motor.h.

References Motor::off().

70  {
71  Motor::off(false);
72  }
void off(bool brake=true)
Stop motor.
Definition: ev3cxx_motor.h:80

Member Function Documentation

◆ currentPower()

int currentPower ( ) const
inline

Get the actual power (equivalent with speed) of a motor. [TODO: power/speed].

When an incorrect motor port number is specified, it always returns 0.

Returns
Motor power

Definition at line 195 of file ev3cxx_motor.h.

195 { return ev3_motor_get_power(m_port); }

◆ degrees()

int degrees ( ) const
inline

Get the actual position of a motor in degrees.

When an incorrect motor port number is specified, it always returns 0.

Returns
Motor position in degrees.

Definition at line 177 of file ev3cxx_motor.h.

177 { return ev3_motor_get_counts(m_port); }

◆ getPort()

motor_port_t getPort ( ) const
inline

Get motor port.

Returns
Return const motor port.

Definition at line 209 of file ev3cxx_motor.h.

209 { return m_port; }

◆ getType()

ER_UINT getType ( ) const
inline

Get curent type of motor which is set in EV3RT (not set by constructor).

Returns
Return const motor type or error.
Return values
>=0Motor type of specified motor port
E_IDIllegal motor port number

Definition at line 218 of file ev3cxx_motor.h.

218  {
219  ER_UINT motor_type = ev3_motor_get_type(m_port);
220 
221  if(motor_type < 0 || motor_type >= TNUM_MOTOR_PORT)
222  return E_ID;
223  return motor_type;
224  }

◆ off()

void off ( bool  brake = true)
inline

Stop motor.

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

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

Definition at line 80 of file ev3cxx_motor.h.

Referenced by MotorTank::off(), and Motor::~Motor().

80  {
81  ev3_motor_stop(m_port, brake);
82  }

◆ on()

void on ( int  power = 50)
inline

Set speed on regulated motor.

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

Parameters
powerMotor speed. Range: -100 to +100. A negative value moves the robot 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 91 of file ev3cxx_motor.h.

Referenced by MotorTank::on().

91  {
92  // change type of motor: UNREGULATED_MOTOR => LARGE_MOTOR/MEDIUM_MOTOR
93  if(m_type != getType()) {
94  ev3_motor_config(m_port, m_type);
95  }
96  ev3_motor_set_power(m_port, power);
97  }
ER_UINT getType() const
Get curent type of motor which is set in EV3RT (not set by constructor).
Definition: ev3cxx_motor.h:218

◆ onForDegrees()

void onForDegrees ( int  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 motor.

Parameters
speedSpeed of motor. Range: -100 to +100. Default value is 50.
degreesNumber of degrees for rotation of motor. A negative value moves the robot backwards. 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 functuion executes. Default wait is 60 ms. It is important because of some race condition when another motor rotation follows immediately

Definition at line 125 of file ev3cxx_motor.h.

Referenced by MotorTank::onForDegrees().

125  {
126  if (speed == 0)
127  return;
128  if (speed < 0) {
129  speed = -speed;
130  degrees = -degrees;
131  }
132  ev3_motor_rotate_brake(m_port, degrees, speed, blocking, brake);
133  tslp_tsk(wait_after_ms);
134  }
int degrees() const
Get the actual position of a motor in degrees.
Definition: ev3cxx_motor.h:177

◆ onForRotations()

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

Set number of rotation to rotate with regulated motor.

Parameters
speedSpeed of motor. Range: -100 to +100. Default value is 50.
rotationsNumber of rotation for rotate with motor. A negative value moves the robot backwards. 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 functuion executes. Default wait is 60 ms. It is important because of some race condition when another motor rotation follows immediately

Definition at line 147 of file ev3cxx_motor.h.

147  {
148  if (speed == 0)
149  return;
150  if (speed < 0) {
151  speed = -speed;
152  rotations = -rotations;
153  }
154  ev3_motor_rotate_brake(m_port, NUMBER_OF_DEGREES_PER_ROTATION * rotations, speed, blocking, brake);
155  tslp_tsk(wait_after_ms);
156  }
float rotations() const
Get the actual position of a motor in rotations.
Definition: ev3cxx_motor.h:185

◆ onForSeconds()

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

Rotates the motor for a given time.

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

Definition at line 165 of file ev3cxx_motor.h.

165  {
166  on(speed);
167  tslp_tsk(time_ms);
168  off(brake);
169  }
void on(int power=50)
Set speed on regulated motor.
Definition: ev3cxx_motor.h:91
void off(bool brake=true)
Stop motor.
Definition: ev3cxx_motor.h:80

◆ resetPosition()

void resetPosition ( )
inline

Reset the angular position of the motor 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 202 of file ev3cxx_motor.h.

Referenced by MotorTank::resetPosition().

202 { ev3_motor_reset_counts(m_port); }

◆ rotations()

float rotations ( ) const
inline

Get the actual position of a motor in rotations.

When an incorrect motor port number is specified, it always returns 0.

Returns
Motor position in rotations (float value).

Definition at line 185 of file ev3cxx_motor.h.

185  {
186  return ev3_motor_get_counts(m_port) / NUMBER_OF_DEGREES_PER_ROTATION;
187  }

◆ unregulated()

void unregulated ( int  power = 50)
inline

Set power on unregulated motor. [TODO: fix - same behavior as on() -> problem in EV3RT].

Motor works with constant power, NOT speed. Speed changes depend on load.

Parameters
powerMotor power. Range: -100 to +100. A negative value moves the robot 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 106 of file ev3cxx_motor.h.

Referenced by MotorTank::unregulated().

106  {
107  // change type of motor: LARGE_MOTOR/MEDIUM_MOTOR => UNREGULATED_MOTOR
108  if(UNREGULATED_MOTOR != getType()) {
109  ev3_motor_config(m_port, UNREGULATED_MOTOR);
110  }
111  ev3_motor_set_power(m_port, power);
112  }
ER_UINT getType() const
Get curent type of motor which is set in EV3RT (not set by constructor).
Definition: ev3cxx_motor.h:218