RBControl
Library for the RB3201-RBControl board with the ESP32 by RoboticsBrno.
RBControl_servo.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mutex>
4 #include <vector>
5 
6 #include <driver/gpio.h>
7 #include <driver/pcnt.h>
8 
9 #include "RBControl_angle.hpp"
10 #include "RBControl_util.hpp"
11 #include "lx16a.hpp"
12 
13 namespace rb {
14 
15 class Manager;
16 class Encoder;
17 
19  friend class Manager;
20 
21 public:
22  SmartServoBus();
24 
25  void set(uint8_t id, Angle ang, float speed = 180.f, float speed_raise = 0.0015f);
26  void limit(uint8_t id, Angle bottom, Angle top);
27 
28  Angle pos(uint8_t id);
29  Angle posOffline(uint8_t id);
30 
31  void setAutoStop(uint8_t id, bool enable = true);
32 
33  void setId(uint8_t newId, uint8_t destId = 254);
34  uint8_t getId(uint8_t destId = 254);
35 
36 private:
37  SmartServoBus(const SmartServoBus&) = delete;
38 
39  void install(uint8_t servo_count, uart_port_t uart, gpio_num_t pin);
40 
41  static void regulatorRoutineTrampoline(void* cookie);
42  void regulatorRoutine();
43  bool regulateServo(QueueHandle_t responseQueue, size_t id, uint32_t timeSliceMs);
44 
45  static void uartRoutineTrampoline(void* cookie);
46  void uartRoutine();
47  size_t uartReceive(uint8_t* buff, size_t bufcap);
48 
49  struct servo_info {
50  servo_info() {
51  current = 0xFFFF;
52  target = 0xFFFF;
53  speed_coef = 0.f;
54  speed_target = 0.f;
55  speed_raise = 0.f;
56  auto_stop = false;
57  auto_stop_counter = 0;
58  }
59 
60  bool hasValidCurrent() const {
61  return current != 0xFFFF;
62  }
63 
64  float speed_coef;
65  float speed_target;
66  float speed_raise;
67  uint16_t current;
68  uint16_t target;
69  bool auto_stop;
70  uint8_t auto_stop_counter;
71  };
72 
73  struct tx_request {
74  char data[16];
75  uint8_t size;
76  bool expect_response;
77  QueueHandle_t responseQueue;
78  };
79 
80  struct rx_response {
81  uint8_t data[16];
82  uint8_t size;
83  };
84 
85  void send(const lw::Packet& pkt,
86  QueueHandle_t responseQueue = NULL, bool expect_response = false, bool to_front = false);
87  void sendAndReceive(const lw::Packet& pkt, SmartServoBus::rx_response& res, bool to_front = false);
88 
89  std::vector<servo_info> m_servos;
90  std::mutex m_mutex;
91 
92  QueueHandle_t m_uart_queue;
93  uart_port_t m_uart;
94  gpio_num_t m_uart_pin;
95 };
96 
97 };
Definition: RBControl_angle.hpp:11
The main library class for working with the RBControl board. Call the install() method at the start o...
Definition: RBControl_manager.hpp:49
Definition: RBControl_servo.hpp:18
void setAutoStop(uint8_t id, bool enable=true)
Definition: RBControl_servo.cpp:141
uint8_t getId(uint8_t destId=254)
Definition: RBControl_servo.cpp:68
~SmartServoBus()
Definition: RBControl_servo.hpp:23
Angle posOffline(uint8_t id)
Definition: RBControl_servo.cpp:128
void setId(uint8_t newId, uint8_t destId=254)
Definition: RBControl_servo.cpp:63
Angle pos(uint8_t id)
Definition: RBControl_servo.cpp:106
void limit(uint8_t id, Angle bottom, Angle top)
Definition: RBControl_servo.cpp:136
void set(uint8_t id, Angle ang, float speed=180.f, float speed_raise=0.0015f)
Definition: RBControl_servo.cpp:76
SmartServoBus()
Definition: RBControl_servo.cpp:17
The base namespace. Contains some logging functions, too.
Definition: half_duplex_uart.cpp:53
Definition: lx16a.hpp:47