2021-06-09 17:33:37 +00:00
|
|
|
#pragma region Includes
|
2021-05-18 11:18:36 +00:00
|
|
|
#include <Arduino.h>
|
2021-05-22 17:06:39 +00:00
|
|
|
#include <string>
|
2021-06-10 13:40:00 +00:00
|
|
|
#include <SoftwareSerial.h> // Software Serial not currently needed
|
2021-06-12 23:56:49 +00:00
|
|
|
#include <SoftwareSerial.h>
|
2021-06-03 20:29:14 +00:00
|
|
|
#include <AsyncTCP.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
#include <ESPmDNS.h>
|
2021-06-05 12:11:09 +00:00
|
|
|
#include "TickerV2.h"
|
2021-06-03 20:29:14 +00:00
|
|
|
#include <WebSocketsServer.h>
|
2021-06-12 11:47:12 +00:00
|
|
|
#include "credentials.h"
|
2021-06-03 20:29:14 +00:00
|
|
|
#include <ArduinoJson.h>
|
2021-06-04 00:02:32 +00:00
|
|
|
#include <SPIFFS.h>
|
2021-06-12 11:50:44 +00:00
|
|
|
#include "status.h"
|
2021-06-12 23:50:38 +00:00
|
|
|
#include "instruction.h"
|
2021-06-12 23:56:49 +00:00
|
|
|
#include <queue>
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma endregion
|
2021-06-04 00:02:32 +00:00
|
|
|
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma region Enable extra debugging info for ESP32
|
2021-06-04 00:02:32 +00:00
|
|
|
#undef LOG_LOCAL_LEVEL
|
2021-06-03 20:29:14 +00:00
|
|
|
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
|
|
|
|
#include "esp_log.h"
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma endregion
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma region Definitions eg pins
|
2021-06-12 23:56:49 +00:00
|
|
|
#define RX1pin 18 // Pin 6 on expansion board, UART1
|
|
|
|
#define TX1pin 5 // Pin 7 on expansion board, UART1
|
|
|
|
#define RX2pin 17 // Pin 8 on expansion board, UART2
|
|
|
|
#define TX2pin 16 // Pin 9 on expansion board, UART2
|
|
|
|
#define RX3pin 14 // Pin 10 on expansion board, UART3
|
|
|
|
#define TX3pin 4 // Pin 11 on expansion board, UART3
|
2021-06-14 16:50:15 +00:00
|
|
|
#define RX4pin 15 // Pin 12 on expansion board, UART4
|
|
|
|
#define TX4pin 2 // Pin 13 on expansion board, UART4
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma endregion
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma region Function Declarations
|
2021-06-03 20:29:14 +00:00
|
|
|
void notFound(AsyncWebServerRequest *request);
|
|
|
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length);
|
2021-06-12 23:56:49 +00:00
|
|
|
void queueInstruction(RoverInstruction instruction);
|
|
|
|
void sendToCommand();
|
|
|
|
void sendToDrive(RoverInstruction instruction);
|
|
|
|
void recvFromDrive();
|
2021-06-13 17:36:13 +00:00
|
|
|
void sendToEnergy(bool instruction);
|
2021-06-12 23:56:49 +00:00
|
|
|
void recvFromEnergy();
|
|
|
|
void sendToVision();
|
|
|
|
void recvFromVision();
|
2021-06-14 16:50:15 +00:00
|
|
|
void recvFromCompass();
|
2021-06-13 17:36:13 +00:00
|
|
|
void emergencyStop();
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma endregion
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma region Global objects
|
2021-06-03 20:29:14 +00:00
|
|
|
AsyncWebServer webserver(80);
|
|
|
|
WebSocketsServer websocketserver(81);
|
2021-06-05 12:11:09 +00:00
|
|
|
Ticker ticker;
|
2021-06-14 16:50:15 +00:00
|
|
|
SoftwareSerial Serial3, Serial4;
|
2021-06-12 23:56:49 +00:00
|
|
|
std::queue<RoverInstruction> InstrQueue;
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma endregion
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma region Global variables
|
2021-06-12 11:50:44 +00:00
|
|
|
ControlStatus_t Status;
|
2021-06-12 23:56:49 +00:00
|
|
|
float batteryVoltage;
|
2021-06-13 17:26:00 +00:00
|
|
|
float batteryLevel;
|
|
|
|
float batteryCycles;
|
2021-06-12 23:56:49 +00:00
|
|
|
int odometer;
|
|
|
|
int heading;
|
|
|
|
int xpos, ypos;
|
|
|
|
int signalStrength;
|
2021-06-13 17:36:13 +00:00
|
|
|
int lastExecutedCommand, lastCompletedCommand;
|
|
|
|
bool driveCommandComplete;
|
|
|
|
int bb_left, bb_right, bb_top, bb_bottom;
|
|
|
|
int bb_centre_x, bb_centre_y;
|
|
|
|
float chargeGoal;
|
2021-06-09 17:33:37 +00:00
|
|
|
#pragma endregion
|
2021-06-03 20:29:14 +00:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
2021-06-04 00:02:32 +00:00
|
|
|
esp_log_level_set("*", ESP_LOG_ERROR); // set all components to ERROR level
|
|
|
|
esp_log_level_set("wifi", ESP_LOG_WARN); // enable WARN logs from WiFi stack
|
|
|
|
esp_log_level_set("dhcpc", ESP_LOG_INFO); // enable INFO logs from DHCP client
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-14 16:50:15 +00:00
|
|
|
Serial.begin(115200); // Set up hardware UART0 (Connected to USB port)
|
|
|
|
Serial1.begin(9600, SERIAL_8N1, RX1pin, TX1pin); // Set up hardware UART1 (Connected to Drive)
|
|
|
|
Serial2.begin(9600, SERIAL_8N1, RX2pin, TX2pin); // Set up hardware UART2 (Connected to Energy)
|
|
|
|
Serial3.begin(9600, SWSERIAL_8N1, RX3pin, TX3pin); // Set up software UART3 (Connected to Vision)
|
|
|
|
Serial4.begin(115200, SWSERIAL_8N1, RX4pin, TX4pin); // Set up software UART4 (Connected to Compass)
|
2021-06-12 23:56:49 +00:00
|
|
|
|
|
|
|
// Set global variable startup values
|
2021-06-12 11:50:44 +00:00
|
|
|
Status = CS_IDLE;
|
2021-06-12 23:56:49 +00:00
|
|
|
batteryVoltage = 0;
|
|
|
|
batteryLevel = 0;
|
2021-06-13 17:36:13 +00:00
|
|
|
batteryCycles = 0;
|
2021-06-12 23:56:49 +00:00
|
|
|
odometer = 0;
|
|
|
|
heading = 0;
|
|
|
|
xpos = 0;
|
|
|
|
ypos = 0;
|
|
|
|
signalStrength = 0;
|
2021-06-13 17:36:13 +00:00
|
|
|
lastExecutedCommand = 0;
|
2021-06-12 23:56:49 +00:00
|
|
|
lastCompletedCommand = 0;
|
2021-06-13 17:36:13 +00:00
|
|
|
driveCommandComplete = 1;
|
|
|
|
chargeGoal = 0;
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-04 16:12:40 +00:00
|
|
|
if (!SPIFFS.begin(true)) // Mount SPIFFS
|
2021-06-04 00:02:32 +00:00
|
|
|
{
|
|
|
|
Serial.println("SPIFFS failed to mount");
|
|
|
|
return;
|
|
|
|
}
|
2021-06-04 16:12:40 +00:00
|
|
|
Serial.println("SPIFFS mounted");
|
2021-06-04 00:02:32 +00:00
|
|
|
|
2021-06-03 20:29:14 +00:00
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PW);
|
2021-06-12 23:56:49 +00:00
|
|
|
while (WiFi.status() != WL_CONNECTED) // Wait for ESP32 to connect to AP in "credentials.h"
|
2021-06-03 20:29:14 +00:00
|
|
|
{
|
|
|
|
delay(500);
|
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
while (!MDNS.begin("rover")) // Set up mDNS cast at "rover.local/"
|
2021-06-03 20:29:14 +00:00
|
|
|
{
|
|
|
|
Serial.println("Error setting up mDNS, retrying in 5s");
|
|
|
|
delay(5000);
|
2021-06-01 16:46:31 +00:00
|
|
|
}
|
2021-06-03 20:29:14 +00:00
|
|
|
Serial.println("mDNS set up, access Control Panel at 'rover.local/'");
|
|
|
|
|
2021-06-04 00:02:32 +00:00
|
|
|
webserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
2021-06-12 23:56:49 +00:00
|
|
|
{ request->send(SPIFFS, "/index.html", "text/html"); }); // Serve "index.html" at root page
|
2021-06-04 00:02:32 +00:00
|
|
|
webserver.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request)
|
2021-06-12 23:56:49 +00:00
|
|
|
{ request->send(SPIFFS, "/favicon.ico", "image/png"); }); // Serve tab icon
|
|
|
|
webserver.onNotFound(notFound); // Set up basic 404NotFound page
|
|
|
|
webserver.begin(); // Start Asynchronous Web Server
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-12 23:56:49 +00:00
|
|
|
websocketserver.begin(); // Start Websocket Server
|
|
|
|
websocketserver.onEvent(webSocketEvent); // Set up function call when event received from Command
|
|
|
|
ticker.attach(0.5, sendToCommand); // Set up recurring function to forward rover status to Command
|
2021-06-01 16:46:31 +00:00
|
|
|
}
|
2021-06-03 20:29:14 +00:00
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2021-06-12 23:56:49 +00:00
|
|
|
websocketserver.loop(); // Handle incoming client connections
|
2021-06-13 17:36:13 +00:00
|
|
|
recvFromDrive(); // Update stats from Drive
|
|
|
|
recvFromEnergy(); // Update stats from Energy
|
2021-06-14 16:50:15 +00:00
|
|
|
// recvFromVision(); // Update stats from Vision
|
|
|
|
recvFromCompass(); // Update stats from Compass
|
2021-06-12 23:56:49 +00:00
|
|
|
switch (Status)
|
2021-06-03 20:29:14 +00:00
|
|
|
{
|
2021-06-12 23:56:49 +00:00
|
|
|
case CS_ERROR:
|
|
|
|
{
|
|
|
|
Serial.println("Rover in error state, rebooting...");
|
|
|
|
exit(1);
|
2021-06-01 16:46:31 +00:00
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
break;
|
|
|
|
case CS_IDLE:
|
2021-06-03 20:29:14 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
if (!InstrQueue.empty()) // If Rover idle and InstrQueue NOT empty: Do the next command in the queue
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
RoverInstruction *instr = &InstrQueue.front(); // Get next command
|
|
|
|
switch (instr->instr) // Determine command type
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
case INSTR_RESET: // Reset telemetry values (zeroing position/distance)
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
|
|
|
odometer = 0;
|
|
|
|
xpos = 0;
|
|
|
|
ypos = 0;
|
2021-06-13 17:36:13 +00:00
|
|
|
DynamicJsonDocument tdoc(128);
|
|
|
|
tdoc["rstD"] = 1;
|
|
|
|
serializeJson(tdoc, Serial1); // Send reset odometer signal to Drive
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-06-13 17:36:13 +00:00
|
|
|
case INSTR_STOP: // Emergency stop
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
Status = CS_ERROR;
|
2021-06-12 23:56:49 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
Serial.println("Emergency Stop should not get queued, hold and print");
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-06-13 17:36:13 +00:00
|
|
|
case INSTR_MOVE: // Normal movement
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
Status = CS_MOVING; // Set moving state
|
|
|
|
driveCommandComplete = 0;
|
|
|
|
sendToDrive(*instr); // Forward to Drive handler
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-06-13 17:36:13 +00:00
|
|
|
case INSTR_CHARGE: // Normal charge
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
Status = CS_CHARGING; // Set charging state
|
|
|
|
chargeGoal = (float)instr->charge; // Set charging goal
|
|
|
|
sendToEnergy(1); // Forward to Energy handler
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
Serial.println("Unknown instruction type in queue, skipping...");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-06-13 17:36:13 +00:00
|
|
|
lastExecutedCommand = instr->id; // Update tracker of last processed command
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
2021-06-01 16:46:31 +00:00
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
break;
|
|
|
|
case CS_MOVING:
|
2021-06-05 12:11:09 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
if (driveCommandComplete) // If movement command complete:
|
|
|
|
{
|
|
|
|
Status = CS_IDLE; // Set rover state back to idle
|
|
|
|
lastCompletedCommand = lastExecutedCommand; // Update last completed command
|
|
|
|
}
|
|
|
|
else // If movement command NOT complete:
|
|
|
|
{ // Send (up to date) current heading to Drive
|
|
|
|
DynamicJsonDocument tdoc(128);
|
|
|
|
tdoc["rH"] = -1;
|
|
|
|
tdoc["cH"] = heading;
|
|
|
|
serializeJson(tdoc, Serial1);
|
|
|
|
}
|
2021-06-05 12:11:09 +00:00
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
break;
|
|
|
|
case CS_CHARGING:
|
2021-06-05 12:11:09 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
if (batteryLevel >= chargeGoal) // Compare batteryLevel to chargeGoal
|
|
|
|
{
|
|
|
|
Status = CS_IDLE;
|
|
|
|
lastCompletedCommand = lastExecutedCommand; // Update last completed command
|
2021-06-14 16:50:15 +00:00
|
|
|
sendToEnergy(0); // Stop charging if goal reached
|
2021-06-13 17:36:13 +00:00
|
|
|
}
|
|
|
|
// Otherwise continue charging, no change
|
2021-06-05 12:11:09 +00:00
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-06-05 12:11:09 +00:00
|
|
|
{
|
2021-06-12 23:56:49 +00:00
|
|
|
Serial.println("Unknown rover state, exiting...");
|
|
|
|
exit(1);
|
2021-06-05 12:11:09 +00:00
|
|
|
}
|
2021-06-13 17:36:13 +00:00
|
|
|
break;
|
2021-06-05 12:11:09 +00:00
|
|
|
}
|
2021-06-13 17:36:13 +00:00
|
|
|
delay(500);
|
2021-06-03 20:29:14 +00:00
|
|
|
}
|
2021-05-18 11:18:36 +00:00
|
|
|
|
2021-06-03 20:29:14 +00:00
|
|
|
void notFound(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
request->send(404, "text/plain", "Page Not found. Check URI/IP address.");
|
2021-05-18 11:18:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 20:29:14 +00:00
|
|
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-06-04 00:02:32 +00:00
|
|
|
case WStype_DISCONNECTED:
|
|
|
|
{
|
|
|
|
Serial.printf("Client[%u] Disconnected!\n", num);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WStype_CONNECTED:
|
|
|
|
{
|
|
|
|
IPAddress ip = websocketserver.remoteIP(num);
|
|
|
|
Serial.printf("Client[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
|
|
|
|
}
|
|
|
|
break;
|
2021-06-12 23:56:49 +00:00
|
|
|
case WStype_TEXT: // MSG received from command panel
|
2021-06-04 00:02:32 +00:00
|
|
|
{
|
2021-06-12 23:56:49 +00:00
|
|
|
Serial.printf("Client[%u] sent Text: %s\n", num, payload); // Echo received command to terminal
|
|
|
|
String command = String((char *)(payload)); // Convert received command to string type
|
2021-06-04 00:02:32 +00:00
|
|
|
|
2021-06-12 23:56:49 +00:00
|
|
|
DynamicJsonDocument rdoc(200); // Create instance of DynamicJsonDocument on heap, 200 Bytes
|
|
|
|
DeserializationError error = deserializeJson(rdoc, command); // Convert command string to JSONDocument and capture any errors
|
2021-06-04 00:02:32 +00:00
|
|
|
if (error)
|
2021-06-03 20:29:14 +00:00
|
|
|
{
|
2021-06-04 00:02:32 +00:00
|
|
|
Serial.print("deserializeJson() failed: ");
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
return;
|
2021-06-03 20:29:14 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 23:56:49 +00:00
|
|
|
RoverInstruction instr;
|
|
|
|
int mode = rdoc["mode"];
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case -1: // Add to queue, reset x/y/odometer (telemetry data)
|
|
|
|
{
|
|
|
|
Serial.println("Reset telemetry command received");
|
|
|
|
instr.id = rdoc["Cid"];
|
|
|
|
instr.instr = INSTR_RESET;
|
|
|
|
// Ignore rdoc["rH"], rdoc["rD"], rdoc["rS"], rdoc["rC"]
|
2021-06-03 20:29:14 +00:00
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
queueInstruction(instr); // Put reset command in InstrQueue
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0: // Stop immediately, clear command cache
|
|
|
|
{
|
|
|
|
Serial.println("Emergency stop command received");
|
|
|
|
// instr.instr = INSTR_STOP; // Not needed as Emergency Stop is not queued
|
|
|
|
// Ignore rdoc["Cid"], rdoc["rH"], rdoc["rD"], rdoc["rS"], rdoc["rC"]
|
2021-06-10 13:40:00 +00:00
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
emergencyStop();
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // Normal movement command, added to end of command cache
|
|
|
|
{
|
|
|
|
Serial.println("Normal movement command received");
|
|
|
|
instr.id = rdoc["Cid"];
|
|
|
|
instr.instr = INSTR_MOVE;
|
|
|
|
instr.heading = rdoc["rH"];
|
|
|
|
instr.distance = rdoc["rD"];
|
|
|
|
instr.speed = rdoc["rS"];
|
|
|
|
// Ignore rdoc["rC"]
|
2021-06-10 13:40:00 +00:00
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
queueInstruction(instr); // Put movement command in InstrQueue
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: // Normal charge command, results in no motion, added to end of command cache
|
|
|
|
{
|
|
|
|
Serial.println("Normal charge command received");
|
|
|
|
instr.id = rdoc["Cid"];
|
|
|
|
instr.instr = INSTR_CHARGE;
|
|
|
|
instr.charge = rdoc["rC"];
|
|
|
|
// Ignore rdoc["rH"], rdoc["rD"], rdoc["rS"]
|
2021-06-14 16:50:15 +00:00
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
queueInstruction(instr); // Put charge command in InstrQueue
|
2021-06-10 13:40:00 +00:00
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
// Default case, print and continue
|
|
|
|
Serial.println("Unknown Command type received, ignoring");
|
|
|
|
// Ignore rdoc["Cid"], rdoc["rH"], rdoc["rD"], rdoc["rS"], rdoc["rC"]
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2021-06-10 13:40:00 +00:00
|
|
|
}
|
2021-06-04 00:02:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WStype_PONG:
|
|
|
|
{
|
|
|
|
Serial.println("Websocket keep-alive PONG");
|
|
|
|
}
|
2021-06-04 16:12:40 +00:00
|
|
|
break;
|
2021-06-04 00:02:32 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
Serial.println(String("Websocket received invalid event type: ") + type + String(", exiting"));
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
break;
|
2021-05-22 17:06:39 +00:00
|
|
|
}
|
2021-06-03 20:29:14 +00:00
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
|
|
|
|
void queueInstruction(RoverInstruction instruction)
|
|
|
|
{
|
|
|
|
InstrQueue.push(instruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendToCommand()
|
|
|
|
{
|
|
|
|
DynamicJsonDocument tdoc(1024);
|
|
|
|
tdoc["st"] = Status;
|
|
|
|
tdoc["bV"] = batteryVoltage;
|
|
|
|
tdoc["bL"] = batteryLevel;
|
2021-06-13 17:36:13 +00:00
|
|
|
tdoc["bC"] = batteryCycles;
|
2021-06-12 23:56:49 +00:00
|
|
|
tdoc["tD"] = odometer;
|
|
|
|
tdoc["cH"] = heading;
|
|
|
|
tdoc["pos"][0] = xpos;
|
|
|
|
tdoc["pos"][1] = ypos;
|
|
|
|
tdoc["rssi"] = signalStrength;
|
|
|
|
tdoc["LCCid"] = lastCompletedCommand;
|
|
|
|
String JSON_Data;
|
|
|
|
serializeJson(tdoc, JSON_Data);
|
|
|
|
websocketserver.broadcastTXT(JSON_Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendToDrive(RoverInstruction instruction)
|
|
|
|
{
|
|
|
|
DynamicJsonDocument tdoc(1024);
|
|
|
|
tdoc["rH"] = instruction.heading;
|
|
|
|
tdoc["dist"] = instruction.distance;
|
|
|
|
tdoc["sp"] = instruction.speed;
|
|
|
|
tdoc["cH"] = heading;
|
|
|
|
serializeJson(tdoc, Serial1);
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
void recvFromDrive() // Update telemetry data and state info from Drive packet
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
if (Serial1.available()) // Check for input from UART1 (Connected to Drive)
|
|
|
|
{
|
|
|
|
DynamicJsonDocument rdoc(1024);
|
|
|
|
deserializeJson(rdoc, Serial1);
|
|
|
|
driveCommandComplete = rdoc["comp"];
|
|
|
|
odometer = rdoc["mm"];
|
|
|
|
xpos = rdoc["pos"][0];
|
|
|
|
ypos = rdoc["pos"][1];
|
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
void sendToEnergy(bool instruction)
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
DynamicJsonDocument tdoc(128);
|
|
|
|
tdoc["ch"] = instruction; // Start charging
|
2021-06-12 23:56:49 +00:00
|
|
|
serializeJson(tdoc, Serial2);
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
void recvFromEnergy() // Update telemetry data and state info from Energy packet
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
if (Serial2.available()) // Check for input from UART2 (Connected to Energy)
|
|
|
|
{
|
|
|
|
DynamicJsonDocument rdoc(1024);
|
|
|
|
deserializeJson(rdoc, Serial2);
|
|
|
|
batteryLevel = rdoc["soc"];
|
|
|
|
batteryVoltage = rdoc["mV"];
|
|
|
|
batteryCycles = rdoc["cyc"];
|
2021-05-22 17:06:39 +00:00
|
|
|
}
|
2021-06-03 20:29:14 +00:00
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
|
|
|
|
void sendToVision()
|
|
|
|
{
|
|
|
|
Serial3.print("R"); // Request new data from Vision
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:36:13 +00:00
|
|
|
void recvFromVision() // Update bounding box and obstacle detection data from Vision packet
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 17:36:13 +00:00
|
|
|
if (Serial3.available()) // Check for input from UART3 (Connected to Vision)
|
|
|
|
{
|
|
|
|
DynamicJsonDocument rdoc(1024);
|
|
|
|
deserializeJson(rdoc, Serial3);
|
|
|
|
bb_left = rdoc["bb"][0];
|
|
|
|
bb_right = rdoc["bb"][1];
|
|
|
|
bb_top = rdoc["bb"][2];
|
|
|
|
bb_bottom = rdoc["bb"][3];
|
|
|
|
bb_centre_x = rdoc["cen"][0];
|
|
|
|
bb_centre_y = rdoc["cen"][1];
|
|
|
|
heading = rdoc["cH"];
|
|
|
|
}
|
2021-06-12 23:56:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:50:15 +00:00
|
|
|
void recvFromCompass()
|
|
|
|
{
|
|
|
|
if (Serial4.available())
|
|
|
|
{
|
|
|
|
DynamicJsonDocument rdoc(128);
|
|
|
|
deserializeJson(rdoc, Serial4);
|
|
|
|
heading = rdoc["cH"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-13 16:26:50 +00:00
|
|
|
void emergencyStop()
|
2021-06-12 23:56:49 +00:00
|
|
|
{
|
2021-06-13 16:26:50 +00:00
|
|
|
DynamicJsonDocument tdoc(1024);
|
|
|
|
tdoc["rH"] = heading;
|
|
|
|
tdoc["dist"] = -1;
|
|
|
|
tdoc["sp"] = -1;
|
|
|
|
tdoc["cH"] = heading;
|
|
|
|
serializeJson(tdoc, Serial1); // Send stop signals to Drive
|
2021-06-13 17:36:13 +00:00
|
|
|
sendToEnergy(0); // Send stop signal to Energy
|
2021-06-12 23:56:49 +00:00
|
|
|
while (InstrQueue.size())
|
|
|
|
{
|
|
|
|
InstrQueue.pop(); // Clear Instruction Queue
|
|
|
|
}
|
2021-06-13 16:26:50 +00:00
|
|
|
Status = CS_IDLE; // Reset rover to idle state
|
2021-06-12 23:56:49 +00:00
|
|
|
Serial.println("Instruction Queue cleared");
|
|
|
|
}
|