Browse Source

fix bugs

master
gepebdevelopers 5 years ago
parent
commit
59f5d1068c
  1. 98
      IMU_tracking/IMU_tracking.ino
  2. 65
      iot/api/insert.php
  3. 5
      iot/index.html
  4. 81
      iot/sketch-2D.js

98
IMU_tracking/IMU_tracking.ino

@ -1,98 +0,0 @@
#include <MPU9250_asukiaaa.h>
#include <ArduinoJson.h>
#ifdef _ESP32_HAL_I2C_H_
#define SDA_PIN 4
#define SCL_PIN 5
#endif
MPU9250_asukiaaa mySensor;
float aX, aY, aZ, aSqrt, gX, gY, gZ, mDirection, mX, mY, mZ;
String magnometer;
String accelarator;
String gyroscope;
String sensorID;
void setup() {
Serial.begin(9600);
while(!Serial);
Serial.println("started");
#ifdef _ESP32_HAL_I2C_H_ // For ESP32
Wire.begin(SDA_PIN, SCL_PIN);
mySensor.setWire(&Wire);
#endif
mySensor.beginAccel();
mySensor.beginGyro();
mySensor.beginMag();
// You can set your own offset for mag values
// mySensor.magXOffset = -50;
// mySensor.magYOffset = -55;
// mySensor.magZOffset = -10;
}
void loop() {
StaticJsonDocument<200> ID;
StaticJsonDocument<200> accelarator;
StaticJsonDocument<200> gyroscope;
StaticJsonDocument<200> magnometer;
uint8_t sensorId;
if (mySensor.readId(&sensorId) == 0) {
ID["Sensor"] = "=== 10 DOF IMU Sensor V2 ===";
ID["ID"] = sensorId;
//serializeJson(ID, Serial);
// Serial.println("sensorId: " + String(sensorId));
} else {
Serial.write("Cannot read sensorId");
}
if (mySensor.accelUpdate() == 0) {
aX = mySensor.accelX();
aY = mySensor.accelY();
aZ = mySensor.accelZ();
aSqrt = mySensor.accelSqrt();
accelarator["sensor"] = "accelarator";
accelarator["X"] = aX;
accelarator["Y"] = aY;
accelarator["Z"] = aZ;
//serializeJsonPretty(accelarator, Serial);
} else {
Serial.write("Cannod read accel values");
}
if (mySensor.gyroUpdate() == 0) {
gX = mySensor.gyroX();
gY = mySensor.gyroY();
gZ = mySensor.gyroZ();
gyroscope["sensor"] = "gyroscope";
gyroscope["X"] = gX;
gyroscope["Y"] = gY;
gyroscope["Z"] = gZ;
//serializeJsonPretty(gyroscope, Serial);
} else {
//Serial.println("Cannot read gyro values");
Serial.write("Cannot read gyro values");
}
if (mySensor.magUpdate() == 0) {
mX = mySensor.magX();
mY = mySensor.magY();
mZ = mySensor.magZ();
mDirection = mySensor.magHorizDirection();
magnometer["sensor"] = "magnometer";
magnometer["X"] = gX;
magnometer["Y"] = gY;
magnometer["Z"] = gZ;
magnometer["Direction"] = mDirection;
serializeJsonPretty(magnometer, Serial);
} else {
Serial.write("Cannot read mag values");
}
//Serial.write("Hello World!");
delay(2000);
}

65
iot/api/insert.php

@ -1,65 +0,0 @@
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tracking_2d";
$tableName = "records";
$dataFileName = "../data/results2.txt";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// $fn = fopen("myfile.txt", "r");
// $result = fgets($fn);
// echo $result;
// fclose($fn);
$handle = fopen($dataFileName, "r");
if ($handle) {
$text="";
$counter = 0;
while (($line = fgets($handle)) !== false) {
// process the line read.
if (substr($line, 0, 1) === '[') { //Remove the first '['
$line = substr($line, 1);
}
if (substr($line, -1, 1) === ']') { //Remove the last ']'
$line = substr($line, 0, -1);
}
if (substr($line, -3, 1) === ',') { //Remove the ',\n'
$line = substr($line, 0, -3);
}
//insert record in database
$sql = "INSERT INTO " . $tableName . " (data, test_name) VALUES ('".$line."', 'TEST_1')";
if ($conn->query($sql) === TRUE) {
$text = $text."Line ".$counter."added\n";
} else {
$text = $text . "Line " . $counter . "NOT added\n";
}
$counter++;
}
fclose($handle);
echo $text;
} else {
echo "Error opening file";
}
// if ($conn->query($sql) === TRUE) {
// echo "New record created successfully";
// } else {
// echo "Error: " . $sql . "<br>" . $conn->error;
// }
$conn->close();

5
iot/index.html

@ -1,10 +1,13 @@
<html>
<head>
<script src="p5.min.js"></script>
<script src="sketch-test2D.js"></script>
<script src="sketch-2D.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="Helper.js"></script>
<style>
body{
background-color: #d0d0d0;
}
.wrapper{
padding: 10px;
}

81
iot/sketch-test2D.js → iot/sketch-2D.js

