This is a good example code for 2.74 lab 3 impedance control

Dependencies:   ExperimentServer QEI_pmw MotorShield

main.cpp

Committer:
elijahsj
Date:
2020-08-26
Revision:
9:97360c92666f
Parent:
8:98a83981c238
Child:
10:a40d180c305c

File content as of revision 9:97360c92666f:

#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 3

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)

AnalogIn currentA(PF_12); //MOTOR A CURRENT SENSOR 
AnalogIn currentB(PF_11); //MOTOR B CURRENT SENSOR 
AnalogIn currentC(PF_13); //MOTOR C CURRENT SENSOR 
AnalogIn currentD(PA_4);  //MOTOR D CURRENT SENSOR 

//MotorShield motorA(PE_5, PE_6);   //MOTOR A PWM 
//MotorShield motorB(PB_14, PB_15); //MOTOR B PWM 
//MotorShield motorC(PA_6, PF_9);   //MOTOR C PWM 
//MotorShield motorD(PF_6, PF_7);   //MOTOR D PWM

int main (void)
{
    initHardware();   // Setup PWM
    wait_us(100);
    
    TIM12->CCR2 = 100;
    TIM12->CCR1 = 200;
    TIM13->CCR1 = 200;
    TIM14->CCR1 = 200;
    TIM15->CCR1 = 200;
    TIM15->CCR2 = 200;
    TIM16->CCR1 = 200;
    TIM17->CCR1 = 200; 
    
    while(1){
    }
    /*
    // Link the terminal with our server and start it up
    server.attachTerminal(pc);
    server.init();

    // PWM period should nominally be a multiple of our control loop
    motorPWM.period_us(500);

    // 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]; // Voltage for first second
            float v2   = input_params[1]; // Voltage for second second

            // Setup experiment
            t.reset();
            t.start();
            encoderA.reset();
            encoderB.reset();
            encoderC.reset();
            encoderD.reset();
            motorFwd = 1;
            motorRev = 0;
            motorPWM.write(0);

            // Run experiment
            while( t.read() < 2 ) {
                // Perform control loop logic
                if (t.read() < 1)
                    motorPWM.write(v1);
                else
                    motorPWM.write(v2);

                // 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();

                // Send data to MATLAB
                server.sendData(output_data,NUM_OUTPUTS);
                wait(.001);
            }
            // Cleanup after experiment
            server.setExperimentComplete();
            motorPWM.write(0);
        } // end if
    } // end while
    */
} // end main