-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNavAssistForVisuallyImpaired.ino
196 lines (179 loc) · 4.81 KB
/
NavAssistForVisuallyImpaired.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
#include <Servo.h>
//Global Servo Init
Servo motorA;
Servo motorB;
#define servoPinA 9
#define servoPinB 10
//Global Ultrasonic Sensor Init
#define trigA 2
#define echoA 3
#define trigB 5
#define echoB 6
// Constrain Variables
const int readingPerSensor = 5;
const int minRotation = 3; //Minimum Servo Rotation Quantum
const int servoDelay = 9; //Delay before updating servo
// Value Variables
int readingArr[2][readingPerSensor]; // Data Matrix
int finalReading[2]; // Smoothed Single Data from each sensor
int lastRead = 0; // Pointer for Array
int rotation[2]; // Servo Rotation
int minSensor = 200;
int maxSensor = 20000;
int servoPressure[2] = {0,160};
int servoRelease[2] = {70,110};
int servoLoc[2];
void setup()
{
//Caliberation Pending after glove is made.
}
/**********************************************************
Function Name: takeReading
Purpose: Takes Raw Data from Sensor and scales it to 0-100%
***********************************************************/
int takeReading(int sensor)
{
long duration;
int reading;
if(sensor==1)
{
digitalWrite(trigA,HIGH);
delayMicroseconds(1000);
digitalWrite(trigA,LOW);
duration = pulseIn(echoA,HIGH);
duration = constrain(duration, minSensor,maxSensor);
reading = map(duration, minSensor,maxSensor, 0,100);
return reading;
}
else if(sensor==2)
{
digitalWrite(trigB,HIGH);
delayMicroseconds(1000);
digitalWrite(trigB,LOW);
duration = pulseIn(echoB,HIGH);
duration = constrain(duration,minSensor,maxSensor);
reading = map(duration, minSensor, maxSensor, 0,100);
return reading;
}
}
/************************************************************
Function Name : calcDist
Purpose: Smooths out using Weighted Average
************************************************************/
int calcDist(int sensorNum)
{
int readOut= maxSensor;
float weightFactor = 0.5;
float flickerFactor = 30;
if(lastRead >= readingPerSensor-1)
{
int total =0;
float currentWeight =1;
float totalPercent = 0;
boolean flickered = false;
for(int i=readingPerSensor-1; i>=0;i--)
{
flickered = false;
if(i==readingPerSensor -1)
{
if((abs(readingArr[sensorNum][i]) - abs(readingArr[sensorNum][i-1]) > flickerFactor) &&
(abs(readingArr[sensorNum][i-1]) - abs(readingArr[sensorNum][i-2]) > flickerFactor))
{
flickered = true;
}
}
if(flickered == false)
{
total +=(readingArr[sensorNum][i] * currentWeight);
totalPercent += currentWeight;
currentWeight *= weightFactor;
}
}
readOut = total/totalPercent;
}
return readOut;
}
/**************************************************************
Function Name: Loop
Purpose: Main function adjusts the servo angle reading the data
**************************************************************/
void loop()
{
unsigned long delayTime;
int oldLoc;
for(int i=0;i<2;i++)
{
readingArr[i][lastRead]= takeReading(i); // Get reading from sensor
finalReading[i] = calcDist(i);
oldLoc = servoLoc[i]; //Correct the Serov Angle
servoLoc[i] = map(finalReading[i], 0,100,servoPressure[i],servoRelease[i]);
if(lastRead >= readingPerSensor - 1)
{
if(abs(servoLoc[i] - oldLoc) >= minRotation)
{
if(i==0)
{
motorA.attach(servoPinA);
delay(10);
motorA.write(servoLoc[i]);
delayTime = (servoDelay *(abs(servoLoc[i] - oldLoc))+20); //Delay for Motor noise
if(abs(delayTime)>500)
{
delayTime=500;
}
delay(delayTime);
motorA.detach();
}
else if(i==1)
{
motorB.attach(servoPinB);
delay(10);
motorB.write(servoLoc[i]);
delayTime = (servoDelay*(abs(servoLoc[i] - oldLoc))+20);
if(abs(delayTime>500))
{
delayTime=500;
}
delay(delayTime);
motorB.detach();
}
}
else
{
if(i==0)
{
motorA.attach(servoPinA);
delay(10);
motorA.write(oldLoc);
delay(50);
motorA.detach();
servoLoc[i]=oldLoc;
}
else if(i==1)
{
motorB.attach(servoPinB);
delay(10);
motorB.write(oldLoc);
delay(50);
motorB.detach();
servoLoc[i] = oldLoc;
}
}
}
delay(20); // Noise Fix
}
lastRead = lastRead+1;
if(lastRead >= readingPerSensor)
{
lastRead = readingPerSensor -1;
//Pop Old Reading
for(int i=0;i<2;i++)
{
for(int j=0; j<4; j++)
{
readingArr[i][j] = readingArr[i][j+1];
}
}
}
}
//****************************************************