Fernon Eijkhoudt
/
ROBOBIRDS_FINAL
EMG driven robot which shoots elastic bands
Fork of RoboBirdV1 by
main.cpp@13:c60942a1da8e, 2015-10-07 (annotated)
- Committer:
- yc238
- Date:
- Wed Oct 07 13:30:46 2015 +0000
- Revision:
- 13:c60942a1da8e
- Parent:
- 12:dcf90cafb2a5
- Child:
- 14:b9c925a8176a
28, Werkende P controller. Nu niks meer aan doen!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Fernon | 0:5a5f417fa1b2 | 1 | #include "mbed.h" |
yc238 | 13:c60942a1da8e | 2 | #include "encoder.h" |
Fernon | 8:a2b725b502d8 | 3 | #include "math.h" |
yc238 | 12:dcf90cafb2a5 | 4 | #include "HIDScope.h" |
Fernon | 0:5a5f417fa1b2 | 5 | |
Fernon | 1:bb11e38dda43 | 6 | DigitalOut Direction(D4); //1 = CCW - 0 = CW |
Fernon | 1:bb11e38dda43 | 7 | PwmOut PowerMotor(D5); //van 0 tot 1 |
yc238 | 13:c60942a1da8e | 8 | DigitalIn Button(D3); |
Fernon | 1:bb11e38dda43 | 9 | AnalogIn PotMeter(A1); |
yc238 | 13:c60942a1da8e | 10 | Encoder MotorEncoder(D12,D13); //Encoder |
Fernon | 1:bb11e38dda43 | 11 | Serial pc(USBTX, USBRX); |
Fernon | 2:f0e9ffc5df09 | 12 | Ticker Pot; |
yc238 | 13:c60942a1da8e | 13 | HIDScope scope(3); |
Fernon | 0:5a5f417fa1b2 | 14 | |
Fernon | 10:e210675cbe71 | 15 | double z=0; //initiele waarde potmeter |
Fernon | 8:a2b725b502d8 | 16 | const double twopi = 6.2831853071795; |
Fernon | 8:a2b725b502d8 | 17 | const double pi = twopi/2; |
Fernon | 8:a2b725b502d8 | 18 | int Pulses; |
Fernon | 10:e210675cbe71 | 19 | double Rotatie; //aantal graden dat de motor is gedraaid |
Fernon | 10:e210675cbe71 | 20 | double Rotatieup; //aantal graden dat de motor is gedraaid in een bereik van n*pi |
Fernon | 11:a9a23042fc9e | 21 | double Goal = 0; //initele waarde goal waar de motor naar toe moet, dit wordt gedaan |
Fernon | 8:a2b725b502d8 | 22 | double Error = 0; |
yc238 | 13:c60942a1da8e | 23 | double Kp = 0.2; //Moet berekend worden aan de hand van Control concept slides |
Fernon | 10:e210675cbe71 | 24 | double v = 0; //snelheid van de motor (0-0.1 |
Fernon | 10:e210675cbe71 | 25 | double upperlimit; //max aantal rotaties |
Fernon | 11:a9a23042fc9e | 26 | bool Excecute = false; |
Fernon | 10:e210675cbe71 | 27 | |
yc238 | 13:c60942a1da8e | 28 | double n = 8; |
Fernon | 2:f0e9ffc5df09 | 29 | |
Fernon | 5:d47e6a96256b | 30 | void readpot() |
Fernon | 2:f0e9ffc5df09 | 31 | { |
Fernon | 10:e210675cbe71 | 32 | z = PotMeter.read(); //uitlezen Potmeter of voor EMG bijvoorbeeld |
Fernon | 2:f0e9ffc5df09 | 33 | } |
Fernon | 0:5a5f417fa1b2 | 34 | |
Fernon | 0:5a5f417fa1b2 | 35 | int main() |
Fernon | 0:5a5f417fa1b2 | 36 | { |
Fernon | 11:a9a23042fc9e | 37 | upperlimit = n*twopi; |
Fernon | 2:f0e9ffc5df09 | 38 | pc.baud(115200); |
Fernon | 2:f0e9ffc5df09 | 39 | PowerMotor.write(0); |
Fernon | 6:cf20f04dbab4 | 40 | Pot.attach(readpot,0.1); // Deze ticker moet de waarde uitlezen van de PotMeter 10 keer per seconde |
Fernon | 0:5a5f417fa1b2 | 41 | while (true) { |
Fernon | 11:a9a23042fc9e | 42 | if (Button == 0) { |
Fernon | 11:a9a23042fc9e | 43 | Excecute =! Excecute; |
yc238 | 12:dcf90cafb2a5 | 44 | } |
yc238 | 12:dcf90cafb2a5 | 45 | while (Excecute ) { |
yc238 | 13:c60942a1da8e | 46 | Pulses = MotorEncoder.getPosition(); |
yc238 | 12:dcf90cafb2a5 | 47 | Rotatie = (Pulses*twopi)/4192; // Aantal graden draaien in radialen |
yc238 | 12:dcf90cafb2a5 | 48 | Rotatieup = fmod(Rotatie,upperlimit); //Aantal graden draaien in radialen binnen het bereik van upperlimit |
yc238 | 12:dcf90cafb2a5 | 49 | pc.printf ("Potmeter = %f\n", z); |
yc238 | 12:dcf90cafb2a5 | 50 | pc.printf ("Rotatie = %f [radialen] \n", Rotatieup); |
yc238 | 12:dcf90cafb2a5 | 51 | Goal = z*upperlimit; // Het doel waar hij naar toe moet |
yc238 | 12:dcf90cafb2a5 | 52 | Error = Goal-Rotatieup; // De error die het motortje maakt ten opzichte van je Goal |
yc238 | 12:dcf90cafb2a5 | 53 | if (Error >= 0) { //Bepalen van de rotatie richting |
yc238 | 13:c60942a1da8e | 54 | Direction =1; |
yc238 | 13:c60942a1da8e | 55 | } else { |
yc238 | 12:dcf90cafb2a5 | 56 | Direction =0; |
Fernon | 11:a9a23042fc9e | 57 | } |
yc238 | 12:dcf90cafb2a5 | 58 | pc.printf("Error = %f\n Goal = %f\n", Error, Goal); |
yc238 | 13:c60942a1da8e | 59 | v = Kp*fabs(Error); // Snelheid van motor |
yc238 | 12:dcf90cafb2a5 | 60 | PowerMotor.write(v); // Snelheid die naar de motor geschreven wordt (wordt alleen in een schaal van 0-1 weergegeven). (uit ervaring van Motor4_Setpoint loopt dit maar tussen 0-0.1). |
yc238 | 13:c60942a1da8e | 61 | //if (Error<=0.001 && Error>=-0.001) { |
yc238 | 13:c60942a1da8e | 62 | // PowerMotor.write(0); |
yc238 | 13:c60942a1da8e | 63 | // Excecute=false; |
yc238 | 13:c60942a1da8e | 64 | //} |
yc238 | 12:dcf90cafb2a5 | 65 | scope.set(0,Goal); |
yc238 | 12:dcf90cafb2a5 | 66 | scope.set(1,Rotatieup); |
yc238 | 13:c60942a1da8e | 67 | scope.set(2,Error); |
yc238 | 12:dcf90cafb2a5 | 68 | scope.send(); |
Fernon | 8:a2b725b502d8 | 69 | } |
Fernon | 0:5a5f417fa1b2 | 70 | } |
yc238 | 12:dcf90cafb2a5 | 71 | } |