Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MatrixMath Matrix ExperimentServer QEI_pmw MotorShield
main.cpp
- Committer:
- elijahsj
- Date:
- 2020-08-26
- Revision:
- 14:c0aa529968a2
- Parent:
- 13:3a1f4e09789b
- Child:
- 15:495333b2ccf1
File content as of revision 14:c0aa529968a2:
#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "ExperimentServer.h"
#include "QEI.h"
#include "MotorShield.h" 
#include "HardwareSetup.h"
#define NUM_INPUTS 2
#define NUM_OUTPUTS 4
Serial pc(USBTX, USBRX);    // USB Serial Terminal
ExperimentServer server;    // Object that lets us communicate with MATLAB
Timer t;                    // Timer to measure elapsed time of experiment
QEI encoderA(PE_9,PE_11, NC, 1200, QEI::X4_ENCODING);  // MOTOR A ENCODER (no index, 1200 counts/rev, Quadrature encoding)
QEI encoderB(PA_5, PB_3, NC, 1200, QEI::X4_ENCODING);  // MOTOR B ENCODER (no index, 1200 counts/rev, Quadrature encoding)
QEI encoderC(PC_6, PC_7, NC, 1200, QEI::X4_ENCODING);  // MOTOR C ENCODER (no index, 1200 counts/rev, Quadrature encoding)
QEI encoderD(PD_12, PD_13, NC, 1200, QEI::X4_ENCODING);// MOTOR D ENCODER (no index, 1200 counts/rev, Quadrature encoding)
MotorShield motorShield(12000); //initialize the motor shield with a period of 12000 ticks or ~20kHZ
int main (void)
{
    // Link the terminal with our server and start it up
    server.attachTerminal(pc);
    server.init();
    
    // Continually get input from MATLAB and run experiments
    float input_params[NUM_INPUTS];
    pc.printf("%f",input_params[0]);
    
    while(1) {
        if (server.getParams(input_params,NUM_INPUTS)) {
            float v1   = input_params[0]; // Duty cycle for first second
            float v2   = input_params[1]; // Duty cycle for second second
            // Setup experiment
            t.reset();
            t.start();
            encoderA.reset();
            encoderB.reset();
            encoderC.reset();
            encoderD.reset();
            motorShield.motorAWrite(0, 0); //turn motor A off, motorShield.motorAWrite(DUTY CYCLE, DIRECTION), DIRECTION = 0 is forward 
            // Run experiment
            while( t.read() < 2 ) {
                // Perform control loop logic
                if (t.read() < 1)
                    motorShield.motorAWrite(v1, 0); //run motor A at "v1" duty cycle and in the forward direction 
                else
                    motorShield.motorAWrite(v2, 0); //run motor A at "v2" duty cycle and in the forward direction 
                // Form output to send to MATLAB
                float output_data[NUM_OUTPUTS];
                
                output_data[0] = t.read();
                output_data[1] = encoderA.getPulses(); 
                output_data[2] = encoderA.getVelocity();
                output_data[3] = motorShield.readCurrentA();
                
                // Send data to MATLAB
                server.sendData(output_data,NUM_OUTPUTS);
                
                wait(.001); //run control loop at 1kHz
            }
            // Cleanup after experiment
            server.setExperimentComplete();
            motorShield.motorAWrite(0, 0); //turn motor A off
        } // end if
    } // end while
    
} // end main
            
    