2.74 Jerboa Robot Code
Dependencies: EthernetInterface ExperimentServer QEI_pmw experiment_example mbed-rtos mbed
Fork of experiment_example by
main.cpp@1:95a7fddd25a9, 2015-09-10 (annotated)
- Committer:
- Patrick Wensing
- Date:
- Thu Sep 10 14:26:20 2015 -0400
- Revision:
- 1:95a7fddd25a9
- Parent:
- 0:43448bf056e8
- Child:
- 3:3c00f1332d9b
fixed PMW misspelling
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" |
pwensing | 0:43448bf056e8 | 5 | #include "QEI.h" |
pwensing | 0:43448bf056e8 | 6 | |
pwensing | 0:43448bf056e8 | 7 | #define NUM_INPUTS 2 |
pwensing | 0:43448bf056e8 | 8 | #define NUM_OUTPUTS 3 |
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 |
Patrick Wensing |
1:95a7fddd25a9 | 12 | PwmOut motorPWM(D5); // Motor PWM output |
pwensing | 0:43448bf056e8 | 13 | DigitalOut motorFwd(D6); // Motor forward enable |
pwensing | 0:43448bf056e8 | 14 | DigitalOut motorRev(D7); // Motor backward enable |
pwensing | 0:43448bf056e8 | 15 | Timer t; // Timer to measure elapsed time of experiment |
pwensing | 0:43448bf056e8 | 16 | |
pwensing | 0:43448bf056e8 | 17 | QEI encoder(D3,D4, NC, 1200 , QEI::X4_ENCODING); // Pins D3, D4, no index, 1200 counts/rev, Quadrature encoding |
pwensing | 0:43448bf056e8 | 18 | |
pwensing | 0:43448bf056e8 | 19 | int main (void) { |
pwensing | 0:43448bf056e8 | 20 | // Link the terminal with our server and start it up |
pwensing | 0:43448bf056e8 | 21 | server.attachTerminal(pc); |
pwensing | 0:43448bf056e8 | 22 | server.init(); |
pwensing | 0:43448bf056e8 | 23 | |
Patrick Wensing |
1:95a7fddd25a9 | 24 | // PWM period should nominally be a multiple of our control loop |
Patrick Wensing |
1:95a7fddd25a9 | 25 | motorPWM.period_us(500); |
pwensing | 0:43448bf056e8 | 26 | |
pwensing | 0:43448bf056e8 | 27 | // Continually get input from MATLAB and run experiments |
pwensing | 0:43448bf056e8 | 28 | float input_params[NUM_INPUTS]; |
pwensing | 0:43448bf056e8 | 29 | |
pwensing | 0:43448bf056e8 | 30 | while(1) { |
pwensing | 0:43448bf056e8 | 31 | if (server.getParams(input_params,NUM_INPUTS)) { |
pwensing | 0:43448bf056e8 | 32 | float v1 = input_params[0]; // Voltage for first second |
pwensing | 0:43448bf056e8 | 33 | float v2 = input_params[1]; // Voltage for second second |
pwensing | 0:43448bf056e8 | 34 | |
pwensing | 0:43448bf056e8 | 35 | // Setup experiment |
pwensing | 0:43448bf056e8 | 36 | t.reset(); |
pwensing | 0:43448bf056e8 | 37 | t.start(); |
pwensing | 0:43448bf056e8 | 38 | encoder.reset(); |
pwensing | 0:43448bf056e8 | 39 | motorFwd = 1; |
pwensing | 0:43448bf056e8 | 40 | motorRev = 0; |
Patrick Wensing |
1:95a7fddd25a9 | 41 | motorPWM.write(0); |
pwensing | 0:43448bf056e8 | 42 | |
pwensing | 0:43448bf056e8 | 43 | // Run experiment |
pwensing | 0:43448bf056e8 | 44 | while( t.read() < 2 ) { |
pwensing | 0:43448bf056e8 | 45 | // Perform control loop logic |
pwensing | 0:43448bf056e8 | 46 | if (t.read() < 1) |
Patrick Wensing |
1:95a7fddd25a9 | 47 | motorPWM.write(v1); |
pwensing | 0:43448bf056e8 | 48 | else |
Patrick Wensing |
1:95a7fddd25a9 | 49 | motorPWM.write(v2); |
pwensing | 0:43448bf056e8 | 50 | |
pwensing | 0:43448bf056e8 | 51 | // Form output to send to MATLAB |
pwensing | 0:43448bf056e8 | 52 | float output_data[NUM_OUTPUTS]; |
pwensing | 0:43448bf056e8 | 53 | output_data[0] = t.read(); |
pwensing | 0:43448bf056e8 | 54 | output_data[1] = encoder.getPulses(); |
pwensing | 0:43448bf056e8 | 55 | output_data[2] = encoder.getVelocity(); |
pwensing | 0:43448bf056e8 | 56 | |
pwensing | 0:43448bf056e8 | 57 | // Send data to MATLAB |
pwensing | 0:43448bf056e8 | 58 | server.sendData(output_data,NUM_OUTPUTS); |
pwensing | 0:43448bf056e8 | 59 | wait(.001); |
pwensing | 0:43448bf056e8 | 60 | } |
pwensing | 0:43448bf056e8 | 61 | // Cleanup after experiment |
pwensing | 0:43448bf056e8 | 62 | server.setExperimentComplete(); |
Patrick Wensing |
1:95a7fddd25a9 | 63 | motorPWM.write(0); |
pwensing | 0:43448bf056e8 | 64 | } // end if |
pwensing | 0:43448bf056e8 | 65 | } // end while |
pwensing | 0:43448bf056e8 | 66 | } // end main |