-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTUFF_Flight_Data_Collector.ino
237 lines (180 loc) · 6.94 KB
/
TUFF_Flight_Data_Collector.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
/*===========================================================================
* T.U.F.F (Tension Under Flight Forces) UMD BPP In-Flight Code
* By Jeremy Kuznetsov, Jim Oliver Villegas, Jaxon Lee
*
* This keeps track of tension data on BPP balloon launches using a load cell.
===========================================================================*/
// Libraries
#include <SPI.h> // Built in
#include <SD.h> // Built in
#include <Wire.h> // Built in
#include <HX711.h> // HX711 0.7.5 by Bogdan Necula
#include <RTClib.h> // RTClib 2.0.2 by Adafruit.
// NOTE: Dependent on Adafruit BusIO 1.11.1 by Adafruit.
#include <Adafruit_BMP280.h> // BMP280 2.6.1 by Adafruit.
// NOTE: Dependent on Adafruit Unified Sensor 1.1.4 by Adafruit.
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
// Misc Defining
#define CS 10 // Chip Select on SD logger
#define ledPin 9 // LED
// Declaring Objects
HX711 loadcell;
RTC_DS1307 rtc;
Adafruit_BNO055 bno = Adafruit_BNO055(55);
Adafruit_BMP280 bmp;
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
// Wiring for the HX711 Amplifier
const int LOADCELL_DOUT_PIN = 2;
const int LOADCELL_SCK_PIN = 3;
// Loadcell calibration values. Calibrated to our load cell particularly.
// Offset - "zeroing the loadcell"
// Divider - "converting amp readings to lbs"
const long LOADCELL_OFFSET = 50682624;
const long LOADCELL_DIVIDER = 169570.1038;
// Variables
float tension = 0; // Tension sensor data
DateTime timelog; // Timestamp data
float pressure = 0; // Measured in Pascals.
float bmptemp = 0; // Measured in degrees Celsius.
float alt = 0; // Measured in meters.
sensors_vec_t abs_orientation;
sensors_vec_t acceleration;
sensors_vec_t magnometer;
// Constant
const float sealevelpressure = 1017.25; //hPa of local sea level pressure, I assume?
/*===========================================================================
* Setup
===========================================================================*/
void setup() {
// MAKE SURE TO SET THE BAUD RATE IN VSCODE TO 9600
Serial.begin(115200);
Serial.println("BEGIN IN-FLIGHT CODE");
//----------------------------
// SD Card Test
Serial.print("Initializing SD card...");
if (!SD.begin(CS)) {
Serial.println("Card failed, or not present");
}
else {
Serial.println("card initialized.");
}
//--------------------------
// Initialize HX711 (load cell) with pinning/offsets/calibration
loadcell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
loadcell.set_offset(LOADCELL_OFFSET);
loadcell.set_scale(LOADCELL_DIVIDER);
// Zero scale
loadcell.tare(100);
/* Initialise the Gyro/Accelerometer/Magnometer sensor */
if(!bno.begin())
{
/* There was a problem detecting the BNO055 ... check your connections */
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
}
delay(5000);
bno.setExtCrystalUse(true);
//--------------------------
// Alert user if RTC fails to open.
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
delay(10);
}
// Ensures rtc begins.
rtc.begin();
// Set RTC Date/Time. This only needs to be run once EVER.
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//-------------------------
// Alert user if the BMP sensor fails to open.
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
}
// Ensures the BMP sensor begins.
bmp.begin();
Serial.print("START LOOP OF IN-FLIGHT CODE");
//-------------------------
pinMode(ledPin, OUTPUT); // Declare LED as an output pin
}
/*===========================================================================
* Loop
===========================================================================*/
void loop() {
int i;
// To write to a file with this SD logger, you must first open the file
File dataFile = SD.open("datalog.txt", FILE_WRITE);
for (i = 0; i < 10; i++) {
//----------------------
// Attempt to get reading from loadcell, retry if failed
tension = loadcell.get_units(1);
//---------------------
// Get reading from RTC
timelog = rtc.now();
//--------------------
// Get BMP readings
bmptemp = bmp.readTemperature();
pressure = bmp.readPressure();
alt = bmp.readAltitude(sealevelpressure);
//--------------------
/* Get BNO sensor "event" (it's readings) */
sensors_event_t event;
bno.getEvent(&event, Adafruit_BNO055::VECTOR_EULER);
abs_orientation = event.orientation;
bno.getEvent(&event, Adafruit_BNO055::VECTOR_ACCELEROMETER);
acceleration = event.acceleration;
bno.getEvent(&event, Adafruit_BNO055::VECTOR_MAGNETOMETER);
magnometer = event.magnetic;
// ==========Writing Data==========
// If the file is available (meaning that it could open the file from the SD card), write to it:
if (dataFile) {
// Timestamp Data
dataFile.print(timelog.year(), DEC); dataFile.print("/"); dataFile.print(timelog.month(), DEC); dataFile.print("/"); dataFile.print(timelog.day(), DEC); dataFile.print("|");
dataFile.print(timelog.hour(), DEC); dataFile.print(":"); dataFile.print(timelog.minute(), DEC); dataFile.print(":"); dataFile.print(timelog.second(), DEC);
dataFile.print(",");
// Tension Data
dataFile.print(tension);
dataFile.print(",");
// BMP Data
dataFile.print(bmptemp);
dataFile.print(",");
dataFile.print(pressure);
dataFile.print(",");
dataFile.print(alt);
dataFile.print(",");
// BNO 9-Axis data
dataFile.print(abs_orientation.x);
dataFile.print(",");
dataFile.print(abs_orientation.y);
dataFile.print(",");
dataFile.print(abs_orientation.z);
dataFile.print(",");
dataFile.print(acceleration.x);
dataFile.print(",");
dataFile.print(acceleration.y);
dataFile.print(",");
dataFile.print(acceleration.z);
dataFile.print(",");
dataFile.print(magnometer.x);
dataFile.print(",");
dataFile.print(magnometer.y);
dataFile.print(",");
dataFile.print(magnometer.z);
dataFile.print(",");
dataFile.println();
}
else {
//Serial.println("error opening datalog.txt");
}
}
dataFile.close();
/*
//==================Serial Monitoring============
// Tension Data
Serial.print("Tension: "); Serial.print(tension); Serial.print(" lbs");
Serial.print(',');
Serial.println();
*/
}