|
| 1 | +/* |
| 2 | + Description: UWB Unit test program. When using, two M5 devices are burned to the program, |
| 3 | + and the buttons A and B are used to configure them to base station mode and Tag mode respectively. |
| 4 | + For more details, please refer to the code comments below. |
| 5 | + Before use, it is recommended to configure the base station first, and then configure the Tag |
| 6 | +*/ |
| 7 | + |
| 8 | +#include <M5Stack.h> |
| 9 | +String DATA = " "; //Used to store distance data |
| 10 | +int UWB_MODE = 2; //Used to set UWB mode |
| 11 | + |
| 12 | +int UWB_T_UI_NUMBER_2 = 0; // flag bit 标志位 |
| 13 | +int UWB_T_UI_NUMBER_1 = 0; |
| 14 | +int UWB_T_NUMBER = 0; |
| 15 | +int UWB_B_NUMBER = 0; |
| 16 | + |
| 17 | + |
| 18 | +hw_timer_t *timer = NULL; |
| 19 | +int timer_flag = 0; |
| 20 | +int base_flag = 0; |
| 21 | +uint32_t timer_data = 0; |
| 22 | +static void IRAM_ATTR Timer0_CallBack(void); |
| 23 | + |
| 24 | +//Data display |
| 25 | +void UWB_display() |
| 26 | +{ |
| 27 | + switch (UWB_MODE) |
| 28 | + { |
| 29 | + case 0: |
| 30 | + |
| 31 | + if (UWB_T_NUMBER > 0 && UWB_T_NUMBER < 5) |
| 32 | + { |
| 33 | + int c = UWB_T_NUMBER; |
| 34 | + int b = 4 - UWB_T_NUMBER; |
| 35 | + while (c > 0) |
| 36 | + { |
| 37 | + c--; |
| 38 | + M5.Lcd.drawString(DATA.substring(2 + c * 11, 3 + c * 11), 37, 50 + c * 40, 4 );//Tag the serial number 标签序号 |
| 39 | + M5.Lcd.drawString(DATA.substring(4 + c * 11, 8 + c * 11), 210, 50 + c * 40, 4); //Distance 距离 |
| 40 | + } |
| 41 | + while (b > 0) |
| 42 | + { |
| 43 | + b--; |
| 44 | + M5.Lcd.fillRect(0, 170 - b * 40, 340, 30, BLACK); |
| 45 | + } |
| 46 | + } |
| 47 | + break; |
| 48 | + case 1: |
| 49 | + if (UWB_B_NUMBER == 1) |
| 50 | + { |
| 51 | + M5.Lcd.drawString(DATA, 60, 90, 4);//Prompt information (Base station 0 only) 提示信息 (仅限基站0) |
| 52 | + } |
| 53 | + break; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +//UI display |
| 58 | +void UWB_ui_display() { |
| 59 | + |
| 60 | + M5.Lcd.drawString("UWB Example", 90, 0, 4); //UI |
| 61 | + M5.Lcd.drawString("Tag", 50, 210, 4); |
| 62 | + M5.Lcd.drawString("Base", 130, 210, 4); |
| 63 | + M5.Lcd.drawString("Reset", 220, 210, 4); |
| 64 | + |
| 65 | + switch (UWB_MODE) |
| 66 | + { |
| 67 | + case 0: |
| 68 | + if (UWB_T_NUMBER > 0 && UWB_T_NUMBER < 5) //Tag mode UI display 标签模式UI显示 |
| 69 | + { |
| 70 | + int c = UWB_T_NUMBER; |
| 71 | + int b = 4 - UWB_T_NUMBER; |
| 72 | + while (c > 0 ) |
| 73 | + { |
| 74 | + c--; |
| 75 | + M5.Lcd.drawString("B", 20, 50 + c * 40, 4 ); |
| 76 | + M5.Lcd.drawString("Distance:", 80, 50 + c * 40, 4 ); |
| 77 | + M5.Lcd.drawString("M", 280, 50 + c * 40, 4); |
| 78 | + } |
| 79 | + |
| 80 | + while (b > 0) |
| 81 | + { |
| 82 | + b--; |
| 83 | + M5.Lcd.fillRect(0, 170 - b * 40, 340, 30, BLACK); |
| 84 | + } |
| 85 | + } |
| 86 | + break; |
| 87 | + case 1: //Base station mode UI display 基站模式UI显示 |
| 88 | + M5.Lcd.drawString("Base station", 80, 50, 4 ); |
| 89 | + M5.Lcd.drawString(String(UWB_B_NUMBER), 240, 50, 4 );//UWB_B_NUMBER |
| 90 | + if (UWB_B_NUMBER == 0) |
| 91 | + { |
| 92 | + M5.Lcd.drawString("loading......", 100, 90, 4 ); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + M5.Lcd.fillRect(0, 90, 340, 30, BLACK); |
| 97 | + } |
| 98 | + |
| 99 | + break; |
| 100 | + } |
| 101 | +} |
| 102 | +// Display and data clear |
| 103 | +void UWB_clear() { //Clear the display and DATA 清除显示和DATA |
| 104 | + if (Serial2.available()) |
| 105 | + { |
| 106 | + delay(3); |
| 107 | + DATA = Serial2.readString(); |
| 108 | + } |
| 109 | + DATA = ""; |
| 110 | + timer_flag = 0; |
| 111 | + timer_data = 0; |
| 112 | + M5.Lcd.fillRect(0, 50, 340, 150, BLACK); |
| 113 | +} |
| 114 | + |
| 115 | +int UWB_readString() //Read UART data 读取串口数据 |
| 116 | +{ |
| 117 | + switch (UWB_MODE) |
| 118 | + { |
| 119 | + case 0: |
| 120 | + if (Serial2.available()) |
| 121 | + { |
| 122 | + delay(20); |
| 123 | + UWB_T_NUMBER = (Serial2.available() / 11); //Count the number of Base stations 计算基站数目 |
| 124 | + delay(20); |
| 125 | + if (UWB_T_NUMBER != UWB_T_UI_NUMBER_1 || UWB_T_UI_NUMBER_2 == 0) //Changing the UI display 更改UI显示 |
| 126 | + { |
| 127 | + UWB_ui_display(); |
| 128 | + UWB_T_UI_NUMBER_1 = UWB_T_NUMBER; |
| 129 | + UWB_T_UI_NUMBER_2 = 1; |
| 130 | + } |
| 131 | + DATA = Serial2.readString(); |
| 132 | + delay(2); |
| 133 | + timer_flag = 0; |
| 134 | + timer_data = 1; |
| 135 | + break; |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + timer_flag = 1; |
| 140 | + } |
| 141 | + if (timer_data == 0 || timer_data > 8) //Count the number of Base stations 提示与基站0断连(测试) |
| 142 | + { |
| 143 | + if(timer_data==9){M5.Lcd.fillRect(210, 50, 50, 30, BLACK);} |
| 144 | + DATA = " 0 2F "; |
| 145 | + timer_flag = 0; |
| 146 | + } |
| 147 | + break; |
| 148 | + case 1: |
| 149 | + if (timer_data == 0 || timer_data > 70) //Indicates successful or lost connection with Tag 提示与标签连接成功或丢失断连 |
| 150 | + { |
| 151 | + if (Serial2.available()) |
| 152 | + { |
| 153 | + delay(2); |
| 154 | + DATA = Serial2.readString(); |
| 155 | + DATA = "set up successfully!"; |
| 156 | + timer_data = 1; |
| 157 | + timer_flag = 1; |
| 158 | + break; |
| 159 | + } |
| 160 | + else if (timer_data > 0 && Serial2.available() == 0 ) |
| 161 | + { |
| 162 | + DATA = "Can't find the tag!!!"; |
| 163 | + timer_flag = 0; |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + break; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +int UWB_setupmode() //AT command |
| 173 | +{ |
| 174 | + switch (UWB_MODE) { |
| 175 | + case 0: |
| 176 | + for (int b = 0; b < 2; b++) |
| 177 | + { //Repeat twice to stabilize the connection |
| 178 | + delay(50); |
| 179 | + Serial2.write("AT+anchor_tag=0\r\n"); //Set up the Tag 设置标签 |
| 180 | + delay(50); |
| 181 | + Serial2.write("AT+interval=5\r\n"); //Set the calculation precision, the larger the response is, the slower it will be |
| 182 | + delay(50); //设置计算精度,越大响应越慢 |
| 183 | + Serial2.write("AT+switchdis=1\r\n"); //Began to distance 开始测距 |
| 184 | + delay(50); |
| 185 | + if (b == 0) { |
| 186 | + Serial2.write("AT+RST\r\n"); //RESET 复位 |
| 187 | + } |
| 188 | + } |
| 189 | + UWB_clear(); |
| 190 | + break; |
| 191 | + case 1: |
| 192 | + for (int b = 0; b < 2; b++) |
| 193 | + { |
| 194 | + delay(50); |
| 195 | + Serial2.write("AT+anchor_tag=1,"); //Set the base station 设置基站 |
| 196 | + Serial2.print(UWB_B_NUMBER); //UWB_B_NUMBER is base station ID0~ID3 |
| 197 | + Serial2.write("\r\n"); |
| 198 | + delay(1); |
| 199 | + delay(50); |
| 200 | + if (b == 0) { |
| 201 | + Serial2.write("AT+RST\r\n"); |
| 202 | + } |
| 203 | + } |
| 204 | + UWB_clear(); |
| 205 | + break; |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | + |
| 210 | +void UWB_Keyscan() { |
| 211 | + if (M5.BtnA.wasReleased()) |
| 212 | + { |
| 213 | + UWB_MODE = 0; |
| 214 | + UWB_setupmode(); |
| 215 | + UWB_clear(); |
| 216 | + UWB_ui_display(); |
| 217 | + UWB_T_UI_NUMBER_2 = 0; |
| 218 | + } |
| 219 | + if (M5.BtnB.wasReleased()) |
| 220 | + { |
| 221 | + UWB_MODE = 1; |
| 222 | + if (UWB_B_NUMBER == 4) |
| 223 | + { |
| 224 | + UWB_B_NUMBER = 0; |
| 225 | + } |
| 226 | + UWB_setupmode(); |
| 227 | + UWB_clear(); |
| 228 | + UWB_ui_display(); |
| 229 | + UWB_B_NUMBER++; |
| 230 | + } |
| 231 | + if (M5.BtnC.wasReleased()) |
| 232 | + { |
| 233 | + Serial2.write("AT+RST\r\n"); |
| 234 | + UWB_setupmode(); |
| 235 | + UWB_clear(); |
| 236 | + UWB_ui_display(); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +void UWB_Timer() |
| 241 | +{ |
| 242 | + timer = timerBegin(0, 80, true); //Timer setting 定时器设置 |
| 243 | + timerAttachInterrupt(timer, Timer0_CallBack, true); |
| 244 | + timerAlarmWrite(timer, 1000000, true); |
| 245 | + timerAlarmEnable(timer); |
| 246 | +} |
| 247 | + |
| 248 | +static void IRAM_ATTR Timer0_CallBack(void) //Timer function 定时器函数 |
| 249 | +{ |
| 250 | + if (timer_flag == 1) |
| 251 | + { |
| 252 | + timer_data++; |
| 253 | + if (timer_data == 4294967280) |
| 254 | + { |
| 255 | + timer_data = 1; |
| 256 | + } |
| 257 | + } |
| 258 | + else |
| 259 | + { |
| 260 | + timer_data = 0; |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +void setup() |
| 265 | +{ |
| 266 | + M5.begin(); |
| 267 | + M5.Power.begin(); |
| 268 | + Serial2.begin(115200, SERIAL_8N1, 16, 17); |
| 269 | + delay(100); |
| 270 | + UWB_Timer(); |
| 271 | + |
| 272 | + UWB_ui_display(); |
| 273 | + |
| 274 | +} |
| 275 | + |
| 276 | +void loop() |
| 277 | +{ |
| 278 | + M5.update(); |
| 279 | + UWB_Keyscan(); |
| 280 | + UWB_readString(); |
| 281 | + UWB_display(); |
| 282 | + |
| 283 | +} |
0 commit comments