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: Encoder MODSERIAL mbed
Fork of P_controller_motor_Bouke by
main.cpp
- Committer:
- ThomasBNL
- Date:
- 2015-09-17
- Revision:
- 7:961dcef17e08
- Parent:
- 6:34b39eb3dcb6
- Child:
- 8:80cb1f5d561e
File content as of revision 7:961dcef17e08:
#include "mbed.h"
#include "HIDScope.h"
#include "encoder.h"
// Define the HIDScope and Ticker object
HIDScope scope(1);
Ticker scopeTimer;
// Define Encoder
Encoder encoder1(PTD0,PTC9,true);
Serial pc(USBTX,USBRX);
// Define Potmeter
AnalogIn potmeter1(PTB0);
////// P Motor Controller
// Controller gain
const double motorP_Kp=2.500 ;
const double Convert_volt_to_position=0.00300 ;
// Reusable P controller (FUNCTIE)
double P (double error, const double Kp)
{
return Kp*error;
}
// Motor function
// 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();
return P(reference_position-position, motorP_Kp);
}
int main()
{
motorP = motorP_Controller();
motorP.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 scopeattach()
{
// Attach the data read and send function at 100 Hz
scopeTimer.attach_us(&scopeSend, 2e4);
while(1) { }
}
