-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOOL_PLC.ino
79 lines (65 loc) · 2.27 KB
/
POOL_PLC.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <SPI.h>
#include <Controllino.h>
#include <stlport.h>
#include <Eigen30.h>
#include <Eigen/Geometry>
#include <Eigen/Core>
#include "AccelerometerFiltering.h"
#include "AccelerometerModel.h"
#include "ADXL355.h"
#include "HighestCornerAlgorithm.h"
#define STATUS_FLASH_PIN CONTROLLINO_D0
#define ZERO_BUTTON CONTROLLINO_A0
#define OUT_TR CONTROLLINO_D16
#define OUT_TL CONTROLLINO_D17
#define OUT_BL CONTROLLINO_D18
#define OUT_BR CONTROLLINO_D19
ADXL355 accel(CONTROLLINO_D4);
AccelerometerFilterMovingAverage ewmaFilter(0, 0.25);
AccelerometerModel accelModel;
HighestCornerAlgo cornerAlgo(2.5 / 180.0 * PI, 5.0 / 180.0 * PI);
void setup() {
// put your setup code here, to run once:
pinMode(STATUS_FLASH_PIN, OUTPUT);
pinMode(OUT_TR, OUTPUT);
pinMode(OUT_BR, OUTPUT);
pinMode(OUT_TL, OUTPUT);
pinMode(OUT_BL, OUTPUT);
pinMode(ZERO_BUTTON, INPUT);
digitalWrite(STATUS_FLASH_PIN, HIGH);
SPI.begin();
Serial.begin(9600);
while (!accel.begin(ADXL355_RANGE_2G, ADXL355_FILTER_LPF_LOW_FREQ)) {
digitalWrite(STATUS_FLASH_PIN, LOW);
delay(100);
digitalWrite(STATUS_FLASH_PIN, HIGH);
delay(100);
Serial.println("ADXL355 Failed to initialize");
}
Serial.println("ADXL355 Initialized");
delay(200);
//Allows a preconfigured offset (useful for YAW)
//All rotations are in Roll-Pitch-Yaw (Left to right).
accelModel.setBaseFrameAnglesRadians(Eigen::Vector3d(0,0,0));
}
void loop() {
// put your main code here, to run repeatedly:
accel.takeSample();
ADXL355Measurement measure = accel.getSample();
ewmaFilter.addData(measure);
measure = ewmaFilter.getAverage();
Eigen::Vector2d angles = accelModel.calculate(Eigen::Vector3d(measure.x, measure.y, measure.z));
Serial.print(angles[0] * 180.0 / PI);
Serial.print("\t");
Serial.print(angles[1] * 180.0 / PI);
Serial.println();
cornerAlgo.update(angles[0], angles[1]);
digitalWrite(OUT_TR, cornerAlgo.getCorner(0));
digitalWrite(OUT_TL, cornerAlgo.getCorner(1));
digitalWrite(OUT_BL, cornerAlgo.getCorner(2));
digitalWrite(OUT_BR, cornerAlgo.getCorner(3));
if (!digitalRead(ZERO_BUTTON)) {
//accelModel.setMeasurementAsZero(Eigen::Vector3d(measure.x, measure.y, measure.z));
}
delay(50);
}