diff --git a/contest/arduino/include/canmvk210.h b/contest/arduino/include/canmvk210.h index 1702017..ff435ca 100644 --- a/contest/arduino/include/canmvk210.h +++ b/contest/arduino/include/canmvk210.h @@ -3,9 +3,18 @@ #include +#define CANMVK210_MSG_LEN 50 + class CanMVK210 { public: CanMVK210(HardwareSerial *serial, int baud); // 构造函数 + void messageRecord(void); // 读取串口数据 + + uint8_t receive[CANMVK210_MSG_LEN]; // 实时记录收到的信息 + uint8_t message[CANMVK210_MSG_LEN]; // 确认无误后用于解码的信息 + + HardwareSerial *getSerial(void); // 获取串口 + private: HardwareSerial *_serial; // 串口 int _baud; // 波特率 diff --git a/contest/arduino/include/jy62.h b/contest/arduino/include/jy62.h index 16e72ea..a474fdf 100644 --- a/contest/arduino/include/jy62.h +++ b/contest/arduino/include/jy62.h @@ -77,9 +77,10 @@ class JY62 { float getTemp(void); // 获取温度 void printData(uint8_t type, HardwareSerial &serial); // 打印数据 + HardwareSerial *getSerial(void); // 获取串口 + void decode(void); // 解码 -protected: uint8_t receive[JY62_MSG_LEN]; // 实时记录收到的信息 uint8_t message[JY62_MSG_LEN]; // 确认无误后用于解码的信息 diff --git a/contest/arduino/include/zigbee.h b/contest/arduino/include/zigbee.h index c612fbe..baed6e6 100644 --- a/contest/arduino/include/zigbee.h +++ b/contest/arduino/include/zigbee.h @@ -15,6 +15,8 @@ class Zigbee { uint8_t receive[ZIGBEE_MSG_LEN]; // 实时记录收到的信息 uint8_t message[ZIGBEE_MSG_LEN]; // 确认无误后用于解码的信息 + HardwareSerial *getSerial(void); // 获取串口 + private: HardwareSerial *_serial; // 串口 int _baud; // 波特率 diff --git a/contest/arduino/src/canmvk210.cpp b/contest/arduino/src/canmvk210.cpp index 695ed9d..48b27df 100644 --- a/contest/arduino/src/canmvk210.cpp +++ b/contest/arduino/src/canmvk210.cpp @@ -3,4 +3,33 @@ CanMVK210::CanMVK210(HardwareSerial *serial, int baud) { _serial = serial; _baud = baud; -} \ No newline at end of file +} + +void CanMVK210::messageRecord() { + uint8_t head = 0; + while (1) { + if (_serial->available()) { + head = _serial->read(); + if (head == 0x55 || head == 0x56) { + break; + } + } + } + receive[0] = head; + _serial->readBytes(receive + 1, 2); + if (receive[1] != 0xAA) { + return; + } + uint8_t len = receive[2]; + _serial->readBytes(receive + 3, len); + uint8_t sum = 0; + for (int i = 0; i < len + 3; i++) { + sum += receive[i]; + } + if (sum == receive[len + 3]) { + memcpy(message, receive, len + 4); + } +} + +// @brief 获取串口 +HardwareSerial *CanMVK210::getSerial(void) { return _serial; } \ No newline at end of file diff --git a/contest/arduino/src/jy62.cpp b/contest/arduino/src/jy62.cpp index 2fb1bcb..782a704 100644 --- a/contest/arduino/src/jy62.cpp +++ b/contest/arduino/src/jy62.cpp @@ -30,9 +30,7 @@ void JY62::messageRecord(void) { sum += receive[i]; } if (sum == receive[JY62_MSG_LEN - 1]) { - for (int i = 0; i < JY62_MSG_LEN; i++) { - message[i] = receive[i]; - } + memcpy(message, receive, JY62_MSG_LEN); decode(); } } @@ -189,3 +187,6 @@ void JY62::decodeAngl(void) { void JY62::decodeTemp(void) { _temp.temp = (float)((int16_t)(message[9] << 8 | message[8])) / 340.0 + 36.53; } + +// @brief 获取串口 +HardwareSerial *JY62::getSerial(void) { return _serial; } \ No newline at end of file diff --git a/contest/arduino/src/main.cpp b/contest/arduino/src/main.cpp index fab7caf..2157967 100644 --- a/contest/arduino/src/main.cpp +++ b/contest/arduino/src/main.cpp @@ -13,6 +13,7 @@ HardwareSerial SerialUART1(PB7, PB6); // 和电脑通信串口 HardwareSerial SerialUART2(PA3, PA2); // 和jy62通信串口 HardwareSerial SerialUART3(PB11, PB10); // 和zigbee通信串口 +CanMVK210 camera(&SerialUART1, 115200); JY62 imu(&SerialUART2, 115200); TB6612FNG motor(PB3, PC12, PD2, PB4, PC10, PC11, PB5); Zigbee zigbee(&SerialUART3, 115200); @@ -27,32 +28,42 @@ boolean setHome = false; boolean runStatus = false; -static void vIMUMessageRecordTask(void *pvParameters) { +static void vCameraMessageRecordTask(void *pvParameters) { UNUSED(pvParameters); while (1) { - if (SerialUART2.available()) { - imu.messageRecord(); + if (camera.getSerial()->available()) { + camera.messageRecord(); } } } -static void vIMUPrintDataTask(void *pvParameters) { +static void vIMUMessageRecordTask(void *pvParameters) { UNUSED(pvParameters); while (1) { - imu.printData(0, SerialUART1); - vTaskDelay(configTICK_RATE_HZ); + if (imu.getSerial()->available()) { + imu.messageRecord(); + } } } +// static void vIMUPrintDataTask(void *pvParameters) { +// UNUSED(pvParameters); +// while (1) { +// imu.printData(0, SerialUART1); +// vTaskDelay(configTICK_RATE_HZ); +// } +// } + static void vZigbeeMessageRecordTask(void *pvParameters) { UNUSED(pvParameters); while (1) { - zigbee.messageRecord(); - vTaskDelay(configTICK_RATE_HZ); + if (zigbee.getSerial()->available()) { + zigbee.messageRecord(); + } } } -static void vPlayerUpdateTask(void *pvParameters) { +static void vPlayerUpdatePlayerInfoTask(void *pvParameters) { UNUSED(pvParameters); while (1) { player.updatePlayerInfo(); @@ -60,14 +71,30 @@ static void vPlayerUpdateTask(void *pvParameters) { } } -static void vPlayerPrintInfoTask(void *pvParameters) { +// static void vPlayerPrintPlayerInfoTask(void *pvParameters) { +// UNUSED(pvParameters); +// while (1) { +// player.printPlayerInfo(SerialUART1); +// vTaskDelay(configTICK_RATE_HZ * 2); +// } +// } + +static void vPlayerUpdateMapInfoTask(void *pvParameters) { UNUSED(pvParameters); while (1) { - player.printPlayerInfo(SerialUART1); - vTaskDelay(configTICK_RATE_HZ * 2); + player.updateMapInfo(); + vTaskDelay(configTICK_RATE_HZ); } } +// static void vPlayerPrintMapInfoTask(void *pvParameters) { +// UNUSED(pvParameters); +// while (1) { +// player.printMapInfo(SerialUART1); +// vTaskDelay(configTICK_RATE_HZ * 2); +// } +// } + static void vPlayerChangeStatusTask(void *pvParameters) { UNUSED(pvParameters); while (1) { @@ -124,18 +151,25 @@ void setup() { imu.initAngle(); imu.calibrate(); motor.init(); + player.setCanMVK210(&camera); player.setJY62(&imu); player.setTB6612FNG(&motor); player.setZigbee(&zigbee); + xTaskCreate(vCameraMessageRecordTask, "CameraMessageRecordTask", 128, NULL, + tskIDLE_PRIORITY, NULL); xTaskCreate(vIMUMessageRecordTask, "IMUMessageRecordTask", 128, NULL, tskIDLE_PRIORITY, &xIMUMessageRecordTask); // xTaskCreate(vIMUPrintDataTask, "IMUPrintDataTask", 128, NULL, // tskIDLE_PRIORITY, &xIMUPrintDataTask); xTaskCreate(vZigbeeMessageRecordTask, "ZigbeeMessageRecordTask", 256, NULL, tskIDLE_PRIORITY, NULL); - xTaskCreate(vPlayerUpdateTask, "PlayerUpdateTask", 128, NULL, + xTaskCreate(vPlayerUpdatePlayerInfoTask, "PlayerUpdatePlayerInfoTask", 128, + NULL, tskIDLE_PRIORITY, NULL); + // xTaskCreate(vPlayerPrintPlayerInfoTask, "PlayerPrintPlayerInfoTask", 128, + // NULL, tskIDLE_PRIORITY, NULL); + xTaskCreate(vPlayerUpdateMapInfoTask, "PlayerUpdateMapInfoTask", 128, NULL, tskIDLE_PRIORITY, NULL); - // xTaskCreate(vPlayerPrintInfoTask, "PlayerPrintInfoTask", 128, NULL, + // xTaskCreate(vPlayerPrintMapInfoTask, "PlayerPrintMapInfoTask", 128, NULL, // tskIDLE_PRIORITY, NULL); xTaskCreate(vPlayerChangeStatusTask, "PlayerChangeStatusTask", 128, NULL, tskIDLE_PRIORITY, NULL); diff --git a/contest/arduino/src/player.cpp b/contest/arduino/src/player.cpp index 0a7e771..e621258 100644 --- a/contest/arduino/src/player.cpp +++ b/contest/arduino/src/player.cpp @@ -60,8 +60,26 @@ void Player::updatePlayerInfo() { } void Player::updateMapInfo() { - if (_canmvk210 != NULL) { - // TODO + if (_canmvk210 != NULL && + (_canmvk210->message[0] == 0x55 || _canmvk210->message[0] == 0x56) && + _canmvk210->message[1] == 0xAA) { + uint8_t type = _canmvk210->message[0]; + uint8_t len = _canmvk210->message[2]; + uint8_t *data = _canmvk210->message + 3; + if (type == 0x55) { + _mapInfo.goldMine.clear(); + for (int i = 0; i < len / 8; i++) { + _mapInfo.goldMine.push_back(Position(*((float_t *)(data + i * 8 + 0)), + *((float_t *)(data + i * 8 + 4)))); + } + } else if (type == 0x56) { + _mapInfo.diamondMine.clear(); + for (int i = 0; i < len / 8; i++) { + _mapInfo.diamondMine.push_back( + Position(*((float_t *)(data + i * 8 + 0)), + *((float_t *)(data + i * 8 + 4)))); + } + } } } diff --git a/contest/arduino/src/zigbee.cpp b/contest/arduino/src/zigbee.cpp index c2c2474..f0063eb 100644 --- a/contest/arduino/src/zigbee.cpp +++ b/contest/arduino/src/zigbee.cpp @@ -36,9 +36,7 @@ void Zigbee::messageRecord(void) { sum ^= receive[i]; } if (sum == receive[4]) { - for (int i = 0; i < len + 5; i++) { - message[i] = receive[i]; - } + memcpy(message, receive, len + 5); } } @@ -48,4 +46,7 @@ void Zigbee::messageRecord(void) { * @param msg 数据 * @param len 数据长度 */ -void Zigbee::send(uint8_t *msg, uint8_t len) { _serial->write(msg, len); } \ No newline at end of file +void Zigbee::send(uint8_t *msg, uint8_t len) { _serial->write(msg, len); } + +// @brief 获取串口 +HardwareSerial *Zigbee::getSerial(void) { return _serial; } \ No newline at end of file