![](/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@13:0b51846cf9e3, 2018-10-16 (annotated)
- Committer:
- 1856413
- Date:
- Tue Oct 16 09:06:44 2018 +0000
- Revision:
- 13:0b51846cf9e3
- Parent:
- 12:1ecd11dc2c00
- Child:
- 14:29236a33b5e4
Functies werken, main moet nog aangepast worden. Nog geen printen van variabelen.
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; |
1856413 | 13:0b51846cf9e3 | 16 | /*Ticker MakeMotorRotate;*/ |
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; |
1856413 | 12:1ecd11dc2c00 | 21 | volatile double motorValue = 0.0; |
1856413 | 12:1ecd11dc2c00 | 22 | volatile double Kp = 0.0; |
nicollevanrijswijk | 5:a1fb2d2fb2d0 | 23 | |
1856413 | 13:0b51846cf9e3 | 24 | //------------------------------------------------------------------------------ |
1856413 | 13:0b51846cf9e3 | 25 | // Functions |
1856413 | 12:1ecd11dc2c00 | 26 | |
1856413 | 12:1ecd11dc2c00 | 27 | double GetReferencePosition() |
1856413 | 12:1ecd11dc2c00 | 28 | { |
1856413 | 12:1ecd11dc2c00 | 29 | double potMeterIn = potMeter1.read(); |
1856413 | 13:0b51846cf9e3 | 30 | referencePosition = 4.0*3.14*potMeterIn - 2.0*3.14 ; // Reference value y, scaled to -2 to 2 revolutions (or 0 to 100 pi) |
1856413 | 12:1ecd11dc2c00 | 31 | return referencePosition; |
1856413 | 0:2e33035d4e86 | 32 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 33 | |
1856413 | 13:0b51846cf9e3 | 34 | double GetMeasuredPosition() |
nicollevanrijswijk | 11:4e3ef6150a2e | 35 | { |
nicollevanrijswijk | 11:4e3ef6150a2e | 36 | double counts = Encoder.getPulses(); |
1856413 | 13:0b51846cf9e3 | 37 | measuredPosition = ( counts / 4.0 * 64.0) * 6.28; // Rotational position in radians |
1856413 | 13:0b51846cf9e3 | 38 | return measuredPosition; |
nicollevanrijswijk | 11:4e3ef6150a2e | 39 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 40 | |
1856413 | 12:1ecd11dc2c00 | 41 | double FeedbackControl(double Error) |
1856413 | 13:0b51846cf9e3 | 42 | { |
1856413 | 13:0b51846cf9e3 | 43 | double Kp = 20.0*potMeter2.read(); // Scale 0 to 20 |
1856413 | 13:0b51846cf9e3 | 44 | // Proportional part: |
1856413 | 12:1ecd11dc2c00 | 45 | double u_k = Kp * Error; |
1856413 | 12:1ecd11dc2c00 | 46 | // Sum all parts and return it |
1856413 | 13:0b51846cf9e3 | 47 | return u_k; //motorValue |
1856413 | 12:1ecd11dc2c00 | 48 | } |
1856413 | 12:1ecd11dc2c00 | 49 | |
1856413 | 12:1ecd11dc2c00 | 50 | void SetMotor1(double motorValue) |
1856413 | 12:1ecd11dc2c00 | 51 | { |
1856413 | 12:1ecd11dc2c00 | 52 | // Given -1<=motorValue<=1, this sets the PWM and direction |
1856413 | 12:1ecd11dc2c00 | 53 | // bits for motor 1. Positive value makes motor rotating |
1856413 | 12:1ecd11dc2c00 | 54 | // clockwise. motorValues outside range are truncated to |
1856413 | 12:1ecd11dc2c00 | 55 | // within range |
1856413 | 12:1ecd11dc2c00 | 56 | if (motorValue >=0) |
1856413 | 12:1ecd11dc2c00 | 57 | { |
1856413 | 12:1ecd11dc2c00 | 58 | motor1DirectionPin=1; |
1856413 | 12:1ecd11dc2c00 | 59 | } |
1856413 | 12:1ecd11dc2c00 | 60 | else |
1856413 | 12:1ecd11dc2c00 | 61 | { |
1856413 | 12:1ecd11dc2c00 | 62 | motor1DirectionPin=0; |
1856413 | 12:1ecd11dc2c00 | 63 | } |
1856413 | 12:1ecd11dc2c00 | 64 | if (fabs(motorValue)>1) |
1856413 | 12:1ecd11dc2c00 | 65 | { |
1856413 | 12:1ecd11dc2c00 | 66 | motor1MagnitudePin = 1; |
1856413 | 12:1ecd11dc2c00 | 67 | } |
1856413 | 12:1ecd11dc2c00 | 68 | else |
1856413 | 12:1ecd11dc2c00 | 69 | { |
1856413 | 12:1ecd11dc2c00 | 70 | motor1MagnitudePin = fabs(motorValue); |
1856413 | 12:1ecd11dc2c00 | 71 | } |
1856413 | 12:1ecd11dc2c00 | 72 | } |
1856413 | 12:1ecd11dc2c00 | 73 | //----------------------------------------------------------------------------- |
1856413 | 12:1ecd11dc2c00 | 74 | // Ticker |
1856413 | 12:1ecd11dc2c00 | 75 | void MeasureAndControl(void) |
1856413 | 12:1ecd11dc2c00 | 76 | { |
1856413 | 12:1ecd11dc2c00 | 77 | // This function determines the desired velocity, measures the |
1856413 | 12:1ecd11dc2c00 | 78 | // actual velocity, and controls the motor with |
1856413 | 12:1ecd11dc2c00 | 79 | // a simple Feedback controller. Call this from a Ticker. |
1856413 | 13:0b51846cf9e3 | 80 | double referencePosition = GetReferencePosition(); |
1856413 | 13:0b51846cf9e3 | 81 | double measuredPosition = GetMeasuredPosition(); |
1856413 | 13:0b51846cf9e3 | 82 | double motorValue = FeedbackControl(referencePosition - measuredPosition); |
1856413 | 12:1ecd11dc2c00 | 83 | SetMotor1(motorValue); |
1856413 | 13:0b51846cf9e3 | 84 | } |
1856413 | 12:1ecd11dc2c00 | 85 | |
1856413 | 12:1ecd11dc2c00 | 86 | //----------------------------------------------------------------------------- |
1856413 | 0:2e33035d4e86 | 87 | int main() |
1856413 | 0:2e33035d4e86 | 88 | { |
1856413 | 12:1ecd11dc2c00 | 89 | //Initialize once |
1856413 | 6:bd73804c8cec | 90 | pc.baud(115200); |
1856413 | 13:0b51846cf9e3 | 91 | motor1MagnitudePin.period_us(60.0); // 60 microseconds PWM period: 16.7 kHz. |
1856413 | 12:1ecd11dc2c00 | 92 | MeasureControl.attach(MeasureAndControl, 0.01); |
1856413 | 13:0b51846cf9e3 | 93 | /*MakeMotorRotate.attach(SetMotor1, 0.01);*/ |
1856413 | 9:b002572e37fd | 94 | |
1856413 | 12:1ecd11dc2c00 | 95 | //Other initializations |
1856413 | 12:1ecd11dc2c00 | 96 | |
1856413 | 13:0b51846cf9e3 | 97 | while(true) |
nicollevanrijswijk | 11:4e3ef6150a2e | 98 | { |
nicollevanrijswijk | 11:4e3ef6150a2e | 99 | } |
nicollevanrijswijk | 11:4e3ef6150a2e | 100 | } |