Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of AltTest2 by
Revision 4:eaaf67c5e1ca, committed 2018-03-04
- Comitter:
- youngs2
- Date:
- Sun Mar 04 20:39:06 2018 +0000
- Parent:
- 3:93ba3e6cdaf3
- Child:
- 5:96b831624184
- Commit message:
- 3/4/18
Changed in this revision
| MPL3115A2.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/MPL3115A2.lib Fri Mar 02 17:41:05 2018 +0000 +++ b/MPL3115A2.lib Sun Mar 04 20:39:06 2018 +0000 @@ -1,1 +1,1 @@ -http://os.mbed.com/teams/MSS/code/MPL3115A2/#1a02333f7445 +https://os.mbed.com/teams/CPS_Lab/code/MPL3115A2/#1a02333f7445
--- a/main.cpp Fri Mar 02 17:41:05 2018 +0000
+++ b/main.cpp Sun Mar 04 20:39:06 2018 +0000
@@ -2,21 +2,70 @@
#include "MPL3115A2.h"
#include <string.h>
#include <stdlib.h>
-
+
Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
+DigitalIn lever(PA_12); //D2 'on' = 1
MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
-/*
-char *int_array_to_string(uint8_t int_array[], int size_of_array) {
- char returnstring[size_of_array];
- for (int temp = 0; temp < size_of_array; temp++)
- returnstring[temp] = itoa(int_array[temp]);
- return returnstring;
+//LocalFileSystem local("local");
+
+/*returns max speed given array of altitudes and length*/
+double calcspeed(double j, double *altitude)
+{
+ int i;
+ double tmp, maxspeed =0;
+ for(i = 1; i < j; i++)
+ {
+ tmp = (altitude[i] - altitude[i-1]);
+ if(tmp > maxspeed)
+ maxspeed = tmp;
+ }
+ return(maxspeed);
+}
+
+void graph(double *alt, int time)
+{
+ FILE *fp = fopen("speed.txt","w");
+ double speeds[time/2];
+ int i;
+ for(i = 1; i < time/2; i++)
+ speeds[i-1] = alt[i]-alt[i-1];
+ for(i = 0; i < time/2; i++)
+ fprintf(fp, "%lf\n\r", speeds[i]);
+
+ fclose(fp);
}
-*/
+
+/* returns max speed */
+void elevator(void)
+{
+ double alt[100], pressure[100], temp[100];
+ double maxspeed;
+ int j=0, i;
+ printf("Data collection starting\n\r");
+ while(1)
+ {
+ pressure[j] = pressure_sensor.getPressure();
+ alt[j] = pressure_sensor.getAltitude();
+ temp[j] = pressure_sensor.getTemperature();
+ myled = !myled;
+ j++;
+ if(lever == 0)
+ break;
+ }
+ printf("Data collection ended\n\r");
+ printf("Time \t Altitude(m) \t Pressure(hPa) \t Temperature(C)\n\r");
+ for(i = 0; i < j; i++)
+ printf("%d \t %lf \t %lf \t %lf\n\r", i, alt[i], pressure[i], temp[i]);
+ maxspeed = calcspeed(j, alt);
+ printf("The max speed was: %lf\n\r", maxspeed);
+ //graph(alt, j);
+}
+
int MPL3115A2_reg_print(int start, int length) {
-
+ uint8_t value; //the array with the register value
+
if(length == 0)
length = 45;
int i = start, end = start + length;
@@ -28,14 +77,10 @@
return(-1);
if (end > 46)
return(-1);
-
- printf("Hola\n\r");
- uint8_t valArray[10]; //the array of register values we'll be returning
- for(int j = 0; j<10; j++)
- valArray[j] = 7;
-
- for( i = start; i <=end; i++) {
+
+ for( i = start; i <= end; i++) {
char name[25];
+ pressure_sensor.readRegs(i, &value, 8); //read the registers 1 at a time
switch (i) {
case 0x00:
strcpy(name,"MPL_STATUS");
@@ -177,41 +222,27 @@
break;
default:
printf("ERR\n\r");
- }
- pressure_sensor.readRegs(i, valArray, 1);
- printf("0x%02X: %s = 0x%02x\n\r", i, name, valArray[0]);
+ }
+ printf("0x%02X: %s = 0x%02X\n\r", i, name, value);
-
}
return(0);
}
-
-
+
+
int main() {
- int start, length, return_val;
- uint8_t id;
- printf("Hola\n\r");
+ int start, length;
+ printf("Hola, buenas tardes\n\r");
printf("Enter start: \n\r");
scanf("%d", &start);
printf("Enter length: \n\r");
scanf("%d", &length);
- printf("Printing registers: \n\r");
- return_val = MPL3115A2_reg_print(start, length);
- printf("%d\n\r", return_val);
- pc.printf("\n\r*** MPL3115A2 Pressure/Temperature Sensor Test *** \n\r");
- while ((id=pressure_sensor.getID())!=0xC4) {
- pc.printf("Status read unsuccessful: Value = 0x%02x\n\r",id);
- pc.printf("Check wiring to the pressure sensor\n\r",id);
- pc.printf("Retesting for correct ID in 1 second...\n\r");
- wait(1);
+ MPL3115A2_reg_print(start, length);
+ while (1){
+ if(lever == 1)
+ elevator();
+ wait(10);
}
- pc.printf("Status read successfully: Value = 0x%02x\n\r",id);
- pc.printf("***1hz readings from the pressure sensor***\n\r");
- while(1) {
- pc.printf("%f hPA; %f m\n\r",pressure_sensor.getPressure(),pressure_sensor.getAltitude());
- myled = !myled;
- wait(1);
- }
-
- //no return; while(1) never exited
+ //no return; while(1) never exited
+
}
\ No newline at end of file
