MatrixMiniR4 1.1.5
Matrix Mini R4 Arduino Library API Documentation
Loading...
Searching...
No Matches
MMLower.h
Go to the documentation of this file.
1
6#ifndef MMLOWER_H
7#define MMLOWER_H
8
9#include <Arduino.h>
10#include <SoftwareSerial.h>
11
12#define MR4_DEBUG_ENABLE false
13#define MR4_DEBUG_SERIAL Serial
14#if MR4_DEBUG_ENABLE
15# define MR4_DEBUG_HEADER() MR4_DEBUG_SERIAL.println(F("\nBMR4\n"))
16# define MR4_DEBUG_TAIL() MR4_DEBUG_SERIAL.println(F("\nEMR4\n"))
17# define MR4_DEBUG_PRINT(...) MR4_DEBUG_SERIAL.print(__VA_ARGS__)
18# define MR4_DEBUG_PRINTLN(...) MR4_DEBUG_SERIAL.println(__VA_ARGS__)
19# define MR4_DEBUG_PRINT_HEADER(...) \
20 do { \
21 MR4_DEBUG_HEADER(); \
22 MR4_DEBUG_SERIAL.println(__VA_ARGS__); \
23 } while (0)
24# define MR4_DEBUG_PRINT_TAIL(...) \
25 do { \
26 MR4_DEBUG_SERIAL.println(__VA_ARGS__); \
27 MR4_DEBUG_TAIL(); \
28 } while (0)
29#else
30# define MR4_DEBUG_HEADER()
31# define MR4_DEBUG_TAIL()
32# define MR4_DEBUG_PRINT_HEADER(...)
33# define MR4_DEBUG_PRINT_TAIL(...)
34# define MR4_DEBUG_PRINT(...)
35# define MR4_DEBUG_PRINTLN(...)
36#endif
37
38#define MatrixR4_COMM_LEAD 0x7B
39
40#define MatrixR4_SERVO_NUM 4
41#define MatrixR4_DC_MOTOR_NUM 4
42#define MatrixR4_ENCODER_NUM 4
43#define MatrixR4_BUTTON_NUM 2
44
45#define DIR_REVERSE (MatrixMiniR4::DIR::REVERSE)
46#define DIR_FORWARD (MatrixMiniR4::DIR::FORWARD)
47
52{
53public:
54 MMLower(uint8_t rx, uint8_t tx, uint32_t baudrate);
55
56 enum class COMM_STATE
57 {
61 ERROR,
62 };
63
121
122 enum class BTN_STATE
123 {
124 NOKEY,
125 F_EDGE,
126 REPEAT,
127 PRESSED,
128 R_EDGE,
129 };
130
131 enum class DIR
132 {
133 REVERSE,
134 FORWARD,
135 };
136
138 {
139 PASSIVE,
140 ACTIVE,
141 MAX,
142 };
143
145 {
146 PASSIVE,
147 ACTIVE,
148 MAX,
149 };
150
151 enum class IMU_ECHO_MODE
152 {
153 PASSIVE,
154 TIMING,
155 ACTIVE,
156 MAX,
157 };
158
159 enum class IMU_ACC_FSR
160 {
161 _2G,
162 _4G,
163 _8G,
164 _16G,
165 };
166
167 enum class IMU_GYRO_FSR
168 {
169 _250DPS,
170 _500DPS,
171 _1000DPS,
172 _2000DPS,
173 };
174
175 enum class IMU_ODR
176 {
177 _10_SPS,
178 _20_SPS,
179 _25_SPS,
180 _50_SPS,
181 _100_SPS,
182 _125_SPS,
183 _250_SPS,
184 _500_SPS,
185 _1000_SPS,
186 _2000_SPS,
187 _4000_SPS,
188 _8000_SPS,
189 };
190
191 enum class IMU_FIFO
192 {
193 ENABLE,
194 DISABLE,
195 };
196
197 enum class MOVE_TYPE
198 {
199 // 兩輪差速
200 DIFF,
201 // 四輪全向
202 OMNI,
203 };
204
205 enum class MOVE_ACTION
206 {
207 STOP,
208 FORWARD,
209 BACKWARD,
210 LEFT,
211 RIGHT,
212 };
213
214 enum class RESULT
215 {
216 OK,
217 ERROR,
222
223 // SERVO PULSE RANGE
226
227 // SERVO ANGLE RANGE
230
233
234 // IMU Init
238
239 // Motor Speed
246
247 // Servo Angle
253
254 // Move Distance
258
259 // RunAutoQC
261
263 };
264
265 typedef struct
266 {
271 uint16_t m1_speed;
272 uint16_t m2_speed;
273 uint16_t m3_speed;
274 uint16_t m4_speed;
276
277 typedef struct
278 {
279 String fwVersion;
281 uint8_t modelIndex;
282 } AllInfo_t;
283
284 typedef void (*BtnChgCallback)(uint8_t num, BTN_STATE newState);
285
286 RESULT Init(uint32_t timeout_ms = 1000);
287 // Application API
288 // Setting-Init
289 RESULT SetDCMotorDir(uint8_t num, DIR dir);
290 RESULT SetEncoderDir(uint8_t num, DIR dir);
291 RESULT SetServoDir(uint8_t num, DIR dir);
292 RESULT SetDCMotorSpeedRange(uint8_t num, uint16_t min, uint16_t max);
293 RESULT SetServoPulseRange(uint8_t num, uint16_t min, uint16_t max);
294 RESULT SetServoAngleRange(uint8_t num, uint16_t min, uint16_t max);
295 RESULT SetIMUEchoMode(IMU_ECHO_MODE mode, uint16_t echoIntervalMs);
296 RESULT SetIMUInit(IMU_ACC_FSR accFSR, IMU_GYRO_FSR gyroFSR, IMU_ODR odr, IMU_FIFO fifo);
297 RESULT SetPowerParam(float fullVolt, float cutOffVolt, float alarmVolt);
298 RESULT SetStateLED(uint8_t brightness, uint32_t colorRGB);
299 RESULT SetIMUToZero(void);
300 // Setting-Commonly used
301 RESULT SetDCMotorPower(uint8_t num, int16_t power);
302 RESULT SetDCMotorSpeed(uint8_t num, int16_t speed);
303 RESULT SetDCMotorRotate(uint8_t num, int16_t maxSpeed, uint16_t degree);
305 RESULT SetServoAngle(uint8_t num, uint16_t angle);
306 RESULT SetAllServoAngle(uint16_t angle1, uint16_t angle2, uint16_t angle3, uint16_t angle4);
307 RESULT SetMoveDistance(MOVE_TYPE type, MOVE_ACTION action, uint16_t speed, uint16_t enCounter);
308 RESULT SetEncoderResetCounter(uint8_t num);
309 RESULT SetPIDParam(uint8_t num, uint8_t pidNum, float kp, float ki, float kd);
310 RESULT SetDCBrake(uint8_t num);
311 // Getting
312 RESULT GetButtonState(uint8_t num, bool& btnState);
313 RESULT GetButtonsState(bool* btnsState);
314 RESULT GetEncoderCounter(uint8_t num, int32_t& enCounter);
316 RESULT GetIMUEuler(int16_t& roll, int16_t& pitch, int16_t& yaw);
317 RESULT GetIMUGyro(double& x, double& y, double& z);
318 RESULT GetIMUAcc(double& x, double& y, double& z);
319 RESULT GetPowerInfo(float& curVolt, float& curVoltPerc);
320 RESULT GetRotateState(uint8_t num, bool& isEnd);
321 // Other-Info
322 RESULT EchoTest(void);
323 RESULT GetFWVersion(String& version);
324 RESULT GetFWBuildDay(String& date);
325 RESULT GetFWDescriptor(String& descriptor);
326 RESULT GetModelIndex(uint8_t& index);
328 RESULT RunAutoQC(void);
329
330 void loop(void);
331 void onBtnChg(BtnChgCallback callback);
332
333 // TODO: 外部存取?
334 // Encoders
336 // IMU
339
340private:
341 uint32_t _baudrate;
342 SoftwareSerial* commSerial;
343 BtnChgCallback callbackFunc;
344
345 void CommSendData(COMM_CMD cmd, uint8_t* data = NULL, uint16_t size = 0);
346 void CommSendData(COMM_CMD cmd, uint8_t data);
347 bool CommReadData(uint8_t* data, uint16_t size = 1, uint32_t timeout_ms = 10);
348 bool WaitData(COMM_CMD cmd = COMM_CMD::NONE, uint32_t timeout_ms = 0);
349 void HandleCommand(uint8_t cmd);
350};
351
352extern MMLower mmL;
353
354#endif // MMLOWER_H
MMLower mmL
#define MatrixR4_ENCODER_NUM
Definition MMLower.h:42
#define min(a, b)
Handling the Lower MCU (STM32) communication.
Definition MMLower.h:52
RESULT SetAllServoAngle(uint16_t angle1, uint16_t angle2, uint16_t angle3, uint16_t angle4)
Definition MMLower.cpp:504
ENCODER_ECHO_MODE
Definition MMLower.h:145
IMU_GYRO_FSR
Definition MMLower.h:168
RESULT GetAllInfo(AllInfo_t &info)
Definition MMLower.cpp:1052
COMM_STATE
Definition MMLower.h:57
double imuGyroX
Definition MMLower.h:337
BUTTON_ECHO_MODE
Definition MMLower.h:138
RESULT GetRotateState(uint8_t num, bool &isEnd)
Definition MMLower.cpp:907
RESULT SetEncoderDir(uint8_t num, DIR dir)
Definition MMLower.cpp:61
MOVE_ACTION
Definition MMLower.h:206
RESULT SetStateLED(uint8_t brightness, uint32_t colorRGB)
Definition MMLower.cpp:669
RESULT SetServoAngleRange(uint8_t num, uint16_t min, uint16_t max)
Definition MMLower.cpp:179
RESULT SetServoPulseRange(uint8_t num, uint16_t min, uint16_t max)
Definition MMLower.cpp:142
RESULT SetDCMotorPower(uint8_t num, int16_t power)
Definition MMLower.cpp:327
RESULT RunAutoQC(void)
Definition MMLower.cpp:1082
void(*) BtnChgCallback(uint8_t num, BTN_STATE newState)
Definition MMLower.h:284
int32_t enCounter[MatrixR4_ENCODER_NUM]
Definition MMLower.h:335
RESULT SetDCMotorRotate(uint8_t num, int16_t maxSpeed, uint16_t degree)
Definition MMLower.cpp:393
RESULT GetIMUGyro(double &x, double &y, double &z)
Definition MMLower.cpp:837
RESULT SetServoAngle(uint8_t num, uint16_t angle)
Definition MMLower.cpp:472
double imuAccX
Definition MMLower.h:338
RESULT EchoTest(void)
Definition MMLower.cpp:929
RESULT GetFWVersion(String &version)
Definition MMLower.cpp:953
RESULT GetFWBuildDay(String &date)
Definition MMLower.cpp:975
void loop(void)
Definition MMLower.cpp:1107
IMU_ECHO_MODE
Definition MMLower.h:152
RESULT Init(uint32_t timeout_ms=1000)
Definition MMLower.cpp:15
RESULT GetFWDescriptor(String &descriptor)
Definition MMLower.cpp:1003
RESULT GetEncoderCounter(uint8_t num, int32_t &enCounter)
Definition MMLower.cpp:767
RESULT SetDCMotorSpeed(uint8_t num, int16_t speed)
Definition MMLower.cpp:360
RESULT GetPowerInfo(float &curVolt, float &curVoltPerc)
Definition MMLower.cpp:883
RESULT GetButtonState(uint8_t num, bool &btnState)
Definition MMLower.cpp:722
double imuGyroY
Definition MMLower.h:337
RESULT SetEncoderResetCounter(uint8_t num)
Definition MMLower.cpp:593
RESULT GetIMUAcc(double &x, double &y, double &z)
Definition MMLower.cpp:860
RESULT SetIMUEchoMode(IMU_ECHO_MODE mode, uint16_t echoIntervalMs)
Definition MMLower.cpp:216
double imuAccZ
Definition MMLower.h:338
RESULT GetModelIndex(uint8_t &index)
Definition MMLower.cpp:1031
RESULT SetAllDCMotorSpeed(Motors_Param_t param)
Definition MMLower.cpp:422
void onBtnChg(BtnChgCallback callback)
Definition MMLower.cpp:1112
RESULT SetServoDir(uint8_t num, DIR dir)
Definition MMLower.cpp:87
RESULT SetMoveDistance(MOVE_TYPE type, MOVE_ACTION action, uint16_t speed, uint16_t enCounter)
Definition MMLower.cpp:550
RESULT GetIMUEuler(int16_t &roll, int16_t &pitch, int16_t &yaw)
Definition MMLower.cpp:813
double imuGyroZ
Definition MMLower.h:337
RESULT GetAllEncoderCounter(int32_t *enCounter)
Definition MMLower.cpp:789
double imuAccY
Definition MMLower.h:338
IMU_ACC_FSR
Definition MMLower.h:160
RESULT SetPIDParam(uint8_t num, uint8_t pidNum, float kp, float ki, float kd)
Definition MMLower.cpp:618
RESULT SetIMUToZero(void)
Definition MMLower.cpp:698
MMLower(uint8_t rx, uint8_t tx, uint32_t baudrate)
Definition MMLower.cpp:9
RESULT SetDCMotorSpeedRange(uint8_t num, uint16_t min, uint16_t max)
Definition MMLower.cpp:113
RESULT GetButtonsState(bool *btnsState)
Definition MMLower.cpp:744
RESULT SetDCBrake(uint8_t num)
Definition MMLower.cpp:644
RESULT SetIMUInit(IMU_ACC_FSR accFSR, IMU_GYRO_FSR gyroFSR, IMU_ODR odr, IMU_FIFO fifo)
Definition MMLower.cpp:251
RESULT SetDCMotorDir(uint8_t num, DIR dir)
Definition MMLower.cpp:36
RESULT SetPowerParam(float fullVolt, float cutOffVolt, float alarmVolt)
Definition MMLower.cpp:294
uint8_t modelIndex
Definition MMLower.h:281