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:
1856413
Date:
Fri Oct 19 14:18:38 2018 +0000
Revision:
18:e522dfbab4c6
Parent:
17:4a0912c93771
Layout opgefrist, niets aan de code veranderd.

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