RBCX
Library for the RB3204-RBCX board with the ESP32 by RoboticsBrno.
Loading...
Searching...
No Matches
RBCXMotor.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <functional>
5#include <memory>
6#include <mutex>
7#include <vector>
8
9#include "RBCXPinout.h"
10#include "RBCXUtil.h"
11
12#include "rbcx.pb.h"
13
14namespace rb {
15
16class Manager;
17class MotorChangeBuilder;
18
19class Motor {
20 friend class Manager;
21 friend class MotorChangeBuilder;
22
23public:
24 typedef std::function<void(Motor&)> callback_t;
25
26 MotorId id() const { return m_id; }
27
28 int32_t position() const { return m_position; }
29
30 void requestInfo(callback_t cb);
31
36 void power(int16_t value);
37
42 void speed(int16_t ticksPerSecond);
43
48 void brake(uint16_t brakingPower);
49
56 void pwmMaxPercent(int8_t percent);
57
62 int8_t pwmMaxPercent() const { return m_pwm_max_percent; }
63
71 void driveToValue(int32_t positionAbsolute, int16_t speedTicksPerSecond,
72 callback_t callback = nullptr);
73
84 void drive(int32_t positionRelative, int16_t speedTicksPerSecond,
85 callback_t callback = nullptr);
86
91 void setCurrentPosition(int32_t pos = 0);
92
97 void setConfig(const MotorConfig& cfg);
98
99private:
100 Motor();
101 Motor(const Motor&) = delete;
102
103 void setId(MotorId id) { m_id = id; }
104
105 void sendPositionReq(bool additive,
106 const CoprocReq_MotorReq_SetPosition& req, callback_t&& callback);
107 void sendMotorReq(const CoprocReq_MotorReq& req);
108 void onMotorStat(const CoprocStat_MotorStat& msg);
109
110 int16_t scale(int16_t val);
111
112 callback_t m_positionCb;
113 std::vector<callback_t> m_infoCbs;
114 CoprocReq_MotorReq m_lastReq;
115 std::mutex m_mutex;
116
117 MotorId m_id;
118 MotorMode m_mode;
119 int32_t m_position;
120 int16_t m_power;
121 int16_t m_speed;
122
123 int8_t m_pwm_max_percent;
124};
125
126} // namespace rb
The main library class for working with the RBCX board. Call the install() method at the start of you...
Definition: RBCXManager.h:61
Helper class for building the motor change event.
Definition: RBCXManager.h:198
Definition: RBCXMotor.h:19
int8_t pwmMaxPercent() const
Get current maximum PWM percent value.
Definition: RBCXMotor.h:62
MotorId id() const
Definition: RBCXMotor.h:26
void brake(uint16_t brakingPower)
Start braking.
Definition: RBCXMotor.cpp:62
void setCurrentPosition(int32_t pos=0)
Set the encoder tick counter to some value.
Definition: RBCXMotor.cpp:121
void power(int16_t value)
Set motor power.
Definition: RBCXMotor.cpp:42
void driveToValue(int32_t positionAbsolute, int16_t speedTicksPerSecond, callback_t callback=nullptr)
Drive motor to set position (according absolute value).
Definition: RBCXMotor.cpp:76
int32_t position() const
Definition: RBCXMotor.h:28
void setConfig(const MotorConfig &cfg)
Set configuration, see MotorConfig & STM32 firmware.
Definition: RBCXMotor.cpp:101
void requestInfo(callback_t cb)
Definition: RBCXMotor.cpp:19
void speed(int16_t ticksPerSecond)
Set motor speed.
Definition: RBCXMotor.cpp:52
void drive(int32_t positionRelative, int16_t speedTicksPerSecond, callback_t callback=nullptr)
Drive motor to set position (according relative value).
Definition: RBCXMotor.cpp:89
std::function< void(Motor &)> callback_t
Definition: RBCXMotor.h:24
Definition: RBCXAngle.cpp:3
MotorId
Definition: RBCXPinout.h:8