-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cansat_2019_Code.ino
331 lines (273 loc) · 8.09 KB
/
Cansat_2019_Code.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/***************************************************************
* Authors:
* Kevin Tom
* Supreeth L Y
* Sharanya Krishnamurthy
*
*
* The software is able to sample every sensor at 1Hz rate(1 sample per second) and relay it back to ground station. So at the ground station,
we get 1 sample each second(through Xbee).
*
* Contact : [email protected]
* Contact : [email protected]
***************************************************************/
//Libraries
#include <SoftwareSerial.h>
#include <DS3231.h>
#include <Wire.h>
#include <XBee.h>
#include <AltSoftSerial.h>
#include <TinyGPS++.h>
#include <Adafruit_BMP085.h>
#include <String.h>
//Objects
AltSoftSerial altSerial;
TinyGPSPlus gps;
Adafruit_BMP085 bmp;
DS3231 rtc(SDA,SCL);
SoftwareSerial XBee(2, 3); // RX, TX
#define NUM_SAMPLES 10
//Variables for data frame
String times;
String hh,mm,ss,packetcounts;
String st,sp,sa,lati,longi,alt,num_sat,hrs,mins,secs;
int na,np,nt,nlati,nlongi,nalt,n_numSat,nhrs,nmins,nsecs,ntimes,packetcount=0,packclen,npitch,nroll,nvolt,sai;
char char_array7[10],char_array8[10],char_array9[10],char_array10[10],char_array11[10],char_array12[10],char_array13[10],char_array14[10], char_array15[25], char_array16[25], char_array17[10];
// The ADXL345 sensor variables
int ADXL345 = 0x53;
float X_out, Y_out, Z_out,volte;
float roll,pitch,rollF,pitchF;
String s_roll,s_pitch;
int sum = 0; // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0; // calculated voltage
//Buzzer
const int buzzer = 10;
void setup()
{
//Xbee Initialzing
XBee.begin(9600);
Serial.begin(9600);
while (!Serial) ;
altSerial.begin(9600);
//BMP Sensor checking
if (!bmp.begin())
{
Serial.println("Could not find BMP085 sensor");
while (1) {}
}
//RTC checking
rtc.begin();
rtc.setTime(0,0,0);
Wire.begin(); // Initiate the Wire library
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
Wire.write(8); // Bit D3 High for measuring enable (8dec -> 0000 1000 binary)
Wire.endTransmission();
delay(10);
//Off-set Calibration
//X-axis
Wire.beginTransmission(ADXL345);
Wire.write(0x1E);
Wire.write(1);
Wire.endTransmission();
delay(10);
//Y-axis
Wire.beginTransmission(ADXL345);
Wire.write(0x1F);
Wire.write(-2);
Wire.endTransmission();
delay(10);
//Z-axis
Wire.beginTransmission(ADXL345);
Wire.write(0x20);
Wire.write(-9);
Wire.endTransmission();
delay(10);
//Buzzer pin initialization
pinMode(buzzer, OUTPUT);
}
void loop()
{
Serial.println("RTC values");
times=rtc.getTimeStr();
ntimes=times.length();
char char_array0[ntimes+1];
strcpy(char_array0, times.c_str());
Serial.println(char_array0);
hh=times.substring(0,2);
nhrs=hh.length();
char char_array1[nhrs+1];
strcpy(char_array1, hh.c_str());
Serial.println(char_array1);
mm=times.substring(3,5);
nmins=mm.length();
char char_array2[nmins+1];
strcpy(char_array2, mm.c_str());
Serial.println(char_array2);
ss=times.substring(6,8);
nsecs=ss.length();
char char_array3[nsecs+1];
strcpy(char_array3, ss.c_str());
Serial.println(char_array3);
Serial.println("BMP values");
st = bmp.readTemperature();
nt=st.length();
char char_array4[nt+1];
strcpy(char_array4, st.c_str());
Serial.println(char_array4);
sp = bmp.readPressure();
np=sp.length();
char char_array5[np+1];
strcpy(char_array5, sp.c_str());
Serial.println(char_array5);
sa = bmp.readAltitude();
sai=sa.toInt();
na=sa.length();
char char_array6[na + 1];
strcpy(char_array6, sa.c_str());
Serial.println(char_array6);
//Packetcount
packetcounts=String(packetcount);
packclen=packetcounts.length();
char char_array14[packclen+1];
strcpy(char_array14,packetcounts.c_str());
Serial.println(char_array14);
//First Printing
XBee.write("5515,");
//XBee.write(char_array0);
//XBee.write(",");
XBee.write(char_array1); //hours
XBee.write(":");
XBee.write(char_array2); //minutes
XBee.write(":");
XBee.write(char_array3); //seconds
XBee.write(",");
XBee.write(char_array14); //Packetcount
XBee.write(",");
XBee.write(char_array6); //Altitude
XBee.write(",");
XBee.write(char_array5); //Pressure
XBee.write(",");
XBee.write(char_array4); //Temprature
XBee.write(",");
//voltage regultor
while (sample_count < NUM_SAMPLES)
{
sum += analogRead(A2);
sample_count++;
delay(10);
}
voltage = ((float)sum / (float)NUM_SAMPLES * 4.3) / 1024.0;
volte=voltage*12.67;
Serial.print(volte);
Serial.println (" V");
sample_count = 0;
sum = 0;
String svoltage;
svoltage=String(volte);
nvolt = svoltage.length();
char char_array17[nvolt+1];
strcpy(char_array17,svoltage.c_str());
XBee.write(char_array17);
gps.encode(altSerial.read());
Serial.println("GPS values");
lati = gps.location.lat();
nlati = lati.length();
char char_array7[nlati+1];
strcpy(char_array7, lati.c_str());
Serial.println(char_array7);
longi = gps.location.lng();
nlongi = longi.length();
char char_array8[nlongi+1];
strcpy(char_array8, longi.c_str());
Serial.println(char_array8);
alt = gps.altitude.meters();
nalt = alt.length();
char char_array9[nalt+1];
strcpy(char_array9, alt.c_str());
Serial.println(char_array9);
num_sat = gps.satellites.value();
n_numSat = num_sat.length();
char char_array10[n_numSat+1];
strcpy(char_array10,num_sat.c_str());
Serial.println(char_array10);
hrs = gps.time.hour();
nhrs = hrs.length();
char char_array11[nhrs+1];
strcpy(char_array11,hrs.c_str());
Serial.print(char_array11);
mins = gps.time.minute();
nmins = mins.length();
char char_array12[nmins+1];
strcpy(char_array12,mins.c_str());
Serial.print(char_array12);
secs = gps.time.second();
nsecs = secs.length();
char char_array13[nmins+1];
strcpy(char_array13,secs.c_str());
XBee.write(",");
XBee.write(char_array11); //hours
XBee.write(":");
XBee.write(char_array12); //minute
XBee.write(":");
XBee.write(char_array13); //seconds
XBee.write(",");
XBee.write(char_array7); //latitude
XBee.write(",");
XBee.write(char_array8); //longitude
XBee.write(",");
XBee.write(char_array9); //altitude
XBee.write(",");
XBee.write(char_array10); //number of satellite
//accelerometer
Serial.println("adxl values");
Wire.beginTransmission(ADXL345);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read() | Wire.read() << 8); // X-axis value
X_out = X_out / 256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_out = ( Wire.read() | Wire.read() << 8); // Y-axis value
Y_out = Y_out / 256;
Z_out = ( Wire.read() | Wire.read() << 8); // Z-axis value
Z_out = Z_out / 256;
// Calculate Roll and Pitch (rotation around X-axis, rotation around Y-axis)
roll = atan(Y_out / sqrt(pow(X_out, 2) + pow(Z_out, 2))) * 180 / PI;
pitch = atan(-1 * X_out / sqrt(pow(Y_out, 2) + pow(Z_out, 2))) * 180 / PI;
// Low-pass filter
rollF = 0.94 * rollF + 0.06 * roll;
pitchF = 0.94 * pitchF + 0.06 * pitch;
s_roll = String(rollF,3);
s_pitch = String(pitchF,3);
Serial.println(s_pitch);
npitch = s_pitch.length();
char char_array15[npitch+1];
strcpy(char_array15,s_pitch.c_str());
Serial.println(char_array15);
Serial.println(s_roll);
nroll = s_roll.length();
char char_array16[nroll+1];
strcpy(char_array16,s_roll.c_str());
Serial.println(char_array16);
XBee.write(",");
XBee.write(char_array15);
XBee.write(",");
XBee.write(char_array16);
XBee.write(",");
//Blade spin rate
XBee.write("0,");
//Software state
if(sai > 600)
XBee.write("1");
if(sai<450)
XBee.write("2");
XBee.write("\n");
//Buzzer
if(sai<340)
digitalWrite(10,HIGH);
Serial.println();
Serial.println("end of frame");
packetcount++;
delay(1000);
}