-
Notifications
You must be signed in to change notification settings - Fork 0
/
Three_sensors_3LEDs.ino
174 lines (138 loc) · 4.14 KB
/
Three_sensors_3LEDs.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
/*
Maxbotix simple test
Instructions:
- At least one of: (comment the appropriate code below)
PW is digital pin 8
TX is digital pin 6
AN is analog pin A0
- Change code below according to your model (LV, XL and
HRLV supported)
Note:
For convenience, the getRange method will always return centimeters.
You can use convert fuctions to convert to another unit (toInches and
toCentimeters are available)
*/
#include "Maxbotix.h"
/*
#define USE_TEENSY_HW_SERIAL
#include <avr/pgmspace.h>
#include <ros.h>
#include <std_msgs/Float32.h>
*/
Maxbotix rangeSensorPWL(7, Maxbotix::PW, Maxbotix::HRLV);
Maxbotix rangeSensorPWR(8, Maxbotix::PW, Maxbotix::HRLV);
Maxbotix rangeSensorPWC(6, Maxbotix::PW, Maxbotix::HRLV);
const int ledPINR = 9;
const int ledPINL = 10;
const int ledPINC = 11;
const float epsilon = 15;
unsigned long pinR = 30, pinL = 30, pinC = 30;
const int N = 10;
const int center_distance_threshold = 31;
void setup()
{
Serial.begin(9600);
pinMode(ledPINR , OUTPUT);
pinMode(ledPINL , OUTPUT);
pinMode(ledPINC , OUTPUT);
}
void loop()
{
unsigned long start;
int measurment_listL[N];
int measurment_listR[N];
int measurment_listC[N];
int xL = 0, xR = 0, xC = 0;
Serial.println("/nReading...");
pinL = rangeSensorPWL.getRange();
pinR = rangeSensorPWR.getRange();
pinC = rangeSensorPWC.getRange();
measurment_listL[0] = pinL;
measurment_listR[0] = pinR;
measurment_listC[0] = pinC;
delay(10);
double sumL = pinL;
double varL = 0;
double avgL = 0;
double sum_square_errorL = 0;
double measurementL = 0;
double sumR = pinR;
double varR = 0;
double avgR = 0;
double sum_square_errorR = 0;
double measurementR = 0;
double sumC = pinC;
double varC = 0;
double avgC = 0;
double sum_square_errorC = 0;
double measurementC = 0;
for (int i = 1; i < N; i++) {
start = millis();
measurementL = rangeSensorPWL.getRange();
sumL += measurementL;
measurment_listL[i] = measurementL;
measurementR = rangeSensorPWR.getRange();
sumR += measurementR;
measurment_listR[i] = measurementR;
measurementC = rangeSensorPWC.getRange();
sumC += measurementC;
measurment_listC[i] = measurementC;
Serial.print(millis()-start);
Serial.println();
delay(1);
}
avgL = sumL / N;
avgR = sumR / N;
avgC = sumC / N;
for (int v = 0; v < N; v++) {
xL = measurment_listL[v];
sum_square_errorL += (xL - avgL) * (xL - avgL);
xR = measurment_listR[v];
sum_square_errorR += (xR - avgR) * (xR - avgR);
xC = measurment_listC[v];
sum_square_errorC += (xC - avgC) * (xC - avgC);
}
varR = sum_square_errorR / N;
varL = sum_square_errorL / N;
varC = sum_square_errorC / N;
Serial.print("\nAvgR: ");
Serial.print(avgR);
Serial.print("\nvarR: ");
Serial.print(varR);
Serial.print("\nAvgL: ");
Serial.print(avgL);
Serial.print("\nvarL: ");
Serial.print(varL);
Serial.print("\nAvgC: ");
Serial.print(avgC);
Serial.print("\nvarC: ");
Serial.print(varC);
if (avgL < (avgR - epsilon)) {
//turn right light on
digitalWrite(ledPINR , HIGH); //Pin in HIGH
digitalWrite(ledPINL , LOW); //Pin in LOW
digitalWrite(ledPINC , HIGH); // Pin in HIGH
Serial.println("/nright...");
Serial.print(start);
}
else if ((avgL - epsilon) > avgR) {
//turn left light on and central light
digitalWrite(ledPINL , HIGH); //Pin in HIGH
digitalWrite(ledPINR , LOW); //Pin in LOW
digitalWrite(ledPINC , HIGH); // Pin in HIGH
Serial.println("/nleft...");
}
else {
//turn left light on
digitalWrite(ledPINL , LOW); //Pin in LOW
digitalWrite(ledPINR , LOW); //Pin in LOW
digitalWrite(ledPINC , HIGH); // Pin in HIGH
Serial.println("/ncentral..");
}
if (avgC < center_distance_threshold) {
//turn center light on
digitalWrite(ledPINC , LOW); // Pin in HIGH
digitalWrite(ledPINL , LOW); //Pin en LOW
digitalWrite(ledPINR , LOW); // Pin en LOW
}
}