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

Committer:
lweersink
Date:
Fri Oct 19 12:08:31 2018 +0000
Revision:
17:4a0912c93771
Parent:
15:c2cfab737a4c
Child:
18:e522dfbab4c6
Child:
19:1353ba4d94db
PID control, waarden voor Kp, Ki en Kd moeten nog bepaald worden

Who changed what in which revision?

UserRevisionLine numberNew contents of line
1856413 0:2e33035d4e86 1 #include "mbed.h"
1856413 13:0b51846cf9e3 2 #include "FastPWM.h"
1856413 2:34c14fb36b5d 3 #include "MODSERIAL.h"
lweersink 4:49c5fd62a192 4 #include "QEI.h"
1856413 12:1ecd11dc2c00 5 #include <math.h>
1856413 2:34c14fb36b5d 6 MODSERIAL pc(USBTX, USBRX);
1856413 13:0b51846cf9e3 7 DigitalOut motor1DirectionPin(D7);
1856413 13:0b51846cf9e3 8 FastPWM motor1MagnitudePin(D6);
1856413 8:ceb9abb5a4a8 9 AnalogIn potMeter1(A4);
1856413 12:1ecd11dc2c00 10 AnalogIn potMeter2(A5);
1856413 8:ceb9abb5a4a8 11 InterruptIn button2(D3);
1856413 9:b002572e37fd 12 QEI Encoder (D12, D13, NC, 64, QEI::X4_ENCODING);
1856413 12:1ecd11dc2c00 13
1856413 12:1ecd11dc2c00 14 //Tickers
1856413 12:1ecd11dc2c00 15 Ticker MeasureControl;
lweersink 14:29236a33b5e4 16 Ticker print;
1856413 9:b002572e37fd 17
1856413 9:b002572e37fd 18 //Global variables
1856413 12:1ecd11dc2c00 19 volatile double measuredPosition = 0.0;
1856413 12:1ecd11dc2c00 20 volatile double referencePosition = 0.0;
lweersink 14:29236a33b5e4 21 volatile double motorValue= 0.01;
lweersink 17:4a0912c93771 22 volatile double Kp = 5.0; //dit maken we variabel, dit zorgt voor een grote of kleine overshoot
lweersink 15:c2cfab737a4c 23 volatile double Ki = 1.0; //dit moeten we bepalen met een plot bijvoorbeeld
lweersink 17:4a0912c93771 24 volatile double Kd = 0.0;
lweersink 15:c2cfab737a4c 25 volatile double Ts = 0.01;
nicollevanrijswijk 5:a1fb2d2fb2d0 26
1856413 13:0b51846cf9e3 27 //------------------------------------------------------------------------------
1856413 13:0b51846cf9e3 28 // Functions
1856413 12:1ecd11dc2c00 29
1856413 12:1ecd11dc2c00 30 double GetReferencePosition()
1856413 12:1ecd11dc2c00 31 {
1856413 12:1ecd11dc2c00 32 double potMeterIn = potMeter1.read();
lweersink 14:29236a33b5e4 33 referencePosition = 4.0*3.14*potMeterIn - 2.0*3.14 ; // Reference value y, scaled to -2 to 2 revolutions (or 0 to 100 pi) WAAROM?
1856413 12:1ecd11dc2c00 34 return referencePosition;
1856413 0:2e33035d4e86 35 }
nicollevanrijswijk 11:4e3ef6150a2e 36
1856413 13:0b51846cf9e3 37 double GetMeasuredPosition()
nicollevanrijswijk 11:4e3ef6150a2e 38 {
nicollevanrijswijk 11:4e3ef6150a2e 39 double counts = Encoder.getPulses();
lweersink 14:29236a33b5e4 40 measuredPosition = ( counts / (8400)) * 6.28; // Rotational position in radians
1856413 13:0b51846cf9e3 41 return measuredPosition;
nicollevanrijswijk 11:4e3ef6150a2e 42 }
nicollevanrijswijk 11:4e3ef6150a2e 43
lweersink 14:29236a33b5e4 44 double FeedbackControl(double Error)
lweersink 14:29236a33b5e4 45 {
lweersink 17:4a0912c93771 46 static double Error_integral = 0;
lweersink 17:4a0912c93771 47 static double Error_prev = Error;
lweersink 17:4a0912c93771 48 //static BiQuad LowPassFilter(..., ..., ..., ..., ...)
lweersink 14:29236a33b5e4 49 // Proportional part:
lweersink 17:4a0912c93771 50 //van 0 tot 20, waardes rond de 5 zijn het beste (minder overshoot + minder trilling motor beste combinatie hiervan)
1856413 12:1ecd11dc2c00 51 double u_k = Kp * Error;
lweersink 15:c2cfab737a4c 52 // Integral part:
lweersink 15:c2cfab737a4c 53 Error_integral = Error_integral + Error * Ts;
lweersink 15:c2cfab737a4c 54 double u_i = Ki * Error_integral;
lweersink 17:4a0912c93771 55 // Derivative part
lweersink 17:4a0912c93771 56 double Error_derivative = (Error - Error_prev)/Ts;
lweersink 17:4a0912c93771 57 Kd = 20*potMeter2.read();
lweersink 17:4a0912c93771 58 double u_d = Kd * Error_derivative;
lweersink 17:4a0912c93771 59 Error_prev = Error;
lweersink 14:29236a33b5e4 60 // Sum all parts and return it
lweersink 17:4a0912c93771 61 return u_k + u_i + u_d; //motorValue
1856413 12:1ecd11dc2c00 62 }
1856413 12:1ecd11dc2c00 63
1856413 12:1ecd11dc2c00 64 void SetMotor1(double motorValue)
1856413 12:1ecd11dc2c00 65 {
1856413 12:1ecd11dc2c00 66 // Given -1<=motorValue<=1, this sets the PWM and direction
1856413 12:1ecd11dc2c00 67 // bits for motor 1. Positive value makes motor rotating
1856413 12:1ecd11dc2c00 68 // clockwise. motorValues outside range are truncated to
1856413 12:1ecd11dc2c00 69 // within range
lweersink 14:29236a33b5e4 70 if (motorValue >=0)
1856413 12:1ecd11dc2c00 71 {
1856413 12:1ecd11dc2c00 72 motor1DirectionPin=1;
1856413 12:1ecd11dc2c00 73 }
1856413 12:1ecd11dc2c00 74 else
1856413 12:1ecd11dc2c00 75 {
1856413 12:1ecd11dc2c00 76 motor1DirectionPin=0;
1856413 12:1ecd11dc2c00 77 }
lweersink 14:29236a33b5e4 78 if (fabs(motorValue)>1)
1856413 12:1ecd11dc2c00 79 {
1856413 12:1ecd11dc2c00 80 motor1MagnitudePin = 1;
1856413 12:1ecd11dc2c00 81 }
1856413 12:1ecd11dc2c00 82 else
1856413 12:1ecd11dc2c00 83 {
1856413 12:1ecd11dc2c00 84 motor1MagnitudePin = fabs(motorValue);
1856413 12:1ecd11dc2c00 85 }
1856413 12:1ecd11dc2c00 86 }
1856413 12:1ecd11dc2c00 87 //-----------------------------------------------------------------------------
lweersink 14:29236a33b5e4 88 // Tickers
1856413 12:1ecd11dc2c00 89 void MeasureAndControl(void)
1856413 12:1ecd11dc2c00 90 {
lweersink 14:29236a33b5e4 91 // This function determines the desired velocity, measures the
1856413 12:1ecd11dc2c00 92 // actual velocity, and controls the motor with
1856413 12:1ecd11dc2c00 93 // a simple Feedback controller. Call this from a Ticker.
lweersink 14:29236a33b5e4 94 referencePosition = GetReferencePosition();
lweersink 14:29236a33b5e4 95 measuredPosition = GetMeasuredPosition();
lweersink 14:29236a33b5e4 96 motorValue = FeedbackControl(referencePosition - measuredPosition);
1856413 12:1ecd11dc2c00 97 SetMotor1(motorValue);
1856413 13:0b51846cf9e3 98 }
1856413 12:1ecd11dc2c00 99
lweersink 14:29236a33b5e4 100 void printen()
lweersink 14:29236a33b5e4 101 {
lweersink 14:29236a33b5e4 102 pc.baud (115200);
lweersink 14:29236a33b5e4 103 pc.printf("Referenceposition %f \r\n", referencePosition);
lweersink 14:29236a33b5e4 104 pc.printf("Measured position %f \r\n", measuredPosition);
lweersink 15:c2cfab737a4c 105 pc.printf("Motorvalue/Error %f \r\n", motorValue);
lweersink 15:c2cfab737a4c 106 pc.printf("Proportional gain %f \r\n", Kp);
lweersink 15:c2cfab737a4c 107 pc.printf("Integral gain %f \r\n", Ki);
lweersink 17:4a0912c93771 108 pc.printf("Derivative gain %f \r\n", Kd);
lweersink 14:29236a33b5e4 109 }
1856413 12:1ecd11dc2c00 110 //-----------------------------------------------------------------------------
1856413 0:2e33035d4e86 111 int main()
1856413 0:2e33035d4e86 112 {
1856413 12:1ecd11dc2c00 113 //Initialize once
1856413 6:bd73804c8cec 114 pc.baud(115200);
1856413 13:0b51846cf9e3 115 motor1MagnitudePin.period_us(60.0); // 60 microseconds PWM period: 16.7 kHz.
1856413 12:1ecd11dc2c00 116 MeasureControl.attach(MeasureAndControl, 0.01);
lweersink 14:29236a33b5e4 117 print.attach(printen, 3);
1856413 9:b002572e37fd 118
1856413 12:1ecd11dc2c00 119 //Other initializations
1856413 12:1ecd11dc2c00 120
1856413 13:0b51846cf9e3 121 while(true)
nicollevanrijswijk 11:4e3ef6150a2e 122 {
nicollevanrijswijk 11:4e3ef6150a2e 123 }
nicollevanrijswijk 11:4e3ef6150a2e 124 }