62 lines
1.8 KiB
C++
62 lines
1.8 KiB
C++
#include "headerFile.h"
|
|
|
|
SensorManager::Rotation offset;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
Serial.println("startup");
|
|
|
|
connectivity.connectWiFi(ssid, pass);
|
|
sensorManager.sensorSetup();
|
|
|
|
//ws server address, port and URL
|
|
webSocket.begin("145.3.245.22", 8001, "");
|
|
// try every 500 again if connection has failed
|
|
webSocket.setReconnectInterval(500);
|
|
}
|
|
|
|
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));
|
|
float yaw = atan2(2.0f * (rotation.w * rotation.k + rotation.i * rotation.j), 1.0f - 2.0f * (rotation.j * rotation.j + rotation.k * rotation.k));
|
|
|
|
// Convert to degrees
|
|
float rollDegrees = roll * 180.0f / PI;
|
|
float pitchDegrees = pitch * 180.0f / PI;
|
|
float yawDegrees = yaw * 180.0f / PI;
|
|
|
|
Serial.print(roll);
|
|
Serial.print(" ");
|
|
Serial.print(pitch);
|
|
Serial.print(" ");
|
|
Serial.print(yaw);
|
|
sendData(roll, pitch, yaw);
|
|
|
|
Serial.println();
|
|
webSocket.loop();
|
|
|
|
if (Serial.available()) {
|
|
String command = Serial.readStringUntil('\n');
|
|
command.trim(); // remove any trailing whitespace
|
|
if (command == "setZeroPoint") {
|
|
setZeroPoint();
|
|
}
|
|
}
|
|
}
|
|
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);
|
|
} |