![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Using HIDScope for P(I)D controller
Dependencies: FastPWM HIDScope MODSERIAL QEI biquadFilter mbed
Fork of PES_tutorial_5 by
main.cpp@14:29236a33b5e4, 2018-10-19 (annotated)
- Committer:
- lweersink
- Date:
- Fri Oct 19 09:02:56 2018 +0000
- Revision:
- 14:29236a33b5e4
- Parent:
- 13:0b51846cf9e3
- Child:
- 15:c2cfab737a4c
P-controller af. Onnauwkeurigheid in potmeter, waardoor de reference position blijft veranderen, maar motor reageert zoals het hoort.
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 | 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; |
lweersink | 14:29236a33b5e4 | 17 | /*Ticker MakeMotorRotate;*/ //Waar zal deze ticker voor dienen? |
1856413 | 9:b002572e37fd | 18 | |
1856413 | 9:b002572e37fd | 19 | //Global variables |
1856413 | 12:1ecd11dc2c00 | 20 | volatile double measuredPosition = 0.0; |
1856413 | 12:1ecd11dc2c00 | 21 | volatile double referencePosition = 0.0; |
lweersink | 14:29236a33b5e4 | 22 | volatile double motorValue= 0.01; |
1856413 | 12:1ecd11dc2c00 | 23 | volatile double Kp = 0.0; |
nicollevanrijswijk | 5:a1fb2d2fb2d0 | 24 | |
1856413 | 13:0b51846cf9e3 | 25 | //------------------------------------------------------------------------------ |
1856413 | 13:0b51846cf9e3 | 26 | // Functions |
1856413 | 12:1ecd11dc2c00 | 27 | |
1856413 | 12:1ecd11dc2c00 | 28 | double GetReferencePosition() |
1856413 | 12:1ecd11dc2c00 | 29 | { |
1856413 | 12:1ecd11dc2c00 | 30 | double potMeterIn = potMeter1.read(); |
lweersink | 14:29236a33b5e4 | 31 | 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 | 32 | return referencePosition; |
1856413 | 0:2e33035d4e86 | 33 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 34 | |
1856413 | 13:0b51846cf9e3 | 35 | double GetMeasuredPosition() |
nicollevanrijswijk | 11:4e3ef6150a2e | 36 | { |
nicollevanrijswijk | 11:4e3ef6150a2e | 37 | double counts = Encoder.getPulses(); |
lweersink | 14:29236a33b5e4 | 38 | measuredPosition = ( counts / (8400)) * 6.28; // Rotational position in radians |
1856413 | 13:0b51846cf9e3 | 39 | return measuredPosition; |
nicollevanrijswijk | 11:4e3ef6150a2e | 40 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 41 | |
lweersink | 14:29236a33b5e4 | 42 | double FeedbackControl(double Error) |
lweersink | 14:29236a33b5e4 | 43 | { |
1856413 | 13:0b51846cf9e3 | 44 | double Kp = 20.0*potMeter2.read(); // Scale 0 to 20 |
lweersink | 14:29236a33b5e4 | 45 | // Proportional part: |
1856413 | 12:1ecd11dc2c00 | 46 | double u_k = Kp * Error; |
lweersink | 14:29236a33b5e4 | 47 | // Sum all parts and return it |
1856413 | 13:0b51846cf9e3 | 48 | return u_k; //motorValue |
1856413 | 12:1ecd11dc2c00 | 49 | } |
1856413 | 12:1ecd11dc2c00 | 50 | |
1856413 | 12:1ecd11dc2c00 | 51 | void SetMotor1(double motorValue) |
1856413 | 12:1ecd11dc2c00 | 52 | { |
1856413 | 12:1ecd11dc2c00 | 53 | // Given -1<=motorValue<=1, this sets the PWM and direction |
1856413 | 12:1ecd11dc2c00 | 54 | // bits for motor 1. Positive value makes motor rotating |
1856413 | 12:1ecd11dc2c00 | 55 | // clockwise. motorValues outside range are truncated to |
1856413 | 12:1ecd11dc2c00 | 56 | // within range |
lweersink | 14:29236a33b5e4 | 57 | if (motorValue >=0) |
1856413 | 12:1ecd11dc2c00 | 58 | { |
1856413 | 12:1ecd11dc2c00 | 59 | motor1DirectionPin=1; |
1856413 | 12:1ecd11dc2c00 | 60 | } |
1856413 | 12:1ecd11dc2c00 | 61 | else |
1856413 | 12:1ecd11dc2c00 | 62 | { |
1856413 | 12:1ecd11dc2c00 | 63 | motor1DirectionPin=0; |
1856413 | 12:1ecd11dc2c00 | 64 | } |
lweersink | 14:29236a33b5e4 | 65 | if (fabs(motorValue)>1) |
1856413 | 12:1ecd11dc2c00 | 66 | { |
1856413 | 12:1ecd11dc2c00 | 67 | motor1MagnitudePin = 1; |
1856413 | 12:1ecd11dc2c00 | 68 | } |
1856413 | 12:1ecd11dc2c00 | 69 | else |
1856413 | 12:1ecd11dc2c00 | 70 | { |
1856413 | 12:1ecd11dc2c00 | 71 | motor1MagnitudePin = fabs(motorValue); |
1856413 | 12:1ecd11dc2c00 | 72 | } |
1856413 | 12:1ecd11dc2c00 | 73 | } |
1856413 | 12:1ecd11dc2c00 | 74 | //----------------------------------------------------------------------------- |
lweersink | 14:29236a33b5e4 | 75 | // Tickers |
1856413 | 12:1ecd11dc2c00 | 76 | void MeasureAndControl(void) |
1856413 | 12:1ecd11dc2c00 | 77 | { |
lweersink | 14:29236a33b5e4 | 78 | // This function determines the desired velocity, measures the |
1856413 | 12:1ecd11dc2c00 | 79 | // actual velocity, and controls the motor with |
1856413 | 12:1ecd11dc2c00 | 80 | // a simple Feedback controller. Call this from a Ticker. |
lweersink | 14:29236a33b5e4 | 81 | referencePosition = GetReferencePosition(); |
lweersink | 14:29236a33b5e4 | 82 | measuredPosition = GetMeasuredPosition(); |
lweersink | 14:29236a33b5e4 | 83 | motorValue = FeedbackControl(referencePosition - measuredPosition); |
1856413 | 12:1ecd11dc2c00 | 84 | SetMotor1(motorValue); |
1856413 | 13:0b51846cf9e3 | 85 | } |
1856413 | 12:1ecd11dc2c00 | 86 | |
lweersink | 14:29236a33b5e4 | 87 | void printen() |
lweersink | 14:29236a33b5e4 | 88 | { |
lweersink | 14:29236a33b5e4 | 89 | pc.baud (115200); |
lweersink | 14:29236a33b5e4 | 90 | pc.printf("Referenceposition %f \r\n", referencePosition); |
lweersink | 14:29236a33b5e4 | 91 | pc.printf("Measured position %f \r\n", measuredPosition); |
lweersink | 14:29236a33b5e4 | 92 | pc.printf("Motorvalue %f \r\n", motorValue); |
lweersink | 14:29236a33b5e4 | 93 | } |
1856413 | 12:1ecd11dc2c00 | 94 | //----------------------------------------------------------------------------- |
1856413 | 0:2e33035d4e86 | 95 | int main() |
1856413 | 0:2e33035d4e86 | 96 | { |
1856413 | 12:1ecd11dc2c00 | 97 | //Initialize once |
1856413 | 6:bd73804c8cec | 98 | pc.baud(115200); |
1856413 | 13:0b51846cf9e3 | 99 | motor1MagnitudePin.period_us(60.0); // 60 microseconds PWM period: 16.7 kHz. |
1856413 | 12:1ecd11dc2c00 | 100 | MeasureControl.attach(MeasureAndControl, 0.01); |
lweersink | 14:29236a33b5e4 | 101 | print.attach(printen, 3); |
1856413 | 9:b002572e37fd | 102 | |
1856413 | 12:1ecd11dc2c00 | 103 | //Other initializations |
1856413 | 12:1ecd11dc2c00 | 104 | |
1856413 | 13:0b51846cf9e3 | 105 | while(true) |
nicollevanrijswijk | 11:4e3ef6150a2e | 106 | { |
nicollevanrijswijk | 11:4e3ef6150a2e | 107 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 108 | } |