Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
feat: ✨ add map
Browse files Browse the repository at this point in the history
  • Loading branch information
panxuc committed Dec 15, 2023
1 parent bd87338 commit 596c576
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 25 deletions.
9 changes: 9 additions & 0 deletions contest/arduino/include/canmvk210.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@

#include <Arduino.h>

#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; // 波特率
Expand Down
3 changes: 2 additions & 1 deletion contest/arduino/include/jy62.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]; // 确认无误后用于解码的信息

Expand Down
2 changes: 2 additions & 0 deletions contest/arduino/include/zigbee.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 波特率
Expand Down
31 changes: 30 additions & 1 deletion contest/arduino/src/canmvk210.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,33 @@
CanMVK210::CanMVK210(HardwareSerial *serial, int baud) {
_serial = serial;
_baud = baud;
}
}

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; }
7 changes: 4 additions & 3 deletions contest/arduino/src/jy62.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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; }
62 changes: 48 additions & 14 deletions contest/arduino/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -27,47 +28,73 @@ 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();
vTaskDelay(configTICK_RATE_HZ);
}
}

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) {
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 20 additions & 2 deletions contest/arduino/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions contest/arduino/src/zigbee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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); }
void Zigbee::send(uint8_t *msg, uint8_t len) { _serial->write(msg, len); }

// @brief 获取串口
HardwareSerial *Zigbee::getSerial(void) { return _serial; }

0 comments on commit 596c576

Please sign in to comment.