Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Encoder HIDScope mbed
main.cpp@6:2887ad4c5d97, 2014-10-28 (annotated)
- Committer:
- wbuysman
- Date:
- Tue Oct 28 11:55:34 2014 +0000
- Revision:
- 6:2887ad4c5d97
- Parent:
- 5:7fb05dfead4d
- Child:
- 7:59116528d895
If loop van motor 1 werkt niet, wanneer deze verwijderd wordt werkt alles
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbuysman | 0:c205dc094877 | 1 | #include "mbed.h" |
wbuysman | 0:c205dc094877 | 2 | #include "encoder.h" |
wbuysman | 0:c205dc094877 | 3 | #include "HIDScope.h" |
wbuysman | 0:c205dc094877 | 4 | |
wbuysman | 0:c205dc094877 | 5 | |
wbuysman | 6:2887ad4c5d97 | 6 | HIDScope scope(6); |
wbuysman | 0:c205dc094877 | 7 | |
wbuysman | 5:7fb05dfead4d | 8 | void keep_in_range(float * in, float min, float max); |
wbuysman | 5:7fb05dfead4d | 9 | |
wbuysman | 0:c205dc094877 | 10 | int main() |
wbuysman | 0:c205dc094877 | 11 | { |
wbuysman | 0:c205dc094877 | 12 | //motor 1, 25D |
wbuysman | 0:c205dc094877 | 13 | Encoder motor1(PTD3, PTD5); |
wbuysman | 0:c205dc094877 | 14 | DigitalOut motor1dir(PTC9); |
wbuysman | 0:c205dc094877 | 15 | PwmOut pwm_motor1(PTC8); |
wbuysman | 0:c205dc094877 | 16 | pwm_motor1.period_us(100); //10kHz PWM frequency |
wbuysman | 0:c205dc094877 | 17 | |
wbuysman | 0:c205dc094877 | 18 | //motor 2, 25D |
wbuysman | 0:c205dc094877 | 19 | Encoder motor2(PTD2,PTD0); |
wbuysman | 0:c205dc094877 | 20 | DigitalOut motor2dir(PTA4); |
wbuysman | 0:c205dc094877 | 21 | PwmOut pwm_motor2(PTA5); |
wbuysman | 0:c205dc094877 | 22 | pwm_motor2.period_us(100); //10kHz PWM frequency |
wbuysman | 0:c205dc094877 | 23 | |
wbuysman | 0:c205dc094877 | 24 | |
wbuysman | 2:302174acdac7 | 25 | float integral1 = 0; |
wbuysman | 4:91b583d4d8c4 | 26 | float setpoint1 = 3; |
wbuysman | 2:302174acdac7 | 27 | float dt1 = 0.01; |
wbuysman | 6:2887ad4c5d97 | 28 | float Kp1 = 2; |
wbuysman | 6:2887ad4c5d97 | 29 | float Ki1 = 0.5; |
wbuysman | 2:302174acdac7 | 30 | float error1 = 0; |
wbuysman | 2:302174acdac7 | 31 | float output1 = 0; |
wbuysman | 6:2887ad4c5d97 | 32 | float omrekenfactor1 = (32*70)/6.28; |
wbuysman | 6:2887ad4c5d97 | 33 | |
wbuysman | 6:2887ad4c5d97 | 34 | float integral2 = 0; |
wbuysman | 6:2887ad4c5d97 | 35 | float setpoint2 = 3.14; |
wbuysman | 6:2887ad4c5d97 | 36 | float dt2 = 0.01; |
wbuysman | 6:2887ad4c5d97 | 37 | float Kp2 = 0.55; |
wbuysman | 6:2887ad4c5d97 | 38 | float Ki2 = 0.45; |
wbuysman | 6:2887ad4c5d97 | 39 | float error2 = 0; |
wbuysman | 6:2887ad4c5d97 | 40 | float output2 = 0; |
wbuysman | 6:2887ad4c5d97 | 41 | float omrekenfactor2 = (24*172)/6.28; |
wbuysman | 3:e40763e01a80 | 42 | |
wbuysman | 0:c205dc094877 | 43 | while(1) { |
wbuysman | 6:2887ad4c5d97 | 44 | if(motor1.getPosition()/omrekenfactor1 < 1.3) { |
wbuysman | 6:2887ad4c5d97 | 45 | error1 = setpoint1 - motor1.getSpeed()/omrekenfactor1; |
wbuysman | 6:2887ad4c5d97 | 46 | //motorpositie omgerekend naar rad/s |
wbuysman | 6:2887ad4c5d97 | 47 | integral1 = integral1 + error1*dt1; |
wbuysman | 6:2887ad4c5d97 | 48 | output1 = Kp1*error1 + Ki1*integral1; |
wbuysman | 6:2887ad4c5d97 | 49 | keep_in_range(&output1,-1,1); |
wbuysman | 6:2887ad4c5d97 | 50 | pwm_motor1.write(abs(output1)); |
wbuysman | 6:2887ad4c5d97 | 51 | wait(dt1); |
wbuysman | 6:2887ad4c5d97 | 52 | } |
wbuysman | 3:e40763e01a80 | 53 | |
wbuysman | 6:2887ad4c5d97 | 54 | else { |
wbuysman | 6:2887ad4c5d97 | 55 | output1=0; |
wbuysman | 6:2887ad4c5d97 | 56 | } |
wbuysman | 3:e40763e01a80 | 57 | |
wbuysman | 0:c205dc094877 | 58 | |
wbuysman | 3:e40763e01a80 | 59 | if(output1 > 0) { |
wbuysman | 5:7fb05dfead4d | 60 | motor1dir = 1; |
wbuysman | 3:e40763e01a80 | 61 | } else { |
wbuysman | 5:7fb05dfead4d | 62 | motor1dir = 0; |
wbuysman | 3:e40763e01a80 | 63 | } |
wbuysman | 6:2887ad4c5d97 | 64 | |
wbuysman | 6:2887ad4c5d97 | 65 | error2 = setpoint2 - motor2.getPosition()/omrekenfactor2; // (omrekenfactor)/dt1; |
wbuysman | 6:2887ad4c5d97 | 66 | //motorpositie omgerekend naar rad/s |
wbuysman | 6:2887ad4c5d97 | 67 | integral2 = integral2+ error2*dt2; |
wbuysman | 6:2887ad4c5d97 | 68 | output2 = Kp2*error2 + Ki2*integral2; |
wbuysman | 6:2887ad4c5d97 | 69 | keep_in_range(&output2,-1,1); |
wbuysman | 6:2887ad4c5d97 | 70 | pwm_motor2.write(abs(output2)); |
wbuysman | 6:2887ad4c5d97 | 71 | wait(dt2); |
wbuysman | 6:2887ad4c5d97 | 72 | |
wbuysman | 6:2887ad4c5d97 | 73 | |
wbuysman | 6:2887ad4c5d97 | 74 | if(output2 > 0) { |
wbuysman | 6:2887ad4c5d97 | 75 | motor2dir = 1; |
wbuysman | 6:2887ad4c5d97 | 76 | } else { |
wbuysman | 6:2887ad4c5d97 | 77 | motor2dir = 0; |
wbuysman | 6:2887ad4c5d97 | 78 | } |
wbuysman | 6:2887ad4c5d97 | 79 | scope.set(0, output1); |
wbuysman | 6:2887ad4c5d97 | 80 | scope.set(1, motor1.getSpeed()/omrekenfactor1); |
wbuysman | 6:2887ad4c5d97 | 81 | scope.set(2, motor1.getPosition()/(omrekenfactor1)); |
wbuysman | 6:2887ad4c5d97 | 82 | scope.set(3, output2); |
wbuysman | 6:2887ad4c5d97 | 83 | scope.set(4, motor2.getSpeed()/omrekenfactor2); |
wbuysman | 6:2887ad4c5d97 | 84 | scope.set(5, motor2.getPosition()/(omrekenfactor2)); |
wbuysman | 4:91b583d4d8c4 | 85 | scope.send(); |
wbuysman | 0:c205dc094877 | 86 | } |
wbuysman | 5:7fb05dfead4d | 87 | } |
wbuysman | 5:7fb05dfead4d | 88 | void keep_in_range(float * in, float min, float max) |
wbuysman | 5:7fb05dfead4d | 89 | { |
wbuysman | 5:7fb05dfead4d | 90 | *in > min ? *in < max? : *in = max: *in = max; |
wbuysman | 0:c205dc094877 | 91 | } |