Components and supplies
SG90 Micro-servo motor
Arduino Nano R3
Jumper wires (generic)
Inertial Measurement Unit (IMU) (6 deg of freedom)
Altitude/Pressure Sensor Breakout MPL3115A2
Memory Socket, SD Card
Project description
Code
Without SD and IMU
arduino
1#include <SFE_BMP180.h> 2#include <Wire.h> 3#include <Servo.h> 4 5// You will need to create an SFE_BMP180 object, here called "pressure": 6 7SFE_BMP180 pressure; 8 9Servo servo; 10int GREENLED=9; 11int REDLED=10; 12int Check_downs = 0; 13 14double baseline; // baseline pressure 15 16void setup() 17{ 18 Serial.begin(9600); 19 Serial.println("REBOOT"); 20 21 // Initialize the sensor (it is important to get calibration values stored on the device). 22 pinMode(REDLED,OUTPUT); 23 pinMode(GREENLED,OUTPUT); 24 if (pressure.begin()){ 25 Serial.println("BMP180 init success"); 26 } 27 else 28 { 29 30 Serial.println("BMP180 init fail (disconnected?)\ 31\ 32"); 33 while(1); // Pause forever. 34 } 35 36 // Get the baseline pressure: 37 38 baseline = getPressure(); 39 40 Serial.print("baseline pressure: "); 41 Serial.print(baseline); 42 Serial.println(" mb"); 43 Serial.print("baseline pressure: "); 44 Serial.print(baseline/33.864,0); 45 Serial.println(" Inhg"); 46 47 servo.attach(5); 48 servo.write(90); //sets servo to its midpoint 49} 50 51void loop() 52{servo.write(90); 53delay(10); 54 double a,P,a1,a2,P2,Difference; 55 56 // Get a new pressure reading: 57 58 // Show the relative altitude difference between 59 // the new reading and the baseline reading: 60 61 //a = pressure.altitude(P,baseline); 62 63 //Serial.print("relative altitude: "); 64 //if (a >= 0.0) Serial.print(" "); // add a space for positive numbers 65 //Serial.print(a,1); 66 //Serial.print(" meters, "); 67 //if (a >= 0.0) Serial.print(" "); // add a space for positive numbers 68 69 P = getPressure(); 70 71 a1 = pressure.altitude(P,baseline); 72 73 delay(3000); 74 75 P2 = getPressure(); 76 77 a2 = pressure.altitude(P2,baseline); 78 79 Difference = a2 - a1; 80 if (abs(Difference) > 1) { 81 if (Difference < 0 ) Check_downs =+ 1; 82 if (Difference > 0 ) Check_downs = 0; 83 } 84 85 Serial.print("Start >"); 86 Serial.print(a1); 87 Serial.print(" meter >"); 88 Serial.println(a2); 89 90 Serial.print("Difference: "); 91 Serial.print(a1 - a2); 92 Serial.println("meters"); 93 Serial.print("Check downs : "); 94 Serial.print(Check_downs); 95 Serial.println(); 96 97 if (Check_downs == 3) { 98 servo.write(90); 99 digitalWrite(REDLED,LOW); 100 digitalWrite(GREENLED,HIGH); 101 102 } 103 else 104 { digitalWrite(REDLED,HIGH); 105 digitalWrite(GREENLED,LOW); 106 } 107 delay(1000); 108 109} 110 111 112double getPressure() 113{ 114 char status; 115 double T,P,p0,a; 116 117 118 status = pressure.startTemperature(); 119 if (status != 0) 120 { 121 // Wait for the measurement to complete: 122 123 delay(status); 124 125 126 status = pressure.getTemperature(T); 127 if (status != 0) 128 { 129 130 status = pressure.startPressure(3); 131 if (status != 0) 132 { 133 // Wait for the measurement to complete: 134 delay(status); 135 136 // Retrieve the completed pressure measurement: 137 // Note that the measurement is stored in the variable P. 138 // Use '&P' to provide the address of P. 139 // Note also that the function requires the previous temperature measurement (T). 140 // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.) 141 // Function returns 1 if successful, 0 if failure. 142 143 status = pressure.getPressure(P,T); 144 if (status != 0) 145 { 146 return(P); 147 } 148 else Serial.println("error retrieving pressure measurement\ 149"); 150 } 151 else Serial.println("error starting pressure measurement\ 152"); 153 } 154 else Serial.println("error retrieving temperature measurement\ 155"); 156 } 157 else Serial.println("error starting temperature measurement\ 158"); 159} 160
Final Code for full project
arduino
1#include <SD.h> 2#include <Wire.h> 3#include <SFE_BMP180.h> 4#include<Servo.h> 5#include "I2Cdev.h" 6#include "MPU6050_6Axis_MotionApps20.h" 7#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 8 #include "Wire.h" 9#endif 10#define INTERRUPT_PIN 2 //I2C Interrupt Pin for MPU6050, use pin 2 on Arduino Uno & most boards 11#define BUZZER_PIN 3 //set buzzer pin 12#define BLUE_LED_PIN 8 //set blue LED pin 13#define YELLOW_LED_PIN 9 //set yellow LED pin 14#define RED_LED_PIN 10 //set red LED pin 15#define BUTTON_PIN 7 //set button pin 16#define chipSelect 4 //set chip select pin for MicroSD Card Adapter (CS pin) 17SFE_BMP180 pressure; 18 19Servo servo; 20 21int Check_downs = 0; 22 23double baseline; 24//declare general use variables 25int buttonState = 0; 26int MODE = 0; //initialize mode to zero 27int t = 0; //create timestamp value 28int dataRate = 10; //set specified sampling rate (data points per second) (somewhere between 10-200 is ideal) 29 30//declare MPU control/status vars 31bool blinkState = false; 32bool dmpReady = false; // set true if DMP init was successful 33uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU 34uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) 35uint16_t packetSize; // expected DMP packet size (default is 42 bytes) 36uint16_t fifoCount; // count of all bytes currently in FIFO 37uint8_t fifoBuffer[64]; // FIFO storage buffer 38 39//declare orientation/motion vars 40Quaternion q; // [w, x, y, z] quaternion container 41VectorInt16 aa; // [x, y, z] accel sensor measurements 42VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements 43VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements 44VectorFloat gravity; // [x, y, z] gravity vector 45float euler[3]; // [psi, theta, phi] Euler angle container 46float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector 47 48//create objects 49File file; 50SFE_BMP180 BMP; 51MPU6050 mpu; 52 53//Interrupt Detection Routine 54volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high 55void dmpDataReady() { 56 mpuInterrupt = true; 57} 58 59void setup() { 60 //--- Serial Debugging --- 61 Serial.begin(9600); 62 //--- Establish Pin Modes and turn off all LEDs --- 63 pinMode(BUZZER_PIN, OUTPUT); 64 pinMode(BLUE_LED_PIN, OUTPUT); 65 pinMode(YELLOW_LED_PIN, OUTPUT); 66 pinMode(RED_LED_PIN, OUTPUT); 67 pinMode(chipSelect, OUTPUT); 68 pinMode(BUTTON_PIN, INPUT); 69 pinMode(INTERRUPT_PIN, INPUT); 70 digitalWrite(YELLOW_LED_PIN, LOW); 71 digitalWrite(RED_LED_PIN, LOW); 72 digitalWrite(BLUE_LED_PIN, LOW); 73 tone(BUZZER_PIN, 500, 250); 74 //initialize SD Card 75 if(!SD.begin(chipSelect)){ 76 //Serial debugging 77 Serial.println("Could not initialize SD card"); 78 } 79 //clear SD data 80 if(SD.exists("file.txt")){ 81 if(SD.remove("file.txt") == true){ 82 Serial.println("removed data"); 83 } 84 } 85 //initialize BMP sensor 86 if(BMP.begin()){ 87 Serial.println("BMP init success"); 88 } 89 //initialize IMU and I2C clock 90 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 91 Wire.begin(); 92 Wire.setClock(400000); 93 #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE 94 Fastwire::setup(400, true); 95 #endif 96 mpu.initialize(); //start MPU 97 Serial.println(F("Testing device connections...")); //debugging serial statement 98 Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); //debugging serial statement 99 devStatus = mpu.dmpInitialize(); 100 // supply your own gyro offsets here, scaled for min sensitivity 101 mpu.setXGyroOffset(0); 102 mpu.setYGyroOffset(0); 103 mpu.setZGyroOffset(0); 104 mpu.setZAccelOffset(1688); // 1688 factory default for my test chip 105 if (devStatus == 0) { 106 // turn on the DMP, now that it's ready 107 Serial.println(F("Enabling DMP...")); 108 mpu.setDMPEnabled(true); 109 // enable Arduino interrupt detection 110 Serial.print(F("Enabling interrupt detection (Arduino external interrupt ")); 111 Serial.print(digitalPinToInterrupt(INTERRUPT_PIN)); 112 Serial.println(F(")...")); 113 attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING); 114 mpuIntStatus = mpu.getIntStatus(); 115 // set our DMP Ready flag so the main loop() function knows it's okay to use it 116 Serial.println(F("DMP ready! Waiting for first interrupt...")); 117 dmpReady = true; 118 // get expected DMP packet size for later comparison 119 packetSize = mpu.dmpGetFIFOPacketSize(); 120 } else { 121 122 Serial.print(F("DMP Initialization failed (code ")); 123 Serial.print(devStatus); 124 Serial.println(F(")")); 125 } 126 servo.attach(9); 127 //set mode 128 MODE = 1; //set to PAD IDLE mode - initialize sensors and SD card 129 //MODE = 2; //set to FLIGHT mode - log data 130 //MODE = 3; //set to RECOVERY mode - close file 131} 132 133void loop() { 134 servo.write(90); 135 delay(100); 136 // put your main code here, to run repeatedly: 137 if(MODE == 1){ //PAD IDLE MODE 138 digitalWrite(RED_LED_PIN, HIGH); 139 file = SD.open("file.txt", FILE_WRITE); //Open SD card file 140 if(file) { 141 Serial.println("t,T,P,ax,ay,az,rx,ry,rz"); //print first line with data labels 142 file.println("t,T,P,ax,ay,az,rx,ry,rz"); 143 MODE = 2; 144 } 145 else{ 146 Serial.println("Error opening file"); 147 delay(5000); //just chill for 5 seconds before trying again 148 } 149 } 150 if(MODE == 2){ //ACTIVE FLIGHT mode 151 digitalWrite(YELLOW_LED_PIN, HIGH); 152 digitalWrite(RED_LED_PIN, LOW); 153 digitalWrite(BLUE_LED_PIN, LOW); 154 //print timestamp and comma to separate data 155 Serial.print(t); 156 Serial.print(","); 157 file.print(t); 158 file.print(","); 159 160 char status; 161 double a,P,a1,a2,P2,Difference; 162 163 164 P = getPressure(); 165 166 a1 = pressure.altitude(P,baseline); 167 168 delay(3000); 169 170 P2 = getPressure(); 171 172 a2 = pressure.altitude(P2,baseline); 173 174 Difference = a2 - a1; 175 if (abs(Difference) > 1) { 176 if (Difference < 0 ) Check_downs =+ 1; 177 if (Difference > 0 ) Check_downs = 0; 178 } 179 180 Serial.print("Start >"); 181 Serial.print(a1); 182 Serial.print(" meter >"); 183 Serial.println(a2); 184 185 Serial.print("Difference: "); 186 Serial.print(a1 - a2); 187 Serial.println("meters"); 188 Serial.print("Check downs : "); 189 Serial.print(Check_downs); 190 Serial.println(); 191 192 if (Check_downs == 3) { 193 servo.write(180); 194 digitalWrite(RED_LED_PIN ,LOW); 195 digitalWrite(BLUE_LED_PIN,HIGH); 196 197 } 198 else 199 { digitalWrite(RED_LED_PIN ,HIGH); 200 digitalWrite(BLUE_LED_PIN,LOW); 201 } 202 delay(1000); 203 204 file.print(","); 205 //get IMU data 206 if (!dmpReady) return; 207 while (!mpuInterrupt && fifoCount < packetSize){ 208 if (mpuInterrupt && fifoCount < packetSize){ 209 fifoCount = mpu.getFIFOCount(); 210 } 211 } 212 mpuInterrupt = false; 213 mpuIntStatus = mpu.getIntStatus(); 214 fifoCount = mpu.getFIFOCount(); 215 if((mpuIntStatus & _BV(MPU6050_INTERRUPT_FIFO_OFLOW_BIT)) || fifoCount >=1024){ 216 mpu.resetFIFO(); 217 fifoCount = mpu.getFIFOCount(); 218 Serial.println("FIFO Overflow!"); 219 } 220 else if(mpuIntStatus & _BV(MPU6050_INTERRUPT_DMP_INT_BIT)){ 221 while(fifoCount < packetSize) fifoCount = mpu.getFIFOCount(); 222 mpu.getFIFOBytes(fifoBuffer, packetSize); 223 fifoCount -= packetSize; 224 //get real-world acceleration 225 mpu.dmpGetQuaternion(&q, fifoBuffer); 226 mpu.dmpGetAccel(&aa, fifoBuffer); 227 mpu.dmpGetGravity(&gravity, &q); 228 mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); 229 //print real-world acceleration 230 Serial.print(aaReal.x); 231 file.print(aaReal.x); 232 Serial.print(","); 233 file.print(","); 234 Serial.print(aaReal.y); 235 file.print(aaReal.y); 236 Serial.print(","); 237 file.print(","); 238 Serial.print(aaReal.z); 239 file.print(aaReal.z); 240 Serial.print(","); 241 file.print(","); 242 //get Euler angles 243 mpu.dmpGetQuaternion(&q, fifoBuffer); 244 mpu.dmpGetEuler(euler, &q); 245 //print Euler angles 246 Serial.print(euler[0]*180/M_PI); 247 file.print(euler[0]*180/M_PI); 248 Serial.print(","); 249 file.print(","); 250 Serial.print(euler[1]*180/M_PI); 251 file.print(euler[1]*180/M_PI); 252 Serial.print(","); 253 file.print(","); 254 Serial.print(euler[2]*180/M_PI); 255 file.print(euler[2]*180/M_PI); 256 } 257 //end data entry line 258 Serial.println(); //ends line 259 file.println(); //ends line 260 //check for mode switch 261 buttonState = digitalRead(BUTTON_PIN); 262 if(buttonState == LOW){ 263 MODE = 3; 264 tone(BUZZER_PIN, 1000, 250); 265 delay(100); 266 } 267 } 268 if(MODE == 3){ //RECOVERY MODE 269 file.close(); 270 digitalWrite(YELLOW_LED_PIN, LOW); 271 digitalWrite(RED_LED_PIN, LOW); 272 digitalWrite(BLUE_LED_PIN, HIGH); 273 delay(1000); 274 } 275 t = t + 1; //increment t value 276 delay(1000/dataRate); //pause so that data output corresponds to data rate 277 if(t > 32765){ //prevents issues related to integers rolling over at 32767 278 t = 1; 279 } 280} 281double getPressure() 282{ 283 char status; 284 double T,P,p0,a; 285 286 287 status = pressure.startTemperature(); 288 if (status != 0) 289 { 290 // Wait for the measurement to complete: 291 292 delay(status); 293 294 295 status = pressure.getTemperature(T); 296 if (status != 0) 297 { 298 299 status = pressure.startPressure(3); 300 if (status != 0) 301 { 302 // Wait for the measurement to complete: 303 delay(status); 304 305 306 307 status = pressure.getPressure(P,T); 308 if (status != 0) 309 { 310 return(P); 311 } 312 else Serial.println("error retrieving pressure measurement\ 313"); 314 } 315 else Serial.println("error starting pressure measurement\ 316"); 317 } 318 else Serial.println("error retrieving temperature measurement\ 319"); 320 } 321 else Serial.println("error starting temperature measurement\ 322"); 323} 324 \\ 325
Final Code for full project
arduino
1#include <SD.h> 2#include <Wire.h> 3#include <SFE_BMP180.h> 4 5#include<Servo.h> 6#include "I2Cdev.h" 7#include "MPU6050_6Axis_MotionApps20.h" 8#if 9 I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 10 #include "Wire.h" 11#endif 12#define 13 INTERRUPT_PIN 2 //I2C Interrupt Pin for MPU6050, use pin 2 on Arduino Uno & most 14 boards 15#define BUZZER_PIN 3 //set buzzer pin 16#define BLUE_LED_PIN 8 //set 17 blue LED pin 18#define YELLOW_LED_PIN 9 //set yellow LED pin 19#define RED_LED_PIN 20 10 //set red LED pin 21#define BUTTON_PIN 7 //set button pin 22#define chipSelect 23 4 //set chip select pin for MicroSD Card Adapter (CS pin) 24SFE_BMP180 pressure; 25 26Servo 27 servo; 28 29int Check_downs = 0; 30 31double baseline; 32//declare general 33 use variables 34int buttonState = 0; 35int MODE = 0; //initialize mode to 36 zero 37int t = 0; //create timestamp value 38int dataRate = 10; //set 39 specified sampling rate (data points per second) (somewhere between 10-200 is ideal) 40 41//declare 42 MPU control/status vars 43bool blinkState = false; 44bool dmpReady = false; // 45 set true if DMP init was successful 46uint8_t mpuIntStatus; // holds actual interrupt 47 status byte from MPU 48uint8_t devStatus; // return status after each device 49 operation (0 = success, !0 = error) 50uint16_t packetSize; // expected DMP packet 51 size (default is 42 bytes) 52uint16_t fifoCount; // count of all bytes currently 53 in FIFO 54uint8_t fifoBuffer[64]; // FIFO storage buffer 55 56//declare orientation/motion 57 vars 58Quaternion q; // [w, x, y, z] quaternion container 59VectorInt16 60 aa; // [x, y, z] accel sensor measurements 61VectorInt16 aaReal; 62 // [x, y, z] gravity-free accel sensor measurements 63VectorInt16 64 aaWorld; // [x, y, z] world-frame accel sensor measurements 65VectorFloat 66 gravity; // [x, y, z] gravity vector 67float euler[3]; // 68 [psi, theta, phi] Euler angle container 69float ypr[3]; // [yaw, pitch, 70 roll] yaw/pitch/roll container and gravity vector 71 72//create objects 73File 74 file; 75SFE_BMP180 BMP; 76MPU6050 mpu; 77 78//Interrupt Detection Routine 79volatile 80 bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high 81void 82 dmpDataReady() { 83 mpuInterrupt = true; 84} 85 86void setup() { 87 //--- 88 Serial Debugging --- 89 Serial.begin(9600); 90 //--- Establish Pin Modes and 91 turn off all LEDs --- 92 pinMode(BUZZER_PIN, OUTPUT); 93 pinMode(BLUE_LED_PIN, 94 OUTPUT); 95 pinMode(YELLOW_LED_PIN, OUTPUT); 96 pinMode(RED_LED_PIN, OUTPUT); 97 98 pinMode(chipSelect, OUTPUT); 99 pinMode(BUTTON_PIN, INPUT); 100 pinMode(INTERRUPT_PIN, 101 INPUT); 102 digitalWrite(YELLOW_LED_PIN, LOW); 103 digitalWrite(RED_LED_PIN, LOW); 104 105 digitalWrite(BLUE_LED_PIN, LOW); 106 tone(BUZZER_PIN, 500, 250); 107 //initialize 108 SD Card 109 if(!SD.begin(chipSelect)){ 110 //Serial debugging 111 Serial.println("Could 112 not initialize SD card"); 113 } 114 //clear SD data 115 if(SD.exists("file.txt")){ 116 117 if(SD.remove("file.txt") == true){ 118 Serial.println("removed data"); 119 120 } 121 } 122 //initialize BMP sensor 123 if(BMP.begin()){ 124 Serial.println("BMP 125 init success"); 126 } 127 //initialize IMU and I2C clock 128 #if I2CDEV_IMPLEMENTATION 129 == I2CDEV_ARDUINO_WIRE 130 Wire.begin(); 131 Wire.setClock(400000); 132 133 #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE 134 Fastwire::setup(400, 135 true); 136 #endif 137 mpu.initialize(); //start MPU 138 Serial.println(F("Testing 139 device connections...")); //debugging serial statement 140 Serial.println(mpu.testConnection() 141 ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); //debugging 142 serial statement 143 devStatus = mpu.dmpInitialize(); 144 // supply your own gyro 145 offsets here, scaled for min sensitivity 146 mpu.setXGyroOffset(0); 147 mpu.setYGyroOffset(0); 148 149 mpu.setZGyroOffset(0); 150 mpu.setZAccelOffset(1688); // 1688 factory default 151 for my test chip 152 if (devStatus == 0) { 153 // turn on the DMP, now that 154 it's ready 155 Serial.println(F("Enabling DMP...")); 156 mpu.setDMPEnabled(true); 157 158 // enable Arduino interrupt detection 159 Serial.print(F("Enabling interrupt 160 detection (Arduino external interrupt ")); 161 Serial.print(digitalPinToInterrupt(INTERRUPT_PIN)); 162 163 Serial.println(F(")...")); 164 attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), 165 dmpDataReady, RISING); 166 mpuIntStatus = mpu.getIntStatus(); 167 // set 168 our DMP Ready flag so the main loop() function knows it's okay to use it 169 Serial.println(F("DMP 170 ready! Waiting for first interrupt...")); 171 dmpReady = true; 172 // 173 get expected DMP packet size for later comparison 174 packetSize = mpu.dmpGetFIFOPacketSize(); 175 176 } else { 177 178 Serial.print(F("DMP Initialization failed (code ")); 179 180 Serial.print(devStatus); 181 Serial.println(F(")")); 182 } 183 servo.attach(9); 184 185 //set mode 186 MODE = 1; //set to PAD IDLE mode - initialize sensors and SD card 187 188 //MODE = 2; //set to FLIGHT mode - log data 189 //MODE = 3; //set to RECOVERY 190 mode - close file 191} 192 193void loop() { 194 servo.write(90); 195 delay(100); 196 197 // put your main code here, to run repeatedly: 198 if(MODE == 1){ //PAD IDLE 199 MODE 200 digitalWrite(RED_LED_PIN, HIGH); 201 file = SD.open("file.txt", 202 FILE_WRITE); //Open SD card file 203 if(file) { 204 Serial.println("t,T,P,ax,ay,az,rx,ry,rz"); 205 //print first line with data labels 206 file.println("t,T,P,ax,ay,az,rx,ry,rz"); 207 208 MODE = 2; 209 } 210 else{ 211 Serial.println("Error opening file"); 212 213 delay(5000); //just chill for 5 seconds before trying again 214 } 215 } 216 217 if(MODE == 2){ //ACTIVE FLIGHT mode 218 digitalWrite(YELLOW_LED_PIN, HIGH); 219 220 digitalWrite(RED_LED_PIN, LOW); 221 digitalWrite(BLUE_LED_PIN, LOW); 222 223 //print timestamp and comma to separate data 224 Serial.print(t); 225 Serial.print(","); 226 227 file.print(t); 228 file.print(","); 229 230 char status; 231 double 232 a,P,a1,a2,P2,Difference; 233 234 235 P = getPressure(); 236 237 a1 = pressure.altitude(P,baseline); 238 239 240 delay(3000); 241 242 P2 = getPressure(); 243 244 a2 = pressure.altitude(P2,baseline); 245 246 247 Difference = a2 - a1; 248 if (abs(Difference) > 1) { 249 if (Difference < 250 0 ) Check_downs =+ 1; 251 if (Difference > 0 ) Check_downs = 0; 252 } 253 254 255 Serial.print("Start >"); 256 Serial.print(a1); 257 Serial.print(" meter 258 >"); 259 Serial.println(a2); 260 261 Serial.print("Difference: "); 262 Serial.print(a1 263 - a2); 264 Serial.println("meters"); 265 Serial.print("Check downs : "); 266 267 Serial.print(Check_downs); 268 Serial.println(); 269 270 if (Check_downs == 271 3) { 272 servo.write(180); 273 digitalWrite(RED_LED_PIN ,LOW); 274 digitalWrite(BLUE_LED_PIN,HIGH); 275 276 277 } 278 else 279 { digitalWrite(RED_LED_PIN ,HIGH); 280 digitalWrite(BLUE_LED_PIN,LOW); 281 282 } 283 delay(1000); 284 285 file.print(","); 286 //get IMU data 287 288 if (!dmpReady) return; 289 while (!mpuInterrupt && fifoCount < packetSize){ 290 291 if (mpuInterrupt && fifoCount < packetSize){ 292 fifoCount = mpu.getFIFOCount(); 293 294 } 295 } 296 mpuInterrupt = false; 297 mpuIntStatus = mpu.getIntStatus(); 298 299 fifoCount = mpu.getFIFOCount(); 300 if((mpuIntStatus & _BV(MPU6050_INTERRUPT_FIFO_OFLOW_BIT)) 301 || fifoCount >=1024){ 302 mpu.resetFIFO(); 303 fifoCount = mpu.getFIFOCount(); 304 305 Serial.println("FIFO Overflow!"); 306 } 307 else if(mpuIntStatus & 308 _BV(MPU6050_INTERRUPT_DMP_INT_BIT)){ 309 while(fifoCount < packetSize) fifoCount 310 = mpu.getFIFOCount(); 311 mpu.getFIFOBytes(fifoBuffer, packetSize); 312 fifoCount 313 -= packetSize; 314 //get real-world acceleration 315 mpu.dmpGetQuaternion(&q, 316 fifoBuffer); 317 mpu.dmpGetAccel(&aa, fifoBuffer); 318 mpu.dmpGetGravity(&gravity, 319 &q); 320 mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); 321 //print real-world 322 acceleration 323 Serial.print(aaReal.x); 324 file.print(aaReal.x); 325 326 Serial.print(","); 327 file.print(","); 328 Serial.print(aaReal.y); 329 330 file.print(aaReal.y); 331 Serial.print(","); 332 file.print(","); 333 334 Serial.print(aaReal.z); 335 file.print(aaReal.z); 336 Serial.print(","); 337 338 file.print(","); 339 //get Euler angles 340 mpu.dmpGetQuaternion(&q, 341 fifoBuffer); 342 mpu.dmpGetEuler(euler, &q); 343 //print Euler angles 344 345 Serial.print(euler[0]*180/M_PI); 346 file.print(euler[0]*180/M_PI); 347 348 Serial.print(","); 349 file.print(","); 350 Serial.print(euler[1]*180/M_PI); 351 352 file.print(euler[1]*180/M_PI); 353 Serial.print(","); 354 file.print(","); 355 356 Serial.print(euler[2]*180/M_PI); 357 file.print(euler[2]*180/M_PI); 358 359 } 360 //end data entry line 361 Serial.println(); //ends line 362 file.println(); 363 //ends line 364 //check for mode switch 365 buttonState = digitalRead(BUTTON_PIN); 366 367 if(buttonState == LOW){ 368 MODE = 3; 369 tone(BUZZER_PIN, 1000, 250); 370 371 delay(100); 372 } 373 } 374 if(MODE == 3){ //RECOVERY MODE 375 file.close(); 376 377 digitalWrite(YELLOW_LED_PIN, LOW); 378 digitalWrite(RED_LED_PIN, LOW); 379 380 digitalWrite(BLUE_LED_PIN, HIGH); 381 delay(1000); 382 } 383 t = t + 1; 384 //increment t value 385 delay(1000/dataRate); //pause so that data 386 output corresponds to data rate 387 if(t > 32765){ //prevents issues related 388 to integers rolling over at 32767 389 t = 1; 390 } 391} 392double getPressure() 393{ 394 395 char status; 396 double T,P,p0,a; 397 398 399 status = pressure.startTemperature(); 400 401 if (status != 0) 402 { 403 // Wait for the measurement to complete: 404 405 406 delay(status); 407 408 409 status = pressure.getTemperature(T); 410 if 411 (status != 0) 412 { 413 414 status = pressure.startPressure(3); 415 416 if (status != 0) 417 { 418 // Wait for the measurement to complete: 419 420 delay(status); 421 422 423 424 status = pressure.getPressure(P,T); 425 426 if (status != 0) 427 { 428 return(P); 429 } 430 else 431 Serial.println("error retrieving pressure measurement\ 432"); 433 } 434 else 435 Serial.println("error starting pressure measurement\ 436"); 437 } 438 else 439 Serial.println("error retrieving temperature measurement\ 440"); 441 } 442 else 443 Serial.println("error starting temperature measurement\ 444"); 445} 446 \\ 447
Downloadable files
Schematic
Schematic
Rocket model guide
All codes , Schematics , CAD are provided here
Rocket model guide
Schematic
Schematic
Rocket model guide
All codes , Schematics , CAD are provided here
Rocket model guide
Comments
Only logged in users can leave comments