nov 18th

Dependencies:   Bezier_Traj_Follower_Example ExperimentServer QEI_pmw MotorShield

Committer:
elijahsj
Date:
Tue Aug 25 23:38:34 2020 +0000
Revision:
8:98a83981c238
Parent:
6:1faceb53dabe
Child:
9:97360c92666f
Fixed HAL Libraries

Who changed what in which revision?

UserRevisionLine numberNew 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 8:98a83981c238 38
elijahsj 8:98a83981c238 39 while(1){
elijahsj 8:98a83981c238 40 }
elijahsj 8:98a83981c238 41 /*
pwensing 0:43448bf056e8 42 // Link the terminal with our server and start it up
pwensing 0:43448bf056e8 43 server.attachTerminal(pc);
pwensing 0:43448bf056e8 44 server.init();
pwensing 0:43448bf056e8 45
Patrick Wensing 1:95a7fddd25a9 46 // PWM period should nominally be a multiple of our control loop
Patrick Wensing 1:95a7fddd25a9 47 motorPWM.period_us(500);
elijahsj 4:7a1b35f081bb 48
pwensing 0:43448bf056e8 49 // Continually get input from MATLAB and run experiments
pwensing 0:43448bf056e8 50 float input_params[NUM_INPUTS];
elijahsj 5:1ab9b2527794 51 pc.printf("%f",input_params[0]);
elijahsj 5:1ab9b2527794 52
pwensing 0:43448bf056e8 53 while(1) {
pwensing 0:43448bf056e8 54 if (server.getParams(input_params,NUM_INPUTS)) {
pwensing 0:43448bf056e8 55 float v1 = input_params[0]; // Voltage for first second
pwensing 0:43448bf056e8 56 float v2 = input_params[1]; // Voltage for second second
elijahsj 4:7a1b35f081bb 57
pwensing 0:43448bf056e8 58 // Setup experiment
pwensing 0:43448bf056e8 59 t.reset();
pwensing 0:43448bf056e8 60 t.start();
elijahsj 5:1ab9b2527794 61 encoderA.reset();
elijahsj 5:1ab9b2527794 62 encoderB.reset();
elijahsj 5:1ab9b2527794 63 encoderC.reset();
elijahsj 5:1ab9b2527794 64 encoderD.reset();
pwensing 0:43448bf056e8 65 motorFwd = 1;
pwensing 0:43448bf056e8 66 motorRev = 0;
Patrick Wensing 1:95a7fddd25a9 67 motorPWM.write(0);
pwensing 0:43448bf056e8 68
pwensing 0:43448bf056e8 69 // Run experiment
elijahsj 4:7a1b35f081bb 70 while( t.read() < 2 ) {
pwensing 0:43448bf056e8 71 // Perform control loop logic
elijahsj 4:7a1b35f081bb 72 if (t.read() < 1)
Patrick Wensing 1:95a7fddd25a9 73 motorPWM.write(v1);
elijahsj 4:7a1b35f081bb 74 else
Patrick Wensing 1:95a7fddd25a9 75 motorPWM.write(v2);
elijahsj 4:7a1b35f081bb 76
elijahsj 4:7a1b35f081bb 77 // Form output to send to MATLAB
pwensing 0:43448bf056e8 78 float output_data[NUM_OUTPUTS];
pwensing 0:43448bf056e8 79 output_data[0] = t.read();
elijahsj 5:1ab9b2527794 80 output_data[1] = encoderA.getPulses();
elijahsj 5:1ab9b2527794 81 output_data[2] = encoderA.getVelocity();
elijahsj 4:7a1b35f081bb 82
pwensing 0:43448bf056e8 83 // Send data to MATLAB
pwensing 0:43448bf056e8 84 server.sendData(output_data,NUM_OUTPUTS);
elijahsj 4:7a1b35f081bb 85 wait(.001);
elijahsj 4:7a1b35f081bb 86 }
pwensing 0:43448bf056e8 87 // Cleanup after experiment
pwensing 0:43448bf056e8 88 server.setExperimentComplete();
Patrick Wensing 1:95a7fddd25a9 89 motorPWM.write(0);
pwensing 0:43448bf056e8 90 } // end if
pwensing 0:43448bf056e8 91 } // end while
elijahsj 8:98a83981c238 92 */
elijahsj 6:1faceb53dabe 93 } // end main
elijahsj 6:1faceb53dabe 94