RBControl
Library for the RB3201-RBControl board with the ESP32 by RoboticsBrno.
RBControl_motor.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 #include <functional>
5 #include <memory>
6 
7 #include "RBControl_encoder.hpp"
8 #include "RBControl_pinout.hpp"
10 #include "RBControl_util.hpp"
11 
12 namespace rb {
13 
14 class Manager;
15 class MotorChangeBuilder;
16 
17 class Motor {
18  friend class Manager;
19  friend class MotorChangeBuilder;
20 
21 public:
26  void power(int8_t value);
27 
33  void pwmMaxPercent(int8_t percent);
34 
38  int8_t pwmMaxPercent() const { return m_pwm_max_percent; }
39 
43  void stop();
44 
48  void driveToValue(int32_t positionAbsolute, uint8_t power, std::function<void(Encoder&)> callback = nullptr);
52  void drive(int32_t positionRelative, uint8_t power, std::function<void(Encoder&)> callback = nullptr);
53 
57  Encoder* encoder();
58 
62  Encoder* enc() { return encoder(); }
63 
64 private:
66  Motor(const Motor&) = delete;
67 
68  bool direct_power(int8_t power);
69  bool direct_pwmMaxPercent(int8_t percent);
70  bool direct_stop(int8_t);
71 
72  Manager& m_man;
73 
74  SerialPWM::value_type& m_pwm0;
75  SerialPWM::value_type& m_pwm1;
76  MotorId m_id;
77 
78  std::mutex m_mutex;
79  std::unique_ptr<Encoder> m_encoder;
80  int8_t m_power;
81 
82  int8_t m_pwm_max_percent;
83  float m_pwm_scale;
84 };
85 
86 } // namespace rb
Definition: RBControl_encoder.hpp:17
The main library class for working with the RBControl board. Call the install() method at the start o...
Definition: RBControl_manager.hpp:49
Helper class for building the motor change event.
Definition: RBControl_manager.hpp:183
Definition: RBControl_motor.hpp:17
int8_t pwmMaxPercent() const
Get current maximum PWM percent value.
Definition: RBControl_motor.hpp:38
void power(int8_t value)
Set motor power.
Definition: RBControl_motor.cpp:71
void drive(int32_t positionRelative, uint8_t power, std::function< void(Encoder &)> callback=nullptr)
Drive motor to set position (according relative value). See Encoder::drive.
Definition: RBControl_motor.cpp:83
Encoder * enc()
Get the Encoder instance for this motor. Same as encoder.
Definition: RBControl_motor.hpp:62
void stop()
Stop motor.
Definition: RBControl_motor.cpp:67
Encoder * encoder()
Get the Encoder instance for this motor. See Encoder.
Definition: RBControl_motor.cpp:87
void driveToValue(int32_t positionAbsolute, uint8_t power, std::function< void(Encoder &)> callback=nullptr)
Drive motor to set position (according absolute value). See Encoder::driveToValue.
Definition: RBControl_motor.cpp:79
int value_type
Definition: RBControl_serialPWM.hpp:15
The base namespace. Contains some logging functions, too.
Definition: half_duplex_uart.cpp:53
MotorId
Definition: RBControl_pinout.hpp:8