diff --git a/Control/src/main.cpp b/Control/src/main.cpp index d3e85ef..a76a5e3 100644 --- a/Control/src/main.cpp +++ b/Control/src/main.cpp @@ -1,61 +1,450 @@ #include #include -#include +// #include Software Serial not currently needed +#include +#include +#include +#include +#include +#include +#include +#ifdef LOG_LOCAL_LEVEL + #undef LOG_LOCAL_LEVEL +#endif +#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE +#include "esp_log.h" -#define RX1pin 14 // Pin 10 on expansion board -#define TX1pin 4 // Pin 11 on expansion board -#define RX2pin 15 // Pin 12 on expansion board -#define TX2pin 2 // Pin 13 on expansion board -#define RX3pin 18 // Pin 6 on expansion board -#define TX3pin 5 // Pin 7 on expansion board -#define RX4pin 17 // Pin 8 on expansion board -#define TX4pin 16 // Pin 9 on expansion board +#define RX1pin 14 // Pin 10 on expansion board, UART1 +#define TX1pin 4 // Pin 11 on expansion board, UART1 -void forwardprint1() { - if(Serial1.available()){ - input1 = String(Serial1.readStringUntil('\n')); - Serial2.println(input1); - } +// Function Declarations +void printFPGAoutput(); +void returnSensorData(); +void notFound(AsyncWebServerRequest *request); +void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length); + +// Global objects +AsyncWebServer webserver(80); +WebSocketsServer websocketserver(81); +Ticker ticker(returnSensorData, 500, 0, MILLIS); + +// Global variables +int battery_voltage = 0; +int distance_travelled = 0; + +#pragma region HTMLsource +char index_html[] PROGMEM = R"=====( + + + + +Rover Control Panel + + + + + + + +

ROVER COMMAND CENTER

+ +
+ +
+
+

Movement Control

+
+ +
+
+ + + +
+ +
+
+ +
+
+

Sensor Data

+
    + +
  • + +
    +
    + +
    +
  • + + +
  • + +
    +
    + 28mm +
    +
  • + +
