-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensores.cpp
More file actions
286 lines (251 loc) · 7.07 KB
/
Sensores.cpp
File metadata and controls
286 lines (251 loc) · 7.07 KB
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
/*
* Name: Sensores.cpp
* Description: Defines the sensor functions.
*
* Sensor configurations
*
*
* Sensor positions relative to its default orientation:
*
* Front
* 1
* -
* Left 4 | | 2 Right
* -
* 3
* Back
*
*/
#include "Arduino.h"
#include "Config.h"
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
#include <SparkFunLSM9DS1.h>
LSM9DS1 imu;
#define LSM9DS1_M 0x1E // Would be 0x1C if SDO_M is LOW
#define LSM9DS1_AG 0x6B // Would be 0x6A if SDO_AG is LOW
#define DECLINATION -8.58 // Declination (degrees) in Boulder, CO.
#define unused 255 // Non-existant pin # for SoftwareSerial
#define colorPinSensorBaud 4800
#define waitDelay 200
// Received RGB values from ColorPAL
int red;
int grn;
int blu;
// Set up two software serials on the same pin.
SoftwareSerial serin(colorPinSensor, unused);
SoftwareSerial serout(unused, colorPinSensor);
// Reset ColorPAL; see ColorPAL documentation for sequence
void reset() {
delay(200);
pinMode(colorPinSensor, OUTPUT);
digitalWrite(colorPinSensor, LOW);
pinMode(colorPinSensor, INPUT);
while (digitalRead(colorPinSensor) != HIGH);
pinMode(colorPinSensor, OUTPUT);
digitalWrite(colorPinSensor, LOW);
delay(80);
pinMode(colorPinSensor, INPUT);
delay(waitDelay);
}
void setupGyro()
{
// [enabled] turns the gyro on or off.
imu.settings.gyro.enabled = true; // Enable the gyro
// [scale] sets the full-scale range of the gyroscope.
// scale can be set to either 245, 500, or 2000
imu.settings.gyro.scale = 245; // Set scale to +/-245dps
// [sampleRate] sets the output data rate (ODR) of the gyro
// sampleRate can be set between 1-6
// 1 = 14.9 4 = 238
// 2 = 59.5 5 = 476
// 3 = 119 6 = 952
imu.settings.gyro.sampleRate = 3; // 59.5Hz ODR
// [bandwidth] can set the cutoff frequency of the gyro.
// Allowed values: 0-3. Actual value of cutoff frequency
// depends on the sample rate. (Datasheet section 7.12)
imu.settings.gyro.bandwidth = 0;
// [lowPowerEnable] turns low-power mode on or off.
imu.settings.gyro.lowPowerEnable = false; // LP mode off
// [HPFEnable] enables or disables the high-pass filter
imu.settings.gyro.HPFEnable = true; // HPF disabled
// [HPFCutoff] sets the HPF cutoff frequency (if enabled)
// Allowable values are 0-9. Value depends on ODR.
// (Datasheet section 7.14)
imu.settings.gyro.HPFCutoff = 1; // HPF cutoff = 4Hz
// [flipX], [flipY], and [flipZ] are booleans that can
// automatically switch the positive/negative orientation
// of the three gyro axes.
imu.settings.gyro.flipX = false; // Don't flip X
imu.settings.gyro.flipY = false; // Don't flip Y
imu.settings.gyro.flipZ = false; // Don't flip Z
}
void sensorSetup() {
// Before initializing the IMU, there are a few settings
// we may need to adjust. Use the settings struct to set
// the device's communication mode and addresses:
imu.settings.device.commInterface = IMU_MODE_I2C;
imu.settings.device.mAddress = LSM9DS1_M;
imu.settings.device.agAddress = LSM9DS1_AG;
// The above lines will only take effect AFTER calling
// imu.begin(), which verifies communication with the IMU
// and turns it on.
if (!imu.begin())
{
Serial.println("Failed to communicate with LSM9DS1.");
Serial.println("Double-check wiring.");
Serial.println("Default settings in this sketch will " \
"work for an out of the box LSM9DS1 " \
"Breakout, but may need to be modified " \
"if the board jumpers are.");
while (1)
;
}
reset(); // Send reset to ColorPal
serout.begin(colorPinSensorBaud);
pinMode(colorPinSensor, OUTPUT);
serout.print("= (00 $ m) !"); // Loop print values, see ColorPAL documentation
serout.end(); // Discontinue serial port for transmitting
serin.begin(colorPinSensorBaud); // Set up serial port for receiving
pinMode(colorPinSensor, INPUT);
setupGyro();
}
// Parse the hex data into integers
void parseAndPrint(char * data) {
sscanf (data, "%3x%3x%3x", &red, &grn, &blu);
char buffer[32];
sprintf(buffer, "R%4.4d G%4.4d B%4.4d", red, grn, blu);
Serial.println(buffer);
}
void readData() {
char buffer[32];
if (serin.available() > 0) {
// Wait for a $ character, then read three 3 digit hex numbers
buffer[0] = serin.read();
if (buffer[0] == '$') {
for(int i = 0; i < 9; i++) {
while (serin.available() == 0); // Wait for next input character
buffer[i] = serin.read();
if (buffer[i] == '$') // Return early if $ character encountered
return;
}
parseAndPrint(buffer);
delay(10);
}
}
}
long getDistance(int iPos)
{
int trigPin = 0;
int echoPin = 0;
switch(iPos)
{
case 1:
trigPin = trigPinFront;
echoPin = echoPinFront;
break;
case 2:
trigPin = trigPinRight;
echoPin = echoPinRight;
break;
case 3:
trigPin = trigPinBack;
echoPin = echoPinBack;
break;
case 4:
trigPin = trigPinLeft;
echoPin = echoPinLeft;
break;
default:
trigPin = trigPinRight;
echoPin = echoPinRight;
break;
}
long lDuration = 0;
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
lDuration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
long lDistance = lDuration / 58.2;
if (lDistance>200)
{
lDistance = 200;
}
return lDistance;
}
int getWalls(int iPos)
{
int m = getDistance(iPos)/30;
return m;
}
bool isVictim(int iPos)
{
int pin = 0;
switch(iPos)
{
case 1:
pin = heatPinFront;
break;
case 2:
pin = heatPinRight;
break;
case 3:
pin = heatPinBack;
break;
case 4:
pin = heatPinLeft;
break;
default:
pin = heatPinBack;
break;
}
return digitalRead(pin);
}
bool isBlack()
{
readData();
if(blu > 200 && red > 200 && grn > 200)
{
return true;
}
return false;
}
int getButton()
{
int value = digitalRead(btnPin);
return value;
}
float orientationSensorHeading()
{
imu.readMag();
imu.readGyro();
float heading = 0;
if (imu.my == 0)
heading = (imu.mx < 0) ? 180.0 : 0;
else
heading = atan2(imu.mx, imu.my);
heading -= DECLINATION * PI / 180;
if (heading > PI) heading -= (2 * PI);
else if (heading < -PI) heading += (2 * PI);
else if (heading < 0) heading += 2 * PI;
// Convert everything from radians to degrees:
heading *= 180.0 / PI;
//return heading;
return imu.calcGyro(imu.gz);
}
float getAngle()
{
return orientationSensorHeading()/119;
}
float getAngleX()
{
imu.readGyro();
float fValue = imu.calcGyro(imu.gx);
return orientationSensorHeading()/119;
}