Using HIDScope for P(I)D controller
Dependencies: FastPWM HIDScope MODSERIAL QEI biquadFilter mbed
Fork of PES_tutorial_5 by
main.cpp@20:e00e41e3cda8, 2018-10-27 (annotated)
- Committer:
- 1856413
- Date:
- Sat Oct 27 15:55:00 2018 +0000
- Revision:
- 20:e00e41e3cda8
- Parent:
- 19:1353ba4d94db
- Child:
- 21:5f88e09d6ab8
Show velocity (through measured position with encoder) in Hidscope. Led works, but Hidscope doesn't show anything. NOTE: PWM still needs to be set maximum
Who changed what in which revision?
User | Revision | Line number | New 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 | 20:e00e41e3cda8 | 5 | #include "HIDScope.h" |
1856413 | 12:1ecd11dc2c00 | 6 | #include <math.h> |
1856413 | 2:34c14fb36b5d | 7 | MODSERIAL pc(USBTX, USBRX); |
1856413 | 13:0b51846cf9e3 | 8 | DigitalOut motor1DirectionPin(D7); |
1856413 | 13:0b51846cf9e3 | 9 | FastPWM motor1MagnitudePin(D6); |
1856413 | 8:ceb9abb5a4a8 | 10 | AnalogIn potMeter1(A4); |
1856413 | 12:1ecd11dc2c00 | 11 | AnalogIn potMeter2(A5); |
1856413 | 8:ceb9abb5a4a8 | 12 | InterruptIn button2(D3); |
1856413 | 20:e00e41e3cda8 | 13 | DigitalOut led(LED1); |
1856413 | 9:b002572e37fd | 14 | QEI Encoder (D12, D13, NC, 64, QEI::X4_ENCODING); |
1856413 | 20:e00e41e3cda8 | 15 | HIDScope scope(1); |
1856413 | 12:1ecd11dc2c00 | 16 | |
1856413 | 12:1ecd11dc2c00 | 17 | //Tickers |
1856413 | 12:1ecd11dc2c00 | 18 | Ticker MeasureControl; |
lweersink | 14:29236a33b5e4 | 19 | Ticker print; |
1856413 | 20:e00e41e3cda8 | 20 | Ticker SenttoHidscope; |
1856413 | 9:b002572e37fd | 21 | |
1856413 | 9:b002572e37fd | 22 | //Global variables |
1856413 | 12:1ecd11dc2c00 | 23 | volatile double measuredPosition = 0.0; |
1856413 | 12:1ecd11dc2c00 | 24 | volatile double referencePosition = 0.0; |
lweersink | 14:29236a33b5e4 | 25 | volatile double motorValue= 0.01; |
lweersink | 17:4a0912c93771 | 26 | volatile double Kp = 5.0; //dit maken we variabel, dit zorgt voor een grote of kleine overshoot |
lweersink | 15:c2cfab737a4c | 27 | volatile double Ki = 1.0; //dit moeten we bepalen met een plot bijvoorbeeld |
lweersink | 17:4a0912c93771 | 28 | volatile double Kd = 0.0; |
lweersink | 15:c2cfab737a4c | 29 | volatile double Ts = 0.01; |
1856413 | 20:e00e41e3cda8 | 30 | volatile double measuredVelocity = 0.0; |
1856413 | 20:e00e41e3cda8 | 31 | volatile double tickertime = 0.001; |
nicollevanrijswijk | 5:a1fb2d2fb2d0 | 32 | |
1856413 | 13:0b51846cf9e3 | 33 | //------------------------------------------------------------------------------ |
1856413 | 13:0b51846cf9e3 | 34 | // Functions |
1856413 | 20:e00e41e3cda8 | 35 | /*double EMGFilters() |
lweersink | 19:1353ba4d94db | 36 | { |
lweersink | 19:1353ba4d94db | 37 | static BiQuad Notchfilter(1.0000, -1.6180, 1.0000, 1.0000, -1.5687, 0.9391); |
lweersink | 19:1353ba4d94db | 38 | static BiQuad HighPassFilter (1.0000, -2.0000, 1.0000, 1.0000, -1.8268, 0.8407); |
lweersink | 19:1353ba4d94db | 39 | static BiQuad LowPassFilter (1.0000, 2.0000, 1.0000, 1.0000, 0.3172, 0.1894); |
1856413 | 20:e00e41e3cda8 | 40 | }*/ |
1856413 | 20:e00e41e3cda8 | 41 | |
1856413 | 12:1ecd11dc2c00 | 42 | double GetReferencePosition() |
1856413 | 12:1ecd11dc2c00 | 43 | { |
1856413 | 12:1ecd11dc2c00 | 44 | double potMeterIn = potMeter1.read(); |
lweersink | 14:29236a33b5e4 | 45 | 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 | 46 | return referencePosition; |
1856413 | 0:2e33035d4e86 | 47 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 48 | |
1856413 | 13:0b51846cf9e3 | 49 | double GetMeasuredPosition() |
nicollevanrijswijk | 11:4e3ef6150a2e | 50 | { |
nicollevanrijswijk | 11:4e3ef6150a2e | 51 | double counts = Encoder.getPulses(); |
lweersink | 14:29236a33b5e4 | 52 | measuredPosition = ( counts / (8400)) * 6.28; // Rotational position in radians |
1856413 | 13:0b51846cf9e3 | 53 | return measuredPosition; |
nicollevanrijswijk | 11:4e3ef6150a2e | 54 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 55 | |
lweersink | 14:29236a33b5e4 | 56 | double FeedbackControl(double Error) |
lweersink | 14:29236a33b5e4 | 57 | { |
lweersink | 17:4a0912c93771 | 58 | static double Error_integral = 0; |
lweersink | 17:4a0912c93771 | 59 | static double Error_prev = Error; |
lweersink | 17:4a0912c93771 | 60 | //static BiQuad LowPassFilter(..., ..., ..., ..., ...) |
lweersink | 14:29236a33b5e4 | 61 | // Proportional part: |
lweersink | 17:4a0912c93771 | 62 | //van 0 tot 20, waardes rond de 5 zijn het beste (minder overshoot + minder trilling motor beste combinatie hiervan) |
1856413 | 12:1ecd11dc2c00 | 63 | double u_k = Kp * Error; |
lweersink | 15:c2cfab737a4c | 64 | // Integral part: |
lweersink | 15:c2cfab737a4c | 65 | Error_integral = Error_integral + Error * Ts; |
lweersink | 15:c2cfab737a4c | 66 | double u_i = Ki * Error_integral; |
lweersink | 17:4a0912c93771 | 67 | // Derivative part |
lweersink | 17:4a0912c93771 | 68 | double Error_derivative = (Error - Error_prev)/Ts; |
lweersink | 17:4a0912c93771 | 69 | Kd = 20*potMeter2.read(); |
lweersink | 17:4a0912c93771 | 70 | double u_d = Kd * Error_derivative; |
lweersink | 17:4a0912c93771 | 71 | Error_prev = Error; |
lweersink | 14:29236a33b5e4 | 72 | // Sum all parts and return it |
lweersink | 17:4a0912c93771 | 73 | return u_k + u_i + u_d; //motorValue |
1856413 | 12:1ecd11dc2c00 | 74 | } |
1856413 | 12:1ecd11dc2c00 | 75 | |
1856413 | 12:1ecd11dc2c00 | 76 | void SetMotor1(double motorValue) |
1856413 | 12:1ecd11dc2c00 | 77 | { |
1856413 | 12:1ecd11dc2c00 | 78 | // Given -1<=motorValue<=1, this sets the PWM and direction |
1856413 | 12:1ecd11dc2c00 | 79 | // bits for motor 1. Positive value makes motor rotating |
1856413 | 12:1ecd11dc2c00 | 80 | // clockwise. motorValues outside range are truncated to |
1856413 | 12:1ecd11dc2c00 | 81 | // within range |
lweersink | 14:29236a33b5e4 | 82 | if (motorValue >=0) |
1856413 | 12:1ecd11dc2c00 | 83 | { |
1856413 | 12:1ecd11dc2c00 | 84 | motor1DirectionPin=1; |
1856413 | 12:1ecd11dc2c00 | 85 | } |
1856413 | 12:1ecd11dc2c00 | 86 | else |
1856413 | 12:1ecd11dc2c00 | 87 | { |
1856413 | 12:1ecd11dc2c00 | 88 | motor1DirectionPin=0; |
1856413 | 12:1ecd11dc2c00 | 89 | } |
lweersink | 14:29236a33b5e4 | 90 | if (fabs(motorValue)>1) |
1856413 | 12:1ecd11dc2c00 | 91 | { |
1856413 | 12:1ecd11dc2c00 | 92 | motor1MagnitudePin = 1; |
1856413 | 12:1ecd11dc2c00 | 93 | } |
1856413 | 12:1ecd11dc2c00 | 94 | else |
1856413 | 12:1ecd11dc2c00 | 95 | { |
1856413 | 12:1ecd11dc2c00 | 96 | motor1MagnitudePin = fabs(motorValue); |
1856413 | 12:1ecd11dc2c00 | 97 | } |
1856413 | 12:1ecd11dc2c00 | 98 | } |
1856413 | 12:1ecd11dc2c00 | 99 | //----------------------------------------------------------------------------- |
lweersink | 14:29236a33b5e4 | 100 | // Tickers |
1856413 | 12:1ecd11dc2c00 | 101 | void MeasureAndControl(void) |
1856413 | 12:1ecd11dc2c00 | 102 | { |
lweersink | 14:29236a33b5e4 | 103 | // This function determines the desired velocity, measures the |
1856413 | 12:1ecd11dc2c00 | 104 | // actual velocity, and controls the motor with |
1856413 | 12:1ecd11dc2c00 | 105 | // a simple Feedback controller. Call this from a Ticker. |
lweersink | 14:29236a33b5e4 | 106 | referencePosition = GetReferencePosition(); |
lweersink | 14:29236a33b5e4 | 107 | measuredPosition = GetMeasuredPosition(); |
lweersink | 14:29236a33b5e4 | 108 | motorValue = FeedbackControl(referencePosition - measuredPosition); |
1856413 | 12:1ecd11dc2c00 | 109 | SetMotor1(motorValue); |
1856413 | 13:0b51846cf9e3 | 110 | } |
1856413 | 12:1ecd11dc2c00 | 111 | |
1856413 | 20:e00e41e3cda8 | 112 | void hidscope() |
1856413 | 20:e00e41e3cda8 | 113 | { |
1856413 | 20:e00e41e3cda8 | 114 | double OldmeasuredPosition = measuredPosition; |
1856413 | 20:e00e41e3cda8 | 115 | double measuredVelocity = (measuredPosition - OldmeasuredPosition) / tickertime; |
1856413 | 20:e00e41e3cda8 | 116 | scope.set(0, measuredVelocity); // set the encoder pulses in channel 0 |
1856413 | 20:e00e41e3cda8 | 117 | scope.send(); |
1856413 | 20:e00e41e3cda8 | 118 | led = !led; |
1856413 | 20:e00e41e3cda8 | 119 | } |
1856413 | 20:e00e41e3cda8 | 120 | |
1856413 | 20:e00e41e3cda8 | 121 | /*void printen() |
lweersink | 14:29236a33b5e4 | 122 | { |
lweersink | 14:29236a33b5e4 | 123 | pc.baud (115200); |
lweersink | 14:29236a33b5e4 | 124 | pc.printf("Referenceposition %f \r\n", referencePosition); |
lweersink | 14:29236a33b5e4 | 125 | pc.printf("Measured position %f \r\n", measuredPosition); |
lweersink | 15:c2cfab737a4c | 126 | pc.printf("Motorvalue/Error %f \r\n", motorValue); |
lweersink | 15:c2cfab737a4c | 127 | pc.printf("Proportional gain %f \r\n", Kp); |
lweersink | 15:c2cfab737a4c | 128 | pc.printf("Integral gain %f \r\n", Ki); |
lweersink | 17:4a0912c93771 | 129 | pc.printf("Derivative gain %f \r\n", Kd); |
1856413 | 20:e00e41e3cda8 | 130 | }*/ |
1856413 | 12:1ecd11dc2c00 | 131 | //----------------------------------------------------------------------------- |
1856413 | 0:2e33035d4e86 | 132 | int main() |
1856413 | 0:2e33035d4e86 | 133 | { |
1856413 | 12:1ecd11dc2c00 | 134 | //Initialize once |
1856413 | 6:bd73804c8cec | 135 | pc.baud(115200); |
1856413 | 13:0b51846cf9e3 | 136 | motor1MagnitudePin.period_us(60.0); // 60 microseconds PWM period: 16.7 kHz. |
1856413 | 12:1ecd11dc2c00 | 137 | MeasureControl.attach(MeasureAndControl, 0.01); |
1856413 | 20:e00e41e3cda8 | 138 | SenttoHidscope.attach(hidscope, 0.02); |
1856413 | 20:e00e41e3cda8 | 139 | /*print.attach(printen, 3);*/ |
1856413 | 9:b002572e37fd | 140 | |
1856413 | 12:1ecd11dc2c00 | 141 | //Other initializations |
1856413 | 12:1ecd11dc2c00 | 142 | |
1856413 | 13:0b51846cf9e3 | 143 | while(true) |
nicollevanrijswijk | 11:4e3ef6150a2e | 144 | { |
nicollevanrijswijk | 11:4e3ef6150a2e | 145 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 146 | } |