Using HIDScope for P(I)D controller
Dependencies: FastPWM HIDScope MODSERIAL QEI biquadFilter mbed
Fork of PES_tutorial_5 by
main.cpp
- Committer:
- 1856413
- Date:
- 2018-10-15
- Revision:
- 12:1ecd11dc2c00
- Parent:
- 11:4e3ef6150a2e
- Child:
- 13:0b51846cf9e3
File content as of revision 12:1ecd11dc2c00:
#include "mbed.h" #include "FastPWM.h" // FastPWM library #include "MODSERIAL.h" #include "QEI.h" #include <math.h> MODSERIAL pc(USBTX, USBRX); DigitalOut Motor1DirectionPin(D7); FastPWM Motor1MagnitudePin(D6); //FastPWM input AnalogIn potMeter1(A4); AnalogIn potMeter2(A5); InterruptIn button2(D3); QEI Encoder (D12, D13, NC, 64, QEI::X4_ENCODING); //Tickers Ticker MeasureControl; Ticker MakeMotorRotate; //Global variables volatile double measuredPosition = 0.0; volatile double referencePosition = 0.0; volatile double motorValue = 0.0; volatile double Kp = 0.0; volatile double /*void Motor() { // Aflezen Potentiometers voor PWM motor1_pwm = potMeter1.read(); // Aflezen PotMeter 1 } */ /*void changeDirectionButton() { Motor1DirectionPin = 1 - Motor1DirectionPin; // pc.printf("Motor direction value %i \r\n", Motor1DirectionPin); } */ double GetReferencePosition() { double potMeterIn = potMeter1.read(); double referencePosition = 4*3.14*potMeterIn - 2*3.14 ; // Reference value y, scaled to -2 to 2 revolutions (or 0 to 100 pi) return referencePosition; } void GetMeasuredPosition() { double counts = Encoder.getPulses(); measuredPosition = ( counts / 4.0 * 64.0) * 6.28; // Rotational position in radians } void PositionGain() { double Kp = 10.0*potMeter2.read(); // Scale 0 to 10 } double Error() { double error = referencePosition - measuredPosition; return error; } double FeedbackControl(double Error) { // Proportional part: double u_k = Kp * Error; // Sum all parts and return it return u_k; } void SetMotor1(double motorValue) { // Given -1<=motorValue<=1, this sets the PWM and direction // bits for motor 1. Positive value makes motor rotating // clockwise. motorValues outside range are truncated to // within range if (motorValue >=0) { motor1DirectionPin=1; } else { motor1DirectionPin=0; } if (fabs(motorValue)>1) { motor1MagnitudePin = 1; } else { motor1MagnitudePin = fabs(motorValue); } } //----------------------------------------------------------------------------- // Ticker void MeasureAndControl(void) { // This function determines the desired velocity, measures the // actual velocity, and controls the motor with // a simple Feedback controller. Call this from a Ticker. float referencePosition = GetReferencePosition(); float measuredPosition = GetMeasuredPosition(); float error = Error(); float motorValue = FeedbackControl(error); SetMotor1(motorValue); //----------------------------------------------------------------------------- int main() { //Initialize once pc.baud(115200); motor1MagnitudePin.period_us(60.0); // 60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to done once), FastPWM variabele MeasureControl.attach(MeasureAndControl, 0.01); //Other initializations button2.rise(changeDirectionButton); while(Error != 0) // when reference postion is reached, stop with the while loop { MakeMotorRotate.attach(SetMotor,0.5); } }