first

Dependencies:   ExperimentServer QEI_pmw MotorShield

Committer:
elijahsj
Date:
Fri Aug 21 16:59:39 2020 +0000
Revision:
4:7a1b35f081bb
Parent:
1:95a7fddd25a9
Child:
5:1ab9b2527794
updated mbed version;

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"
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
elijahsj 4:7a1b35f081bb 17 QEI encoder(D3,D4, NC, 1200, QEI::X4_ENCODING); // Pins D3, D4, no index, 1200 counts/rev, Quadrature encoding
pwensing 0:43448bf056e8 18
elijahsj 4:7a1b35f081bb 19 int main (void)
elijahsj 4:7a1b35f081bb 20 {
pwensing 0:43448bf056e8 21 // Link the terminal with our server and start it up
pwensing 0:43448bf056e8 22 server.attachTerminal(pc);
pwensing 0:43448bf056e8 23 server.init();
pwensing 0:43448bf056e8 24
Patrick Wensing 1:95a7fddd25a9 25 // PWM period should nominally be a multiple of our control loop
Patrick Wensing 1:95a7fddd25a9 26 motorPWM.period_us(500);
elijahsj 4:7a1b35f081bb 27
pwensing 0:43448bf056e8 28 // Continually get input from MATLAB and run experiments
pwensing 0:43448bf056e8 29 float input_params[NUM_INPUTS];
elijahsj 4:7a1b35f081bb 30
pwensing 0:43448bf056e8 31 while(1) {
pwensing 0:43448bf056e8 32 if (server.getParams(input_params,NUM_INPUTS)) {
pwensing 0:43448bf056e8 33 float v1 = input_params[0]; // Voltage for first second
pwensing 0:43448bf056e8 34 float v2 = input_params[1]; // Voltage for second second
elijahsj 4:7a1b35f081bb 35
pwensing 0:43448bf056e8 36 // Setup experiment
pwensing 0:43448bf056e8 37 t.reset();
pwensing 0:43448bf056e8 38 t.start();
pwensing 0:43448bf056e8 39 encoder.reset();
pwensing 0:43448bf056e8 40 motorFwd = 1;
pwensing 0:43448bf056e8 41 motorRev = 0;
Patrick Wensing 1:95a7fddd25a9 42 motorPWM.write(0);
pwensing 0:43448bf056e8 43
pwensing 0:43448bf056e8 44 // Run experiment
elijahsj 4:7a1b35f081bb 45 while( t.read() < 2 ) {
pwensing 0:43448bf056e8 46 // Perform control loop logic
elijahsj 4:7a1b35f081bb 47 if (t.read() < 1)
Patrick Wensing 1:95a7fddd25a9 48 motorPWM.write(v1);
elijahsj 4:7a1b35f081bb 49 else
Patrick Wensing 1:95a7fddd25a9 50 motorPWM.write(v2);
elijahsj 4:7a1b35f081bb 51
elijahsj 4:7a1b35f081bb 52 // Form output to send to MATLAB
pwensing 0:43448bf056e8 53 float output_data[NUM_OUTPUTS];
pwensing 0:43448bf056e8 54 output_data[0] = t.read();
pwensing 0:43448bf056e8 55 output_data[1] = encoder.getPulses();
pwensing 0:43448bf056e8 56 output_data[2] = encoder.getVelocity();
elijahsj 4:7a1b35f081bb 57
pwensing 0:43448bf056e8 58 // Send data to MATLAB
pwensing 0:43448bf056e8 59 server.sendData(output_data,NUM_OUTPUTS);
elijahsj 4:7a1b35f081bb 60 wait(.001);
elijahsj 4:7a1b35f081bb 61 }
pwensing 0:43448bf056e8 62 // Cleanup after experiment
pwensing 0:43448bf056e8 63 server.setExperimentComplete();
Patrick Wensing 1:95a7fddd25a9 64 motorPWM.write(0);
pwensing 0:43448bf056e8 65 } // end if
pwensing 0:43448bf056e8 66 } // end while
pwensing 0:43448bf056e8 67 } // end main