Work in progress

Dependencies:   MMA8451Q MPL3115A2 SDFileSystem Servo TSI ledDriver mbed

Committer:
iLyngklip
Date:
Sun Nov 23 02:11:47 2014 +0000
Revision:
0:ea1809c84d7a
Work in progress

Who changed what in which revision?

UserRevisionLine numberNew contents of line
iLyngklip 0:ea1809c84d7a 1 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 2 // Settings *
iLyngklip 0:ea1809c84d7a 3 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 4 // Serial Settings
iLyngklip 0:ea1809c84d7a 5 #define DEBUG_TO_SERIAL 1 // Echo to PC serial
iLyngklip 0:ea1809c84d7a 6 #define SD_CARD 1 // Are we using a SD card at this moment?
iLyngklip 0:ea1809c84d7a 7 #define SECONDS_BETWEEN_MESSAGE 1 // How long between sending a "i'm online" message
iLyngklip 0:ea1809c84d7a 8
iLyngklip 0:ea1809c84d7a 9
iLyngklip 0:ea1809c84d7a 10 // Servo positioning
iLyngklip 0:ea1809c84d7a 11 #define intPos 0.2 // Position for locking the chute
iLyngklip 0:ea1809c84d7a 12 #define rlsPos 1.0 // Position for releasing the chute
iLyngklip 0:ea1809c84d7a 13
iLyngklip 0:ea1809c84d7a 14
iLyngklip 0:ea1809c84d7a 15
iLyngklip 0:ea1809c84d7a 16
iLyngklip 0:ea1809c84d7a 17
iLyngklip 0:ea1809c84d7a 18 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 19 // Includes *
iLyngklip 0:ea1809c84d7a 20 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 21 #include "mbed.h" // Mbed library
iLyngklip 0:ea1809c84d7a 22 #include "SDFileSystem.h" // SD Libary
iLyngklip 0:ea1809c84d7a 23 #include "MMA8451Q.h" // Built-in Accelerometer Libary
iLyngklip 0:ea1809c84d7a 24 #include "TSISensor.h" // Built-in Touch-sensor Libary
iLyngklip 0:ea1809c84d7a 25 #include "MPL3115A2.h" // MPL3115A2 Libary Altitude & pressure sensor
iLyngklip 0:ea1809c84d7a 26 #include "Servo.h" // Servo library
iLyngklip 0:ea1809c84d7a 27
iLyngklip 0:ea1809c84d7a 28
iLyngklip 0:ea1809c84d7a 29
iLyngklip 0:ea1809c84d7a 30
iLyngklip 0:ea1809c84d7a 31 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 32 // Definitions *
iLyngklip 0:ea1809c84d7a 33 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 34 // Adress for the I2C sensors
iLyngklip 0:ea1809c84d7a 35 #define MPL3115A2_I2C_ADDRESS (0x60<<1) // Alt & temp sensor
iLyngklip 0:ea1809c84d7a 36 #define MMA8451_I2C_ADDRESS (0x1d<<1) // Accelerometer
iLyngklip 0:ea1809c84d7a 37
iLyngklip 0:ea1809c84d7a 38
iLyngklip 0:ea1809c84d7a 39 // Calibrating the servo
iLyngklip 0:ea1809c84d7a 40 #define calFirst 0.0010 // First value for calibration
iLyngklip 0:ea1809c84d7a 41 #define calSecond 90.0 // Second value for calibration
iLyngklip 0:ea1809c84d7a 42
iLyngklip 0:ea1809c84d7a 43
iLyngklip 0:ea1809c84d7a 44 // Messages expected to be recieved. In DEC instead of char!
iLyngklip 0:ea1809c84d7a 45 //#define MESSAGE_LP_ONLINE 49 // Online message from LaunchPad (LP) 49 DEC
iLyngklip 0:ea1809c84d7a 46 //#define MESSAGE_RKT_ONLINE 50 // Online message from Rocket (RKT)
iLyngklip 0:ea1809c84d7a 47 //#define MESSAGE_RKT_RDY 52 // Rocket beginning countdown sequence
iLyngklip 0:ea1809c84d7a 48 //#define MESSAGE_LP_RDY 53 // LaunchPad beginning countdown sequence
iLyngklip 0:ea1809c84d7a 49
iLyngklip 0:ea1809c84d7a 50
iLyngklip 0:ea1809c84d7a 51 // Messages expected to be sending.
iLyngklip 0:ea1809c84d7a 52 //#define CONFIRM_ONLINE 8 // Prints "8" in DEC for confirmation to launchpad and rocket (dec 56)
iLyngklip 0:ea1809c84d7a 53 //#define CONFIRM_LAUNCH 3 // "3" for Launch
iLyngklip 0:ea1809c84d7a 54
iLyngklip 0:ea1809c84d7a 55
iLyngklip 0:ea1809c84d7a 56 // Confirmations expected to be sent
iLyngklip 0:ea1809c84d7a 57 #define CONFIRM_ONLINE 0x32 // HEX for "2" - the "i'm online!" message
iLyngklip 0:ea1809c84d7a 58 #define CONFIRM_LAUNCH 0x34 // HEX for "4" - The "OK we'll launch soon"
iLyngklip 0:ea1809c84d7a 59
iLyngklip 0:ea1809c84d7a 60 // Messages expected to recieve
iLyngklip 0:ea1809c84d7a 61 #define MESSAGE_KTP_ONLINE 0x38 // HEX for "8" - Kontrolpanel confirms connection
iLyngklip 0:ea1809c84d7a 62 #define MESSAGE_KTP_LAUNCH 0x33 // HEX for "3" - LAUNCH command from Kontrolpanel
iLyngklip 0:ea1809c84d7a 63 #define MESSAGE_KTP_SD 0x41 // HEX for "A" - For starting to save data to the SD card. Used in pre-launch (in countdown sequence)
iLyngklip 0:ea1809c84d7a 64
iLyngklip 0:ea1809c84d7a 65 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 66 // Constructors *
iLyngklip 0:ea1809c84d7a 67 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 68 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); // Built-in Accelerometer
iLyngklip 0:ea1809c84d7a 69 MPL3115A2 wigo_sensor1( PTE0, PTE1, MPL3115A2_I2C_ADDRESS); // Pressure- and temperature-sensor!
iLyngklip 0:ea1809c84d7a 70 TSISensor tsi; //-------------------------------------------// TouchSensor på Mbed'en
iLyngklip 0:ea1809c84d7a 71 SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); // SD Shield
iLyngklip 0:ea1809c84d7a 72 Serial pc(USBTX, USBRX); // Serial w/ PC through wire
iLyngklip 0:ea1809c84d7a 73 Serial XB(PTC4, PTC3); // Serial XBee
iLyngklip 0:ea1809c84d7a 74
iLyngklip 0:ea1809c84d7a 75 Timer t; // Timer "t" for keeping track of time
iLyngklip 0:ea1809c84d7a 76
iLyngklip 0:ea1809c84d7a 77
iLyngklip 0:ea1809c84d7a 78
iLyngklip 0:ea1809c84d7a 79 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 80 // Helper-functions for the Altimeter *
iLyngklip 0:ea1809c84d7a 81 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 82 float print_PressureValue( unsigned char *dt);
iLyngklip 0:ea1809c84d7a 83 float print_AltimiterValue( unsigned char *dt);
iLyngklip 0:ea1809c84d7a 84 float print_TemperatureValue( unsigned char *dt);
iLyngklip 0:ea1809c84d7a 85 float sensor_data[2];
iLyngklip 0:ea1809c84d7a 86 void dataready( void); // callback function for data streaming using Interrupt
iLyngklip 0:ea1809c84d7a 87 void alttrigger( void);
iLyngklip 0:ea1809c84d7a 88
iLyngklip 0:ea1809c84d7a 89
iLyngklip 0:ea1809c84d7a 90
iLyngklip 0:ea1809c84d7a 91
iLyngklip 0:ea1809c84d7a 92
iLyngklip 0:ea1809c84d7a 93
iLyngklip 0:ea1809c84d7a 94
iLyngklip 0:ea1809c84d7a 95 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 96 // Variables *
iLyngklip 0:ea1809c84d7a 97 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 98 // Filecreation
iLyngklip 0:ea1809c84d7a 99 int fileOpen = 0;
iLyngklip 0:ea1809c84d7a 100 char fileName[23] = "/sd/mydir/LOGGER00.CSV";
iLyngklip 0:ea1809c84d7a 101
iLyngklip 0:ea1809c84d7a 102
iLyngklip 0:ea1809c84d7a 103 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 104 // Setup *
iLyngklip 0:ea1809c84d7a 105 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 106 int main(){
iLyngklip 0:ea1809c84d7a 107 Servo myservo(PTA4);
iLyngklip 0:ea1809c84d7a 108
iLyngklip 0:ea1809c84d7a 109 myservo.calibrate(calFirst, calSecond);
iLyngklip 0:ea1809c84d7a 110 myservo.write(intPos);
iLyngklip 0:ea1809c84d7a 111
iLyngklip 0:ea1809c84d7a 112
iLyngklip 0:ea1809c84d7a 113 #if DEBUG_TO_SERIAL
iLyngklip 0:ea1809c84d7a 114 pc.baud(9600);
iLyngklip 0:ea1809c84d7a 115 #endif
iLyngklip 0:ea1809c84d7a 116 XB.baud(9600);
iLyngklip 0:ea1809c84d7a 117
iLyngklip 0:ea1809c84d7a 118
iLyngklip 0:ea1809c84d7a 119 // ***********************************************
iLyngklip 0:ea1809c84d7a 120 // Setting up the altimeter & temp sensor *
iLyngklip 0:ea1809c84d7a 121 // ***********************************************
iLyngklip 0:ea1809c84d7a 122
iLyngklip 0:ea1809c84d7a 123 wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_8); // Set over sampling value (see MPL3115A2.h for details)
iLyngklip 0:ea1809c84d7a 124 wigo_sensor1.Altimeter_Mode(); // Configure the sensor
iLyngklip 0:ea1809c84d7a 125
iLyngklip 0:ea1809c84d7a 126
iLyngklip 0:ea1809c84d7a 127 // ***********************************************
iLyngklip 0:ea1809c84d7a 128 // Filecreating *
iLyngklip 0:ea1809c84d7a 129 // ***********************************************
iLyngklip 0:ea1809c84d7a 130 #if DEBUG_TO_SERIAL
iLyngklip 0:ea1809c84d7a 131 XB.printf("SD Namecalling\n");
iLyngklip 0:ea1809c84d7a 132 #endif
iLyngklip 0:ea1809c84d7a 133
iLyngklip 0:ea1809c84d7a 134 #if SD_CARD
iLyngklip 0:ea1809c84d7a 135 mkdir("/sd/mydir", 0777); // Making the folder in where to put the file
iLyngklip 0:ea1809c84d7a 136 int i = 1;
iLyngklip 0:ea1809c84d7a 137 nameFinder:
iLyngklip 0:ea1809c84d7a 138 fileName[18] = '.'; // Making the dot in .CSV
iLyngklip 0:ea1809c84d7a 139 fileName[17] = i%10 + '0'; // Numbering the filename
iLyngklip 0:ea1809c84d7a 140 fileName[16] = i/10 + '0'; // to something not already there
iLyngklip 0:ea1809c84d7a 141 i++;
iLyngklip 0:ea1809c84d7a 142 FILE *fp = fopen(fileName, "r" ); // This checks if there is a file called fileName
iLyngklip 0:ea1809c84d7a 143 if(fp != NULL){
iLyngklip 0:ea1809c84d7a 144 #if DEBUG_TO_SERIAL
iLyngklip 0:ea1809c84d7a 145 XB.printf("File existed.\n");
iLyngklip 0:ea1809c84d7a 146 #endif
iLyngklip 0:ea1809c84d7a 147
iLyngklip 0:ea1809c84d7a 148 goto nameFinder;
iLyngklip 0:ea1809c84d7a 149 }
iLyngklip 0:ea1809c84d7a 150
iLyngklip 0:ea1809c84d7a 151 if(fp == NULL){
iLyngklip 0:ea1809c84d7a 152 #if DEBUG_TO_SERIAL
iLyngklip 0:ea1809c84d7a 153 XB.printf("fileName %c did not exist, creating one now!\n", fileName );
iLyngklip 0:ea1809c84d7a 154 XB.printf(fileName);
iLyngklip 0:ea1809c84d7a 155 XB.printf("\n");
iLyngklip 0:ea1809c84d7a 156 #endif
iLyngklip 0:ea1809c84d7a 157
iLyngklip 0:ea1809c84d7a 158 fp = fopen(fileName, "w" ); // This creates a file called fileName
iLyngklip 0:ea1809c84d7a 159 if(fp == NULL){
iLyngklip 0:ea1809c84d7a 160 error("failed");
iLyngklip 0:ea1809c84d7a 161 }
iLyngklip 0:ea1809c84d7a 162 }
iLyngklip 0:ea1809c84d7a 163 #if DEBUG_TO_SERIAL
iLyngklip 0:ea1809c84d7a 164 XB.printf("SD File created in the Rocket.\n");
iLyngklip 0:ea1809c84d7a 165 #endif
iLyngklip 0:ea1809c84d7a 166 #endif //if SD
iLyngklip 0:ea1809c84d7a 167
iLyngklip 0:ea1809c84d7a 168
iLyngklip 0:ea1809c84d7a 169
iLyngklip 0:ea1809c84d7a 170
iLyngklip 0:ea1809c84d7a 171
iLyngklip 0:ea1809c84d7a 172
iLyngklip 0:ea1809c84d7a 173
iLyngklip 0:ea1809c84d7a 174
iLyngklip 0:ea1809c84d7a 175
iLyngklip 0:ea1809c84d7a 176
iLyngklip 0:ea1809c84d7a 177
iLyngklip 0:ea1809c84d7a 178
iLyngklip 0:ea1809c84d7a 179
iLyngklip 0:ea1809c84d7a 180 // ***********************************************
iLyngklip 0:ea1809c84d7a 181 // Mode 0: Handshaking with KTP *
iLyngklip 0:ea1809c84d7a 182 // ***********************************************
iLyngklip 0:ea1809c84d7a 183 t.start();
iLyngklip 0:ea1809c84d7a 184 int time1 = t.read();
iLyngklip 0:ea1809c84d7a 185 bool confirmed_rdy = false;
iLyngklip 0:ea1809c84d7a 186
iLyngklip 0:ea1809c84d7a 187 while(confirmed_rdy == false){
iLyngklip 0:ea1809c84d7a 188 if(time1 - t.read() >= SECONDS_BETWEEN_MESSAGE * 1000){
iLyngklip 0:ea1809c84d7a 189 XB.putc(CONFIRM_ONLINE);
iLyngklip 0:ea1809c84d7a 190 time1 = t.read();
iLyngklip 0:ea1809c84d7a 191 }// if
iLyngklip 0:ea1809c84d7a 192
iLyngklip 0:ea1809c84d7a 193 while(time1 - t.read() < SECONDS_BETWEEN_MESSAGE * 1000){
iLyngklip 0:ea1809c84d7a 194 if(XB.getc() == MESSAGE_KTP_ONLINE){
iLyngklip 0:ea1809c84d7a 195 t.stop();
iLyngklip 0:ea1809c84d7a 196 confirmed_rdy = true;
iLyngklip 0:ea1809c84d7a 197 }// if
iLyngklip 0:ea1809c84d7a 198 }// While time
iLyngklip 0:ea1809c84d7a 199 }// while !confirmed_rdy
iLyngklip 0:ea1809c84d7a 200 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 201 // Loop *
iLyngklip 0:ea1809c84d7a 202 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 203 while(1){
iLyngklip 0:ea1809c84d7a 204 // ***********************************************
iLyngklip 0:ea1809c84d7a 205 // Mode 1: Listening for MESSAGE_KTP_SD *
iLyngklip 0:ea1809c84d7a 206 // ***********************************************
iLyngklip 0:ea1809c84d7a 207 bool confirmed_sd = false;
iLyngklip 0:ea1809c84d7a 208 while(confirmed_sd == false){
iLyngklip 0:ea1809c84d7a 209 if(XB.readable()){
iLyngklip 0:ea1809c84d7a 210 if(XB.getc() == MESSAGE_KTP_SD){
iLyngklip 0:ea1809c84d7a 211 confirmed_sd = true;
iLyngklip 0:ea1809c84d7a 212 }// if MESSAGE_KTP_SD
iLyngklip 0:ea1809c84d7a 213 }// if READABLE
iLyngklip 0:ea1809c84d7a 214 }// while Mode 1
iLyngklip 0:ea1809c84d7a 215 // ***********************************************
iLyngklip 0:ea1809c84d7a 216 // Mode 2: Start logging data to SD *
iLyngklip 0:ea1809c84d7a 217 // ***********************************************
iLyngklip 0:ea1809c84d7a 218
iLyngklip 0:ea1809c84d7a 219
iLyngklip 0:ea1809c84d7a 220
iLyngklip 0:ea1809c84d7a 221
iLyngklip 0:ea1809c84d7a 222 }// While loop. The code should stop looping after this point. Keep this close to "Recovered stage"
iLyngklip 0:ea1809c84d7a 223 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 224 // Recovered stage: Writes end-message to SD-card datastream. *
iLyngklip 0:ea1809c84d7a 225 // *********************************************************************************************************************************************************
iLyngklip 0:ea1809c84d7a 226
iLyngklip 0:ea1809c84d7a 227
iLyngklip 0:ea1809c84d7a 228
iLyngklip 0:ea1809c84d7a 229
iLyngklip 0:ea1809c84d7a 230 }// main