altb_pmic / Mbed 2 deprecated Grove-Barometric_Pressure_Sensor_Example

Dependencies:   HP206C mbed

Committer:
pmic
Date:
Tue Jun 25 15:23:40 2019 +0000
Revision:
7:3f4048c1cc81
Parent:
6:898a83da0661
Child:
8:542ae9ef1003
changes to work with interrupt in main, it is not working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 0:5119eeafd9ce 1 #include "mbed.h"
pmic 2:ecd5c6118888 2 #include "HP206C.h"
pmic 6:898a83da0661 3 #include "IIR_filter.h"
pmic 0:5119eeafd9ce 4
pmic 6:898a83da0661 5 /* Notes pmic 25.06.2019:
pmic 7:3f4048c1cc81 6 -
pmic 7:3f4048c1cc81 7 */
pmic 7:3f4048c1cc81 8
pmic 7:3f4048c1cc81 9 /*
pmic 7:3f4048c1cc81 10 Serial pc(SERIAL_TX, SERIAL_RX);
pmic 7:3f4048c1cc81 11 HP206C barometer(D14, D15);
pmic 7:3f4048c1cc81 12 float altitude = 0.0f;
pmic 7:3f4048c1cc81 13
pmic 7:3f4048c1cc81 14
pmic 6:898a83da0661 15 */
pmic 6:898a83da0661 16
pmic 6:898a83da0661 17 HP206C barometer(D14, D15); // 20 Hz parametrization
pmic 0:5119eeafd9ce 18 float altitude = 0.0f;
pmic 0:5119eeafd9ce 19
pmic 6:898a83da0661 20 Serial pc(SERIAL_TX, SERIAL_RX); // serial connection via USB - programmer
pmic 6:898a83da0661 21 InterruptIn Button(USER_BUTTON); // User Button
pmic 6:898a83da0661 22 Ticker LoopTimer; // interrupt for control loop
pmic 6:898a83da0661 23 Timer t; // timer to analyse Button
pmic 6:898a83da0661 24
pmic 6:898a83da0661 25 int k;
pmic 6:898a83da0661 26 bool doRun = false;
pmic 7:3f4048c1cc81 27 float Ts = 1.0f; // sample time of main loop, 20 Hz
pmic 6:898a83da0661 28
pmic 6:898a83da0661 29 IIR_filter pt1(0.2f, Ts, 1.0f);
pmic 6:898a83da0661 30 float altitudef = 0.0f;
pmic 6:898a83da0661 31
pmic 6:898a83da0661 32 // user defined functions
pmic 6:898a83da0661 33 void updateLoop(void); // loop (via interrupt)
pmic 6:898a83da0661 34 void pressed(void); // user Button pressed
pmic 6:898a83da0661 35 void released(void); // user Button released
pmic 6:898a83da0661 36
pmic 6:898a83da0661 37 // main program and control loop
pmic 6:898a83da0661 38 // -----------------------------------------------------------------------------
pmic 0:5119eeafd9ce 39 int main()
pmic 0:5119eeafd9ce 40 {
pmic 7:3f4048c1cc81 41 pc.baud(115200); // for serial comm.
pmic 7:3f4048c1cc81 42 // LoopTimer.attach(&updateLoop, Ts); // attach loop to timer interrupt
pmic 6:898a83da0661 43 Button.fall(&pressed); // attach key pressed function
pmic 6:898a83da0661 44 Button.rise(&released); // attach key pressed function
pmic 6:898a83da0661 45 k = 0;
pmic 6:898a83da0661 46 pt1.reset(0.0f);
pmic 2:ecd5c6118888 47 barometer.reset();
pmic 7:3f4048c1cc81 48 ///*
pmic 7:3f4048c1cc81 49 while(1) {
pmic 7:3f4048c1cc81 50 altitude = barometer();
pmic 7:3f4048c1cc81 51 altitudef = pt1(altitude);
pmic 7:3f4048c1cc81 52 if(doRun) {
pmic 7:3f4048c1cc81 53 // use this section to do dynamical measurements
pmic 7:3f4048c1cc81 54 if(doRun && k++ < 4000) {
pmic 7:3f4048c1cc81 55 pc.printf("%10i %10.9e\r\n", k, altitude);
pmic 7:3f4048c1cc81 56 }
pmic 7:3f4048c1cc81 57 }
pmic 7:3f4048c1cc81 58 wait_us(16000);
pmic 7:3f4048c1cc81 59 }
pmic 7:3f4048c1cc81 60 //*/
pmic 6:898a83da0661 61 }
pmic 6:898a83da0661 62
pmic 7:3f4048c1cc81 63 // original
pmic 7:3f4048c1cc81 64 /*
pmic 7:3f4048c1cc81 65 pc.baud(115200);
pmic 7:3f4048c1cc81 66 barometer.reset();
pmic 7:3f4048c1cc81 67 while(1) {
pmic 7:3f4048c1cc81 68 altitude = barometer();
pmic 7:3f4048c1cc81 69 pc.printf("altitude %f\r\n", altitude);
pmic 7:3f4048c1cc81 70 wait_ms(20); // don't hammer the serial console
pmic 7:3f4048c1cc81 71 }
pmic 7:3f4048c1cc81 72 */
pmic 7:3f4048c1cc81 73
pmic 6:898a83da0661 74 // the updateLoop starts as soon as you pressed the blue botton
pmic 6:898a83da0661 75 void updateLoop(void)
pmic 6:898a83da0661 76 {
pmic 6:898a83da0661 77 altitude = barometer();
pmic 6:898a83da0661 78 altitudef = pt1(altitude);
pmic 6:898a83da0661 79 if(doRun) {
pmic 7:3f4048c1cc81 80 ///*
pmic 6:898a83da0661 81 // use this section to do dynamical measurements
pmic 6:898a83da0661 82 if(doRun && k++ < 4000) {
pmic 6:898a83da0661 83 pc.printf("%10i %10.9e\r\n", k, altitude);
pmic 6:898a83da0661 84 }
pmic 7:3f4048c1cc81 85 //*/
pmic 7:3f4048c1cc81 86 /*
pmic 6:898a83da0661 87 // use this section to do static measurements
pmic 6:898a83da0661 88 if(doRun && k++%25 == 0) {
pmic 6:898a83da0661 89 pc.printf("%10i %10.6e\r\n", k, altitudef);
pmic 6:898a83da0661 90 }
pmic 7:3f4048c1cc81 91 */
pmic 0:5119eeafd9ce 92 }
pmic 0:5119eeafd9ce 93 }
pmic 6:898a83da0661 94
pmic 6:898a83da0661 95 // buttonhandling
pmic 6:898a83da0661 96 // -----------------------------------------------------------------------------
pmic 6:898a83da0661 97 // start timer as soon as button is pressed
pmic 6:898a83da0661 98 void pressed()
pmic 6:898a83da0661 99 {
pmic 6:898a83da0661 100 t.start();
pmic 6:898a83da0661 101 }
pmic 6:898a83da0661 102
pmic 6:898a83da0661 103 // evaluating statemachine
pmic 6:898a83da0661 104 void released()
pmic 6:898a83da0661 105 {
pmic 6:898a83da0661 106 // toggle state over boolean
pmic 6:898a83da0661 107 if(doRun) {
pmic 6:898a83da0661 108 k = 0;
pmic 6:898a83da0661 109 pt1.reset(0.0f);
pmic 6:898a83da0661 110 }
pmic 6:898a83da0661 111 doRun = !doRun;
pmic 6:898a83da0661 112 t.stop();
pmic 6:898a83da0661 113 t.reset();
pmic 6:898a83da0661 114 }