![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
2.131 test rig DAQ for MTB suspension characterization project
Dependencies: EthernetInterface ExperimentServer HX711 QEI_pmw mbed-rtos mbed
Fork of Dolphin by
main.cpp@7:d5f354b450a3, 2016-05-03 (annotated)
- Committer:
- laskowsk
- Date:
- Tue May 03 17:22:31 2016 +0000
- Revision:
- 7:d5f354b450a3
- Parent:
- 6:b7f6433cc765
This is the coding used in the 2.131 Biking Beavers test rig to characterize a mountain bikes suspension system
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pwensing | 0:43448bf056e8 | 1 | #include "mbed.h" |
pwensing | 0:43448bf056e8 | 2 | #include "rtos.h" |
pwensing | 0:43448bf056e8 | 3 | #include "EthernetInterface.h" |
pwensing | 0:43448bf056e8 | 4 | #include "ExperimentServer.h" |
laskowsk | 4:300ced917633 | 5 | #include "HX711.h" |
pwensing | 0:43448bf056e8 | 6 | |
laskowsk | 7:d5f354b450a3 | 7 | #define NUM_INPUTS 5 |
laskowsk | 7:d5f354b450a3 | 8 | #define NUM_OUTPUTS 4 |
pwensing | 0:43448bf056e8 | 9 | |
pwensing | 0:43448bf056e8 | 10 | Serial pc(USBTX, USBRX); // USB Serial Terminal |
pwensing | 0:43448bf056e8 | 11 | ExperimentServer server; // Object that lets us communicate with MATLAB |
pwensing | 0:43448bf056e8 | 12 | Timer t; // Timer to measure elapsed time of experiment |
laskowsk | 7:d5f354b450a3 | 13 | AnalogIn pot(A0); // Linear Potentiometer |
laskowsk | 7:d5f354b450a3 | 14 | HX711 scale(A1, A2); |
laskowsk | 7:d5f354b450a3 | 15 | DigitalOut led(LED_BLUE); |
laskowsk | 7:d5f354b450a3 | 16 | Ticker randloop; |
laskowsk | 4:300ced917633 | 17 | |
laskowsk | 7:d5f354b450a3 | 18 | |
laskowsk | 7:d5f354b450a3 | 19 | |
laskowsk | 7:d5f354b450a3 | 20 | |
laskowsk | 4:300ced917633 | 21 | // Timing parameters |
laskowsk | 4:300ced917633 | 22 | float pwm_period_us; |
laskowsk | 4:300ced917633 | 23 | float current_control_period_us; |
laskowsk | 4:300ced917633 | 24 | float impedance_control_period_us; |
laskowsk | 4:300ced917633 | 25 | float exp_period; |
laskowsk | 6:b7f6433cc765 | 26 | float calibration_factor; |
laskowsk | 4:300ced917633 | 27 | |
laskowsk | 7:d5f354b450a3 | 28 | void randomloop() |
laskowsk | 4:300ced917633 | 29 | { |
laskowsk | 7:d5f354b450a3 | 30 | int random = rand()%100; |
laskowsk | 7:d5f354b450a3 | 31 | if (random > 85) { |
laskowsk | 7:d5f354b450a3 | 32 | led.write(0); |
laskowsk | 7:d5f354b450a3 | 33 | } |
laskowsk | 7:d5f354b450a3 | 34 | else { |
laskowsk | 7:d5f354b450a3 | 35 | led.write(1); |
laskowsk | 7:d5f354b450a3 | 36 | } |
pwensing | 0:43448bf056e8 | 37 | |
laskowsk | 7:d5f354b450a3 | 38 | } // end randomloop |
pwensing | 0:43448bf056e8 | 39 | int main (void) { |
laskowsk | 7:d5f354b450a3 | 40 | scale.tare(); // tare scale |
laskowsk | 7:d5f354b450a3 | 41 | |
laskowsk | 4:300ced917633 | 42 | // Link the terminal with our server and start it up |
laskowsk | 4:300ced917633 | 43 | server.attachTerminal(pc); |
laskowsk | 4:300ced917633 | 44 | server.init(); |
laskowsk | 4:300ced917633 | 45 | // Continually get input from MATLAB and run experiments |
laskowsk | 4:300ced917633 | 46 | float input_params[NUM_INPUTS]; |
laskowsk | 4:300ced917633 | 47 | while(1) { |
laskowsk | 4:300ced917633 | 48 | if (server.getParams(input_params,NUM_INPUTS)) { |
laskowsk | 4:300ced917633 | 49 | pwm_period_us = input_params[0]; // PWM_Period in mirco seconds |
laskowsk | 4:300ced917633 | 50 | current_control_period_us = input_params[1]; // Current control period in micro seconds |
laskowsk | 4:300ced917633 | 51 | impedance_control_period_us = input_params[2]; // Impedance control period in microseconds seconds |
laskowsk | 4:300ced917633 | 52 | exp_period = input_params[3]; // Experiment time in seconds |
laskowsk | 7:d5f354b450a3 | 53 | calibration_factor = input_params[4]; // calibration factor for load cell |
pwensing | 0:43448bf056e8 | 54 | |
laskowsk | 7:d5f354b450a3 | 55 | randloop.attach_us(randomloop,current_control_period_us); |
laskowsk | 7:d5f354b450a3 | 56 | pc.printf("This is working: %4f/n", pwm_period_us); |
laskowsk | 6:b7f6433cc765 | 57 | scale.setScale(calibration_factor); //Adjust to this calibration factor |
laskowsk | 7:d5f354b450a3 | 58 | |
laskowsk | 7:d5f354b450a3 | 59 | |
laskowsk | 4:300ced917633 | 60 | // Setup experiment |
laskowsk | 4:300ced917633 | 61 | t.reset(); |
laskowsk | 4:300ced917633 | 62 | t.start(); |
laskowsk | 7:d5f354b450a3 | 63 | |
laskowsk | 7:d5f354b450a3 | 64 | // Run experiment, keep this line |
laskowsk | 7:d5f354b450a3 | 65 | while ( t.read() < exp_period ) { |
laskowsk | 4:300ced917633 | 66 | // Perform control loop logic |
laskowsk | 4:300ced917633 | 67 | |
laskowsk | 6:b7f6433cc765 | 68 | // take measurement from load cell |
laskowsk | 7:d5f354b450a3 | 69 | float F = scale.getGram();//+ 0.116f*t.read(); // value from load cell that uses calibration factor |
laskowsk | 7:d5f354b450a3 | 70 | float Length = pot.read()*105.0f; // value from linear potentiometer * 1.05 calibration factor (105mm stroke length |
laskowsk | 7:d5f354b450a3 | 71 | float light = led.read(); |
laskowsk | 7:d5f354b450a3 | 72 | |
laskowsk | 7:d5f354b450a3 | 73 | |
laskowsk | 4:300ced917633 | 74 | // Form output to send to MATLAB |
laskowsk | 4:300ced917633 | 75 | float output_data[NUM_OUTPUTS]; |
laskowsk | 4:300ced917633 | 76 | output_data[0] = t.read(); |
laskowsk | 7:d5f354b450a3 | 77 | output_data[1] = Length; |
laskowsk | 7:d5f354b450a3 | 78 | output_data[2] = F; |
laskowsk | 7:d5f354b450a3 | 79 | output_data[3] = light; |
laskowsk | 7:d5f354b450a3 | 80 | //output_data[4] = current_des1; |
laskowsk | 7:d5f354b450a3 | 81 | //output_data[5] = duty_factor1; |
laskowsk | 7:d5f354b450a3 | 82 | //pc.printf("Output data",output_data); |
laskowsk | 4:300ced917633 | 83 | |
laskowsk | 4:300ced917633 | 84 | // Send data to MATLAB |
laskowsk | 4:300ced917633 | 85 | server.sendData(output_data,NUM_OUTPUTS); |
laskowsk | 4:300ced917633 | 86 | wait_us(impedance_control_period_us); |
laskowsk | 4:300ced917633 | 87 | } |
laskowsk | 4:300ced917633 | 88 | // Cleanup after experiment |
laskowsk | 4:300ced917633 | 89 | server.setExperimentComplete(); |
laskowsk | 4:300ced917633 | 90 | // control.detach(); |
laskowsk | 7:d5f354b450a3 | 91 | randloop.detach(); |
laskowsk | 4:300ced917633 | 92 | |
laskowsk | 4:300ced917633 | 93 | } // end if |
laskowsk | 7:d5f354b450a3 | 94 | } // end while |
laskowsk | 4:300ced917633 | 95 | } // end main |