1+ #ifndef SERVO_AX12_H
2+ #define SERVO_AX12_H
3+
4+ #include < Dynamixel2Arduino.h>
5+ #include < unordered_map>
6+
7+ #include " ESP32_Helper.h"
8+
9+ namespace ServoAX12
10+ {
11+ // https://github.com/ROBOTIS-GIT/Dynamixel2Arduino/tree/master
12+
13+ constexpr size_t MAX_BAUD = 5 ;
14+ const BaudRate dxlBaud[MAX_BAUD] = {BaudRate::BAUD_RATE_57600,
15+ BaudRate::BAUD_RATE_115200,
16+ BaudRate::BAUD_RATE_1000000,
17+ BaudRate::BAUD_RATE_2000000,
18+ BaudRate::BAUD_RATE_3000000};
19+
20+ enum class DxlProtocolVersion
21+ {
22+ PROTOCOL_1 = 1 ,
23+ PROTOCOL_2 = 2
24+ };
25+
26+ constexpr size_t MAX_PROTOCOL = 2 ;
27+ const DxlProtocolVersion dxlProtocol[MAX_PROTOCOL] = {DxlProtocolVersion::PROTOCOL_1,
28+ DxlProtocolVersion::PROTOCOL_2};
29+
30+ // id vitesse acceleration position command_position ledState
31+ /* *
32+ * @brief Structure représentant l'état d'un servo moteur.
33+ * @param id Identifiant du servo moteur.
34+ * @param vitesse Vitesse maximale du servo moteur en degrés par seconde.
35+ * @param positionMin Position minimale du servo moteur.
36+ * @param positionMax Position maximale du servo moteur.
37+ * @param command_position Position cible du servo moteur.
38+ * @param ledState État de la LED du servo moteur (allumée en mouvement, éteinte
39+ * sinon).
40+ */
41+ struct ServoMotion
42+ {
43+ uint8_t id;
44+ String name;
45+ float position;
46+ uint16_t positionMin;
47+ uint16_t positionMax;
48+ float command_position;
49+ bool IsMoving;
50+ bool ledState;
51+
52+ ServoMotion ()
53+ {
54+ id = (uint8_t )DXL_BROADCAST_ID;
55+ name = " " ;
56+ position = 0 ;
57+ positionMin = 0 ;
58+ positionMax = 0 ;
59+ command_position = 0 ;
60+ IsMoving = false ;
61+ ledState = false ;
62+ }
63+
64+ /* *
65+ * @brief Construct a new Servo Motion object
66+ *
67+ * @param _id Identifiant du servo moteur
68+ * @param _vitesse Vitesse maximale du servo moteur en degrés par seconde
69+ * @param _acceleration Accélération maximale du servo moteur en degrés par
70+ * seconde carrée
71+ */
72+ ServoMotion (uint8_t _id,
73+ String _name,
74+ uint16_t _positionMin,
75+ uint16_t _positionMax)
76+ {
77+ // Initialisation des valeurs
78+ id = _id;
79+ name = _name;
80+ position = 0 ;
81+ positionMin = _positionMin;
82+ positionMax = _positionMax;
83+ command_position = 0 ;
84+ IsMoving = false ;
85+ ledState = false ;
86+ }
87+
88+ bool operator ==(const ServoMotion &other) const
89+ {
90+ return id == other.id ;
91+ }
92+ };
93+
94+ void Initialisation (HardwareSerial &serial, int8_t rxPin, int8_t txPin, int8_t dirPin, BaudRate baudRate = BaudRate::BAUD_RATE_1000000, DxlProtocolVersion dxlProtocolVersion = DxlProtocolVersion::PROTOCOL_1);
95+
96+ [[noreturn]] void TaskUpdateServo (void *pvParameters);
97+
98+ void InitAllServo ();
99+ void InitServo (ServoMotion &servo);
100+
101+ void AddServo (uint8_t id, String name, uint16_t positionMin, uint16_t positionMax);
102+
103+ void StopAllServo ();
104+ void StopServo (ServoMotion &servo);
105+
106+ void StartAllServo ();
107+ void StartServo (ServoMotion &servo);
108+
109+ void UpdateAllServo ();
110+ void UpdateServo (ServoMotion &servo);
111+
112+ bool AreAllServoMoving ();
113+ bool IsServoMoving (uint8_t id);
114+
115+ void SetServoPosition (uint8_t id, float position);
116+
117+ void HandleCommand (Command cmd);
118+ const void PrintCommandHelp ();
119+ int16_t Scan ();
120+ int16_t Scan (DxlProtocolVersion _protocol, BaudRate _dxlBaud);
121+ void PrintDxlInfo (uint8_t id = DXL_BROADCAST_ID);
122+
123+ void TeleplotPosition ();
124+ void PrintPosition ();
125+ } // namespace ServoAX12
126+
127+ namespace std
128+ {
129+ template <> struct hash <ServoAX12::ServoMotion>
130+ {
131+ std::size_t operator ()(const ServoAX12::ServoMotion &k) const
132+ {
133+ // Hash only the name, since operator== only compares id
134+ return std::hash<uint8_t >()(k.id );
135+ }
136+ };
137+ } // namespace std
138+ #endif
0 commit comments