cleanup + serial communication for setting zeropoint and put wifi in its own class

This commit is contained in:
2024-05-14 16:01:41 +02:00
parent 7bc97e93c2
commit 1c39bf9153
5 changed files with 83 additions and 47 deletions

View File

@@ -1,22 +1,26 @@
#include "headerFile.h"
SensorManager sensorManager;
ESP8266WiFiMulti wifi;
WebSocketsClient webSocket;
#define USE_SERIAL Serial
SensorManager::Rotation offset;
void setup() {
Serial.begin(9600);
Serial.println("startup");
delay(5000);
connectWiFi();
connectivity.connectWiFi(ssid, pass);
sensorManager.sensorSetup();
websocketSetup();
connectivity.websocketSetup();
}
void loop() {
SensorManager::Rotation rotation = sensorManager.readLoop();
// Subtract offset
rotation.i -= offset.i;
rotation.j -= offset.j;
rotation.k -= offset.k;
rotation.w -= offset.w;
// Convert quaternion to Euler angles in radians
float roll = atan2(2.0f * (rotation.w * rotation.i + rotation.j * rotation.k), 1.0f - 2.0f * (rotation.i * rotation.i + rotation.j * rotation.j));
float pitch = asin(2.0f * (rotation.w * rotation.j - rotation.k * rotation.i));
@@ -32,34 +36,19 @@ void loop() {
Serial.print(pitch);
Serial.print(" ");
Serial.print(yaw);
sendData(roll, pitch, yaw);
connectivity.sendData(roll, pitch, yaw);
Serial.println();
webSocket.loop();
}
void connectWiFi(){
WiFi.mode(WIFI_STA);
wifi.addAP(ssid, pass);
WiFi.begin();
while (WiFi.status() != WL_CONNECTED) {
Serial.println("connecting to wifi");
delay(1000);
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
command.trim(); // remove any trailing whitespace
if (command == "setZeroPoint") {
setZeroPoint();
}
}
Serial.println(WiFi.localIP());
}
void websocketSetup(){
//ws server address, port and URL
webSocket.begin("192.168.178.118", 8001, "");
// try every 500 again if connection has failed
webSocket.setReconnectInterval(500);
void setZeroPoint() {
offset = sensorManager.readLoop();
}
void sendData(float roll, float pitch, float yaw){
String message = "{\"Sensor\": 1, \"roll\":\"" + String(roll) + "\",\"pitch\":\"" + String(pitch) + "\",\"yaw\":\"" + String(yaw) + "\"}";
webSocket.sendTXT(message);
}