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 10:29:38 2018 +0000
Revision:
16:0a8d4d746454
Parent:
15:c2cfab737a4c
Met hidscope maar doet het niet, gaan we niet gebruiken

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