|
| 1 | +///////////////////////////////////////////////////////////////// |
| 2 | +/* |
| 3 | + ESP32 + UWB + IMU | Indoor Position & Rotation + Unity Visualization |
| 4 | + For More Information: https://youtu.be/fPuxcjHsfpc |
| 5 | + Created by Eric N. (ThatProject) |
| 6 | +*/ |
| 7 | +///////////////////////////////////////////////////////////////// |
| 8 | +/////////////////// |
| 9 | +//ESP Version 1.0.6 |
| 10 | +/////////////////// |
| 11 | +#include <Wire.h> |
| 12 | +#include <Adafruit_Sensor.h> |
| 13 | +#include <Adafruit_BNO055.h> |
| 14 | +#include <SPI.h> |
| 15 | +#include <DW1000Ranging.h> |
| 16 | +#include <WiFi.h> |
| 17 | +#include <WiFiUdp.h> |
| 18 | +#include "MyDisplay.h" |
| 19 | + |
| 20 | +#define I2C_SDA 17 |
| 21 | +#define I2C_SCL 16 |
| 22 | + |
| 23 | +const uint8_t MY_PIN_SCK = 18; |
| 24 | +const uint8_t MY_PIN_MOSI = 23; |
| 25 | +const uint8_t MY_PIN_MISO = 19; |
| 26 | +const uint8_t MY_PIN_SS = 15; |
| 27 | +const uint8_t MY_PIN_RST = 2; |
| 28 | +const uint8_t MY_PIN_IRQ = 22; |
| 29 | + |
| 30 | +const char *ssid = "ThatProject"; |
| 31 | +const char *password = "California"; |
| 32 | +const char *udpAddress = "192.168.0.2"; //My UDP Server IP |
| 33 | +const int udpPort = 8080; |
| 34 | +unsigned long udpTimer = 0; |
| 35 | +bool isWiFiConnected = false; |
| 36 | + |
| 37 | +WiFiUDP udp; |
| 38 | + |
| 39 | +TwoWire I2CBNO = TwoWire(0); |
| 40 | +Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x29, &I2CBNO); |
| 41 | + |
| 42 | +MyDisplay *display; |
| 43 | + |
| 44 | +static String uwbDevice[2]; |
| 45 | +static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; |
| 46 | +static const uint16_t timer_divider = 80; |
| 47 | +static const uint64_t timer_max_count = 1000; |
| 48 | +static hw_timer_t *timer = NULL; |
| 49 | +static SemaphoreHandle_t bin_sem = NULL; |
| 50 | +uint32_t cp0_regs[18]; |
| 51 | +void IRAM_ATTR onTimer() { |
| 52 | + uint32_t cp_state = xthal_get_cpenable(); |
| 53 | + if (cp_state) { |
| 54 | + xthal_save_cp0(cp0_regs); |
| 55 | + } else { |
| 56 | + xthal_set_cpenable(1); |
| 57 | + } |
| 58 | + |
| 59 | + portENTER_CRITICAL_ISR(&mux); |
| 60 | + DW1000Ranging.loop(); |
| 61 | + portEXIT_CRITICAL_ISR(&mux); |
| 62 | + |
| 63 | + BaseType_t task_woken = pdFALSE; |
| 64 | + xSemaphoreGiveFromISR(bin_sem, &task_woken); |
| 65 | + if (task_woken == pdTRUE) { |
| 66 | + portYIELD_FROM_ISR(); |
| 67 | + } |
| 68 | + |
| 69 | + if (cp_state) { |
| 70 | + xthal_restore_cp0(cp0_regs); |
| 71 | + } else { |
| 72 | + xthal_set_cpenable(0); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +void setup() { |
| 77 | + Serial.begin(115200); |
| 78 | + initDisplay(); |
| 79 | + |
| 80 | + bin_sem = xSemaphoreCreateBinary(); |
| 81 | + if (bin_sem == NULL) { |
| 82 | + Serial.println("Could not create semaphore"); |
| 83 | + ESP.restart(); |
| 84 | + } |
| 85 | + |
| 86 | + udpTimer = 0; |
| 87 | + |
| 88 | + xTaskCreatePinnedToCore(system_task, |
| 89 | + "Task System", |
| 90 | + 2048, |
| 91 | + NULL, |
| 92 | + 1, |
| 93 | + NULL, |
| 94 | + 1); |
| 95 | + |
| 96 | + connectToWiFi(ssid, password); |
| 97 | +} |
| 98 | + |
| 99 | +void setTimerInterrupt() { |
| 100 | + timer = timerBegin(0, timer_divider, true); |
| 101 | + timerAttachInterrupt(timer, &onTimer, true); |
| 102 | + timerAlarmWrite(timer, timer_max_count, true); |
| 103 | + timerAlarmEnable(timer); |
| 104 | +} |
| 105 | + |
| 106 | +void loop() {} |
| 107 | + |
| 108 | +void system_task(void *parameters) { |
| 109 | + |
| 110 | + I2CBNO.begin(I2C_SDA, I2C_SCL); |
| 111 | + |
| 112 | + if (!bno.begin()) { |
| 113 | + Serial.println("No BNO055 detected"); |
| 114 | + while (1) { |
| 115 | + vTaskDelay(10); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + vTaskDelay(100); |
| 120 | + |
| 121 | + uint8_t system, gyro, accel, mag = 0; |
| 122 | + display->imuLog("Calibration!", false); |
| 123 | + while (system != 3) { |
| 124 | + bno.getCalibration(&system, &gyro, &accel, &mag); |
| 125 | + vTaskDelay(100); |
| 126 | + } |
| 127 | + |
| 128 | + display->imuLog("All Set!", true); |
| 129 | + display->setIsAllReady(true); |
| 130 | + |
| 131 | + while (1) { |
| 132 | + if (xSemaphoreTake(bin_sem, portMAX_DELAY) == pdTRUE) { |
| 133 | + portENTER_CRITICAL(&mux); |
| 134 | + display->loop(); |
| 135 | + display->uwbLog(uwbDevice[0] + " m", 0); |
| 136 | + display->uwbLog(uwbDevice[1] + " m", 1); |
| 137 | + portEXIT_CRITICAL(&mux); |
| 138 | + if (millis() - udpTimer >= 100) { |
| 139 | + udpTimer = millis(); |
| 140 | + imu::Quaternion quat = bno.getQuat(); |
| 141 | + String strData = String(quat.w(), 4) + ", " + String(quat.x(), 4) + ", " + String(quat.y(), 4) + ", " + String(quat.z(), 4); |
| 142 | + sendData(strData); |
| 143 | + }else{ |
| 144 | + if(uwbDevice[0].length() > 1 && uwbDevice[1].length() >1){ |
| 145 | + String strData = uwbDevice[0]; |
| 146 | + strData += ", "; |
| 147 | + strData += uwbDevice[1]; |
| 148 | + sendData(strData); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + vTaskDelay(30); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +void sendData(String strData){ |
| 157 | + if (!isWiFiConnected) return; |
| 158 | + udp.beginPacket(udpAddress, udpPort); |
| 159 | + udp.write((uint8_t *)strData.c_str(), strlen(strData.c_str())); |
| 160 | + udp.endPacket(); |
| 161 | +} |
| 162 | + |
| 163 | +void initDisplay() { |
| 164 | + display = new MyDisplay(); |
| 165 | + display->initTFT(); |
| 166 | + display->systemLog("[System Up]", true); |
| 167 | +} |
| 168 | + |
| 169 | +String getQuaternion() { |
| 170 | + imu::Quaternion quat = bno.getQuat(); |
| 171 | + return String(quat.w(), 4) + ", " + String(quat.x(), 4) + ", " + String(quat.y(), 4) + ", " + String(quat.z(), 4); |
| 172 | +} |
| 173 | + |
| 174 | +void initUWB() { |
| 175 | + SPI.begin(MY_PIN_SCK, MY_PIN_MISO, MY_PIN_MOSI); |
| 176 | + |
| 177 | + DW1000Ranging.initCommunication(MY_PIN_RST, MY_PIN_SS, MY_PIN_IRQ); |
| 178 | + DW1000Ranging.attachNewRange(newRange); |
| 179 | + DW1000Ranging.attachNewDevice(newDevice); |
| 180 | + DW1000Ranging.attachInactiveDevice(inactiveDevice); |
| 181 | + |
| 182 | + DW1000.enableDebounceClock(); |
| 183 | + DW1000.enableLedBlinking(); |
| 184 | + DW1000.setGPIOMode(MSGP0, LED_MODE); |
| 185 | + DW1000.setGPIOMode(MSGP1, LED_MODE); |
| 186 | + DW1000.setGPIOMode(MSGP2, LED_MODE); |
| 187 | + DW1000.setGPIOMode(MSGP3, LED_MODE); |
| 188 | + |
| 189 | + DW1000Ranging.startAsTag("7D:00:22:EA:82:60:3B:9C", DW1000.MODE_LONGDATA_RANGE_LOWPOWER); |
| 190 | + setTimerInterrupt(); |
| 191 | +} |
| 192 | + |
| 193 | +void newRange() { |
| 194 | + float projectedRange = DW1000Ranging.getDistantDevice()->getRange() * 2 / 5; |
| 195 | + String shortName = String(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX); |
| 196 | + uwbDevice[shortName == "aabb" ? 0 : 1] = String(projectedRange); |
| 197 | +} |
| 198 | + |
| 199 | +void newDevice(DW1000Device *device) { |
| 200 | + String shortName = String(device->getShortAddress(), HEX); |
| 201 | + display->uwbStatus(shortName == "aabb", true); |
| 202 | +} |
| 203 | + |
| 204 | +void inactiveDevice(DW1000Device *device) { |
| 205 | + String shortName = String(device->getShortAddress(), HEX); |
| 206 | + display->uwbStatus(shortName == "aabb", false); |
| 207 | +} |
| 208 | + |
| 209 | +void WiFiEvent(WiFiEvent_t event) { |
| 210 | + switch (event) { |
| 211 | + case SYSTEM_EVENT_STA_GOT_IP: |
| 212 | + display->systemLog(WiFi.localIP().toString()); |
| 213 | + display->systemLog("IP address:"); |
| 214 | + display->systemLog("Connected!"); |
| 215 | + display->systemLog("WiFi", true); |
| 216 | + |
| 217 | + Serial.print("WiFi connected! IP address: "); |
| 218 | + Serial.println(WiFi.localIP()); |
| 219 | + udp.begin(WiFi.localIP(), udpPort); |
| 220 | + |
| 221 | + display->systemLog("UDP begins!", true); |
| 222 | + isWiFiConnected = true; |
| 223 | + |
| 224 | + initUWB(); |
| 225 | + break; |
| 226 | + case SYSTEM_EVENT_STA_DISCONNECTED: |
| 227 | + |
| 228 | + isWiFiConnected = false; |
| 229 | + display->systemLog("Disconnected!"); |
| 230 | + display->systemLog("WiFi", true); |
| 231 | + display->setIsAllReady(false); |
| 232 | + break; |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +void connectToWiFi(const char *ssid, const char *pwd) { |
| 237 | + WiFi.disconnect(true); |
| 238 | + WiFi.onEvent(WiFiEvent); |
| 239 | + WiFi.begin(ssid, pwd); |
| 240 | + display->systemLog("Connecting!"); |
| 241 | + display->systemLog("WIFI", true); |
| 242 | +} |
0 commit comments