RBControl
Library for the RB3201-RBControl board with the ESP32 by RoboticsBrno.
RBControl_manager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <freertos/FreeRTOS.h>
4 #include <freertos/queue.h>
5 #include <functional>
6 #include <list>
7 #include <memory>
8 #include <mutex>
9 #include <vector>
10 
11 #include "Adafruit_MCP23017.h"
12 #include "RBControl_battery.hpp"
13 #include "RBControl_encoder.hpp"
14 #include "RBControl_leds.hpp"
15 #include "RBControl_motor.hpp"
16 #include "RBControl_nvs.hpp"
17 #include "RBControl_piezo.hpp"
18 #include "RBControl_servo.hpp"
19 #include "RBControl_timers.hpp"
20 
21 namespace rb {
22 
23 class MotorChangeBuilder;
24 
27  MAN_NONE = 0,
28  MAN_DISABLE_MOTOR_FAILSAFE = (1 << 0),
32  MAN_DISABLE_PIEZO = (1 << 2),
36 };
37 
39  return static_cast<ManagerInstallFlags>(static_cast<int>(a) | static_cast<int>(b));
40 }
41 
42 // Periodically print info about all rbcontrol tasks to the console
43 //#define RB_DEBUG_MONITOR_TASKS 1
44 
49 class Manager {
50  friend class MotorChangeBuilder;
51  friend class Encoder;
52  friend class PcntInterruptHandler;
53 
54 public:
55  Manager(Manager const&) = delete;
56  void operator=(Manager const&) = delete;
57 
64  static Manager& get() {
65  static Manager instance;
66  return instance;
67  }
68 
74  void install(ManagerInstallFlags flags = MAN_NONE);
75 
80  SmartServoBus& initSmartServoBus(uint8_t servo_count, gpio_num_t pin = GPIO_NUM_32, uart_port_t uart = UART_NUM_1);
85  SmartServoBus& servoBus() { return m_servos; };
86 
87  Adafruit_MCP23017& expander() { return m_expander; }
88  Piezo& piezo() { return m_piezo; }
89  Battery& battery() { return m_battery; }
90  Leds& leds() { return m_leds; }
91 
92  Motor& motor(MotorId id) { return *m_motors[static_cast<int>(id)]; };
94 
95  Nvs& config() { return m_config; }
96 
105  void schedule(uint32_t period_ms, std::function<bool()> callback) {
106  timers().schedule(period_ms, callback);
107  }
108 
109  inline Timers& timers() { return rb::Timers::get(); }
110 
111  // internal api to monitor RBControl tasks
112  void monitorTask(TaskHandle_t task);
113 
114 private:
115  Manager();
116  ~Manager();
117 
118  enum EventType {
119  EVENT_MOTORS,
120  EVENT_MOTORS_STOP_ALL,
121  EVENT_ENCODER_EDGE,
122  EVENT_ENCODER_PCNT,
123  };
124 
125  struct EventMotorsData {
126  bool (Motor::*setter_func)(int8_t);
127  MotorId id;
128  int8_t value;
129  };
130 
131  struct Event {
132  EventType type;
133  union {
134  std::vector<EventMotorsData>* motors;
135 
136  struct {
137  int64_t timestamp;
138  MotorId id;
139  uint8_t pinLevel;
140  } encoderEdge;
141 
142  struct {
143  uint32_t status;
144  MotorId id;
145  } encoderPcnt;
146  } data;
147  };
148 
149  void queue(const Event* event, bool toFront = false);
150  bool queueFromIsr(const Event* event, bool toFront = false);
151  static void consumerRoutineTrampoline(void* cookie);
152  void consumerRoutine();
153  void processEvent(struct Event* ev);
154 
155  bool motorsFailSafe();
156 
157  void setupExpander();
158 
159 #ifdef RB_DEBUG_MONITOR_TASKS
160  bool printTasksDebugInfo();
161 
162  std::vector<TaskHandle_t> m_tasks;
163  std::mutex m_tasks_mutex;
164 #endif
165 
166  QueueHandle_t m_queue;
167 
168  TickType_t m_motors_last_set;
169  std::vector<std::unique_ptr<Motor>> m_motors;
170  SerialPWM m_motors_pwm;
171 
172  Adafruit_MCP23017 m_expander;
173  rb::Piezo m_piezo;
174  rb::Leds m_leds;
175  rb::Battery m_battery;
176  rb::SmartServoBus m_servos;
177  rb::Nvs m_config;
178 };
179 
184 public:
185  MotorChangeBuilder(Manager& manager);
189 
195  MotorChangeBuilder& power(MotorId id, int8_t value);
196 
202  MotorChangeBuilder& pwmMaxPercent(MotorId id, int8_t percent);
203 
209 
214  void set(bool toFront = false);
215 
216 private:
217  Manager& m_manager;
218  std::unique_ptr<std::vector<Manager::EventMotorsData>> m_values;
219 };
220 
221 } // namespace rb
Controls the expander pins.
Definition: Adafruit_MCP23017.h:24
Contains the battery state and can control the robot's power.
Definition: RBControl_battery.hpp:17
Definition: RBControl_encoder.hpp:17
Helper class for controlling the LEDs connected to the expander.
Definition: RBControl_leds.hpp:10
The main library class for working with the RBControl board. Call the install() method at the start o...
Definition: RBControl_manager.hpp:49
static Manager & get()
Get manager instance.
Definition: RBControl_manager.hpp:64
SmartServoBus & servoBus()
Get the SmartServoBus for working with intelligent servos LX-16..
Definition: RBControl_manager.hpp:85
Nvs & config()
Definition: RBControl_manager.hpp:95
Timers & timers()
Definition: RBControl_manager.hpp:109
SmartServoBus & initSmartServoBus(uint8_t servo_count, gpio_num_t pin=GPIO_NUM_32, uart_port_t uart=UART_NUM_1)
Initialize the UART servo bus for intelligent servos LX-16.
Definition: RBControl_manager.cpp:183
friend class PcntInterruptHandler
Definition: RBControl_manager.hpp:52
Motor & motor(MotorId id)
Definition: RBControl_manager.hpp:92
void monitorTask(TaskHandle_t task)
Definition: RBControl_manager.cpp:192
Adafruit_MCP23017 & expander()
Get the expander Adafruit_MCP23017. LEDs and buttons are connected to it.
Definition: RBControl_manager.hpp:87
void schedule(uint32_t period_ms, std::function< bool()> callback)
Schedule callback to fire after period (in millisecond).
Definition: RBControl_manager.hpp:105
Piezo & piezo()
Get the Piezo controller.
Definition: RBControl_manager.hpp:88
MotorChangeBuilder setMotors()
Get a motor instance.
Definition: RBControl_manager.cpp:188
Manager(Manager const &)=delete
Leds & leds()
Get the Leds helper.
Definition: RBControl_manager.hpp:90
void install(ManagerInstallFlags flags=MAN_NONE)
Initialize the manager - must be called once at the start of the program.
Definition: RBControl_manager.cpp:38
Battery & battery()
Get the Battery interface.
Definition: RBControl_manager.hpp:89
void operator=(Manager const &)=delete
Helper class for building the motor change event.
Definition: RBControl_manager.hpp:183
MotorChangeBuilder & power(MotorId id, int8_t value)
Set single motor power.
Definition: RBControl_manager.cpp:228
MotorChangeBuilder & stop(MotorId id)
Stop motor.
Definition: RBControl_manager.cpp:244
MotorChangeBuilder(const MotorChangeBuilder &o)=delete
void set(bool toFront=false)
Finish the changes and submit the events.
Definition: RBControl_manager.cpp:252
MotorChangeBuilder(Manager &manager)
Definition: RBControl_manager.cpp:215
MotorChangeBuilder & pwmMaxPercent(MotorId id, int8_t percent)
Limit motor index's power to percent.
Definition: RBControl_manager.cpp:236
~MotorChangeBuilder()
Definition: RBControl_manager.cpp:225
Definition: RBControl_motor.hpp:17
Definition: RBControl_nvs.hpp:11
Helper class for controlling the piezo.
Definition: RBControl_piezo.hpp:10
Definition: RBControl_servo.hpp:18
Definition: RBControl_timers.hpp:14
uint16_t schedule(uint32_t period_ms, std::function< bool()> callback)
Schedule callback to fire after period (in millisecond).
Definition: RBControl_timers.cpp:45
static Timers & get()
Definition: RBControl_timers.cpp:18
The base namespace. Contains some logging functions, too.
Definition: half_duplex_uart.cpp:53
ManagerInstallFlags operator|(ManagerInstallFlags a, ManagerInstallFlags b)
Definition: RBControl_manager.hpp:38
MotorId
Definition: RBControl_pinout.hpp:8
ManagerInstallFlags
This enum contains flags for the Manager's install() method.
Definition: RBControl_manager.hpp:26
@ MAN_DISABLE_BATTERY_MANAGEMENT
auto-shutdown on low battery voltage.
Definition: RBControl_manager.hpp:30
@ MAN_DISABLE_PIEZO
Definition: RBControl_manager.hpp:32
@ MAN_DISABLE_MOTOR_FAILSAFE
after 300ms of no set motor power calls.
Definition: RBControl_manager.hpp:28
@ MAN_NONE
Definition: RBControl_manager.hpp:27