2.74 Nucleo Code
Dependencies: ExperimentServer QEI_pmw MotorShield
main.cpp@9:97360c92666f, 2020-08-26 (annotated)
- Committer:
- elijahsj
- Date:
- Wed Aug 26 00:24:56 2020 +0000
- Revision:
- 9:97360c92666f
- Parent:
- 8:98a83981c238
- Child:
- 10:a40d180c305c
Cleanup
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" |
elijahsj | 6:1faceb53dabe | 6 | #include "MotorShield.h" |
elijahsj | 8:98a83981c238 | 7 | #include "HardwareSetup.h" |
pwensing | 0:43448bf056e8 | 8 | |
pwensing | 0:43448bf056e8 | 9 | #define NUM_INPUTS 2 |
pwensing | 0:43448bf056e8 | 10 | #define NUM_OUTPUTS 3 |
pwensing | 0:43448bf056e8 | 11 | |
pwensing | 0:43448bf056e8 | 12 | Serial pc(USBTX, USBRX); // USB Serial Terminal |
pwensing | 0:43448bf056e8 | 13 | ExperimentServer server; // Object that lets us communicate with MATLAB |
elijahsj | 5:1ab9b2527794 | 14 | Timer t; // Timer to measure elapsed time of experiment |
elijahsj | 5:1ab9b2527794 | 15 | |
elijahsj | 5:1ab9b2527794 | 16 | QEI encoderA(PE_9,PE_11, NC, 1200, QEI::X4_ENCODING); // MOTOR A ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 5:1ab9b2527794 | 17 | QEI encoderB(PA_5, PB_3, NC, 1200, QEI::X4_ENCODING); // MOTOR B ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 5:1ab9b2527794 | 18 | QEI encoderC(PC_6, PC_7, NC, 1200, QEI::X4_ENCODING); // MOTOR C ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 5:1ab9b2527794 | 19 | QEI encoderD(PD_12, PD_13, NC, 1200, QEI::X4_ENCODING);// MOTOR D ENCODER (no index, 1200 counts/rev, Quadrature encoding) |
elijahsj | 5:1ab9b2527794 | 20 | |
elijahsj | 6:1faceb53dabe | 21 | AnalogIn currentA(PF_12); //MOTOR A CURRENT SENSOR |
elijahsj | 6:1faceb53dabe | 22 | AnalogIn currentB(PF_11); //MOTOR B CURRENT SENSOR |
elijahsj | 6:1faceb53dabe | 23 | AnalogIn currentC(PF_13); //MOTOR C CURRENT SENSOR |
elijahsj | 6:1faceb53dabe | 24 | AnalogIn currentD(PA_4); //MOTOR D CURRENT SENSOR |
elijahsj | 6:1faceb53dabe | 25 | |
elijahsj | 8:98a83981c238 | 26 | //MotorShield motorA(PE_5, PE_6); //MOTOR A PWM |
elijahsj | 8:98a83981c238 | 27 | //MotorShield motorB(PB_14, PB_15); //MOTOR B PWM |
elijahsj | 8:98a83981c238 | 28 | //MotorShield motorC(PA_6, PF_9); //MOTOR C PWM |
elijahsj | 8:98a83981c238 | 29 | //MotorShield motorD(PF_6, PF_7); //MOTOR D PWM |
elijahsj | 6:1faceb53dabe | 30 | |
elijahsj | 4:7a1b35f081bb | 31 | int main (void) |
elijahsj | 4:7a1b35f081bb | 32 | { |
elijahsj | 8:98a83981c238 | 33 | initHardware(); // Setup PWM |
elijahsj | 8:98a83981c238 | 34 | wait_us(100); |
elijahsj | 8:98a83981c238 | 35 | |
elijahsj | 8:98a83981c238 | 36 | TIM12->CCR2 = 100; |
elijahsj | 8:98a83981c238 | 37 | TIM12->CCR1 = 200; |
elijahsj | 9:97360c92666f | 38 | TIM13->CCR1 = 200; |
elijahsj | 9:97360c92666f | 39 | TIM14->CCR1 = 200; |
elijahsj | 9:97360c92666f | 40 | TIM15->CCR1 = 200; |
elijahsj | 9:97360c92666f | 41 | TIM15->CCR2 = 200; |
elijahsj | 9:97360c92666f | 42 | TIM16->CCR1 = 200; |
elijahsj | 9:97360c92666f | 43 | TIM17->CCR1 = 200; |
elijahsj | 8:98a83981c238 | 44 | |
elijahsj | 8:98a83981c238 | 45 | while(1){ |
elijahsj | 8:98a83981c238 | 46 | } |
elijahsj | 8:98a83981c238 | 47 | /* |
pwensing | 0:43448bf056e8 | 48 | // Link the terminal with our server and start it up |
pwensing | 0:43448bf056e8 | 49 | server.attachTerminal(pc); |
pwensing | 0:43448bf056e8 | 50 | server.init(); |
pwensing | 0:43448bf056e8 | 51 | |
Patrick Wensing |
1:95a7fddd25a9 | 52 | // PWM period should nominally be a multiple of our control loop |
Patrick Wensing |
1:95a7fddd25a9 | 53 | motorPWM.period_us(500); |
elijahsj | 4:7a1b35f081bb | 54 | |
pwensing | 0:43448bf056e8 | 55 | // Continually get input from MATLAB and run experiments |
pwensing | 0:43448bf056e8 | 56 | float input_params[NUM_INPUTS]; |
elijahsj | 5:1ab9b2527794 | 57 | pc.printf("%f",input_params[0]); |
elijahsj | 5:1ab9b2527794 | 58 | |
pwensing | 0:43448bf056e8 | 59 | while(1) { |
pwensing | 0:43448bf056e8 | 60 | if (server.getParams(input_params,NUM_INPUTS)) { |
pwensing | 0:43448bf056e8 | 61 | float v1 = input_params[0]; // Voltage for first second |
pwensing | 0:43448bf056e8 | 62 | float v2 = input_params[1]; // Voltage for second second |
elijahsj | 4:7a1b35f081bb | 63 | |
pwensing | 0:43448bf056e8 | 64 | // Setup experiment |
pwensing | 0:43448bf056e8 | 65 | t.reset(); |
pwensing | 0:43448bf056e8 | 66 | t.start(); |
elijahsj | 5:1ab9b2527794 | 67 | encoderA.reset(); |
elijahsj | 5:1ab9b2527794 | 68 | encoderB.reset(); |
elijahsj | 5:1ab9b2527794 | 69 | encoderC.reset(); |
elijahsj | 5:1ab9b2527794 | 70 | encoderD.reset(); |
pwensing | 0:43448bf056e8 | 71 | motorFwd = 1; |
pwensing | 0:43448bf056e8 | 72 | motorRev = 0; |
Patrick Wensing |
1:95a7fddd25a9 | 73 | motorPWM.write(0); |
pwensing | 0:43448bf056e8 | 74 | |
pwensing | 0:43448bf056e8 | 75 | // Run experiment |
elijahsj | 4:7a1b35f081bb | 76 | while( t.read() < 2 ) { |
pwensing | 0:43448bf056e8 | 77 | // Perform control loop logic |
elijahsj | 4:7a1b35f081bb | 78 | if (t.read() < 1) |
Patrick Wensing |
1:95a7fddd25a9 | 79 | motorPWM.write(v1); |
elijahsj | 4:7a1b35f081bb | 80 | else |
Patrick Wensing |
1:95a7fddd25a9 | 81 | motorPWM.write(v2); |
elijahsj | 4:7a1b35f081bb | 82 | |
elijahsj | 4:7a1b35f081bb | 83 | // Form output to send to MATLAB |
pwensing | 0:43448bf056e8 | 84 | float output_data[NUM_OUTPUTS]; |
pwensing | 0:43448bf056e8 | 85 | output_data[0] = t.read(); |
elijahsj | 5:1ab9b2527794 | 86 | output_data[1] = encoderA.getPulses(); |
elijahsj | 5:1ab9b2527794 | 87 | output_data[2] = encoderA.getVelocity(); |
elijahsj | 4:7a1b35f081bb | 88 | |
pwensing | 0:43448bf056e8 | 89 | // Send data to MATLAB |
pwensing | 0:43448bf056e8 | 90 | server.sendData(output_data,NUM_OUTPUTS); |
elijahsj | 4:7a1b35f081bb | 91 | wait(.001); |
elijahsj | 4:7a1b35f081bb | 92 | } |
pwensing | 0:43448bf056e8 | 93 | // Cleanup after experiment |
pwensing | 0:43448bf056e8 | 94 | server.setExperimentComplete(); |
Patrick Wensing |
1:95a7fddd25a9 | 95 | motorPWM.write(0); |
pwensing | 0:43448bf056e8 | 96 | } // end if |
pwensing | 0:43448bf056e8 | 97 | } // end while |
elijahsj | 8:98a83981c238 | 98 | */ |
elijahsj | 6:1faceb53dabe | 99 | } // end main |
elijahsj | 6:1faceb53dabe | 100 |