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.
main.cpp@8:542ae9ef1003, 2019-06-26 (annotated)
- Committer:
- pmic
- Date:
- Wed Jun 26 09:51:41 2019 +0000
- Revision:
- 8:542ae9ef1003
- Parent:
- 7:3f4048c1cc81
commit before holiday
Who changed what in which revision?
User | Revision | Line number | New 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 | 8:542ae9ef1003 | 24 | Timer twhile; // timer for time measurement (usage in while(1), without timer attached) |
pmic | 6:898a83da0661 | 25 | |
pmic | 6:898a83da0661 | 26 | int k; |
pmic | 6:898a83da0661 | 27 | bool doRun = false; |
pmic | 8:542ae9ef1003 | 28 | float Ts = 0.05f; // sample time of main loop, 20 Hz |
pmic | 6:898a83da0661 | 29 | |
pmic | 6:898a83da0661 | 30 | IIR_filter pt1(0.2f, Ts, 1.0f); |
pmic | 6:898a83da0661 | 31 | float altitudef = 0.0f; |
pmic | 6:898a83da0661 | 32 | |
pmic | 6:898a83da0661 | 33 | // user defined functions |
pmic | 6:898a83da0661 | 34 | void updateLoop(void); // loop (via interrupt) |
pmic | 6:898a83da0661 | 35 | void pressed(void); // user Button pressed |
pmic | 6:898a83da0661 | 36 | void released(void); // user Button released |
pmic | 6:898a83da0661 | 37 | |
pmic | 6:898a83da0661 | 38 | // main program and control loop |
pmic | 6:898a83da0661 | 39 | // ----------------------------------------------------------------------------- |
pmic | 0:5119eeafd9ce | 40 | int main() |
pmic | 0:5119eeafd9ce | 41 | { |
pmic | 8:542ae9ef1003 | 42 | pc.baud(2000000); // for serial comm. |
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 | 8:542ae9ef1003 | 48 | wait(1.0f); |
pmic | 8:542ae9ef1003 | 49 | // LoopTimer.attach(&updateLoop, Ts); // attach loop to timer interrupt |
pmic | 7:3f4048c1cc81 | 50 | ///* |
pmic | 8:542ae9ef1003 | 51 | twhile.start(); // timer for time measurement (usage in while(1), without timer attached) |
pmic | 7:3f4048c1cc81 | 52 | while(1) { |
pmic | 7:3f4048c1cc81 | 53 | altitude = barometer(); |
pmic | 7:3f4048c1cc81 | 54 | altitudef = pt1(altitude); |
pmic | 7:3f4048c1cc81 | 55 | if(doRun) { |
pmic | 7:3f4048c1cc81 | 56 | // use this section to do dynamical measurements |
pmic | 8:542ae9ef1003 | 57 | if(doRun && k++ < 2000) { |
pmic | 8:542ae9ef1003 | 58 | // if(doRun) { |
pmic | 8:542ae9ef1003 | 59 | // k++; |
pmic | 8:542ae9ef1003 | 60 | pc.printf("%10i %10.6e %10.6e\r\n", k, twhile.read(), altitude); |
pmic | 7:3f4048c1cc81 | 61 | } |
pmic | 7:3f4048c1cc81 | 62 | } |
pmic | 7:3f4048c1cc81 | 63 | } |
pmic | 7:3f4048c1cc81 | 64 | //*/ |
pmic | 6:898a83da0661 | 65 | } |
pmic | 6:898a83da0661 | 66 | |
pmic | 7:3f4048c1cc81 | 67 | // original |
pmic | 7:3f4048c1cc81 | 68 | /* |
pmic | 7:3f4048c1cc81 | 69 | pc.baud(115200); |
pmic | 7:3f4048c1cc81 | 70 | barometer.reset(); |
pmic | 7:3f4048c1cc81 | 71 | while(1) { |
pmic | 7:3f4048c1cc81 | 72 | altitude = barometer(); |
pmic | 7:3f4048c1cc81 | 73 | pc.printf("altitude %f\r\n", altitude); |
pmic | 7:3f4048c1cc81 | 74 | wait_ms(20); // don't hammer the serial console |
pmic | 7:3f4048c1cc81 | 75 | } |
pmic | 7:3f4048c1cc81 | 76 | */ |
pmic | 7:3f4048c1cc81 | 77 | |
pmic | 6:898a83da0661 | 78 | // the updateLoop starts as soon as you pressed the blue botton |
pmic | 6:898a83da0661 | 79 | void updateLoop(void) |
pmic | 6:898a83da0661 | 80 | { |
pmic | 6:898a83da0661 | 81 | altitude = barometer(); |
pmic | 6:898a83da0661 | 82 | altitudef = pt1(altitude); |
pmic | 6:898a83da0661 | 83 | if(doRun) { |
pmic | 7:3f4048c1cc81 | 84 | ///* |
pmic | 6:898a83da0661 | 85 | // use this section to do dynamical measurements |
pmic | 8:542ae9ef1003 | 86 | if(doRun && k++ < 2000) { |
pmic | 8:542ae9ef1003 | 87 | pc.printf("%10i %10.6e\r\n", k, altitude); |
pmic | 6:898a83da0661 | 88 | } |
pmic | 7:3f4048c1cc81 | 89 | //*/ |
pmic | 7:3f4048c1cc81 | 90 | /* |
pmic | 6:898a83da0661 | 91 | // use this section to do static measurements |
pmic | 6:898a83da0661 | 92 | if(doRun && k++%25 == 0) { |
pmic | 6:898a83da0661 | 93 | pc.printf("%10i %10.6e\r\n", k, altitudef); |
pmic | 6:898a83da0661 | 94 | } |
pmic | 7:3f4048c1cc81 | 95 | */ |
pmic | 0:5119eeafd9ce | 96 | } |
pmic | 0:5119eeafd9ce | 97 | } |
pmic | 6:898a83da0661 | 98 | |
pmic | 6:898a83da0661 | 99 | // buttonhandling |
pmic | 6:898a83da0661 | 100 | // ----------------------------------------------------------------------------- |
pmic | 6:898a83da0661 | 101 | // start timer as soon as button is pressed |
pmic | 6:898a83da0661 | 102 | void pressed() |
pmic | 6:898a83da0661 | 103 | { |
pmic | 6:898a83da0661 | 104 | t.start(); |
pmic | 6:898a83da0661 | 105 | } |
pmic | 6:898a83da0661 | 106 | |
pmic | 6:898a83da0661 | 107 | // evaluating statemachine |
pmic | 6:898a83da0661 | 108 | void released() |
pmic | 6:898a83da0661 | 109 | { |
pmic | 6:898a83da0661 | 110 | // toggle state over boolean |
pmic | 6:898a83da0661 | 111 | if(doRun) { |
pmic | 6:898a83da0661 | 112 | k = 0; |
pmic | 6:898a83da0661 | 113 | pt1.reset(0.0f); |
pmic | 8:542ae9ef1003 | 114 | twhile.reset(); // timer for time measurement (usage in while(1), without timer attached) |
pmic | 6:898a83da0661 | 115 | } |
pmic | 6:898a83da0661 | 116 | doRun = !doRun; |
pmic | 6:898a83da0661 | 117 | t.stop(); |
pmic | 6:898a83da0661 | 118 | t.reset(); |
pmic | 6:898a83da0661 | 119 | } |