Using HIDScope for P(I)D controller

Dependencies:   FastPWM HIDScope MODSERIAL QEI biquadFilter mbed

Fork of PES_tutorial_5 by BMT Module 9 Group 4

main.cpp

Committer:
1856413
Date:
2018-10-15
Revision:
8:ceb9abb5a4a8
Parent:
7:3b503177ff5c
Child:
9:b002572e37fd
Child:
10:076eb8beea30

File content as of revision 8:ceb9abb5a4a8:

#include "mbed.h"
#include "FastPWM.h"    // FastPWM library
#include "MODSERIAL.h"
#include "QEI.h"
MODSERIAL pc(USBTX, USBRX);
DigitalOut motor1_direction(D7);
AnalogIn potMeter1(A4);
InterruptIn button2(D3);
FastPWM motor1_pwm(D6); //FastPWM input, PES lecture 2
Ticker MotorSpeedCounts;
QEI Encoder (D12, D13, NC, 64, QEI::X4_ENCODING);

void Motor()
{
    // Aflezen Potentiometers voor PWM
    float potMeterIn = potMeter1.read(); // Aflezen PotMeter 1
    motor1_pwm = potMeterIn;
    // Encoder counts printen
    pc.printf("%i\r\n", Encoder.getPulses());
}
   
void changeDirectionButton()
{
    motor1_direction = 1 - motor1_direction;
    
    //float motor1_velocity = pot1.read() *6.2;
    //pc.printf("Velocity is %f \r\n", motor1_velocity);
}

double GetReferenceVelocity()
{
    // Returns reference velocity in rad/s. 
    // Positive value means clockwise rotation.
    // 60 rpm = 60*2*pi/60 = 6.28 ~ 6.2 rad/s
    const float maxVelocity=6.2; // in rad/s of course!    
    double referenceVelocity;  // in rad/s
    if (button2)
        {
        // Clockwise rotation      
        referenceVelocity = potMeterIn * maxVelocity;  
        } 
        else
        {
        // Counterclockwise rotation       
        referenceVelocity = -1*potMeterIn * maxVelocity;   
        }
    return referenceVelocity;
}

double GetMeasuredVelocity()
{
    // Get actual velocity from the motor plant
    // Use encoder (counts)
    
    int counts = Encoder.getPulses();
    double RotationalPosition = ( counts / 4 * 64) * 6.28; // Rotational position in radians   
    
    double measuredVelocity;
    // hier komt de berekening van measured velocity
    return measuredVelocity;
}


int main()
{
    pc.baud(115200);
    motor1_pwm.period_us(60.0); // 60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to done once), FastPWM variabele
    MotorSpeedCounts.attach(Motor, 0.5); // Ticker die de functie Motor aanroept elke halve seconde, meer tijd is tragere respons op potmeter
    button2.rise(changeDirectionButton);
    while(true){} // Endless loop  
}