Version 3 is with update to the test rig with a linear actuator
Dependencies: SPTE_10Bar_5V mbed AS5048 SDFileSystem MODSERIAL PinDetect LCM101 LinearActuator
main.cpp@5:63063a9fa51c, 2019-12-09 (annotated)
- Committer:
- cnckiwi31
- Date:
- Mon Dec 09 10:51:46 2019 +0000
- Revision:
- 5:63063a9fa51c
- Parent:
- 4:1cdce6c6c94e
- Child:
- 10:77fcbad99a31
Ready to run on test rig
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cnckiwi31 | 5:63063a9fa51c | 1 | /** |
cnckiwi31 | 5:63063a9fa51c | 2 | * Author: Allan Veale |
cnckiwi31 | 5:63063a9fa51c | 3 | * Date: 27/11/19 |
cnckiwi31 | 5:63063a9fa51c | 4 | * Purpose: Datalog from the active wearable test rig fitted with the first |
cnckiwi31 | 5:63063a9fa51c | 5 | * realistic (foam tissue) leg |
cnckiwi31 | 5:63063a9fa51c | 6 | */ |
megrootens | 0:3855d4588f76 | 7 | |
cnckiwi31 | 5:63063a9fa51c | 8 | //Both the general mbed header and the test rig bench header are needed |
cnckiwi31 | 5:63063a9fa51c | 9 | #include "mbed.h" |
cnckiwi31 | 5:63063a9fa51c | 10 | #include "bench.h" |
megrootens | 0:3855d4588f76 | 11 | |
cnckiwi31 | 5:63063a9fa51c | 12 | //Example experiment method |
cnckiwi31 | 5:63063a9fa51c | 13 | void runDemoExperiment0(int cycles, float targetkPa); |
megrootens | 0:3855d4588f76 | 14 | |
cnckiwi31 | 5:63063a9fa51c | 15 | // Create bench object - this is used to control the test rig |
cnckiwi31 | 5:63063a9fa51c | 16 | Bench leg; |
megrootens | 0:3855d4588f76 | 17 | |
megrootens | 0:3855d4588f76 | 18 | /** |
cnckiwi31 | 5:63063a9fa51c | 19 | * Main loop |
megrootens | 0:3855d4588f76 | 20 | */ |
megrootens | 0:3855d4588f76 | 21 | int main() |
cnckiwi31 | 5:63063a9fa51c | 22 | { |
cnckiwi31 | 5:63063a9fa51c | 23 | leg.setLoggingFrequency(100); //Set datalogging frequency |
cnckiwi31 | 5:63063a9fa51c | 24 | |
cnckiwi31 | 5:63063a9fa51c | 25 | /* Two extra columns of data will be recorded in this experiment. |
cnckiwi31 | 5:63063a9fa51c | 26 | One for the target pressure, and the other for the number of sit and |
cnckiwi31 | 5:63063a9fa51c | 27 | stand cycles currently completed in the experiment */ |
cnckiwi31 | 5:63063a9fa51c | 28 | string colNames[] = {"Target pressure (kPa)","Cycle"}; //add data headings |
cnckiwi31 | 5:63063a9fa51c | 29 | leg.setExtraColumns(colNames,2); |
megrootens | 0:3855d4588f76 | 30 | |
cnckiwi31 | 5:63063a9fa51c | 31 | float targetP = 200; //Target pressure in kPa |
cnckiwi31 | 5:63063a9fa51c | 32 | int expCycles = 2; //Number of sit to stand to sit cycles |
cnckiwi31 | 5:63063a9fa51c | 33 | float vals[] = {targetP,0}; //set initial values of data that will be logged |
cnckiwi31 | 5:63063a9fa51c | 34 | leg.setExtraData(vals); |
cnckiwi31 | 4:1cdce6c6c94e | 35 | |
cnckiwi31 | 5:63063a9fa51c | 36 | /* Setup all peripherals on rig, display info about SD card and the |
cnckiwi31 | 5:63063a9fa51c | 37 | user interface menu */ |
cnckiwi31 | 5:63063a9fa51c | 38 | leg.initialise(); |
megrootens | 0:3855d4588f76 | 39 | |
cnckiwi31 | 5:63063a9fa51c | 40 | |
cnckiwi31 | 5:63063a9fa51c | 41 | /*Run an experiment when the button is pressed to start datalogging and |
cnckiwi31 | 5:63063a9fa51c | 42 | stop it if the button is pressed again to stop datalogging |
cnckiwi31 | 5:63063a9fa51c | 43 | (or when experiment stops - then datalogging stops by itself) */ |
cnckiwi31 | 5:63063a9fa51c | 44 | while (true) { |
cnckiwi31 | 5:63063a9fa51c | 45 | if (leg.isLogging()) { |
cnckiwi31 | 5:63063a9fa51c | 46 | runDemoExperiment0(expCycles,targetP); |
cnckiwi31 | 5:63063a9fa51c | 47 | } |
megrootens | 0:3855d4588f76 | 48 | } |
megrootens | 0:3855d4588f76 | 49 | } |
megrootens | 0:3855d4588f76 | 50 | |
cnckiwi31 | 5:63063a9fa51c | 51 | /** |
cnckiwi31 | 5:63063a9fa51c | 52 | * Shows how a demo experiment works. This experiment pressurises the leg to |
cnckiwi31 | 5:63063a9fa51c | 53 | * pressure targetkPa, depressurises it, and then repeats the process cycles |
cnckiwi31 | 5:63063a9fa51c | 54 | * number of times |
cnckiwi31 | 5:63063a9fa51c | 55 | * @param cycles: the number of cycles the leg goes up and down |
cnckiwi31 | 5:63063a9fa51c | 56 | * @param targetkPa: the pressure at which the valve is opened to let the leg go down |
cnckiwi31 | 5:63063a9fa51c | 57 | */ |
cnckiwi31 | 5:63063a9fa51c | 58 | void runDemoExperiment0(int cycles, float targetkPa) |
megrootens | 0:3855d4588f76 | 59 | { |
cnckiwi31 | 5:63063a9fa51c | 60 | //The experiment starts when logging does |
cnckiwi31 | 5:63063a9fa51c | 61 | bool experimentRunning = leg.isLogging(); |
cnckiwi31 | 5:63063a9fa51c | 62 | |
cnckiwi31 | 5:63063a9fa51c | 63 | //Stop the Bench class from printing, so this method can print |
cnckiwi31 | 5:63063a9fa51c | 64 | leg.pausePrint(); |
cnckiwi31 | 5:63063a9fa51c | 65 | |
cnckiwi31 | 5:63063a9fa51c | 66 | if (experimentRunning) { |
cnckiwi31 | 5:63063a9fa51c | 67 | // Pressurise and depressurise the leg cycles number of times |
cnckiwi31 | 5:63063a9fa51c | 68 | for (int i=0; i<cycles; i++) { |
cnckiwi31 | 5:63063a9fa51c | 69 | leg.pc.printf("\r\nCycle: \t%i out of \t%i",i+1,cycles); |
cnckiwi31 | 5:63063a9fa51c | 70 | |
cnckiwi31 | 5:63063a9fa51c | 71 | //Update cycles logged |
cnckiwi31 | 5:63063a9fa51c | 72 | float data[] = {targetkPa,i+1}; |
cnckiwi31 | 5:63063a9fa51c | 73 | leg.setExtraData(data); |
cnckiwi31 | 5:63063a9fa51c | 74 | |
cnckiwi31 | 5:63063a9fa51c | 75 | //Pressurise |
cnckiwi31 | 5:63063a9fa51c | 76 | leg.setValve(true); |
megrootens | 0:3855d4588f76 | 77 | |
cnckiwi31 | 5:63063a9fa51c | 78 | //Wait until measured pressure reaches target pressure |
cnckiwi31 | 5:63063a9fa51c | 79 | float measureP = 0; |
cnckiwi31 | 5:63063a9fa51c | 80 | while(measureP < targetkPa) { |
cnckiwi31 | 5:63063a9fa51c | 81 | //Keep checking logging is going |
cnckiwi31 | 5:63063a9fa51c | 82 | experimentRunning = leg.isLogging(); |
cnckiwi31 | 5:63063a9fa51c | 83 | |
cnckiwi31 | 5:63063a9fa51c | 84 | if (experimentRunning) { |
cnckiwi31 | 5:63063a9fa51c | 85 | measureP = leg.getPressure0()*100;//Conversion of bar to kPa |
cnckiwi31 | 5:63063a9fa51c | 86 | wait(0.2);//Wait a bit |
cnckiwi31 | 5:63063a9fa51c | 87 | } else { //Logging stopped |
cnckiwi31 | 5:63063a9fa51c | 88 | leg.stopLogging(); //Stop logging data |
cnckiwi31 | 5:63063a9fa51c | 89 | leg.setValve(false); //Depressurise |
cnckiwi31 | 5:63063a9fa51c | 90 | leg.resumePrint(); //Let the Bench class print |
cnckiwi31 | 5:63063a9fa51c | 91 | return; |
cnckiwi31 | 5:63063a9fa51c | 92 | } |
cnckiwi31 | 5:63063a9fa51c | 93 | } |
megrootens | 0:3855d4588f76 | 94 | |
cnckiwi31 | 5:63063a9fa51c | 95 | //Depressurise |
cnckiwi31 | 5:63063a9fa51c | 96 | leg.setValve(false); |
megrootens | 0:3855d4588f76 | 97 | |
cnckiwi31 | 5:63063a9fa51c | 98 | /*Wait until depressurised (completely depressurised is |
cnckiwi31 | 5:63063a9fa51c | 99 | around 10-12 kPa due to current sensor calibration)*/ |
cnckiwi31 | 5:63063a9fa51c | 100 | while(measureP > 15) { |
cnckiwi31 | 5:63063a9fa51c | 101 | //Keep checking logging is going |
cnckiwi31 | 5:63063a9fa51c | 102 | experimentRunning = leg.isLogging(); |
cnckiwi31 | 5:63063a9fa51c | 103 | |
cnckiwi31 | 5:63063a9fa51c | 104 | if (experimentRunning) { |
cnckiwi31 | 5:63063a9fa51c | 105 | measureP = leg.getPressure0()*100;//Conversion of bar to kpa |
cnckiwi31 | 5:63063a9fa51c | 106 | wait(0.2);//Wait a bit |
cnckiwi31 | 5:63063a9fa51c | 107 | } else { //Logging stopped |
cnckiwi31 | 5:63063a9fa51c | 108 | leg.stopLogging(); //Stop logging data |
cnckiwi31 | 5:63063a9fa51c | 109 | leg.resumePrint(); //Let the Bench class print |
cnckiwi31 | 5:63063a9fa51c | 110 | return; |
cnckiwi31 | 5:63063a9fa51c | 111 | } |
megrootens | 0:3855d4588f76 | 112 | } |
megrootens | 0:3855d4588f76 | 113 | } |
cnckiwi31 | 5:63063a9fa51c | 114 | // Logging stopped as experiment is fully completed |
cnckiwi31 | 5:63063a9fa51c | 115 | leg.stopLogging(); //Stop logging data |
cnckiwi31 | 5:63063a9fa51c | 116 | leg.resumePrint(); //Let the Bench class print |
megrootens | 0:3855d4588f76 | 117 | } |
megrootens | 0:3855d4588f76 | 118 | } |
megrootens | 0:3855d4588f76 | 119 | |
megrootens | 0:3855d4588f76 | 120 |