@ -1,11 +1,3 @@
/*
* rootApostolos
* P5js example
* IMU 2D
*
*
*
*/
var dataObjects = [];
var objectIndex = 0;
var debugFlag = true;
@ -37,7 +29,6 @@ var coordinatesTranformationStep = 300;
var endFlagMessage = true;
// Put any asynchronous data loading in preload to complete before "setup" is run
function preload() {
// data = loadJSON(file);
}
function setup() {
@ -57,16 +48,12 @@ function setup() {
endFlagMessage = true;
transformJsonDataToObject();
createCanvas(canvasX, canvasY);
// createCanvas(canvasX, canvasY, WEBGL);
// camera(450, 300, 0);
frameRate(10); //This must the same as collect data frequency
}
function draw() {
if(start){
// move();
move2();
move();
background(100, 100, 100);
@ -74,7 +61,6 @@ function draw() {
if (bubbles[bubbles.length - 1].x > canvasX){
xTranform -= coordinatesTranformationStep;
for (var i = 0; i < bubbles.length; i++) {
// bubbles[i].x += xTranform;
bubbles[i].x -= coordinatesTranformationStep;
}
}
@ -82,7 +68,6 @@ function draw() {
if (bubbles[bubbles.length - 1].x < 0){
xTranform += coordinatesTranformationStep;
for (var i = 0; i < bubbles.length; i++) {
// bubbles[i].x += xTranform;
bubbles[i].x += coordinatesTranformationStep;
}
}
@ -110,15 +95,9 @@ function draw() {
}
}
//============= 2 END ==================
}
}
// Bubble class
class Bubble {
constructor(x, y, diameter) {
@ -141,13 +120,8 @@ class Bubble {
line(xx1, yy1, xx2, yy2);
xx1 = xx2;
yy1 = yy2;
//https://el.wikipedia.org/wiki/%CE%91%CE%BA%CF%84%CE%AF%CE%BD%CE%B9%CE%BF_(%CE%BC%CE%BF%CE%BD%CE%AC%CE%B4%CE%B1_%CE%BC%CE%AD%CF%84%CF%81%CE%B7%CF%83%CE%B7%CF%82)
// https://stackoverflow.com/questions/12959237/get-point-coordinates-based-on-direction-and-distance-vector
// ihttps://en.wikipedia.org/wiki/Euler_angles#Geometrical_definition
//angleMode(DEGREES);
angleMode(RADIANS);
v.rotate(Math.PI - anglesX);
//v.rotate(XXX);
stroke(255, 0, 0);
line(xx1, yy1, xx1 + v.x, yy1 + v.y);
ellipse(this.x, this.y, this.diameter, this.diameter);
@ -155,49 +129,29 @@ class Bubble {
}
}
function move2(){
function move(){
if(objectIndex < dataObjects.length){
var diameter = 5;
//calculate DX, DY and DZ
let aX = dataObjects[objectIndex].accelerator.X * G;
let aY = dataObjects[objectIndex].accelerator.Y * G;
let aZ = dataObjects[objectIndex].accelerator.Z * G;
AX=AX+aX*dt; // new position X
AY=AY+aY*dt; // new position Y
let gX = dataObjects[objectIndex].gyroscope.X * dt;
let gY = dataObjects[objectIndex].gyroscope.Y * dt;
let gZ = dataObjects[objectIndex].gyroscope.Z * dt;
anglesZ = Math.acos((-gY)/(Math.pow(1-Math.pow(gZ,2),0.5)));
//peristrofi ston x axona mikos
anglesX=acos(gZ);
//peristrofi ston y axona
anglesY=acos(gX/(Math.pow(1-pow(gZ,2),0.5)));
//========================== WHAT IS THAT??? ======================
// // {"sensor":"magnometer","X":-0.000647,"Y":-0.000583,"Z":0.000652}
// var iii=i+2
// mX = dataObjects[objectIndex].magnometer.X;
// mY = dataObjects[objectIndex].magnometer.Y;
// mZ = dataObjects[objectIndex].magnometer.Z;
// sensor = data[iii].sensor;
let aX = dataObjects[objectIndex].accelerator.X * G;
let aY = dataObjects[objectIndex].accelerator.Y * G;
let aZ = dataObjects[objectIndex].accelerator.Z * G;
AX=AX+aX*dt; // new position X
AY=AY+aY*dt; // new position Y
// var anglesXX = anglesX * 180 / Math.PI;
let gX = dataObjects[objectIndex].gyroscope.X * dt;
let gY = dataObjects[objectIndex].gyroscope.Y * dt;
let gZ = dataObjects[objectIndex].gyroscope.Z * dt;
// BX=BX+aX*3;
// BY=BY+aY*3;
// CX=CX+aX*3;
// CY=CY+aY*3;
//=================================================================
anglesZ = Math.acos((-gY)/(Math.pow(1-Math.pow(gZ,2),0.5)));
//peristrofi ston x axona mikos
anglesX=acos(gZ);
//peristrofi ston y axona
anglesY=acos(gX/(Math.pow(1-pow(gZ,2),0.5)));
currentBubble = new Bubble(AX + xTranform, AY + yTranform, diameter);
bubbles.push(currentBubble);
@ -243,11 +197,6 @@ class SensorData{
}
}
function moveWindowRight(xMove){
for (var i = 0; i < bubbles.length; i++) {
bubbles[i].x = bubbles[i].x - xMove;
}
}
function debug(msg){
console.log("============ DEBUG ===============");
console.log(msg);
Loading…
Cancel
Save