MatrixMiniR4 1.1.9
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
63
64 enum class COMM_CMD
65 {
66 NONE = 0x00,
67 // Setting-Init
80 SET_ALL_ENCODER_PPR, // 2025/05/22
81 SET_IMU_Calib_Data, // 2025/05/22
82
83 // Setting-Commonly used
96 SET_ALL_DC_BRAKE, // 2025/05/22
98
99 // Getting
110 GET_IMU_ACC_NOcal, // 2025/05/22
111 GET_ENCODER_DEGREES, // 2025/07/15
112
113 // Auto-Send
119
120 // New Function // 2025/05/22
130
131 // New Function // 2025/05/22
135
140
144
150
151
152 // Other-Info
153 ECHO_TEST = 0xFF,
154 F_VERSION = 0xFE,
160 };
161
170
171 enum class DIR
172 {
175 };
176
178 {
182 };
183
190
198
199 enum class IMU_ACC_FSR
200 {
205 };
206
214
230
231 enum class IMU_FIFO
232 {
235 };
236
237 enum class MOVE_TYPE
238 {
239 // 兩輪差速
241 // 四輪全向
243 };
244
253
304
320
321 typedef struct
322 {
327
328 uint16_t m1_speed;
329 uint16_t m2_speed;
330 uint16_t m3_speed;
331 uint16_t m4_speed;
332
333 uint16_t m1_power;
334 uint16_t m2_power;
335 uint16_t m3_power;
336 uint16_t m4_power;
337
339
340 typedef struct
341 {
342 String fwVersion;
344 uint8_t modelIndex;
345 } AllInfo_t;
346
347 typedef void (*BtnChgCallback)(uint8_t num, BTN_STATE newState);
348
349 RESULT Init(uint32_t timeout_ms = 1000);
350 // Application API
351 // Setting-Init
352 RESULT SetDCMotorDir(uint8_t num, DIR dir);
353 RESULT SetEncoderDir(uint8_t num, DIR dir);
354 RESULT SetServoDir(uint8_t num, DIR dir);
355 RESULT SetDCMotorSpeedRange(uint8_t num, uint16_t min, uint16_t max);
356 RESULT SetServoPulseRange(uint8_t num, uint16_t min, uint16_t max);
357 RESULT SetServoAngleRange(uint8_t num, uint16_t min, uint16_t max);
358 RESULT SetIMUEchoMode(IMU_ECHO_MODE mode, uint16_t echoIntervalMs);
359 RESULT SetIMUInit(IMU_ACC_FSR accFSR, IMU_GYRO_FSR gyroFSR, IMU_ODR odr, IMU_FIFO fifo);
360 RESULT SetPowerParam(float fullVolt, float cutOffVolt, float alarmVolt);
361 RESULT SetStateLED(uint8_t brightness, uint32_t colorRGB);
362 RESULT SetIMUToZero(void);
363 RESULT SetIMU_Calib_data(float * bufdata); // 2025/05/22
364 RESULT SetEncode_PPR_MaxRPM(uint8_t num, uint16_t ppr, uint16_t Maxspeed); // 2025/07/07
365 RESULT SetALL_Encode_PPR(uint16_t * ppr); // 2025/05/24
366 // Setting-Commonly used
367 RESULT SetDCMotorPower(uint8_t num, int16_t power);
368 RESULT SetDCMotorSpeed(uint8_t num, int16_t speed);
369 RESULT SetDCMotorRotate(uint8_t num, int16_t maxSpeed, uint16_t degree);
371 RESULT SetServoAngle(uint8_t num, uint16_t angle);
372 RESULT SetAllServoAngle(uint16_t angle1, uint16_t angle2, uint16_t angle3, uint16_t angle4);
373 RESULT SetMoveDistance(MOVE_TYPE type, MOVE_ACTION action, uint16_t speed, uint16_t enCounter);
374 RESULT SetEncoderResetCounter(uint8_t num);
375 RESULT SetPIDParam(uint8_t num, uint8_t pidNum, float kp, float ki, float kd);
376 RESULT SetDCBrake(uint8_t num);
377 RESULT SetALLDCBrake(void); // 2025/05/22
378 RESULT SetDC_Type_Brake(uint8_t num, uint8_t type); // 2025/05/23
379 RESULT SetALLDC_Type_Brake(uint8_t * type); // 2025/05/23
380 RESULT SetAllDCMotorPower(Motors_Param_t param); // 2025/05/23
381 // Getting
382 RESULT GetButtonState(uint8_t num, bool& btnState);
383 RESULT GetButtonsState(bool* btnsState);
384 RESULT GetEncoderCounter(uint8_t num, int32_t& enCounter);
386 RESULT GetIMUEuler(double& roll, double& pitch, double& yaw);
387 RESULT GetIMUGyro(double& x, double& y, double& z);
388 RESULT GetIMUAcc(double& x, double& y, double& z);
389 RESULT GetPowerInfo(float& curVolt, float& curVoltPerc);
390 RESULT GetRotateState(uint8_t num, bool& isEnd);
391 RESULT GetALLEncoderSpeed(int32_t * enSpeed); // 2025/05/22
392 RESULT Get_IMU_nancalib_acc(float * accdata); // 2025/05/22
393 RESULT GetEncoderDegrees(uint8_t num, int32_t& enDeges); // 2025/07/15
394 // Other-Info
395 RESULT EchoTest(void);
396 RESULT GetFWVersion(String& version);
397 RESULT GetFWBuildDay(String& date);
398 RESULT GetFWDescriptor(String& descriptor);
399 RESULT GetModelIndex(uint8_t& index);
401 RESULT RunAutoQC(void);
402
403 // Drive DC Function // 2025/05/30
404 Drive_RESULT Set_Drive2Motor_PARAM(uint8_t m1_num, uint8_t m2_num, DIR m1_dir, DIR m2_dir, uint8_t num);
405 RESULT Set_Drive_MoveSync_PID(float Kp, float Ki, float Kd, uint8_t num);
406 RESULT Set_Drive_MoveGyro_PID(float Kp, float Ki, float Kd, uint8_t num);
407 RESULT Set_Drive_MoveTurn_PID(float Kp, float Ki, float Kd, uint8_t num);
408 Drive_RESULT Set_Drive_Reset_Count(uint8_t num, bool reset);
409 Drive_RESULT Set_Drive_Encode_PPR(uint8_t num, uint16_t* ppr);
410 Drive_RESULT Set_Drive_Motor_Type(uint8_t num, uint8_t type);
411
412 Drive_RESULT Set_Drive_Move_Func(int16_t power_left, int16_t power_right, uint8_t num);
413 Drive_RESULT Set_Drive_Move_Degs(int16_t power_left, int16_t power_right, uint16_t Degree_c, bool brake, bool async, uint8_t num);
414 Drive_RESULT Set_Drive_Move_Time(int16_t power_left, int16_t power_right, uint32_t Time_mS, bool brake, bool async, uint8_t num);
415
416 Drive_RESULT Set_Drive_MoveSync_Func(int16_t power_left, int16_t power_right, uint8_t num);
417 Drive_RESULT Set_Drive_MoveSync_Degs(int16_t power_left, int16_t power_right, uint16_t Degree_c, bool brake, bool async, uint8_t num);
418 Drive_RESULT Set_Drive_MoveSync_Time(int16_t power_left, int16_t power_right, uint32_t Time_mS, bool brake, bool async, uint8_t num);
419
420 Drive_RESULT Set_Drive_MoveGyro_Func(int16_t power, int16_t Target_dri, uint8_t num);
421 Drive_RESULT Set_Drive_MoveGyro_Degs(int16_t power, int16_t Target_dri, uint16_t Degree_c, bool brake, bool async, uint8_t num);
422 Drive_RESULT Set_Drive_MoveGyro_Time(int16_t power, int16_t Target_dri, uint32_t Time_mS, bool brake, bool async, uint8_t num);
423
424 Drive_RESULT Set_Drive_TurnGyro(int16_t power, int16_t Target_dri, uint8_t mode, bool brake, bool async, uint8_t num);
425
426 Drive_RESULT Set_Drive_Brake(bool brake, uint8_t num);
427 Drive_RESULT Get_Drive_isTaskDone(uint8_t num, bool * isEnd);
428 Drive_RESULT Get_Drive_EncoderCounter(uint8_t num, int32_t& enCounter);
429 Drive_RESULT Get_Drive_Degrees(uint8_t num, int32_t& Degs);
430
431
432
433 void loop(void);
434 void onBtnChg(BtnChgCallback callback);
435
436 // TODO: 外部存取?
437 // Encoders
439 // IMU
442
443private:
444 uint32_t _baudrate;
445 SoftwareSerial* commSerial;
446 BtnChgCallback callbackFunc;
447
448 void CommSendData(COMM_CMD cmd, uint8_t* data = NULL, uint16_t size = 0);
449 void CommSendData(COMM_CMD cmd, uint8_t data);
450 bool CommReadData(uint8_t* data, uint16_t size = 1, uint32_t timeout_ms = 10);
451 bool WaitData(COMM_CMD cmd = COMM_CMD::NONE, uint32_t timeout_ms = 0);
452 void HandleCommand(uint8_t cmd);
453};
454
455extern MMLower mmL;
456
457#endif // MMLOWER_H
MMLower mmL(8, 9, 57600)
#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:564
ENCODER_ECHO_MODE
Definition MMLower.h:185
IMU_GYRO_FSR
Definition MMLower.h:208
Drive_RESULT Set_Drive_Move_Time(int16_t power_left, int16_t power_right, uint32_t Time_mS, bool brake, bool async, uint8_t num)
Definition MMLower.cpp:1685
RESULT Set_Drive_MoveGyro_PID(float Kp, float Ki, float Kd, uint8_t num)
Definition MMLower.cpp:1482
Drive_RESULT Set_Drive_Motor_Type(uint8_t num, uint8_t type)
Definition MMLower.cpp:1571
RESULT GetAllInfo(AllInfo_t &info)
Definition MMLower.cpp:1361
COMM_STATE
Definition MMLower.h:57
double imuGyroX
Definition MMLower.h:440
RESULT SetALLDCBrake(void)
Definition MMLower.cpp:760
BUTTON_ECHO_MODE
Definition MMLower.h:178
RESULT GetRotateState(uint8_t num, bool &isEnd)
Definition MMLower.cpp:1214
RESULT SetEncoderDir(uint8_t num, DIR dir)
Definition MMLower.cpp:70
MOVE_ACTION
Definition MMLower.h:246
Drive_RESULT Set_Drive_Move_Degs(int16_t power_left, int16_t power_right, uint16_t Degree_c, bool brake, bool async, uint8_t num)
Definition MMLower.cpp:1645
RESULT SetStateLED(uint8_t brightness, uint32_t colorRGB)
Definition MMLower.cpp:905
RESULT SetServoAngleRange(uint8_t num, uint16_t min, uint16_t max)
Definition MMLower.cpp:188
RESULT SetServoPulseRange(uint8_t num, uint16_t min, uint16_t max)
Definition MMLower.cpp:151
Drive_RESULT Set_Drive_MoveSync_Degs(int16_t power_left, int16_t power_right, uint16_t Degree_c, bool brake, bool async, uint8_t num)
Definition MMLower.cpp:1762
Drive_RESULT Set_Drive_MoveSync_Func(int16_t power_left, int16_t power_right, uint8_t num)
Definition MMLower.cpp:1725
RESULT GetIMUEuler(double &roll, double &pitch, double &yaw)
Definition MMLower.cpp:1120
Drive_RESULT Set_Drive_MoveGyro_Time(int16_t power, int16_t Target_dri, uint32_t Time_mS, bool brake, bool async, uint8_t num)
Definition MMLower.cpp:1919
RESULT Set_Drive_MoveSync_PID(float Kp, float Ki, float Kd, uint8_t num)
Definition MMLower.cpp:1455
RESULT SetDCMotorPower(uint8_t num, int16_t power)
Definition MMLower.cpp:336
@ SET_ENCODER_RESET_COUNTER
Definition MMLower.h:91
@ SET_DC_TWO_MOTOR_Reset_count
Definition MMLower.h:127
@ AUTO_SEND_ENCODER_COUNTER
Definition MMLower.h:115
@ SET_ENCODER_PPR_MAXSPEED
Definition MMLower.h:79
@ SET_DC_MOTOR_SPEED_RANGE
Definition MMLower.h:71
@ SET_Drive_MoveSyncDegsACC
Definition MMLower.h:138
RESULT RunAutoQC(void)
Definition MMLower.cpp:1391
RESULT SetALL_Encode_PPR(uint16_t *ppr)
Definition MMLower.cpp:873
Drive_RESULT Get_Drive_isTaskDone(uint8_t num, bool *isEnd)
Definition MMLower.cpp:2004
int32_t enCounter[MatrixR4_ENCODER_NUM]
Definition MMLower.h:438
Drive_RESULT Set_Drive_Brake(bool brake, uint8_t num)
Definition MMLower.cpp:2037
RESULT SetDCMotorRotate(uint8_t num, int16_t maxSpeed, uint16_t degree)
Definition MMLower.cpp:402
RESULT SetIMU_Calib_data(float *bufdata)
Definition MMLower.cpp:678
RESULT GetIMUGyro(double &x, double &y, double &z)
Definition MMLower.cpp:1144
RESULT SetServoAngle(uint8_t num, uint16_t angle)
Definition MMLower.cpp:532
double imuAccX
Definition MMLower.h:441
RESULT EchoTest(void)
Definition MMLower.cpp:1236
RESULT GetFWVersion(String &version)
Definition MMLower.cpp:1262
RESULT GetFWBuildDay(String &date)
Definition MMLower.cpp:1284
void loop(void)
Definition MMLower.cpp:2175
IMU_ECHO_MODE
Definition MMLower.h:192
RESULT Init(uint32_t timeout_ms=1000)
Definition MMLower.cpp:15
RESULT GetFWDescriptor(String &descriptor)
Definition MMLower.cpp:1312
RESULT GetEncoderCounter(uint8_t num, int32_t &enCounter)
Definition MMLower.cpp:1074
RESULT SetDCMotorSpeed(uint8_t num, int16_t speed)
Definition MMLower.cpp:369
RESULT GetPowerInfo(float &curVolt, float &curVoltPerc)
Definition MMLower.cpp:1190
RESULT GetButtonState(uint8_t num, bool &btnState)
Definition MMLower.cpp:958
Drive_RESULT Set_Drive_Reset_Count(uint8_t num, bool reset)
Definition MMLower.cpp:2073
double imuGyroY
Definition MMLower.h:440
RESULT SetEncoderResetCounter(uint8_t num)
Definition MMLower.cpp:653
RESULT GetIMUAcc(double &x, double &y, double &z)
Definition MMLower.cpp:1167
RESULT SetIMUEchoMode(IMU_ECHO_MODE mode, uint16_t echoIntervalMs)
Definition MMLower.cpp:225
double imuAccZ
Definition MMLower.h:441
Drive_RESULT Get_Drive_Degrees(uint8_t num, int32_t &Degs)
Definition MMLower.cpp:2139
RESULT GetModelIndex(uint8_t &index)
Definition MMLower.cpp:1340
RESULT Get_IMU_nancalib_acc(float *accdata)
Definition MMLower.cpp:1029
RESULT SetAllDCMotorSpeed(Motors_Param_t param)
Definition MMLower.cpp:431
void onBtnChg(BtnChgCallback callback)
Definition MMLower.cpp:2180
RESULT SetServoDir(uint8_t num, DIR dir)
Definition MMLower.cpp:96
Drive_RESULT Set_Drive_MoveGyro_Func(int16_t power, int16_t Target_dri, uint8_t num)
Definition MMLower.cpp:1842
RESULT SetMoveDistance(MOVE_TYPE type, MOVE_ACTION action, uint16_t speed, uint16_t enCounter)
Definition MMLower.cpp:610
RESULT GetALLEncoderSpeed(int32_t *enSpeed)
Definition MMLower.cpp:1004
RESULT Set_Drive_MoveTurn_PID(float Kp, float Ki, float Kd, uint8_t num)
Definition MMLower.cpp:1510
Drive_RESULT
Definition MMLower.h:306
Drive_RESULT Set_Drive_MoveSync_Time(int16_t power_left, int16_t power_right, uint32_t Time_mS, bool brake, bool async, uint8_t num)
Definition MMLower.cpp:1802
Drive_RESULT Set_Drive_Move_Func(int16_t power_left, int16_t power_right, uint8_t num)
Definition MMLower.cpp:1608
RESULT SetAllDCMotorPower(Motors_Param_t param)
Definition MMLower.cpp:481
@ ERROR_SERVO_MAX_PULSE
Definition MMLower.h:265
@ ERROR_SERVO_MIN_PULSE
Definition MMLower.h:264
@ ERROR_POWER_VOLT_RANGE
Definition MMLower.h:302
@ ERROR_SOFTSERIAL_BEGIN
Definition MMLower.h:258
@ ERROR_SERVO_MAX_ANGLE
Definition MMLower.h:269
@ ERROR_SERVO_MIN_ANGLE
Definition MMLower.h:268
Drive_RESULT Get_Drive_EncoderCounter(uint8_t num, int32_t &enCounter)
Definition MMLower.cpp:2109
Drive_RESULT Set_Drive2Motor_PARAM(uint8_t m1_num, uint8_t m2_num, DIR m1_dir, DIR m2_dir, uint8_t num)
Definition MMLower.cpp:1421
double imuGyroZ
Definition MMLower.h:440
RESULT GetAllEncoderCounter(int32_t *enCounter)
Definition MMLower.cpp:1096
double imuAccY
Definition MMLower.h:441
IMU_ACC_FSR
Definition MMLower.h:200
RESULT SetPIDParam(uint8_t num, uint8_t pidNum, float kp, float ki, float kd)
Definition MMLower.cpp:709
RESULT SetIMUToZero(void)
Definition MMLower.cpp:934
MMLower(uint8_t rx, uint8_t tx, uint32_t baudrate)
Definition MMLower.cpp:9
RESULT SetALLDC_Type_Brake(uint8_t *type)
Definition MMLower.cpp:786
RESULT SetDCMotorSpeedRange(uint8_t num, uint16_t min, uint16_t max)
Definition MMLower.cpp:122
RESULT GetButtonsState(bool *btnsState)
Definition MMLower.cpp:981
RESULT SetDCBrake(uint8_t num)
Definition MMLower.cpp:735
RESULT SetIMUInit(IMU_ACC_FSR accFSR, IMU_GYRO_FSR gyroFSR, IMU_ODR odr, IMU_FIFO fifo)
Definition MMLower.cpp:260
void(* BtnChgCallback)(uint8_t num, BTN_STATE newState)
Definition MMLower.h:347
Drive_RESULT Set_Drive_MoveGyro_Degs(int16_t power, int16_t Target_dri, uint16_t Degree_c, bool brake, bool async, uint8_t num)
Definition MMLower.cpp:1879
RESULT SetDC_Type_Brake(uint8_t num, uint8_t type)
Definition MMLower.cpp:816
Drive_RESULT Set_Drive_TurnGyro(int16_t power, int16_t Target_dri, uint8_t mode, bool brake, bool async, uint8_t num)
Definition MMLower.cpp:1959
Drive_RESULT Set_Drive_Encode_PPR(uint8_t num, uint16_t *ppr)
Definition MMLower.cpp:1537
RESULT SetEncode_PPR_MaxRPM(uint8_t num, uint16_t ppr, uint16_t Maxspeed)
Definition MMLower.cpp:843
RESULT SetDCMotorDir(uint8_t num, DIR dir)
Definition MMLower.cpp:45
RESULT GetEncoderDegrees(uint8_t num, int32_t &enDeges)
Definition MMLower.cpp:1052
RESULT SetPowerParam(float fullVolt, float cutOffVolt, float alarmVolt)
Definition MMLower.cpp:303
uint8_t modelIndex
Definition MMLower.h:344