EV3RT CXX API Reference [English]
An RTOS-based development platform for LEGO Mindstorms EV3.
ev3cxx_motor.h
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include "ev3cxx.h"
10 
11 namespace ev3cxx {
12 
17 enum class MotorPort {
18  A = 0,
19  B,
20  C,
21  D
22 };
23 
28 enum class MotorType {
29  MEDIUM = 1,
30  LARGE,
31  UNREGULATED,
32 };
33 
34 namespace detail {
36 {
37  static const int POWER_MAX = 100;
38  static const int POWER_MIN = -100;
39  static constexpr float NUMBER_OF_DEGREES_PER_ROTATION = 360.0;
40 }; // struct MotorConstants
41 } // namespace detail
42 
47 class Motor
48  : public detail::MotorConstants
49 {
50 public:
51 
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  }
64 
70  ~Motor() {
71  Motor::off(false);
72  }
73 
80  void off(bool brake = true) {
81  ev3_motor_stop(m_port, brake);
82  }
83 
91  void on(int power = 50) {
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  }
98 
106  void unregulated(int power = 50) {
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  }
113 
125  void onForDegrees(int speed = 50, int degrees = 360, bool_t brake = true, bool_t blocking = true, unsigned int wait_after_ms = 60) {
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  }
135 
147  void onForRotations(int speed = 50, float rotations = 1, bool_t brake = true, bool_t blocking = true, unsigned int wait_after_ms = 60) {
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  }
157 
165  void onForSeconds(int speed = 50, unsigned int time_ms = 1000, bool_t brake = true) {
166  on(speed);
167  tslp_tsk(time_ms);
168  off(brake);
169  }
170 
177  int degrees() const { return ev3_motor_get_counts(m_port); }
178 
185  float rotations() const {
186  return ev3_motor_get_counts(m_port) / NUMBER_OF_DEGREES_PER_ROTATION;
187  }
188 
195  int currentPower() const { return ev3_motor_get_power(m_port); }
196 
202  void resetPosition() { ev3_motor_reset_counts(m_port); }
203 
209  motor_port_t getPort() const { return m_port; }
210 
218  ER_UINT getType() const {
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  }
225 
226 private:
227  motor_port_t m_port;
228  motor_type_t m_type;
229 }; // class Motor
230 } // namespace ev3cxx
void unregulated(int power=50)
Set power on unregulated motor. [TODO: fix - same behavior as on() -> problem in EV3RT].
Definition: ev3cxx_motor.h:106
Definition: ev3cxx.h:35
Large servo motor.
EV3RT CPP API.
Class Motor. API for working with motor.
Definition: ev3cxx_motor.h:47
MotorType
Enum with index of motor type.
Definition: ev3cxx_motor.h:28
float rotations() const
Get the actual position of a motor in rotations.
Definition: ev3cxx_motor.h:185
void on(int power=50)
Set speed on regulated motor.
Definition: ev3cxx_motor.h:91
motor_port_t getPort() const
Get motor port.
Definition: ev3cxx_motor.h:209
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.
Definition: ev3cxx_motor.h:147
void onForSeconds(int speed=50, unsigned int time_ms=1000, bool_t brake=true)
Rotates the motor for a given time.
Definition: ev3cxx_motor.h:165
int degrees() const
Get the actual position of a motor in degrees.
Definition: ev3cxx_motor.h:177
MotorPort
Enum with index of motor port (A - D)
Definition: ev3cxx_motor.h:17
Motor port B.
void resetPosition()
Reset the angular position of the motor to zero.
Definition: ev3cxx_motor.h:202
int currentPower() const
Get the actual power (equivalent with speed) of a motor. [TODO: power/speed].
Definition: ev3cxx_motor.h:195
Medium servo motor.
Motor port C.
ER_UINT getType() const
Get curent type of motor which is set in EV3RT (not set by constructor).
Definition: ev3cxx_motor.h:218
Motor(MotorPort port, MotorType type=MotorType::LARGE)
Constructor of class motor.
Definition: ev3cxx_motor.h:58
Motor port A.
void off(bool brake=true)
Stop motor.
Definition: ev3cxx_motor.h:80
~Motor()
Destructor of class motor.
Definition: ev3cxx_motor.h:70
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
Motor port D.