Lab 7

Dependencies:   MPL3115A2 mbed

Fork of AltTest2 by CPS_Lab*

Committer:
youngs2
Date:
Sun Mar 04 20:39:06 2018 +0000
Revision:
4:eaaf67c5e1ca
Parent:
3:93ba3e6cdaf3
Child:
5:96b831624184
3/4/18

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ajwhelan 0:d7735965d4ca 1 #include "mbed.h"
ajwhelan 0:d7735965d4ca 2 #include "MPL3115A2.h"
ajwhelan 0:d7735965d4ca 3 #include <string.h>
ajwhelan 0:d7735965d4ca 4 #include <stdlib.h>
youngs2 4:eaaf67c5e1ca 5
ajwhelan 0:d7735965d4ca 6 Serial pc(SERIAL_TX, SERIAL_RX);
ajwhelan 0:d7735965d4ca 7 DigitalOut myled(LED1);
youngs2 4:eaaf67c5e1ca 8 DigitalIn lever(PA_12); //D2 'on' = 1
ajwhelan 0:d7735965d4ca 9 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
ajwhelan 0:d7735965d4ca 10
youngs2 4:eaaf67c5e1ca 11 //LocalFileSystem local("local");
youngs2 4:eaaf67c5e1ca 12
youngs2 4:eaaf67c5e1ca 13 /*returns max speed given array of altitudes and length*/
youngs2 4:eaaf67c5e1ca 14 double calcspeed(double j, double *altitude)
youngs2 4:eaaf67c5e1ca 15 {
youngs2 4:eaaf67c5e1ca 16 int i;
youngs2 4:eaaf67c5e1ca 17 double tmp, maxspeed =0;
youngs2 4:eaaf67c5e1ca 18 for(i = 1; i < j; i++)
youngs2 4:eaaf67c5e1ca 19 {
youngs2 4:eaaf67c5e1ca 20 tmp = (altitude[i] - altitude[i-1]);
youngs2 4:eaaf67c5e1ca 21 if(tmp > maxspeed)
youngs2 4:eaaf67c5e1ca 22 maxspeed = tmp;
youngs2 4:eaaf67c5e1ca 23 }
youngs2 4:eaaf67c5e1ca 24 return(maxspeed);
youngs2 4:eaaf67c5e1ca 25 }
youngs2 4:eaaf67c5e1ca 26
youngs2 4:eaaf67c5e1ca 27 void graph(double *alt, int time)
youngs2 4:eaaf67c5e1ca 28 {
youngs2 4:eaaf67c5e1ca 29 FILE *fp = fopen("speed.txt","w");
youngs2 4:eaaf67c5e1ca 30 double speeds[time/2];
youngs2 4:eaaf67c5e1ca 31 int i;
youngs2 4:eaaf67c5e1ca 32 for(i = 1; i < time/2; i++)
youngs2 4:eaaf67c5e1ca 33 speeds[i-1] = alt[i]-alt[i-1];
youngs2 4:eaaf67c5e1ca 34 for(i = 0; i < time/2; i++)
youngs2 4:eaaf67c5e1ca 35 fprintf(fp, "%lf\n\r", speeds[i]);
youngs2 4:eaaf67c5e1ca 36
youngs2 4:eaaf67c5e1ca 37 fclose(fp);
ajwhelan 0:d7735965d4ca 38 }
youngs2 4:eaaf67c5e1ca 39
youngs2 4:eaaf67c5e1ca 40 /* returns max speed */
youngs2 4:eaaf67c5e1ca 41 void elevator(void)
youngs2 4:eaaf67c5e1ca 42 {
youngs2 4:eaaf67c5e1ca 43 double alt[100], pressure[100], temp[100];
youngs2 4:eaaf67c5e1ca 44 double maxspeed;
youngs2 4:eaaf67c5e1ca 45 int j=0, i;
youngs2 4:eaaf67c5e1ca 46 printf("Data collection starting\n\r");
youngs2 4:eaaf67c5e1ca 47 while(1)
youngs2 4:eaaf67c5e1ca 48 {
youngs2 4:eaaf67c5e1ca 49 pressure[j] = pressure_sensor.getPressure();
youngs2 4:eaaf67c5e1ca 50 alt[j] = pressure_sensor.getAltitude();
youngs2 4:eaaf67c5e1ca 51 temp[j] = pressure_sensor.getTemperature();
youngs2 4:eaaf67c5e1ca 52 myled = !myled;
youngs2 4:eaaf67c5e1ca 53 j++;
youngs2 4:eaaf67c5e1ca 54 if(lever == 0)
youngs2 4:eaaf67c5e1ca 55 break;
youngs2 4:eaaf67c5e1ca 56 }
youngs2 4:eaaf67c5e1ca 57 printf("Data collection ended\n\r");
youngs2 4:eaaf67c5e1ca 58 printf("Time \t Altitude(m) \t Pressure(hPa) \t Temperature(C)\n\r");
youngs2 4:eaaf67c5e1ca 59 for(i = 0; i < j; i++)
youngs2 4:eaaf67c5e1ca 60 printf("%d \t %lf \t %lf \t %lf\n\r", i, alt[i], pressure[i], temp[i]);
youngs2 4:eaaf67c5e1ca 61 maxspeed = calcspeed(j, alt);
youngs2 4:eaaf67c5e1ca 62 printf("The max speed was: %lf\n\r", maxspeed);
youngs2 4:eaaf67c5e1ca 63 //graph(alt, j);
youngs2 4:eaaf67c5e1ca 64 }
youngs2 4:eaaf67c5e1ca 65
ajwhelan 0:d7735965d4ca 66 int MPL3115A2_reg_print(int start, int length) {
youngs2 4:eaaf67c5e1ca 67 uint8_t value; //the array with the register value
youngs2 4:eaaf67c5e1ca 68
ajwhelan 0:d7735965d4ca 69 if(length == 0)
ajwhelan 0:d7735965d4ca 70 length = 45;
ajwhelan 0:d7735965d4ca 71 int i = start, end = start + length;
ajwhelan 0:d7735965d4ca 72 if(start > 0x2D || start < 0x00)
ajwhelan 0:d7735965d4ca 73 return(-1);
ajwhelan 0:d7735965d4ca 74 if(length < 0)
ajwhelan 0:d7735965d4ca 75 return(-1);
ajwhelan 0:d7735965d4ca 76 if (pressure_sensor.getID()!=0xC4)
ajwhelan 0:d7735965d4ca 77 return(-1);
ajwhelan 0:d7735965d4ca 78 if (end > 46)
ajwhelan 0:d7735965d4ca 79 return(-1);
youngs2 4:eaaf67c5e1ca 80
youngs2 4:eaaf67c5e1ca 81 for( i = start; i <= end; i++) {
ajwhelan 0:d7735965d4ca 82 char name[25];
youngs2 4:eaaf67c5e1ca 83 pressure_sensor.readRegs(i, &value, 8); //read the registers 1 at a time
ajwhelan 0:d7735965d4ca 84 switch (i) {
ajwhelan 0:d7735965d4ca 85 case 0x00:
ajwhelan 0:d7735965d4ca 86 strcpy(name,"MPL_STATUS");
ajwhelan 0:d7735965d4ca 87 break;
ajwhelan 0:d7735965d4ca 88 case 0x01:
ajwhelan 0:d7735965d4ca 89 strcpy(name,"MPL_OUT_P_MSB");
ajwhelan 0:d7735965d4ca 90 break;
ajwhelan 0:d7735965d4ca 91 case 0x02:
ajwhelan 0:d7735965d4ca 92 strcpy(name, "MPL_OUT_P_CSB");
ajwhelan 0:d7735965d4ca 93 break;
ajwhelan 0:d7735965d4ca 94 case 0x03:
ajwhelan 0:d7735965d4ca 95 strcpy(name,"MPL_OUT_P_LSB");
ajwhelan 0:d7735965d4ca 96 break;
ajwhelan 0:d7735965d4ca 97 case 0x04:
ajwhelan 0:d7735965d4ca 98 strcpy(name,"MPL_OUT_T_MSB");
ajwhelan 0:d7735965d4ca 99 break;
ajwhelan 0:d7735965d4ca 100 case 0x05:
ajwhelan 0:d7735965d4ca 101 strcpy(name,"MPL_OUT_T_LSB");
ajwhelan 0:d7735965d4ca 102 break;
ajwhelan 0:d7735965d4ca 103 case 0x06:
ajwhelan 0:d7735965d4ca 104 strcpy(name,"MPL_DR_STATUS");
ajwhelan 0:d7735965d4ca 105 break;
ajwhelan 0:d7735965d4ca 106 case 0x07:
ajwhelan 0:d7735965d4ca 107 strcpy(name,"MPL_OUT_P_DELTA_MSB");
ajwhelan 0:d7735965d4ca 108 break;
ajwhelan 0:d7735965d4ca 109 case 0x08:
ajwhelan 0:d7735965d4ca 110 strcpy(name,"MPL_OUT_P_DELTA_CSB");
ajwhelan 0:d7735965d4ca 111 break;
ajwhelan 0:d7735965d4ca 112 case 0x09:
ajwhelan 0:d7735965d4ca 113 strcpy(name,"MPL_OUT_P_DELTA_LSB");
ajwhelan 0:d7735965d4ca 114 break;
ajwhelan 0:d7735965d4ca 115 case 0x0A:
ajwhelan 0:d7735965d4ca 116 strcpy(name,"MPL_OUT_T_DELTA_MSB");
ajwhelan 0:d7735965d4ca 117 break;
ajwhelan 0:d7735965d4ca 118 case 0x0B:
ajwhelan 0:d7735965d4ca 119 strcpy(name,"MPL_OUT_T_DELTA_LSB");
ajwhelan 0:d7735965d4ca 120 break;
ajwhelan 0:d7735965d4ca 121 case 0x0C:
ajwhelan 0:d7735965d4ca 122 strcpy(name,"MPL_WHO_AM_I");
ajwhelan 0:d7735965d4ca 123 break;
ajwhelan 0:d7735965d4ca 124 case 0x0D:
ajwhelan 0:d7735965d4ca 125 strcpy(name,"MPL_F_STATUS");
ajwhelan 0:d7735965d4ca 126 break;
ajwhelan 0:d7735965d4ca 127 case 0x0E:
ajwhelan 0:d7735965d4ca 128 strcpy(name,"MPL_F_DATA");
ajwhelan 0:d7735965d4ca 129 break;
ajwhelan 0:d7735965d4ca 130 case 0x0F:
ajwhelan 0:d7735965d4ca 131 strcpy(name,"MPL_F_SETUP");
ajwhelan 0:d7735965d4ca 132 break;
ajwhelan 0:d7735965d4ca 133 case 0x10:
ajwhelan 0:d7735965d4ca 134 strcpy(name,"MPL_TIME_DLY");
ajwhelan 0:d7735965d4ca 135 break;
ajwhelan 0:d7735965d4ca 136 case 0x11:
ajwhelan 0:d7735965d4ca 137 strcpy(name,"MPL_SYSMOD");
ajwhelan 0:d7735965d4ca 138 break;
ajwhelan 0:d7735965d4ca 139 case 0x12:
ajwhelan 0:d7735965d4ca 140 strcpy(name,"MPL_INT_SOURCE");
ajwhelan 0:d7735965d4ca 141 break;
ajwhelan 0:d7735965d4ca 142 case 0x13:
ajwhelan 0:d7735965d4ca 143 strcpy(name,"MPL_PT_DATA_CFG");
ajwhelan 0:d7735965d4ca 144 break;
ajwhelan 0:d7735965d4ca 145 case 0x14:
ajwhelan 0:d7735965d4ca 146 strcpy(name,"MPL_BAR_IN_MSB");
ajwhelan 0:d7735965d4ca 147 break;
ajwhelan 0:d7735965d4ca 148 case 0x15:
ajwhelan 0:d7735965d4ca 149 strcpy(name,"MPL_BAR_IN_LSB");
ajwhelan 0:d7735965d4ca 150 break;
ajwhelan 0:d7735965d4ca 151 case 0x16:
ajwhelan 0:d7735965d4ca 152 strcpy(name,"MPL_P_TGT_MSB");
ajwhelan 0:d7735965d4ca 153 break;
ajwhelan 0:d7735965d4ca 154 case 0x17:
ajwhelan 0:d7735965d4ca 155 strcpy(name,"MPL_P_TGT_LSB");
ajwhelan 0:d7735965d4ca 156 break;
ajwhelan 0:d7735965d4ca 157 case 0x18:
ajwhelan 0:d7735965d4ca 158 strcpy(name,"MPL_T_TGT");
ajwhelan 0:d7735965d4ca 159 break;
ajwhelan 0:d7735965d4ca 160 case 0x19:
ajwhelan 0:d7735965d4ca 161 strcpy(name,"MPL_P_WND_MSB");
ajwhelan 0:d7735965d4ca 162 break;
ajwhelan 0:d7735965d4ca 163 case 0x1A:
ajwhelan 0:d7735965d4ca 164 strcpy(name,"MPL_P_WND_LSB");
ajwhelan 0:d7735965d4ca 165 break;
ajwhelan 0:d7735965d4ca 166 case 0x1B:
ajwhelan 0:d7735965d4ca 167 strcpy(name,"MPL_T_WND");
ajwhelan 0:d7735965d4ca 168 break;
ajwhelan 0:d7735965d4ca 169 case 0x1C:
ajwhelan 0:d7735965d4ca 170 strcpy(name,"MPL_P_MIN_MSB");
ajwhelan 0:d7735965d4ca 171 break;
ajwhelan 0:d7735965d4ca 172 case 0x1D:
ajwhelan 0:d7735965d4ca 173 strcpy(name,"MPL_P_MIN_CSB");
ajwhelan 0:d7735965d4ca 174 break;
ajwhelan 0:d7735965d4ca 175 case 0x1E:
ajwhelan 0:d7735965d4ca 176 strcpy(name,"MPL_P_MIN_LSB");
ajwhelan 0:d7735965d4ca 177 break;
ajwhelan 0:d7735965d4ca 178 case 0x1F:
ajwhelan 0:d7735965d4ca 179 strcpy(name,"MPL_T_MIN_MSB");
ajwhelan 0:d7735965d4ca 180 break;
ajwhelan 0:d7735965d4ca 181 case 0x20:
ajwhelan 0:d7735965d4ca 182 strcpy(name,"MPL_T_MIN_LSB");
ajwhelan 0:d7735965d4ca 183 break;
ajwhelan 0:d7735965d4ca 184 case 0x21:
ajwhelan 0:d7735965d4ca 185 strcpy(name,"MPL_P_MAX_MSB");
ajwhelan 0:d7735965d4ca 186 break;
ajwhelan 0:d7735965d4ca 187 case 0x22:
ajwhelan 0:d7735965d4ca 188 strcpy(name,"MPL_P_MAX_CSB");
ajwhelan 0:d7735965d4ca 189 break;
ajwhelan 0:d7735965d4ca 190 case 0x23:
ajwhelan 0:d7735965d4ca 191 strcpy(name,"MPL_P_MAX_LSB");
ajwhelan 0:d7735965d4ca 192 break;
ajwhelan 0:d7735965d4ca 193 case 0x24:
ajwhelan 0:d7735965d4ca 194 strcpy(name,"MPL_T_MAX_MSB");
ajwhelan 0:d7735965d4ca 195 break;
ajwhelan 0:d7735965d4ca 196 case 0x25:
ajwhelan 0:d7735965d4ca 197 strcpy(name,"MPL_T_MAX_LSB");
ajwhelan 0:d7735965d4ca 198 break;
ajwhelan 0:d7735965d4ca 199 case 0x26:
ajwhelan 0:d7735965d4ca 200 strcpy(name,"MPL_CTRL_REG1");
ajwhelan 0:d7735965d4ca 201 break;
ajwhelan 0:d7735965d4ca 202 case 0x27:
ajwhelan 0:d7735965d4ca 203 strcpy(name,"MPL_CTRL_REG2");
ajwhelan 0:d7735965d4ca 204 break;
ajwhelan 0:d7735965d4ca 205 case 0x28:
ajwhelan 0:d7735965d4ca 206 strcpy(name,"MPL_CTRL_REG3");
ajwhelan 0:d7735965d4ca 207 break;
ajwhelan 0:d7735965d4ca 208 case 0x29:
ajwhelan 0:d7735965d4ca 209 strcpy(name,"MPL_CTRL_REG4");
ajwhelan 0:d7735965d4ca 210 break;
ajwhelan 0:d7735965d4ca 211 case 0x2A:
ajwhelan 0:d7735965d4ca 212 strcpy(name,"MPL_CTRL_REG5");
ajwhelan 0:d7735965d4ca 213 break;
ajwhelan 0:d7735965d4ca 214 case 0x2B:
ajwhelan 0:d7735965d4ca 215 strcpy(name,"MPL_OFF_P");
ajwhelan 0:d7735965d4ca 216 break;
ajwhelan 0:d7735965d4ca 217 case 0x2C:
ajwhelan 0:d7735965d4ca 218 strcpy(name,"MPL_OFF_T");
ajwhelan 0:d7735965d4ca 219 break;
ajwhelan 0:d7735965d4ca 220 case 0x2D:
ajwhelan 0:d7735965d4ca 221 strcpy(name,"MPL_OFF_H");
ajwhelan 0:d7735965d4ca 222 break;
ajwhelan 0:d7735965d4ca 223 default:
ajwhelan 0:d7735965d4ca 224 printf("ERR\n\r");
youngs2 4:eaaf67c5e1ca 225 }
youngs2 4:eaaf67c5e1ca 226 printf("0x%02X: %s = 0x%02X\n\r", i, name, value);
ajwhelan 0:d7735965d4ca 227
ajwhelan 0:d7735965d4ca 228 }
ajwhelan 0:d7735965d4ca 229 return(0);
ajwhelan 0:d7735965d4ca 230 }
youngs2 4:eaaf67c5e1ca 231
youngs2 4:eaaf67c5e1ca 232
ajwhelan 0:d7735965d4ca 233 int main() {
youngs2 4:eaaf67c5e1ca 234 int start, length;
youngs2 4:eaaf67c5e1ca 235 printf("Hola, buenas tardes\n\r");
youngs2 3:93ba3e6cdaf3 236 printf("Enter start: \n\r");
youngs2 3:93ba3e6cdaf3 237 scanf("%d", &start);
youngs2 3:93ba3e6cdaf3 238 printf("Enter length: \n\r");
youngs2 3:93ba3e6cdaf3 239 scanf("%d", &length);
youngs2 4:eaaf67c5e1ca 240 MPL3115A2_reg_print(start, length);
youngs2 4:eaaf67c5e1ca 241 while (1){
youngs2 4:eaaf67c5e1ca 242 if(lever == 1)
youngs2 4:eaaf67c5e1ca 243 elevator();
youngs2 4:eaaf67c5e1ca 244 wait(10);
ajwhelan 1:03565a1413e6 245 }
youngs2 4:eaaf67c5e1ca 246 //no return; while(1) never exited
youngs2 4:eaaf67c5e1ca 247
ajwhelan 0:d7735965d4ca 248 }