+
+ +
+ +
+ + + + + +)====="; +#pragma endregion + +void setup() +{ + 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 + + Serial.begin(115200); // Set up hardware UART0 (Connected to USB port) + Serial1.begin(9600, SERIAL_8N1, RX1pin, TX1pin); // Set up hardware UART1 + // Set up remaining communication ports here (Energy, Drive, Vision) + + Serial.println("Connecting to AP"); + WiFi.begin(WIFI_SSID, WIFI_PW); + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println("\nConnected to AP"); + while (!MDNS.begin("rover")) + { + Serial.println("Error setting up mDNS, retrying in 5s"); + delay(5000); + } + Serial.println("mDNS set up, access Control Panel at 'rover.local/'"); + + webserver.on("/", [](AsyncWebServerRequest *request) + { request->send_P(200, "text/html", index_html); }); + webserver.onNotFound(notFound); + webserver.begin(); + + websocketserver.begin(); + websocketserver.onEvent(webSocketEvent); + ticker.start(); +} + +void loop() +{ + printFPGAoutput(); + + String FPGAinput; // Forward serial monitor input to FPGA + if (Serial.available()) + { + FPGAinput = String(Serial.readStringUntil('\n')); + Serial1.println(FPGAinput); + } + + websocketserver.loop(); // Handle incoming client connections +} + +void printFPGAoutput() +{ // Print serial communication from FPGA to serial monitor + String FPGAoutput; + if (Serial1.available()) + { + FPGAoutput = String(Serial1.readStringUntil('\n')); + Serial.println(FPGAoutput); } } -int counter; -String input, input1, input2, input3, input4; -SoftwareSerial Serial3; -SoftwareSerial Serial4; - -void setup() { - Serial.begin(115200); // Set up hardware UART 0 (Connected to USB port) - Serial1.begin(9600, SERIAL_8N1, RX1pin, TX1pin); // Set up hardware UART 1 - Serial2.begin(9600, SERIAL_8N1, RX2pin, TX2pin); // Set up hardware UART 2 - Serial3.begin(9600, SWSERIAL_8N1, RX3pin, TX3pin); // Set up software UART 3 - Serial4.begin(9600, SWSERIAL_8N1, RX4pin, TX4pin); // Set up software UART 4 +void returnSensorData() +{ + // Collect sensor data here? + String JSON_Data = String("{\"BTRY_VOLT\":") + battery_voltage + String(",\"ODO_DIST\":") + distance_travelled + "}"; + websocketserver.broadcastTXT(JSON_Data); } -void loop() { - if(Serial.available()){ - input = String(Serial.readStringUntil('\n')); - Serial1.println(input); +void notFound(AsyncWebServerRequest *request) +{ + request->send(404, "text/plain", "Page Not found. Check URI/IP address."); +} + +void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) +{ + switch (type) + { + 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; + case WStype_TEXT: + { + Serial.printf("Client[%u] sent Text: %s\n", num, payload); + String command = String((char *)(payload)); + + DynamicJsonDocument doc(200); //creating an instance of a DynamicJsonDocument allocating 200bytes on the heap. + DeserializationError error = deserializeJson(doc, command); // deserialize 'doc' and parse for parameters we expect to receive. + if (error) + { + Serial.print("deserializeJson() failed: "); + Serial.println(error.c_str()); + return; + } + + int MVM_F_status = doc["MVM_F"]; + int MVM_L_status = doc["MVM_L"]; + int MVM_R_status = doc["MVM_R"]; + int MVM_B_status = doc["MVM_B"]; + + Serial.println('<' + MVM_F_status + ',' + MVM_B_status + ',' + MVM_L_status + ',' + MVM_R_status + '>'); + } + break; + case WStype_PONG: + { + Serial.println("Websocket keep-alive PONG"); + } + default: + { + Serial.println(String("Websocket received invalid event type: ") + type + String(", exiting")); + exit(1); + } } - forwardprint1(); - forwardprint2(); - forwardprint3(); - forwardprint4(); -} \ No newline at end of file +} diff --git a/Control/src/temp.cpp b/Control/src/temp.cpp deleted file mode 100644 index fb81261..0000000 --- a/Control/src/temp.cpp +++ /dev/null @@ -1,447 +0,0 @@ -#include -#include -#include -#include -#include -#include "Ticker.h" - -const int potPin = 34; //used to simulate battery voltage. -const int butPin = 16; //used to increment a variable to simulate distance increasing. -const int U_Led = 14; //LED subsitute for the 'movement forward command'. -const int L_Led = 12; //LED subsitute for the 'movement left command'. -const int R_Led = 15; //LED subsitute for the 'movement right command'. -const int D_Led = 13; //LED subsitute for the 'movement back command'. - -/* const char* ssid = "ssid"; -const char* password = "xxxxxxxx"; */ - -int potVal = 0; -bool butState = 1; //Variables only for testing - will be removed in final - -int d = 0; //Initializing variable for odometer distance. - -void send_sensor(); -Ticker timer; - -char index_html[] PROGMEM = R"=====( - - - - -Rover Command Center - - - - - - - -

ROVER COMMAND CENTER

- -
- -
-
-

Movement Control

-
- -
-
- - - -
- -
-
- -
-
-

Sensor Data

-
    - -
  • - -
    -
    - -
    -
  • - - -
  • - -
    -
    - 28mm -
    -
  • - -
-
- -
- -
- - - - - -)====="; - -AsyncWebServer server(80); // server port 80 for initial HTTP request for the main webpage. -WebSocketsServer websockets(81); // server port 81 for real time data flow through websockets. - -void notFound(AsyncWebServerRequest *request) -{ - request->send(404, "text/plain", "Page Not found. Check URI/IP address."); -} - -void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { - - switch (type) - { - case WStype_DISCONNECTED: - Serial.printf("Client[%u] Disconnected!\n", num); - break; - case WStype_CONNECTED: { - IPAddress ip = websockets.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; - case WStype_TEXT: { - Serial.printf("Client[%u] sent Text: %s\n", num, payload); - String command = String((char*)( payload)); - - DynamicJsonDocument doc(200); //creating an instance of a DynamicJsonDocument allocating 200bytes on the heap. - DeserializationError error = deserializeJson(doc, command); // deserialize 'doc' and parse for parameters we expect to receive. - if (error) { - Serial.print("deserializeJson() failed: "); - Serial.println(error.c_str()); - return; - } - - int MVM_F_status = doc["MVM_F"]; - int MVM_L_status = doc["MVM_L"]; - int MVM_R_status = doc["MVM_R"]; - int MVM_B_status = doc["MVM_B"]; - - digitalWrite(U_Led,MVM_F_status); - digitalWrite(L_Led,MVM_L_status); - digitalWrite(R_Led,MVM_R_status); - digitalWrite(D_Led,MVM_B_status); - } - } -} - -void setup() -{ - - Serial.begin(115200); - pinMode(U_Led,OUTPUT); - pinMode(L_Led,OUTPUT); - pinMode(R_Led,OUTPUT); - pinMode(D_Led,OUTPUT); - pinMode(butPin, INPUT_PULLUP); - - -/* Serial.println(); - Serial.println(); - Serial.print("Connecting to "); - Serial.println(ssid); - - WiFi.begin(ssid, password); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.print("."); - } - - Serial.println(""); - Serial.println("Rover connected to "); - Serial.println(ssid); - Serial.println(); - Serial.println("Rover IP address: "); - Serial.println(WiFi.localIP()); */ - - WiFi.softAP("RoverAP", "SplendidCheeks"); - Serial.println(); - Serial.println("RoverAP running"); - Serial.print("Rover IP address: "); - Serial.println(WiFi.softAPIP()); - - - if (!MDNS.begin("rover")) { - Serial.println("Error setting up MDNS responder!"); - while (1) { - delay(2000); - } - } - Serial.println("mDNS responder started! Rover Command Center can now be accessed at 'rover.local' "); - - - - server.on("/", [](AsyncWebServerRequest * request) - { - request->send_P(200, "text/html", index_html); - }); - - server.onNotFound(notFound); - - server.begin(); - websockets.begin(); - websockets.onEvent(webSocketEvent); - timer.attach(0.5,send_sensor_data); -} - -void loop() -{ - websockets.loop(); - potVal = analogRead(potPin); -} - -void send_sensor_data() -{ - - butState = digitalRead(butPin); - if (butState == LOW) { - //increment ODO: - d += 10; - } - // JSON_Data = {"BTRY_VOLT":v,"ODO_DIST":d} - String JSON_Data = "{\"BTRY_VOLT\":"; - JSON_Data += potVal; - JSON_Data += ",\"ODO_DIST\":"; - JSON_Data += d; - JSON_Data += "}"; - websockets.broadcastTXT(JSON_Data); -} \ No newline at end of file