forked from rjsmith1999/ProjectStompRocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstompRocket.ino
310 lines (225 loc) · 8.32 KB
/
stompRocket.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
#include <RFM69.h>
#include <SPI.h>
#include "quaternionFilters.h"
#include "MPU9250.h"
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#include "SPI.h"
// Addresses for this node. CHANGE THESE FOR EACH NODE!
#define NETWORKID 0 // Must be the same for all nodes
#define MYNODEID 2 // My node ID
#define TONODEID 255 // Destination node ID
// RFM69 frequency, uncomment the frequency of your module:
//#define FREQUENCY RF69_433MHZ
#define FREQUENCY RF69_915MHZ
// AES encryption (or not):
#define ENCRYPT false // Set to "true" to use encryption
#define ENCRYPTKEY "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes
// Use ACKnowledge when sending messages (or not):
#define USEACK true // Request ACKs or not
// Packet sent/received indicator LED (optional):
#define LED 9 // LED positive pin
#define GND 8 // LED ground pin
#define AHRS true // Set to false for basic data read
#define SerialDebug true // Set to true to get Serial output for debugging
// Create a library object for our RFM69HCW module:
RFM69 radio;
MPU9250 myIMU;
BME280 mySensor;
int oldTime = 0;
// Use this to translate between floats and integers
// A union uses the same amount of data for storing ints or floats, so it can translate between the two
// without loosing the data in a float.
union floatInt_t{
int i;
float f;
};
void setup()
{
// Open a serial port so we can send keystrokes to the module:
Serial.begin(9600);
Serial.print("Node ");
Serial.print(MYNODEID,DEC);
Serial.println(" ready");
// Set up the indicator LED (optional):
// Initialize the RFM69HCW:
Serial.println(radio.initialize(FREQUENCY, MYNODEID, NETWORKID) );
radio.setHighPower(); // Always use this for RFM69HCW
// Turn on encryption if desired:
radio.promiscuous(true);
// Read the WHO_AM_I register, this is a good test of communication
byte c = myIMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
Serial.print("MPU9250 "); Serial.print("I AM "); Serial.print(c, HEX);
Serial.print(" I should be "); Serial.println(0x71, HEX);
if (c == 0x71) // WHO_AM_I should always be 0x68
{
Serial.println("MPU9250 is online...");
// Start by performing self test and reporting values
myIMU.MPU9250SelfTest(myIMU.selfTest);
Serial.print("x-axis self test: acceleration trim within : ");
Serial.print(myIMU.selfTest[0],1); Serial.println("% of factory value");
Serial.print("y-axis self test: acceleration trim within : ");
Serial.print(myIMU.selfTest[1],1); Serial.println("% of factory value");
Serial.print("z-axis self test: acceleration trim within : ");
Serial.print(myIMU.selfTest[2],1); Serial.println("% of factory value");
Serial.print("x-axis self test: gyration trim within : ");
Serial.print(myIMU.selfTest[3],1); Serial.println("% of factory value");
Serial.print("y-axis self test: gyration trim within : ");
Serial.print(myIMU.selfTest[4],1); Serial.println("% of factory value");
Serial.print("z-axis self test: gyration trim within : ");
Serial.print(myIMU.selfTest[5],1); Serial.println("% of factory value");
// Calibrate gyro and accelerometers, load biases in bias registers
myIMU.calibrateMPU9250(myIMU.gyroBias, myIMU.accelBias);
myIMU.initMPU9250();
// Initialize device for active mode read of acclerometer, gyroscope, and
// temperature
Serial.println("MPU9250 initialized for active data mode....");
// Read the WHO_AM_I register of the magnetometer, this is a good test of
// communication
byte d = myIMU.readByte(AK8963_ADDRESS, WHO_AM_I_AK8963);
Serial.print("AK8963 "); Serial.print("I AM "); Serial.print(d, HEX);
Serial.print(" I should be "); Serial.println(0x48, HEX);
Serial.println("AK8963 initialized for active data mode....");
} // if (c == 0x71)
else
{
Serial.print("Could not connect to MPU9250: 0x");
Serial.println(c, HEX);
while(1) ; // Loop forever if communication doesn't happen
}
//***Driver settings********************************//
//commInterface can be I2C_MODE or SPI_MODE
//specify chipSelectPin using arduino pin names
//specify I2C address. Can be 0x77(default) or 0x76
//For I2C, enable the following and disable the SPI section
mySensor.settings.commInterface = I2C_MODE;
mySensor.settings.I2CAddress = 0x77;
//For SPI enable the following and dissable the I2C section
//mySensor.settings.commInterface = SPI_MODE;
//mySensor.settings.chipSelectPin = 10;
//***Operation settings*****************************//
//runMode can be:
// 0, Sleep mode
// 1 or 2, Forced mode
// 3, Normal mode
mySensor.settings.runMode = 3; //Forced mode
//tStandby can be:
// 0, 0.5ms
// 1, 62.5ms
// 2, 125ms
// 3, 250ms
// 4, 500ms
// 5, 1000ms
// 6, 10ms
// 7, 20ms
mySensor.settings.tStandby = 0;
//filter can be off or number of FIR coefficients to use:
// 0, filter off
// 1, coefficients = 2
// 2, coefficients = 4
// 3, coefficients = 8
// 4, coefficients = 16
mySensor.settings.filter = 0;
/*
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensor.settings.humidOverSample = 1;
*/
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. Serial.begin(57600);
Serial.print("Starting BME280... result of .begin(): 0x");
//Calling .begin() causes the settings to be loaded
Serial.println(mySensor.begin(), HEX);
oldTime = millis();
}
void loop()
{
// Set up a "buffer" for characters that we'll send:
static int sendbuffer[7];
static int sendlength = 4*7;
static int newTime = 0;
static int counter = 0;
sendbuffer[0] = counter;
getIMUData(sendbuffer);
union floatInt_t height;
height.f = getHeight();
sendbuffer[5] = height.i;
union floatInt_t test;
test.f = 3.14159;
sendbuffer[6] = test.i; //This can be used as a test to confirm proper transmission;
newTime = millis();
sendbuffer[1] = newTime - oldTime;
oldTime = newTime;
// SENDING
//Serial.println( radio.getFrequency() );
Serial.println("Sending: ");
printSendbuffer(sendbuffer);
if (USEACK)
{
if (radio.sendWithRetry(TONODEID, sendbuffer, sendlength))
Serial.println("ACK received!");
else
Serial.println("no ACK received");
}
// If you don't need acknowledgements, just use send():
else // don't use ACK
{
radio.send(TONODEID, sendbuffer, sendlength);
Serial.println("Message Sent");
}
//sendlength = 0; // reset the packet
//Serial.println("after the point");
}
void getIMUData(int* sendbuffer)
{
union floatInt_t converter;
if (myIMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
{
myIMU.readAccelData(myIMU.accelCount); // Read the x/y/z adc values
myIMU.getAres();
// Now we'll calculate the accleration value into actual g's
// This depends on scale being set
converter.f = (float)myIMU.accelCount[0]*myIMU.aRes; // - accelBias[0];
sendbuffer[2] = converter.i;
converter.f = (float)myIMU.accelCount[1]*myIMU.aRes; // - accelBias[1];
sendbuffer[3] = converter.i;
converter.f = (float)myIMU.accelCount[2]*myIMU.aRes; // - accelBias[2];
sendbuffer[4] = converter.i;
}
/*sendbuffer[3]; //xdata
sendbuffer[4]; //ydata
sendbuffer[5]; //zdata
*/
}
float getHeight()
{
return mySensor.readFloatAltitudeFeet();
}
void printSendbuffer(int* sendbuffer){
union floatInt_t conv;
Serial.print("Count = ");
Serial.println(sendbuffer[0]);
Serial.print("dTime = ");
Serial.println(sendbuffer[1]);
Serial.print("Accel X = ");
conv.i = sendbuffer[2];
Serial.println(conv.f);
Serial.print("Accel Y = ");
conv.i = sendbuffer[3];
Serial.println(conv.f);
Serial.print("Accel Z = ");
conv.i = sendbuffer[4];
Serial.println(conv.f);
Serial.print("Height = ");
conv.i = sendbuffer[5];
Serial.println(conv.f);
}