first

Dependencies:   ExperimentServer QEI_pmw MotorShield

Committer:
elijahsj
Date:
Wed Aug 26 14:37:19 2020 +0000
Revision:
12:84a6dcb60422
Parent:
11:4eb579687dda
Child:
13:3a1f4e09789b
PWM + Encoders work;

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"
pwensing 0:43448bf056e8 7
pwensing 0:43448bf056e8 8 #define NUM_INPUTS 2
pwensing 0:43448bf056e8 9 #define NUM_OUTPUTS 3
pwensing 0:43448bf056e8 10
pwensing 0:43448bf056e8 11 Serial pc(USBTX, USBRX); // USB Serial Terminal
pwensing 0:43448bf056e8 12 ExperimentServer server; // Object that lets us communicate with MATLAB
elijahsj 5:1ab9b2527794 13 Timer t; // Timer to measure elapsed time of experiment
elijahsj 5:1ab9b2527794 14
elijahsj 5:1ab9b2527794 15 QEI encoderA(PE_9,PE_11, NC, 1200, QEI::X4_ENCODING); // MOTOR A ENCODER (no index, 1200 counts/rev, Quadrature encoding)
elijahsj 5:1ab9b2527794 16 QEI encoderB(PA_5, PB_3, NC, 1200, QEI::X4_ENCODING); // MOTOR B ENCODER (no index, 1200 counts/rev, Quadrature encoding)
elijahsj 5:1ab9b2527794 17 QEI encoderC(PC_6, PC_7, NC, 1200, QEI::X4_ENCODING); // MOTOR C ENCODER (no index, 1200 counts/rev, Quadrature encoding)
elijahsj 5:1ab9b2527794 18 QEI encoderD(PD_12, PD_13, NC, 1200, QEI::X4_ENCODING);// MOTOR D ENCODER (no index, 1200 counts/rev, Quadrature encoding)
elijahsj 5:1ab9b2527794 19
elijahsj 6:1faceb53dabe 20 AnalogIn currentA(PF_12); //MOTOR A CURRENT SENSOR
elijahsj 6:1faceb53dabe 21 AnalogIn currentB(PF_11); //MOTOR B CURRENT SENSOR
elijahsj 6:1faceb53dabe 22 AnalogIn currentC(PF_13); //MOTOR C CURRENT SENSOR
elijahsj 6:1faceb53dabe 23 AnalogIn currentD(PA_4); //MOTOR D CURRENT SENSOR
elijahsj 6:1faceb53dabe 24
elijahsj 12:84a6dcb60422 25 MotorShield motorShield(12000); //initialize the motor shield with a period of 12000 ticks or ~20kHZ
elijahsj 6:1faceb53dabe 26
elijahsj 4:7a1b35f081bb 27 int main (void)
elijahsj 4:7a1b35f081bb 28 {
pwensing 0:43448bf056e8 29 // Link the terminal with our server and start it up
pwensing 0:43448bf056e8 30 server.attachTerminal(pc);
pwensing 0:43448bf056e8 31 server.init();
pwensing 0:43448bf056e8 32
pwensing 0:43448bf056e8 33 // Continually get input from MATLAB and run experiments
pwensing 0:43448bf056e8 34 float input_params[NUM_INPUTS];
elijahsj 5:1ab9b2527794 35 pc.printf("%f",input_params[0]);
elijahsj 5:1ab9b2527794 36
pwensing 0:43448bf056e8 37 while(1) {
pwensing 0:43448bf056e8 38 if (server.getParams(input_params,NUM_INPUTS)) {
elijahsj 12:84a6dcb60422 39 float v1 = input_params[0]; // Duty cycle for first second
elijahsj 12:84a6dcb60422 40 float v2 = input_params[1]; // Duty cycle for second second
elijahsj 4:7a1b35f081bb 41
pwensing 0:43448bf056e8 42 // Setup experiment
pwensing 0:43448bf056e8 43 t.reset();
pwensing 0:43448bf056e8 44 t.start();
elijahsj 5:1ab9b2527794 45 encoderA.reset();
elijahsj 5:1ab9b2527794 46 encoderB.reset();
elijahsj 5:1ab9b2527794 47 encoderC.reset();
elijahsj 5:1ab9b2527794 48 encoderD.reset();
elijahsj 10:a40d180c305c 49
elijahsj 12:84a6dcb60422 50 motorShield.motorAWrite(0, 0); //turn motor A off, motorShield.motorAWrite(DUTY CYCLE, DIRECTION), DIRECTION = 0 is forward
pwensing 0:43448bf056e8 51
pwensing 0:43448bf056e8 52 // Run experiment
elijahsj 4:7a1b35f081bb 53 while( t.read() < 2 ) {
pwensing 0:43448bf056e8 54 // Perform control loop logic
elijahsj 4:7a1b35f081bb 55 if (t.read() < 1)
elijahsj 12:84a6dcb60422 56 motorShield.motorAWrite(v1, 0); //run motor A at "v1" duty cycle and in the forward direction
elijahsj 4:7a1b35f081bb 57 else
elijahsj 12:84a6dcb60422 58 motorShield.motorAWrite(v2, 0); //run motor A at "v2" duty cycle and in the forward direction
elijahsj 4:7a1b35f081bb 59
elijahsj 4:7a1b35f081bb 60 // Form output to send to MATLAB
pwensing 0:43448bf056e8 61 float output_data[NUM_OUTPUTS];
pwensing 0:43448bf056e8 62 output_data[0] = t.read();
elijahsj 5:1ab9b2527794 63 output_data[1] = encoderA.getPulses();
elijahsj 5:1ab9b2527794 64 output_data[2] = encoderA.getVelocity();
elijahsj 4:7a1b35f081bb 65
pwensing 0:43448bf056e8 66 // Send data to MATLAB
pwensing 0:43448bf056e8 67 server.sendData(output_data,NUM_OUTPUTS);
elijahsj 4:7a1b35f081bb 68 wait(.001);
elijahsj 4:7a1b35f081bb 69 }
pwensing 0:43448bf056e8 70 // Cleanup after experiment
pwensing 0:43448bf056e8 71 server.setExperimentComplete();
elijahsj 12:84a6dcb60422 72 motorShield.motorAWrite(0, 0); //turn motor A off
pwensing 0:43448bf056e8 73 } // end if
pwensing 0:43448bf056e8 74 } // end while
elijahsj 10:a40d180c305c 75
elijahsj 6:1faceb53dabe 76 } // end main
elijahsj 6:1faceb53dabe 77