-
Notifications
You must be signed in to change notification settings - Fork 0
/
ML_crash_thresholds.ino
108 lines (94 loc) · 2.37 KB
/
ML_crash_thresholds.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//Gautam Banuru and JJ Hsu CS 190 12/8/18
//This file was used for data analytics. We collected the gyroscope sensor readings when we crashed our bike
//10 times and used the average of those readings to set our crash detection thresholds.
#include <SparkFunLSM6DS3.h>
#include "Wire.h"
#include "SPI.h"
//unsigned long time;
LSM6DS3 myIMU; //Default constructor is I2C, addr 0x6B
const int brakelight = 12;
// Raw Ranges:
// initialize to mid-range and allow calibration to
// find the minimum and maximum for each axis
int xRawMin = 512;
int xRawMax = 512;
int yRawMin = 512;
int yRawMax = 512;
int zRawMin = 512;
int zRawMax = 512;
int GxRawMin = 512;
int GxRawMax = 512;
int GyRawMin = 512;
int GyRawMax = 512;
int GzRawMin = 512;
int GzRawMax = 512;
int gx;
int gy;
int gz;
int ax;
int ay;
int az;
float gxdsum = 0;
float gydsum = 0;
float gzdsum = 0;
float axdsum = 0;
float aydsum = 0;
float azdsum = 0;
int calibrate_count = 0;
// Take multiple samples to reduce noise
const int sampleSize = 10;
bool okaytorun = true;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(1000); //relax...
Serial.println("Processor came out of reset.\n");
myIMU.begin();
}
void loop() {
// put your main code here, to run repeatedly:
if (okaytorun == true)
{
collectfall();
Serial.println("Ran Collect Fall");
delay(1000);
okaytorun = false;
}
}
// Read "sampleSize" samples and report the average
int average (int * array, int len) // assuming array is int.
{
long sum = 0; // sum will be larger than an item, long for safety.
for (int i = 0 ; i < len ; i++)
sum += array[i] ;
return sum / len ; // average will be fractional, so float may be appropriate.
}
void collectfall()
{
Serial.println("Start Collect Data");
delay(1000);
for (int num = 0; num < 10; num++)
{
Serial.print("Data Trial ");
Serial.println(num + 1);
Serial.print("Read in 3..");
delay(1000);
Serial.print("2..");
delay(1000);
Serial.println("1");
delay(500);
gx = myIMU.readFloatGyroX();
gy = myIMU.readFloatGyroY();
gz = myIMU.readFloatGyroZ();
gxdsum += gx;
gydsum += gy;
gzdsum += gz;
delay(500);
}
Serial.print("Fall Avg. GX GY GZ: ");
Serial.print(gxdsum/10.0);
Serial.print(" ");
Serial.print(gydsum/10.0);
Serial.print(" ");
Serial.println(gzdsum/10.0);
}