optimization
This commit is contained in:
@@ -1,14 +1,18 @@
|
|||||||
#include "headerFile.h"
|
#include "headerFile.h"
|
||||||
|
|
||||||
// SensorManager::Rotation offset;
|
// SensorManager::Rotation offset;
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
#define DEVICE_ID 1
|
||||||
|
|
||||||
|
char *buffer = (char *)malloc(sizeof(char) * BUFFER_SIZE);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("startup");
|
// Serial.println("startup");
|
||||||
//connect to internet and start sensor
|
//connect to internet and start sensor
|
||||||
connectivity.connectWiFi(ssid, pass);
|
connectivity.connectWiFi(ssid, pass);
|
||||||
sensorManager.sensorSetup();
|
sensorManager.sensorSetup();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@@ -16,22 +20,26 @@ void loop() {
|
|||||||
SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
|
SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
|
||||||
unsigned long lastTime = 0; // will store the last time the code was run
|
unsigned long lastTime = 0; // will store the last time the code was run
|
||||||
|
|
||||||
Serial.print(eulerRotation.roll);
|
|
||||||
Serial.print(" ");
|
|
||||||
Serial.print(eulerRotation.yaw);
|
|
||||||
Serial.print(" ");
|
|
||||||
Serial.print(eulerRotation.pitch);
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
unsigned long currentTime = millis();
|
unsigned long currentTime = millis();
|
||||||
if (currentTime - lastTime >= 100) { // 100 ms has passed
|
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\"}";
|
memset(buffer, 0, BUFFER_SIZE);
|
||||||
Serial.println(connectivity.httpPost("192.168.137.146", "/", 3445, message.c_str(), message.length(), "json"));
|
sprintf(
|
||||||
Serial.println(message);
|
buffer,
|
||||||
|
"{\"deviceId\": %d, \"rotationX\": %d, \"rotationY\": %d, \"rotationZ\": %d, \"accelerationX\": %d, \"accelerationY\": %d, \"accelerationZ\": %d, \"type\": %s}",
|
||||||
|
DEVICE_ID,
|
||||||
|
eulerRotation.roll,
|
||||||
|
eulerRotation.pitch,
|
||||||
|
eulerRotation.yaw,
|
||||||
|
rotationAcceleration.x,
|
||||||
|
rotationAcceleration.y,
|
||||||
|
rotationAcceleration.z,
|
||||||
|
"data");
|
||||||
|
// Serial.println(connectivity.httpPost("192.168.137.45", "/", 3445, message.c_str(), message.length(), "json"));
|
||||||
|
// Serial.println(message);
|
||||||
|
connectivity.httpPost("192.168.137.45", "/", 3445, buffer, strlen(buffer), "application/json");
|
||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//acceleration.X
|
//acceleration.X
|
||||||
//acceleration.Y
|
//acceleration.Y
|
||||||
//acceleration.Z
|
//acceleration.Z
|
||||||
|
|
||||||
|
@@ -9,15 +9,15 @@ void SensorManager::sensorSetup() {
|
|||||||
//wait for the sensor to start before continue
|
//wait for the sensor to start before continue
|
||||||
if (myIMU.begin() == false) {
|
if (myIMU.begin() == false) {
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.println(".");
|
// Serial.println(".");
|
||||||
}
|
}
|
||||||
//start sensorfunction and start autocalibration
|
//start sensorfunction and start autocalibration
|
||||||
//once calibration is enabled it attempts to every 5 min
|
//once calibration is enabled it attempts to every 5 min
|
||||||
|
|
||||||
Wire.setClock(400000);
|
|
||||||
myIMU.enableGyroIntegratedRotationVector(100); //send data every 100ms
|
myIMU.enableGyroIntegratedRotationVector(100); //send data every 100ms
|
||||||
myIMU.enableAccelerometer(100); //Send data update every 100ms
|
myIMU.enableAccelerometer(100); //Send data update every 100ms
|
||||||
Serial.println(F("magnetometer rotation enabled"));
|
Serial.println(F("magnetometer rotation enabled"));
|
||||||
|
myIMU.enableStepCounter(500); //Send data update every 500ms
|
||||||
}
|
}
|
||||||
//get sensordata
|
//get sensordata
|
||||||
SensorManager::RotationQuintillions SensorManager::getQuintillions() {
|
SensorManager::RotationQuintillions SensorManager::getQuintillions() {
|
||||||
@@ -39,7 +39,7 @@ SensorManager::RotationQuintillions SensorManager::getQuintillions() {
|
|||||||
return rotation;
|
return rotation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//calculate Quintillions to Euler angles from -1π to +1π
|
//calculate Quintillions to Euler angles from -1π to +1π
|
||||||
SensorManager::eulerAngles SensorManager::getEulerAngles() {
|
SensorManager::eulerAngles SensorManager::getEulerAngles() {
|
||||||
SensorManager::RotationQuintillions rotation = getQuintillions();
|
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));
|
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));
|
||||||
@@ -48,11 +48,24 @@ SensorManager::eulerAngles SensorManager::getEulerAngles() {
|
|||||||
eulerAngles EulerAngles = { roll, pitch, yaw };
|
eulerAngles EulerAngles = { roll, pitch, yaw };
|
||||||
return EulerAngles;
|
return EulerAngles;
|
||||||
}
|
}
|
||||||
SensorManager::acceleration SensorManager::getAcelleration(){
|
SensorManager::acceleration SensorManager::getAcelleration() {
|
||||||
float x = myIMU.getAccelX();
|
float x = myIMU.getAccelX();
|
||||||
float y = myIMU.getAccelY();
|
float y = myIMU.getAccelY();
|
||||||
float z = myIMU.getAccelZ();
|
float z = myIMU.getAccelZ();
|
||||||
acceleration Acceleration = { x, y, z };
|
acceleration Acceleration = { x, y, z };
|
||||||
|
|
||||||
return Acceleration;
|
return Acceleration;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SensorManager::sensorTap() {
|
||||||
|
int taps = 0;
|
||||||
|
if (myIMU.dataAvailable() == true) {
|
||||||
|
int taps = myIMU.getStepCount();
|
||||||
|
}
|
||||||
|
if (taps) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -21,7 +21,7 @@ public:
|
|||||||
|
|
||||||
eulerAngles getEulerAngles();
|
eulerAngles getEulerAngles();
|
||||||
acceleration getAcelleration();
|
acceleration getAcelleration();
|
||||||
|
bool sensorTap();
|
||||||
private:
|
private:
|
||||||
struct RotationQuintillions {
|
struct RotationQuintillions {
|
||||||
float i;
|
float i;
|
||||||
|
Reference in New Issue
Block a user