-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesting.ino
292 lines (222 loc) · 8.19 KB
/
testing.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* webSerial for testing javascript connection with an arduino
*
* Latest work at https://github.com/hpssjellis/webMLserial
*
* Note: On the Arduino Serial monitor make sure no line-ending or if statements will not work
*
* Android https://hpssjellis.github.io/web-serial-polyfill/index.html
* Laptops Desktops https://hpssjellis.github.io/my-examples-of-arduino-webUSB-webSerial/public/index.html
* IOS not really sure
*
* RocksettaTinyML by Jeremy Ellis,[email protected]
*
* Based on the Arduino Library
* EloquentTinyML by Simone Salerno,[email protected]
* https://github.com/eloquentarduino/EloquentTinyML
*
* Passed from the main program
* #define NUMBER_OF_INPUTS 8
* #define TYPE_OF_INPUTS int32 // uint8_t | int32 | float | bool | double | String
*
* #define NUMBER_OF_OUTPUTS 4
* #define TYPE_OF_OUTPUTS float // uint8_t |int32 | float | bool | double | String
*
* #define RETURN_TYPE_FROM_PREDICT float // void | uint8_t | int32 | float | bool | double | String
*
*/
#include <RocksettaTinyML.h>
//#include "proximityModel.h"
#include "niclaMotion.h"
#define NUMBER_OF_INPUTS 75 // 25 x 3
#define NUMBER_OF_OUTPUTS 3
#define RETURN_TYPE_FROM_PREDICT float
#define TENSOR_ARENA_SIZE 8*1024
Eloquent::TinyML::TfLite<NUMBER_OF_INPUTS, NUMBER_OF_OUTPUTS, TENSOR_ARENA_SIZE> ml;
int myCurrentIndex = 0;
float myInput[NUMBER_OF_INPUTS];
float myOutput[NUMBER_OF_OUTPUTS];
bool myPredictProximity = false;
bool myPredictMotion = false;
#include <Arduino.h> // Only needed for https://platformio.org/
#include <Arduino_LSM9DS1.h>
//#include <Arduino_LSM6DSOX.h>
//#include "VL53L1X.h"
// GLOBAL VAERIABLES
//VL53L1X proximity;
#define FREQUENCY_HZ 25 // how many samples per second
#define COLLECTION_SECONDS 1 // how many seconds to collect samples
#define INTERVAL_MS (1000 / (FREQUENCY_HZ + 1)) // need for the timer
#define CONVERT_G_TO_MS2 9.80665f // accleration conversion
int myMaxData = FREQUENCY_HZ * COLLECTION_SECONDS;
int myCount = 0;
int myDelay = INTERVAL_MS; // non-block delay in milliseconds
unsigned long myStart;
String readString;
bool mySendData = true;
bool mySendProximity = false;
int myProximityReading = 0;
void setup() {
ml.begin(model_tflite); // Not the name of the header file, name of the unsigned char Array
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // onboard LED, HIGH = off
//while (!Serial) {} // do nothing and wait
myStart = millis(); // set delay
randomSeed(analogRead(A0)); // AO pin on XIAO does not have to be connected to anything
Serial.println("accX,accY,accZ"); // CSV file heading titles
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
// Initilize the Proximity sensor and com using wire1
Wire1.begin();
Wire1.setClock(400000); // use 400 kHz I2C
/*
proximity.setBus(&Wire1);
if (!proximity.init()) {
Serial.println("Failed to detect and initialize sensor!");
while (1);
}
proximity.setDistanceMode(VL53L1X::Long);
proximity.setMeasurementTimingBudget(10000);
proximity.startContinuous(10);
*/
// initialize buffer to zeros
for (int i = 0; i < NUMBER_OF_INPUTS; i++) {
myInput[i] = 0.0;
}
}
void loop() {
float x, y, z;
// myProximityReading = proximity.read();
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
x *= 1.00; //9.81;
y *= 1.00; //9.81;
z *= 1.00; //9.81;
}
myInput[myCurrentIndex] = x;
myInput[myCurrentIndex+1] = y;
myInput[myCurrentIndex+2] = z;
myCurrentIndex = (myCurrentIndex + 3) % NUMBER_OF_INPUTS; // Wrap around to the beginning when the end is reached.
if ( (millis() - myStart ) >= myDelay) {
myStart = millis(); // reset the delay time
myCount += 1;
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
x *= 1.00; //9.81;
y *= 1.00; //9.81;
z *= 1.00; //9.81;
}
if (myCount >= myMaxData){
mySendData = false; // stop sending data when amount reached
}
if (myPredictMotion) {
myCurrentIndex = (myCurrentIndex + 3) % NUMBER_OF_INPUTS; // Wrap around to the beginning when the end is reached.
if (myCurrentIndex % NUMBER_OF_INPUTS == 0) { // slows down showing the results
float myPredict = ml.predict(myInput, myOutput); // make the proximity prediction on the last 25 samples
Serial.print("Predicted: "+String(myPredict) ); // output with minimal formating
// Serial.println(String(ml.output[0]) );
// Serial.println("--" );
Serial.println(", same as Label[0]:"+String(myOutput[0]) + ", label[1]:" + String(myOutput[1]) + ", label[2]:" + String(myOutput[2]) ); // output for plotter minimal formating
if (myPredict > 0.50) {
digitalWrite(LED_BUILTIN, LOW); // on for portenta, off for Nano33BleSense
}
else {
digitalWrite(LED_BUILTIN, HIGH);
}
delay(40); // just to slow it a bit
}
}
/*
if (myPredictProximity) {
myCurrentIndex = (myCurrentIndex + 1) % NUMBER_OF_INPUTS; // Wrap around to the beginning when the end is reached.
myInput[myCurrentIndex] = myProximityReading;
// First print all the inputs to the model
// for (int myLoop2 = 0; myLoop2 < NUMBER_OF_INPUTS; myLoop2++){
// Serial.print(String(myInput[myLoop2]) + ", ");
// }
// Serial.println();
// Serial.print("Predicted: " + String(myPredicted,3) ); // not needed since same as myOutput[0]
*/
if (myCurrentIndex == 0) { // only send data to the model every 25 readings
float myPredicted = ml.predict(myInput, myOutput);
Serial.println("Proximity: " + String(myProximityReading) +", Far label[0]: "+String(myOutput[0]) + ", Near label[1]:" + String(myOutput[1]) ); // output for plotter minimal formating
if (myPredicted >= 0.5){ // same as the first myOutput[0]
digitalWrite(LED_BUILTIN, LOW); // grounds the LED turns it on only on portenta, off for the nano 33 BLE
} else {
digitalWrite(LED_BUILTIN, HIGH); // turns it off only on portenta and on for the nano 33 BLE
}
}
delay(10);
}
// This should be written better perhaps a case statement etc
if (mySendData){
if (mySendProximity){
Serial.println(String(myProximityReading));
} else {
Serial.println( String(x) + "," + String(y) + "," + String(z) );
}
}
}
while (Serial.available()) {
delay(3);
char myChar = Serial.read();
readString += myChar;
}
if (readString.length() > 0) {
readString.trim(); // get rid of last weird character
if (readString == "a"){
digitalWrite(LED_BUILTIN, LOW); //onboard LED LOW = on
}
if (readString == "b"){
digitalWrite(LED_BUILTIN, HIGH);
}
if (readString == "start"){
myPredictProximity = false;
mySendProximity = false;
myPredictMotion = false;
mySendData = true;
myStart = millis(); // reset the delay time
myCount = 0;
}
if (readString == "stop"){
myPredictProximity = false;
myPredictMotion = false;
mySendData = false;
Serial.println("Stopping at count: "+ String(myCount)); // CSV file heading titles
}
if (readString == "firstline"){
myPredictProximity = false;
if (mySendProximity){
Serial.println("proximity");
} else {
Serial.println("accX,accY,accZ"); // CSV file heading titles
}
}
/*
if (readString == "proximity"){
myPredictProximity = false;
myPredictMotion = false;
mySendProximity = true;
mySendData = true;
myStart = millis(); // reset the delay time
myCount = 0;
}
if (readString == "PredictProximity"){
myPredictProximity = true;
myPredictMotion = false;
mySendProximity = false;
mySendData = false;
}
if (readString == "PredictMotion"){
myPredictMotion = true;
myPredictProximity = false;
mySendProximity = false;
mySendData = false;
}
*/
readString="";
}
}