12 commSerial =
new SoftwareSerial(rx, tx);
19 commSerial->begin(_baudrate, SERIAL_8N1);
49 uint8_t data[2] = {(1 << --num), (uint8_t)dir};
57 if (!CommReadData(b, 1)) {
74 uint8_t data[2] = {(1 << --num), (uint8_t)dir};
82 if (!CommReadData(b, 1)) {
100 uint8_t data[2] = {(1 << --num), (uint8_t)dir};
108 if (!CommReadData(b, 1)) {
127 data[0] = (1 << --num);
137 if (!CommReadData(b, 1)) {
156 data[0] = (1 << --num);
166 if (!CommReadData(b, 1)) {
193 data[0] = (1 << --num);
203 if (!CommReadData(b, 1)) {
230 data[0] = (uint8_t)mode;
239 if (!CommReadData(b, 1)) {
266 data[0] = (uint8_t)accFSR;
267 data[1] = (uint8_t)gyroFSR;
268 data[2] = (uint8_t)odr;
269 data[3] = (uint8_t)fifo;
277 if (!CommReadData(b, 1)) {
308 data[0] = (uint8_t)(fullVolt * 10.0f);
309 data[1] = (uint8_t)(cutOffVolt * 10.0f);
310 data[2] = (uint8_t)(alarmVolt * 10.0f);
318 if (!CommReadData(b, 1, 5)) {
341 data[0] = (1 << --num);
351 if (!CommReadData(b, 1)) {
374 data[0] = (1 << --num);
384 if (!CommReadData(b, 1)) {
407 data[0] = (1 << --num);
417 if (!CommReadData(b, 1)) {
436 data[0] = (uint8_t)param.
m1_dir;
437 data[0] |= ((uint8_t)param.
m2_dir) << 1;
438 data[0] |= ((uint8_t)param.
m3_dir) << 2;
439 data[0] |= ((uint8_t)param.
m4_dir) << 3;
452 if (!CommReadData(b, 1)) {
486 data[0] = (uint8_t)param.
m1_dir;
487 data[0] |= ((uint8_t)param.
m2_dir) << 1;
488 data[0] |= ((uint8_t)param.
m3_dir) << 2;
489 data[0] |= ((uint8_t)param.
m4_dir) << 3;
502 if (!CommReadData(b, 1)) {
537 data[0] = (1 << --num);
546 if (!CommReadData(b, 1)) {
565 uint16_t angle1, uint16_t angle2, uint16_t angle3, uint16_t angle4)
581 if (!CommReadData(b, 1)) {
616 data[0] = (uint8_t)type;
617 data[1] = (uint8_t)action;
627 if (!CommReadData(b, 1)) {
657 uint8_t data[1] = {(1 << --num)};
665 if (!CommReadData(b, 1)) {
697 if (!CommReadData(b, 1)) {
711 uint8_t data[8] = {(1 << --num), pidNum};
723 if (!CommReadData(b, 1)) {
739 uint8_t data[1] = {(1 << --num)};
747 if (!CommReadData(b, 1)) {
773 if (!CommReadData(b, 1)) {
803 if (!CommReadData(b, 1)) {
830 if (!CommReadData(b, 1)) {
860 if (!CommReadData(b, 1)) {
892 if (!CommReadData(b, 1)) {
910 data[0] = brightness;
911 data[1] = (uint8_t)(colorRGB >> 16);
912 data[2] = (uint8_t)(colorRGB >> 8);
913 data[3] = (uint8_t)(colorRGB);
921 if (!CommReadData(b, 1, 10)) {
945 if (!CommReadData(b, 1, 10)) {
962 uint8_t data[1] = {--num};
970 if (!CommReadData(b, 1)) {
975 btnState = (bool)b[0];
992 if (!CommReadData(b, 2)) {
997 btnsState[0] = (bool)(flag);
998 btnsState[1] = (bool)(flag >> 1);
1015 if (!CommReadData(b, 17)) {
1040 if (!CommReadData(b, 13)) {
1056 uint8_t data[1] = {--num};
1064 if (!CommReadData(b, 4)) {
1078 uint8_t data[1] = {--num};
1086 if (!CommReadData(b, 4)) {
1107 if (!CommReadData(b, 16)) {
1131 if (!CommReadData(b, 6)) {
1155 if (!CommReadData(b, 6)) {
1178 if (!CommReadData(b, 6)) {
1201 if (!CommReadData(b, 3)) {
1207 curVolt = (float)voltRaw / 1000.0f;
1208 curVoltPerc = (float)b[2];
1218 uint8_t data[1] = {--num};
1226 if (!CommReadData(b, 1)) {
1241 uint8_t data[1] = {0x55};
1249 if (!CommReadData(b, 1)) {
1254 if (b[0] == data[0]) {
1273 if (!CommReadData(b, 1)) {
1278 version = String(b[0] / 10.0f);
1295 if (!CommReadData(b, 4)) {
1301 uint8_t month = b[2];
1305 sprintf(str,
"%04d-%02d-%02d", year, month, day);
1323 if (!CommReadData(b, 1)) {
1329 uint8_t str[len + 1];
1330 if (!CommReadData(str, len)) {
1335 descriptor = String((
char*)str);
1351 if (!CommReadData(b, 1)) {
1372 if (!CommReadData(b, 6)) {
1377 uint8_t month = b[2];
1381 sprintf(str,
"%04d-%02d-%02d", year, month, day);
1402 if (!CommReadData(b, 1)) {
1407 if ((b[0] & 0x01) == 0x00) {
1425 uint8_t data[5] = {m1_num, m2_num, (uint8_t)m1_dir, (uint8_t)m2_dir, (num - 1)};
1434 if (!CommReadData(b, 1)) {
1442 }
else if(b[0] == 0x08){
1445 }
else if(b[0] == 0x09){
1470 if (!CommReadData(b, 1)) {
1497 if (!CommReadData(b, 1)) {
1525 if (!CommReadData(b, 1)) {
1554 if (!CommReadData(b, 1)) {
1562 }
else if(b[0] == 0x07){
1583 Serial.println(F(
"ERROR_WAIT_TIMEOUT"));
1588 if (!CommReadData(b, 1)) {
1596 }
else if(b[0] == 0x07){
1625 if (!CommReadData(b, 1)) {
1633 }
else if (b[0] == 0x02) {
1636 }
else if(b[0] == 0x07){
1665 if (!CommReadData(b, 1)) {
1673 }
else if (b[0] == 0x02) {
1676 }
else if(b[0] == 0x07){
1705 if (!CommReadData(b, 1)) {
1713 }
else if (b[0] == 0x02) {
1716 }
else if(b[0] == 0x07){
1742 if (!CommReadData(b, 1)) {
1750 }
else if (b[0] == 0x02) {
1753 }
else if(b[0] == 0x07){
1782 if (!CommReadData(b, 1)) {
1790 }
else if (b[0] == 0x02) {
1793 }
else if(b[0] == 0x07){
1822 if (!CommReadData(b, 1)) {
1830 }
else if (b[0] == 0x02) {
1833 }
else if(b[0] == 0x07){
1859 if (!CommReadData(b, 1)) {
1867 }
else if (b[0] == 0x02) {
1870 }
else if(b[0] == 0x07){
1899 if (!CommReadData(b, 1)) {
1907 }
else if (b[0] == 0x02) {
1910 }
else if(b[0] == 0x07){
1939 if (!CommReadData(b, 1)) {
1947 }
else if (b[0] == 0x02) {
1950 }
else if(b[0] == 0x07){
1979 if (!CommReadData(b, 1)) {
1987 }
else if (b[0] == 0x02) {
1990 }
else if(b[0] == 0x07){
2019 if (!CommReadData(b, 1)) {
2026 }
else if(b[0] == 0x02){
2028 }
else if(b[0] == 0x07){
2053 if (!CommReadData(b, 1)) {
2061 }
else if (b[0] == 0x02) {
2064 }
else if(b[0] == 0x07){
2079 data[0] = (uint8_t)reset;
2089 if (!CommReadData(b, 1)) {
2097 }
else if (b[0] == 0x02) {
2100 }
else if(b[0] == 0x07){
2123 if (!CommReadData(b, 4)) {
2153 if (!CommReadData(b, 4)) {
2182 callbackFunc = callback;
2185void MMLower::CommSendData(COMM_CMD cmd, uint8_t* data, uint16_t size)
2187 uint8_t arr[3 + size];
2192 *ptr++ = (uint8_t)cmd;
2194 for (uint16_t i = 0; i < size; i++) {
2197 commSerial->write(arr, 3 + size);
2198 commSerial->flush();
2201void MMLower::CommSendData(COMM_CMD cmd, uint8_t data)
2203 uint8_t _data[1] = {data};
2204 CommSendData((
COMM_CMD)cmd, _data, 1);
2207bool MMLower::CommReadData(uint8_t* data, uint16_t size, uint32_t timeout_ms)
2209 uint32_t timeout = millis() + timeout_ms;
2210 while (millis() <= timeout) {
2211 if (commSerial->available() >= size) {
2212 for (uint16_t i = 0; i < size; i++) {
2213 data[i] = commSerial->read();
2220 while (commSerial->available() > 0) {
2226bool MMLower::WaitData(COMM_CMD cmd, uint32_t timeout_ms)
2230 uint32_t timeout = millis() + timeout_ms;
2231 while (millis() <= timeout) {
2232 if (commSerial->available() <= 0) {
2238 uint8_t b = commSerial->read();
2246 uint8_t b = commSerial->read();
2255 uint8_t b = commSerial->read();
2256 if (b == (uint8_t)cmd) {
2280void MMLower::HandleCommand(uint8_t cmd)
2286 if (CommReadData(b, 2)) {
2287 if (callbackFunc == NULL)
break;
2289 callbackFunc(b[0] + 1, (
BTN_STATE)b[1]);
2296 if (CommReadData(b, 8)) {
2305 if (CommReadData(b, 6)) {}
2310 if (CommReadData(b, 6)) {
2319 if (CommReadData(b, 6)) {
MiniR4 low level functions.
Handling the Lower MCU (STM32) communication.
#define MR4_DEBUG_PRINT_HEADER(...)
#define MatrixR4_ENCODER_NUM
#define MR4_DEBUG_PRINTLN(...)
#define MatrixR4_BUTTON_NUM
#define MR4_DEBUG_PRINT_TAIL(...)
#define MatrixR4_COMM_LEAD
#define MR4_DEBUG_PRINT(...)
static int16_t ToInt16(uint8_t *value, int startIdx)
static void GetBytes(uint8_t *buff, uint32_t value)
static int32_t ToInt32(uint8_t *value, int startIdx)
static float Tofloat(uint8_t *value, int startIdx)
static uint16_t ToUInt16(uint8_t *value, int startIdx)
static void FloatGetBytes(uint8_t *buff, float value)
Handling the Lower MCU (STM32) communication.
RESULT SetAllServoAngle(uint16_t angle1, uint16_t angle2, uint16_t angle3, uint16_t angle4)
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)
RESULT Set_Drive_MoveGyro_PID(float Kp, float Ki, float Kd, uint8_t num)
Drive_RESULT Set_Drive_Motor_Type(uint8_t num, uint8_t type)
RESULT GetAllInfo(AllInfo_t &info)
RESULT SetALLDCBrake(void)
RESULT GetRotateState(uint8_t num, bool &isEnd)
RESULT SetEncoderDir(uint8_t num, DIR dir)
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)
RESULT SetStateLED(uint8_t brightness, uint32_t colorRGB)
RESULT SetServoAngleRange(uint8_t num, uint16_t min, uint16_t max)
RESULT SetServoPulseRange(uint8_t num, uint16_t min, uint16_t max)
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)
Drive_RESULT Set_Drive_MoveSync_Func(int16_t power_left, int16_t power_right, uint8_t num)
RESULT GetIMUEuler(double &roll, double &pitch, double &yaw)
Drive_RESULT Set_Drive_MoveGyro_Time(int16_t power, int16_t Target_dri, uint32_t Time_mS, bool brake, bool async, uint8_t num)
RESULT Set_Drive_MoveSync_PID(float Kp, float Ki, float Kd, uint8_t num)
RESULT SetDCMotorPower(uint8_t num, int16_t power)
@ SET_ENCODER_RESET_COUNTER
@ SET_DC_TWO_MOTOR_Reset_count
@ SET_DC_TWO_TurnGyro_PID
@ AUTO_SEND_ENCODER_COUNTER
@ GET_ALL_ENCODER_COUNTER
@ SET_DC_TWO_MoveSync_PID
@ SET_ENCODER_PPR_MAXSPEED
@ SET_DC_MOTOR_SPEED_RANGE
@ SET_DC_TWO_MoveGyro_PID
RESULT SetALL_Encode_PPR(uint16_t *ppr)
Drive_RESULT Get_Drive_isTaskDone(uint8_t num, bool *isEnd)
int32_t enCounter[MatrixR4_ENCODER_NUM]
Drive_RESULT Set_Drive_Brake(bool brake, uint8_t num)
RESULT SetDCMotorRotate(uint8_t num, int16_t maxSpeed, uint16_t degree)
RESULT SetIMU_Calib_data(float *bufdata)
RESULT GetIMUGyro(double &x, double &y, double &z)
RESULT SetServoAngle(uint8_t num, uint16_t angle)
RESULT GetFWVersion(String &version)
RESULT GetFWBuildDay(String &date)
RESULT Init(uint32_t timeout_ms=1000)
RESULT GetFWDescriptor(String &descriptor)
RESULT GetEncoderCounter(uint8_t num, int32_t &enCounter)
RESULT SetDCMotorSpeed(uint8_t num, int16_t speed)
RESULT GetPowerInfo(float &curVolt, float &curVoltPerc)
RESULT GetButtonState(uint8_t num, bool &btnState)
Drive_RESULT Set_Drive_Reset_Count(uint8_t num, bool reset)
RESULT SetEncoderResetCounter(uint8_t num)
RESULT GetIMUAcc(double &x, double &y, double &z)
RESULT SetIMUEchoMode(IMU_ECHO_MODE mode, uint16_t echoIntervalMs)
Drive_RESULT Get_Drive_Degrees(uint8_t num, int32_t &Degs)
RESULT GetModelIndex(uint8_t &index)
RESULT Get_IMU_nancalib_acc(float *accdata)
RESULT SetAllDCMotorSpeed(Motors_Param_t param)
void onBtnChg(BtnChgCallback callback)
RESULT SetServoDir(uint8_t num, DIR dir)
Drive_RESULT Set_Drive_MoveGyro_Func(int16_t power, int16_t Target_dri, uint8_t num)
RESULT SetMoveDistance(MOVE_TYPE type, MOVE_ACTION action, uint16_t speed, uint16_t enCounter)
RESULT GetALLEncoderSpeed(int32_t *enSpeed)
RESULT Set_Drive_MoveTurn_PID(float Kp, float Ki, float Kd, uint8_t num)
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)
Drive_RESULT Set_Drive_Move_Func(int16_t power_left, int16_t power_right, uint8_t num)
RESULT SetAllDCMotorPower(Motors_Param_t param)
Drive_RESULT Get_Drive_EncoderCounter(uint8_t num, int32_t &enCounter)
Drive_RESULT Set_Drive2Motor_PARAM(uint8_t m1_num, uint8_t m2_num, DIR m1_dir, DIR m2_dir, uint8_t num)
RESULT GetAllEncoderCounter(int32_t *enCounter)
RESULT SetPIDParam(uint8_t num, uint8_t pidNum, float kp, float ki, float kd)
RESULT SetIMUToZero(void)
MMLower(uint8_t rx, uint8_t tx, uint32_t baudrate)
RESULT SetALLDC_Type_Brake(uint8_t *type)
RESULT SetDCMotorSpeedRange(uint8_t num, uint16_t min, uint16_t max)
RESULT GetButtonsState(bool *btnsState)
RESULT SetDCBrake(uint8_t num)
RESULT SetIMUInit(IMU_ACC_FSR accFSR, IMU_GYRO_FSR gyroFSR, IMU_ODR odr, IMU_FIFO fifo)
void(* BtnChgCallback)(uint8_t num, BTN_STATE newState)
Drive_RESULT Set_Drive_MoveGyro_Degs(int16_t power, int16_t Target_dri, uint16_t Degree_c, bool brake, bool async, uint8_t num)
RESULT SetDC_Type_Brake(uint8_t num, uint8_t type)
Drive_RESULT Set_Drive_TurnGyro(int16_t power, int16_t Target_dri, uint8_t mode, bool brake, bool async, uint8_t num)
Drive_RESULT Set_Drive_Encode_PPR(uint8_t num, uint16_t *ppr)
RESULT SetEncode_PPR_MaxRPM(uint8_t num, uint16_t ppr, uint16_t Maxspeed)
RESULT SetDCMotorDir(uint8_t num, DIR dir)
RESULT GetEncoderDegrees(uint8_t num, int32_t &enDeges)
RESULT SetPowerParam(float fullVolt, float cutOffVolt, float alarmVolt)