Thomas Burgers / Mbed 2 deprecated P_controller_motor_Bouke2

Dependencies:   Encoder MODSERIAL mbed

Fork of P_controller_motor_Bouke by Thomas Burgers

main.cpp

Committer:
ThomasBNL
Date:
2015-09-17
Revision:
4:dfdfcb518e60
Parent:
3:eee8d5461256
Child:
5:8ea7a765c1f7

File content as of revision 4:dfdfcb518e60:

#include "mbed.h"
#include "HIDScope.h"
#include "encoder.h"

// Define the HIDScope and Ticker object
HIDScope    scope(1);
Ticker      scopeTimer;

// Define Encoder

Encoder motorP(PTD0,PTC9,true);
Serial pc(USBTX,USBRX);
pc.baud(115200);
while(1) {
    wait(0.2);
    pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed());
    }

// Define Potmeter


////// P Motor Controller

// Controller gain
const double motorP_Kp=2.5;
const double Convert_volt_to_position=0.00300;

// Reusable P controller (FUNCTIE)
double P (double error, const double Kp)
    {
            return Kp*error;
    }




// encoder1
// potmeter1
    
// Error measurement and apply the output to the plant
void motorP_Controller()
    {
        double reference_position = potmeter1.read();
        double position = Convert_volt_to_position*encoder1.getPosition();
        motorP=P(reference_position - position, motorP_Kp);
    }
    
int main()
    {
        myControllerTicker.attach(&motorP_Controller,0.01f); //100Hz
        while(1){}
    }


// Read the analog input
float triangle_signal = 2.05;
 
// The data read and send function
void scopeSend()
{
    scope.set(0,motorP);
    scope.send();
}
 
int main()
{
    // Attach the data read and send function at 200 Hz
    scopeTimer.attach_us(&scopeSend, 2e4);   
    
    while(1) { }
}