Afgesplitste versie van motor control waarbij we ook iets met EMG gaan doen

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

main.cpp

Committer:
Jellehierck
Date:
2019-10-04
Revision:
3:2d45e3d0b0f0
Parent:
2:6f5f300f0569
Child:
4:28d71b0a29aa

File content as of revision 3:2d45e3d0b0f0:

#include "mbed.h"
//#include "HIDScope.h"
#include "QEI.h"
#include "MODSERIAL.h"
//#include "BiQuad.h"
#include "FastPWM.h"

DigitalOut ledr(LED_RED);
DigitalOut ledg(LED_GREEN);
DigitalOut ledb(LED_BLUE);
PwmOut led1(D10);
DigitalIn button1(D11);
AnalogIn potmeter(A0);
DigitalIn sw(SW2);
MODSERIAL pc(USBTX, USBRX);
DigitalIn encA(D13);
DigitalIn encB(D12);
QEI encoder(D13,D12,NC,64,QEI::X4_ENCODING);

DigitalOut motor2Direction(D4);
FastPWM motor2Power(D5);
DigitalOut motor1Direction(D7);
FastPWM motor1Power(D6);

Ticker motorTicker;

const float maxVelocity = 7.958701; // 76 RPM to rad/s
const double PWM_freq = 1e-6;

int counts; // Encoder counts
float degreein; // Angle of DC motor shaft input (before gearbox)
float factorin= 360/64; // Convert encoder counts to angle in degrees
float degreeout; // Angle of motor shaft output (after gearbox)
float gearratio= 131.25; // Gear ratio of gearbox

float getRefVelocity()
{
    float refVelocity;

    if (button1) {
        refVelocity = potmeter.read() * maxVelocity;
    } else {
        refVelocity = potmeter.read() * maxVelocity * -1;
    }
    return refVelocity;
}

void motorControl()
{
    float potValue = potmeter.read();
    motor1Power.pulsewidth(potValue * PWM_freq);
}

int main()
{
    pc.baud(115200);
    pc.printf("\r\nStarting...\r\n\r\n");

    motor1Power.period(PWM_freq);

    motorTicker.attach(motorControl, 0.01);

    motor1Direction = 0;

    while (true) {
        counts = encoder.getPulses(); // Get encoder pulses
        degreein = counts*factorin;  // Convert encoder data to angle
        degreeout = degreein/gearratio; // Derived output angle
        //pc.printf("%f \r\n", degreein); //draaigraden van inputgear en zo ook encoder.
        pc.printf("%f \r\n", degreeout); // Angle of output

        float potValue = potmeter.read(); // Read potmeter
        pc.printf("Potmeter: %f \r\n", potValue);
        
        // motor1Power.pulsewidth(potValue * PWM_freq);

        wait(0.5);
    }
}