RBCX
Library for the RB3204-RBCX board with the ESP32 by RoboticsBrno.
Loading...
Searching...
No Matches
RBCXManager.h
Go to the documentation of this file.
1#pragma once
2
3// clang-format off
4#include <freertos/FreeRTOS.h>
5#include <freertos/queue.h>
6#include "freertos/semphr.h"
7// clang-format on
8#include <functional>
9#include <list>
10#include <memory>
11#include <mutex>
12#include <vector>
13
14#include "RBCXBattery.h"
15#include "RBCXButtons.h"
16#include "RBCXLeds.h"
17#include "RBCXMotor.h"
18#include "RBCXMpu.h"
19#include "RBCXOled.h"
20#include "RBCXPiezo.h"
21#include "RBCXStupidServo.h"
22#include "RBCXTimers.h"
23#include "RBCXUltrasound.h"
25#include "RBCXStupidServo.h"
26
27#include "coproc_link_parser.h"
28#include "rbcx.pb.h"
29
30namespace rb {
31
32class MotorChangeBuilder;
33
36 MAN_NONE = 0,
40
46};
47
50 return static_cast<ManagerInstallFlags>(
51 static_cast<int>(a) | static_cast<int>(b));
52}
53
54// Periodically print info about all RBCX tasks to the console
55// #define RB_DEBUG_MONITOR_TASKS 1
56
61class Manager {
62 friend class MotorChangeBuilder;
63 friend class Encoder;
65 friend class Motor;
66
67public:
68 Manager(Manager const&) = delete;
69 void operator=(Manager const&) = delete;
70
77 static Manager& get() {
78 static Manager instance;
79 return instance;
80 }
81
88 BaseType_t managerLoopStackSize = 3072);
89
94 //SmartServoBus& initSmartServoBus(uint8_t servo_count);
95
100 //SmartServoBus& servoBus() { return m_servos; };
101
102 Ultrasound& ultrasound(uint8_t index) { return m_ultrasounds[index]; }
103
104 StupidServo& stupidServo(uint8_t index);
105
106 Oled& oled() { return m_oled; }
107 Mpu& mpu() { return m_mpu; }
108
109 // Pass to Esp32-lx16a library's begin()
111
112 Piezo& piezo() { return m_piezo; }
114 return m_battery;
115 }
116 Leds& leds() { return m_leds; }
117 Buttons& buttons() { return m_buttons; }
118
120 return m_motors[static_cast<int>(id)];
121 };
122
124 setMotors();
125
134 void schedule(uint32_t period_ms, std::function<bool()> callback) {
135 timers().schedule(period_ms, callback);
136 }
137
138 inline Timers& timers() { return rb::Timers::get(); }
139
140 // internal api to monitor RBCX tasks
141 void monitorTask(TaskHandle_t task);
142
143 void sendToCoproc(const CoprocReq& msg);
144
145 const CoprocStat_VersionStat& coprocFwVersion() const {
146 return m_coprocFwVersion;
147 }
148
149 void coprocFwVersionAssert(uint32_t minVersion, const char* name);
150
151private:
152 Manager();
153 ~Manager();
154
155 static void consumerRoutineTrampoline(void* cookie);
156 void consumerRoutine();
157
158 static void keepaliveRoutine(void* cookie);
159
160 void resetMotorsFailSafe();
161 bool motorsFailSafe();
162
163#ifdef RB_DEBUG_MONITOR_TASKS
164 bool printTasksDebugInfo();
165
166 std::vector<TaskHandle_t> m_tasks;
167 std::mutex m_tasks_mutex;
168#endif
169 void fault(CoprocStat_FaultStat faultStat);
170
171 TaskHandle_t m_keepaliveTask;
172
173 CoprocCodec m_codec;
174 uint8_t m_txBuf[CoprocCodec::MaxFrameSize];
175 std::mutex m_codecTxMutex;
176
177 uint16_t m_coprocWatchdogTimer;
178
179 CoprocStat_VersionStat m_coprocFwVersion;
180 SemaphoreHandle_t m_coprocSemaphore;
181
182 TickType_t m_motors_last_set;
183 Motor m_motors[size_t(MotorId::MAX)];
184
185 rb::Oled m_oled;
186 rb::Mpu m_mpu;
187 rb::Piezo m_piezo;
188 rb::Leds m_leds;
189 rb::Buttons m_buttons;
190 rb::Battery m_battery;
191 rb::Ultrasound m_ultrasounds[UltrasoundsCount];
192 rb::SmartServoBusBackend m_smartServoBusBackend;
193};
194
199public:
204
210 MotorChangeBuilder& power(MotorId id, int16_t value);
211
217 MotorChangeBuilder& speed(MotorId id, int16_t ticksPerSecond);
218
224 MotorChangeBuilder& brake(MotorId id, uint16_t brakingPower);
225
231 MotorChangeBuilder& pwmMaxPercent(MotorId id, int8_t percent);
232
241 MotorChangeBuilder& drive(MotorId id, int32_t positionRelative,
242 int16_t speedTicksPerSecond, Motor::callback_t callback = nullptr);
243
252 MotorChangeBuilder& driveToValue(MotorId id, int32_t positionAbsolute,
253 int16_t speedTicksPerSecond, Motor::callback_t callback = nullptr);
254
260 void set();
261
262private:
263 std::vector<std::function<void()>> m_calls;
264};
265
266} // namespace rb
Contains the battery state and can control the robot's power.
Definition: RBCXBattery.h:12
Definition: RBCXButtons.h:39
Definition: RBCXLeds.h:31
The main library class for working with the RBCX board. Call the install() method at the start of you...
Definition: RBCXManager.h:61
void sendToCoproc(const CoprocReq &msg)
Definition: RBCXManager.cpp:185
void install(ManagerInstallFlags flags=MAN_NONE, BaseType_t managerLoopStackSize=3072)
Initialize the manager - must be called once at the start of the program.
Definition: RBCXManager.cpp:25
Timers & timers()
Definition: RBCXManager.h:138
void coprocFwVersionAssert(uint32_t minVersion, const char *name)
Definition: RBCXManager.cpp:197
static Manager & get()
Get manager instance.
Definition: RBCXManager.h:77
friend class PcntInterruptHandler
Definition: RBCXManager.h:64
Motor & motor(MotorId id)
Definition: RBCXManager.h:119
Leds & leds()
Get the Leds helper.
Definition: RBCXManager.h:116
Battery & battery()
Get the Battery interface.
Definition: RBCXManager.h:113
Mpu & mpu()
Get the Piezo controller.
Definition: RBCXManager.h:107
Oled & oled()
Get the Piezo controller.
Definition: RBCXManager.h:106
friend class Encoder
Definition: RBCXManager.h:63
StupidServo & stupidServo(uint8_t index)
Definition: RBCXManager.cpp:216
void monitorTask(TaskHandle_t task)
Definition: RBCXManager.cpp:246
Ultrasound & ultrasound(uint8_t index)
Initialize the UART servo bus for intelligent servos LX-16. TODO: smart servos not implemented!
Definition: RBCXManager.h:102
void schedule(uint32_t period_ms, std::function< bool()> callback)
Schedule callback to fire after period (in millisecond).
Definition: RBCXManager.h:134
MotorChangeBuilder setMotors()
Get a motor instance.
Definition: RBCXManager.cpp:244
Manager(Manager const &)=delete
const CoprocStat_VersionStat & coprocFwVersion() const
Definition: RBCXManager.h:145
Piezo & piezo()
Get the Piezo controller.
Definition: RBCXManager.h:112
SmartServoBusBackend & smartServoBusBackend()
Definition: RBCXManager.cpp:211
void operator=(Manager const &)=delete
Buttons & buttons()
Definition: RBCXManager.h:117
Helper class for building the motor change event.
Definition: RBCXManager.h:198
void set()
Finish the changes and submit the events. This method must be called at the end of the motor change e...
Definition: RBCXManager.cpp:340
MotorChangeBuilder & drive(MotorId id, int32_t positionRelative, int16_t speedTicksPerSecond, Motor::callback_t callback=nullptr)
Drive motor to relative position.
Definition: RBCXManager.cpp:319
MotorChangeBuilder & driveToValue(MotorId id, int32_t positionAbsolute, int16_t speedTicksPerSecond, Motor::callback_t callback=nullptr)
Drive motor to absolute position.
Definition: RBCXManager.cpp:330
MotorChangeBuilder(const MotorChangeBuilder &o)=delete
MotorChangeBuilder & brake(MotorId id, uint16_t brakingPower)
Start braking.
Definition: RBCXManager.cpp:312
MotorChangeBuilder & power(MotorId id, int16_t value)
Set single motor power.
Definition: RBCXManager.cpp:292
MotorChangeBuilder()
Definition: RBCXManager.cpp:285
MotorChangeBuilder & speed(MotorId id, int16_t ticksPerSecond)
Set single motor power.
Definition: RBCXManager.cpp:298
MotorChangeBuilder & pwmMaxPercent(MotorId id, int8_t percent)
Limit motor index's power to percent.
Definition: RBCXManager.cpp:305
~MotorChangeBuilder()
Definition: RBCXManager.cpp:290
Definition: RBCXMotor.h:19
std::function< void(Motor &)> callback_t
Definition: RBCXMotor.h:24
Helper class for controlling the Mpu.
Definition: RBCXMpu.h:33
Helper class for controlling the Oled.
Definition: RBCXOled.h:11
Helper class for controlling the piezo.
Definition: RBCXPiezo.h:10
Definition: RBCXSmartServoBusBackend.h:11
Definition: RBCXStupidServo.h:12
Definition: RBCXTimers.h:14
uint16_t schedule(uint32_t period_ms, std::function< bool()> callback)
Schedule callback to fire after period (in millisecond).
Definition: RBCXTimers.cpp:44
static Timers & get()
Definition: RBCXTimers.cpp:19
Definition: RBCXUltrasound.h:17
Definition: RBCXAngle.cpp:3
static constexpr int UltrasoundsCount
Definition: RBCXPinout.h:27
ManagerInstallFlags operator|(ManagerInstallFlags a, ManagerInstallFlags b)
Definition: RBCXManager.h:48
MotorId
Definition: RBCXPinout.h:8
@ MAX
Maximal motor ID.
ManagerInstallFlags
This enum contains flags for the Manager's install() method.
Definition: RBCXManager.h:35
@ MAN_DISABLE_ESP_WATCHDOG
Definition: RBCXManager.h:45
@ MAN_DISABLE_MOTOR_FAILSAFE
Definition: RBCXManager.h:39
@ MAN_NONE
Definition: RBCXManager.h:36