Werkend aansturingsscript voor 2 motoren, incl werkende program switch. Motoren oscilleren nog iets. Vuur mechanisme ontbreekt nog.
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed
Fork of frdm_Motor_V2_2 by
main.cpp@22:56c3a5918bfc, 2015-10-03 (annotated)
- Committer:
- Rvs94
- Date:
- Sat Oct 03 14:04:36 2015 +0000
- Revision:
- 22:56c3a5918bfc
- Parent:
- 20:f5091e29cd26
Tickers naar een lagere frequentie gehaald, mocht niet baten. Instabiliteit van het programma blijft het probleem
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Margreeth95 | 0:284ed397e046 | 1 | #include "mbed.h" |
Margreeth95 | 0:284ed397e046 | 2 | #include "MODSERIAL.h" |
Margreeth95 | 0:284ed397e046 | 3 | #include "HIDScope.h" |
Margreeth95 | 0:284ed397e046 | 4 | #include "QEI.h" |
Rvs94 | 12:69ab81cf5b7d | 5 | #include "biquadFilter.h" |
Margreeth95 | 0:284ed397e046 | 6 | |
Margreeth95 | 0:284ed397e046 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
Rvs94 | 20:f5091e29cd26 | 8 | QEI Encoder(D3, D2, NC, 128); |
Rvs94 | 20:f5091e29cd26 | 9 | HIDScope scope(3); |
Rvs94 | 20:f5091e29cd26 | 10 | |
Rvs94 | 20:f5091e29cd26 | 11 | //Ledjes |
Margreeth95 | 19:9417d2011e8b | 12 | DigitalOut LedR(LED_RED); |
Margreeth95 | 19:9417d2011e8b | 13 | DigitalOut LedG(LED_GREEN); |
Margreeth95 | 19:9417d2011e8b | 14 | DigitalOut LedB(LED_BLUE); |
Rvs94 | 20:f5091e29cd26 | 15 | |
Rvs94 | 20:f5091e29cd26 | 16 | //Motor |
Margreeth95 | 0:284ed397e046 | 17 | DigitalOut motor2direction(D4); //D4 en D5 zijn motor 2 (op het motorshield) |
Margreeth95 | 0:284ed397e046 | 18 | PwmOut motor2speed(D5); |
Rvs94 | 12:69ab81cf5b7d | 19 | |
Rvs94 | 20:f5091e29cd26 | 20 | //Tickers |
Margreeth95 | 0:284ed397e046 | 21 | Ticker ScopeTime; |
Rvs94 | 9:774fc3c6a39e | 22 | Ticker myControllerTicker; |
Rvs94 | 7:67b50d4fb03c | 23 | |
Rvs94 | 20:f5091e29cd26 | 24 | //Startwaarden |
Rvs94 | 10:80fe931a71e4 | 25 | double reference; |
Rvs94 | 10:80fe931a71e4 | 26 | double position; |
Rvs94 | 15:7fbee317af2d | 27 | double m2_ref = 0; |
Margreeth95 | 19:9417d2011e8b | 28 | int count = 0; |
Rvs94 | 2:099da0fc31b6 | 29 | |
Rvs94 | 20:f5091e29cd26 | 30 | //Sample time (motor2-step) |
Rvs94 | 20:f5091e29cd26 | 31 | const double m2_Ts = 0.01; |
Rvs94 | 20:f5091e29cd26 | 32 | |
Rvs94 | 20:f5091e29cd26 | 33 | //Controller gain Motor 2 |
Rvs94 | 20:f5091e29cd26 | 34 | const double m2_Kp = 0.5,m2_Ki = 0.005, m2_Kd = 0.5; |
Rvs94 | 20:f5091e29cd26 | 35 | double m2_err_int = 0, m2_prev_err = 0; |
Rvs94 | 20:f5091e29cd26 | 36 | |
Rvs94 | 20:f5091e29cd26 | 37 | //Derivative filter coeffs Motor 2 |
Rvs94 | 20:f5091e29cd26 | 38 | const double BiGain = 0.016955; |
Rvs94 | 20:f5091e29cd26 | 39 | const double m2_f_a1 = -0.96608908283*BiGain, m2_f_a2 = 0.0*BiGain, m2_f_b0 = 1.0*BiGain, m2_f_b1 = 1.0*BiGain, m2_f_b2 = 0.0*BiGain; |
Rvs94 | 20:f5091e29cd26 | 40 | |
Rvs94 | 20:f5091e29cd26 | 41 | // Filter variables |
Rvs94 | 20:f5091e29cd26 | 42 | double m2_f_v1 = 0, m2_f_v2 = 0; |
Rvs94 | 20:f5091e29cd26 | 43 | |
Rvs94 | 20:f5091e29cd26 | 44 | |
Rvs94 | 20:f5091e29cd26 | 45 | //HIDScope |
Margreeth95 | 0:284ed397e046 | 46 | void ScopeSend()//Functie die de gegevens voor de scope uitleest en doorstuurt |
Margreeth95 | 0:284ed397e046 | 47 | { |
Margreeth95 | 0:284ed397e046 | 48 | scope.set(0, motor2direction.read()); |
Margreeth95 | 0:284ed397e046 | 49 | scope.set(1, motor2speed.read()); |
Rvs94 | 15:7fbee317af2d | 50 | scope.set(2, position); |
Rvs94 | 4:0d4aff8b57b3 | 51 | |
Margreeth95 | 0:284ed397e046 | 52 | scope.send(); |
Rvs94 | 1:48aba8d5610a | 53 | |
Margreeth95 | 0:284ed397e046 | 54 | } |
Rvs94 | 12:69ab81cf5b7d | 55 | |
Rvs94 | 12:69ab81cf5b7d | 56 | // Biquad filter |
Rvs94 | 12:69ab81cf5b7d | 57 | double biquad( double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2 ) |
Rvs94 | 9:774fc3c6a39e | 58 | { |
Rvs94 | 13:a6770307a5d2 | 59 | double v = u - a1*v1 - a2*v2; |
Rvs94 | 13:a6770307a5d2 | 60 | double y = b0*v + b1*v1 + b2*v2; |
Rvs94 | 12:69ab81cf5b7d | 61 | v2 = v1; v1 = v; |
Rvs94 | 12:69ab81cf5b7d | 62 | return y; |
Rvs94 | 12:69ab81cf5b7d | 63 | } |
Rvs94 | 12:69ab81cf5b7d | 64 | |
Rvs94 | 12:69ab81cf5b7d | 65 | |
Rvs94 | 12:69ab81cf5b7d | 66 | // Reusable PID controller |
Rvs94 | 12:69ab81cf5b7d | 67 | double PID( double e, const double Kp, const double Ki, const double Kd, double Ts, double &e_int, double &e_prev, double &f_v1, double &f_v2, |
Rvs94 | 13:a6770307a5d2 | 68 | const double f_a1,const double f_a2, const double f_b0, const double f_b1, const double f_b2) |
Rvs94 | 12:69ab81cf5b7d | 69 | { |
Rvs94 | 12:69ab81cf5b7d | 70 | // Derivative |
Rvs94 | 12:69ab81cf5b7d | 71 | double e_der = (e-e_prev)/Ts; |
Rvs94 | 12:69ab81cf5b7d | 72 | e_der = biquad(e_der,f_v1,f_v2,f_a1,f_a2,f_b0,f_b1,f_b2); |
Rvs94 | 13:a6770307a5d2 | 73 | e_prev = e; |
Rvs94 | 12:69ab81cf5b7d | 74 | // Integral |
Rvs94 | 12:69ab81cf5b7d | 75 | e_int = e_int + Ts*e; |
Rvs94 | 12:69ab81cf5b7d | 76 | // PID |
Rvs94 | 12:69ab81cf5b7d | 77 | return Kp * e + Ki*e_int + Kd*e_der; |
Rvs94 | 9:774fc3c6a39e | 78 | } |
Margreeth95 | 0:284ed397e046 | 79 | |
Margreeth95 | 18:6f71bb91b8bd | 80 | // Motor2 control |
Rvs94 | 11:0793a78109a2 | 81 | void motor2_Controller() |
Rvs94 | 9:774fc3c6a39e | 82 | { |
Rvs94 | 15:7fbee317af2d | 83 | reference = m2_ref; // Setpoint |
Margreeth95 | 18:6f71bb91b8bd | 84 | double pulses = Encoder.getPulses(); |
Margreeth95 | 19:9417d2011e8b | 85 | position = Encoder.getPulses()*360/(0.5*128*131); // Aantal Degs |
Rvs94 | 13:a6770307a5d2 | 86 | double P2 = PID( reference - position, m2_Kp, m2_Ki, m2_Kd, m2_Ts, m2_err_int, m2_prev_err, m2_f_v1, m2_f_v2, m2_f_a1, m2_f_a2, |
Rvs94 | 12:69ab81cf5b7d | 87 | m2_f_b0, m2_f_b1, m2_f_b2); |
Rvs94 | 15:7fbee317af2d | 88 | motor2speed = abs(P2); // Speed control |
Rvs94 | 15:7fbee317af2d | 89 | if(P2 > 0) // Direction control |
Rvs94 | 9:774fc3c6a39e | 90 | { |
Rvs94 | 9:774fc3c6a39e | 91 | motor2direction = 0; |
Rvs94 | 9:774fc3c6a39e | 92 | } |
Rvs94 | 9:774fc3c6a39e | 93 | else |
Rvs94 | 9:774fc3c6a39e | 94 | { |
Rvs94 | 9:774fc3c6a39e | 95 | motor2direction = 1; |
Rvs94 | 9:774fc3c6a39e | 96 | } |
Rvs94 | 22:56c3a5918bfc | 97 | |
Rvs94 | 9:774fc3c6a39e | 98 | } |
Rvs94 | 3:687729d7996e | 99 | |
Margreeth95 | 0:284ed397e046 | 100 | int main() |
Rvs94 | 9:774fc3c6a39e | 101 | { |
Rvs94 | 20:f5091e29cd26 | 102 | LedR.write(1); |
Rvs94 | 20:f5091e29cd26 | 103 | LedB.write(1); |
Rvs94 | 20:f5091e29cd26 | 104 | LedG.write(1); |
Margreeth95 | 0:284ed397e046 | 105 | pc.baud(115200); |
Rvs94 | 3:687729d7996e | 106 | pc.printf("Tot aan loop werkt\n"); |
Rvs94 | 9:774fc3c6a39e | 107 | |
Rvs94 | 22:56c3a5918bfc | 108 | ScopeTime.attach(&ScopeSend, 0.01f); |
Rvs94 | 22:56c3a5918bfc | 109 | myControllerTicker.attach( &motor2_Controller, 0.1f ); // 100 Hz |
Rvs94 | 9:774fc3c6a39e | 110 | while(true) |
Rvs94 | 20:f5091e29cd26 | 111 | { |
Rvs94 | 20:f5091e29cd26 | 112 | char c = pc.getc(); |
Rvs94 | 20:f5091e29cd26 | 113 | if(c == 'e') //Ga 1 programma omhoog |
Rvs94 | 20:f5091e29cd26 | 114 | { |
Rvs94 | 20:f5091e29cd26 | 115 | count = count + 1; |
Rvs94 | 20:f5091e29cd26 | 116 | if(count > 2) |
Rvs94 | 20:f5091e29cd26 | 117 | { |
Rvs94 | 20:f5091e29cd26 | 118 | count = 2; |
Rvs94 | 20:f5091e29cd26 | 119 | } |
Rvs94 | 20:f5091e29cd26 | 120 | |
Rvs94 | 20:f5091e29cd26 | 121 | } |
Rvs94 | 20:f5091e29cd26 | 122 | if(c == 'd') //Ga 1 programma omlaag |
Rvs94 | 20:f5091e29cd26 | 123 | { |
Rvs94 | 20:f5091e29cd26 | 124 | count = count - 1; |
Rvs94 | 20:f5091e29cd26 | 125 | if(count < 0) |
Rvs94 | 20:f5091e29cd26 | 126 | { |
Rvs94 | 20:f5091e29cd26 | 127 | count = 0; |
Rvs94 | 20:f5091e29cd26 | 128 | } |
Rvs94 | 20:f5091e29cd26 | 129 | } |
Rvs94 | 20:f5091e29cd26 | 130 | if(count == 0) //Motor 1 control |
Rvs94 | 20:f5091e29cd26 | 131 | { |
Rvs94 | 20:f5091e29cd26 | 132 | |
Rvs94 | 20:f5091e29cd26 | 133 | LedR = LedB = 1; |
Rvs94 | 20:f5091e29cd26 | 134 | LedG = 0; |
Margreeth95 | 19:9417d2011e8b | 135 | if(c == 'r') |
Margreeth95 | 19:9417d2011e8b | 136 | { |
Margreeth95 | 19:9417d2011e8b | 137 | m2_ref = m2_ref + 5; |
Margreeth95 | 19:9417d2011e8b | 138 | if (m2_ref > 90) |
Margreeth95 | 19:9417d2011e8b | 139 | { |
Margreeth95 | 19:9417d2011e8b | 140 | m2_ref = 90; |
Margreeth95 | 19:9417d2011e8b | 141 | } |
Margreeth95 | 19:9417d2011e8b | 142 | } |
Margreeth95 | 19:9417d2011e8b | 143 | if(c == 'f') |
Margreeth95 | 19:9417d2011e8b | 144 | { |
Margreeth95 | 19:9417d2011e8b | 145 | m2_ref = m2_ref - 5; |
Margreeth95 | 19:9417d2011e8b | 146 | if (m2_ref < -90) |
Margreeth95 | 19:9417d2011e8b | 147 | { |
Margreeth95 | 19:9417d2011e8b | 148 | m2_ref = -90; |
Margreeth95 | 19:9417d2011e8b | 149 | } |
Margreeth95 | 19:9417d2011e8b | 150 | } |
Rvs94 | 20:f5091e29cd26 | 151 | } |
Rvs94 | 20:f5091e29cd26 | 152 | if(count == 1) //Motor 2 control |
Rvs94 | 20:f5091e29cd26 | 153 | { |
Rvs94 | 20:f5091e29cd26 | 154 | LedG = LedB = 1; |
Rvs94 | 20:f5091e29cd26 | 155 | LedR = 0; |
Rvs94 | 20:f5091e29cd26 | 156 | } |
Rvs94 | 20:f5091e29cd26 | 157 | if(count == 2) //Vuur mechanisme |
Rvs94 | 20:f5091e29cd26 | 158 | { |
Rvs94 | 20:f5091e29cd26 | 159 | |
Rvs94 | 20:f5091e29cd26 | 160 | LedR = LedG = 1; |
Rvs94 | 20:f5091e29cd26 | 161 | LedB = 0; |
Rvs94 | 20:f5091e29cd26 | 162 | } |
Rvs94 | 22:56c3a5918bfc | 163 | pc.printf("position = %f aantal degs = %f, count = %i\n",reference,position,count); |
Margreeth95 | 0:284ed397e046 | 164 | } |
Rvs94 | 9:774fc3c6a39e | 165 | |
Margreeth95 | 0:284ed397e046 | 166 | } |