Merge remote-tracking branch 'origin/main'

This commit is contained in:
Luca Warmenhoven
2024-05-29 13:41:00 +02:00
13 changed files with 285 additions and 230 deletions

View File

@@ -5,56 +5,33 @@
void setup() {
Serial.begin(9600);
Serial.println("startup");
//connect to internet and start sensor
connectivity.connectWiFi(ssid, pass);
sensorManager.sensorSetup();
//ws server address, port and URL
// webSocket.begin("145.28.160.108", 8001, "");
// try every 500 again if connection has failed
// webSocket.setReconnectInterval(500);
}
void loop() {
SensorManager::eulerAngles eulerRotation = sensorManager.getEulerAngles();
SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
unsigned long lastTime = 0; // will store the last time the code was run
// Subtract offset
// rotation.i -= offset.i;
// rotation.j -= offset.j;
// rotation.k -= offset.k;
// rotation.w -= offset.w;
Serial.print(eulerRotation.roll);
Serial.print(" ");
Serial.print(eulerRotation.yaw);
Serial.print(" ");
Serial.print(eulerRotation.pitch);
Serial.println();
// Convert quaternion to Euler angles in radians
// Convert to degrees
// float rollDegrees = roll * 180.0f / PI;
// float pitchDegrees = pitch * 180.0f / PI;
// float yawDegrees = yaw * 180.0f / PI;
Serial.print(eulerRotation.roll);
Serial.print(" ");
Serial.print(eulerRotation.pitch);
Serial.print(" ");
Serial.print(eulerRotation.yaw);
sendData(eulerRotation.roll, eulerRotation.pitch, eulerRotation.yaw);
Serial.println();
// webSocket.loop();
unsigned long currentTime = millis();
if (currentTime - lastTime >= 100) { // 100 ms has passed
String message = "{\"deviceId\": 1, \"rotationX\":\"" + String(eulerRotation.roll) + "\",\"rotationY\":\"" + String(eulerRotation.pitch) + "\",\"rotationZ\":\"" + String(eulerRotation.yaw) + "\",\"accelerationX\":\"" + String(rotationAcceleration.x) + "\",\"accelerationY\":\"" + String(rotationAcceleration.y) + "\",\"accelerationZ\":\"" + String(rotationAcceleration.z) + "\",\"type\":\"data\"}";
Serial.println(connectivity.httpPost("192.168.137.146", "/", 3445, message.c_str(), message.length(), "json"));
Serial.println(message);
lastTime = currentTime;
}
// if (Serial.available()) {
// String command = Serial.readStringUntil('\n');
// command.trim(); // remove any trailing whitespace
// if (command == "setZeroPoint") {
// setZeroPoint();
// }
// }
// }
// void setZeroPoint() {
// offset = sensorManager.readLoop();
// }
}
//acceleration.X
//acceleration.Y
//acceleration.Z
void sendData(float roll, float pitch, float yaw){
String message = "{\"Sensor\": 1, \"roll\":\"" + String(roll) + "\",\"pitch\":\"" + String(pitch) + "\",\"yaw\":\"" + String(yaw) + "\"}";
webSocket.sendTXT(message);
}

View File

@@ -5,7 +5,6 @@
SensorManager::SensorManager() {}
void SensorManager::sensorSetup() {
Wire.setClockStretchLimit(150000L); // Default stretch limit 150mS
Wire.begin();
//wait for the sensor to start before continue
if (myIMU.begin() == false) {
@@ -15,20 +14,12 @@ void SensorManager::sensorSetup() {
//start sensorfunction and start autocalibration
//once calibration is enabled it attempts to every 5 min
Wire.setClock(400000); //Increase I2C data rate to 400kHz
myIMU.calibrateAll(); //Turn on cal for Accel, Gyro, and Mag
Wire.setClock(400000);
myIMU.enableGyroIntegratedRotationVector(100); //send data every 100ms
myIMU.enableMagnetometer(100); //Send data update every 100ms
myIMU.saveCalibration(); //Saves the current dynamic calibration data (DCD) to memory
myIMU.requestCalibrationStatus(); //Sends command to get the latest calibration status
if (myIMU.calibrationComplete() == true) {
Serial.println("Calibration data successfully stored");
}
myIMU.enableAccelerometer(100); //Send data update every 100ms
Serial.println(F("magnetometer rotation enabled"));
}
//get sensordata
SensorManager::RotationQuintillions SensorManager::getQuintillions() {
if (myIMU.dataAvailable() == true) {
float i = myIMU.getQuatI();
@@ -48,7 +39,7 @@ SensorManager::RotationQuintillions SensorManager::getQuintillions() {
return rotation;
}
}
//calculate Quintillions to Euler angles from -1π to +1π
SensorManager::eulerAngles SensorManager::getEulerAngles() {
SensorManager::RotationQuintillions rotation = getQuintillions();
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));
@@ -56,4 +47,12 @@ SensorManager::eulerAngles SensorManager::getEulerAngles() {
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));
eulerAngles EulerAngles = { roll, pitch, yaw };
return EulerAngles;
}
SensorManager::acceleration SensorManager::getAcelleration(){
float x = myIMU.getAccelX();
float y = myIMU.getAccelY();
float z = myIMU.getAccelZ();
acceleration Acceleration = { x, y, z };
return Acceleration;
}

View File

@@ -13,8 +13,14 @@ public:
float pitch;
float yaw;
};
eulerAngles getEulerAngles();
struct acceleration {
float x;
float y;
float z;
};
eulerAngles getEulerAngles();
acceleration getAcelleration();
private:
struct RotationQuintillions {

View File

@@ -31,16 +31,19 @@ function handleIncoming(request, response, app, pool)
else
{
// Send back the data in the right format
let firstRow = rows[0];
let converted = rows.map(row => {
return {
name: row.Name,
description: row.Description,
muscleGroup: row.MuscleGroup,
imageUrl: row.ImageURL,
videoUrl: row.VideoURL
};
});
response
.status(200)
.send(JSON.stringify({
name: firstRow.Name,
description: firstRow.Description,
muscleGroup: firstRow.MuscleGroup,
imageUrl: firstRow.ImageURL,
videoUrl: firstRow.VideoURL
}));
.send(JSON.stringify(converted));
}
})
.catch(error => {
@@ -64,5 +67,5 @@ function handleIncoming(request, response, app, pool)
// Export the function that registers the incoming request handlers
module.exports = function(app, pool) {
app.get('/', (req, res) => handleIncoming(req, res, app, pool));
app.post('/', (req, res) => handleIncoming(req, res, app, pool));
};

View File

@@ -0,0 +1,24 @@
const address = 'http://145.92.8.135:3445/';
const data = {
rotationX: 1,
rotationY: .4,
rotationZ: .1,
accelerationX: 1,
accelerationY: 2,
accelerationZ: 4,
deviceId: 1,
type: 'data'
};
for ( let i = 0; i < 10; i++)
{
setTimeout(() => {
console.log("Sending data");
fetch(address, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
}, i * 1000);
}

View File

@@ -7,7 +7,7 @@ app.use(bodyParser.json());
const serverPort = 3000;
const databaseCredentials = {
host: 'localhost',
host: '127.0.0.1',
user: 'fitbot',
password: 'fitbot123',
database: 'fitbot',