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@4:91b583d4d8c4, 2014-10-23 (annotated)
- Committer:
- wbuysman
- Date:
- Thu Oct 23 13:47:21 2014 +0000
- Revision:
- 4:91b583d4d8c4
- Parent:
- 3:e40763e01a80
- Child:
- 5:7fb05dfead4d
Eenheid van de snelheid klopt, regelaar werkt nog niet helemaal;
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 | 4:91b583d4d8c4 | 6 | HIDScope scope(4); |
wbuysman | 0:c205dc094877 | 7 | |
wbuysman | 0:c205dc094877 | 8 | int main() |
wbuysman | 0:c205dc094877 | 9 | { |
wbuysman | 0:c205dc094877 | 10 | //motor 1, 25D |
wbuysman | 0:c205dc094877 | 11 | Encoder motor1(PTD3, PTD5); |
wbuysman | 0:c205dc094877 | 12 | DigitalOut motor1dir(PTC9); |
wbuysman | 0:c205dc094877 | 13 | PwmOut pwm_motor1(PTC8); |
wbuysman | 0:c205dc094877 | 14 | pwm_motor1.period_us(100); //10kHz PWM frequency |
wbuysman | 0:c205dc094877 | 15 | |
wbuysman | 0:c205dc094877 | 16 | //motor 2, 25D |
wbuysman | 0:c205dc094877 | 17 | Encoder motor2(PTD2,PTD0); |
wbuysman | 0:c205dc094877 | 18 | DigitalOut motor2dir(PTA4); |
wbuysman | 0:c205dc094877 | 19 | PwmOut pwm_motor2(PTA5); |
wbuysman | 0:c205dc094877 | 20 | pwm_motor2.period_us(100); //10kHz PWM frequency |
wbuysman | 0:c205dc094877 | 21 | |
wbuysman | 0:c205dc094877 | 22 | |
wbuysman | 2:302174acdac7 | 23 | float integral1 = 0; |
wbuysman | 4:91b583d4d8c4 | 24 | float setpoint1 = 3; |
wbuysman | 2:302174acdac7 | 25 | float dt1 = 0.01; |
wbuysman | 4:91b583d4d8c4 | 26 | float Kp1 = 1; |
wbuysman | 4:91b583d4d8c4 | 27 | float Ki1 = 0.5; |
wbuysman | 2:302174acdac7 | 28 | float error1 = 0; |
wbuysman | 2:302174acdac7 | 29 | float output1 = 0; |
wbuysman | 4:91b583d4d8c4 | 30 | float omrekenfactor = 4480.0/6.28; |
wbuysman | 3:e40763e01a80 | 31 | |
wbuysman | 0:c205dc094877 | 32 | while(1) { |
wbuysman | 3:e40763e01a80 | 33 | |
wbuysman | 4:91b583d4d8c4 | 34 | error1 = setpoint1 - motor1.getSpeed()/omrekenfactor; // (omrekenfactor)/dt1; |
wbuysman | 3:e40763e01a80 | 35 | //motorpositie omgerekend naar rad/s |
wbuysman | 0:c205dc094877 | 36 | integral1 = integral1 + error1*dt1; |
wbuysman | 0:c205dc094877 | 37 | output1 = Kp1*error1 + Ki1*integral1; |
wbuysman | 2:302174acdac7 | 38 | pwm_motor1.write(abs(output1)); |
wbuysman | 4:91b583d4d8c4 | 39 | wait(dt1); |
wbuysman | 3:e40763e01a80 | 40 | |
wbuysman | 0:c205dc094877 | 41 | |
wbuysman | 3:e40763e01a80 | 42 | if(output1 > 0) { |
wbuysman | 4:91b583d4d8c4 | 43 | motor1dir = 0; |
wbuysman | 3:e40763e01a80 | 44 | } else { |
wbuysman | 4:91b583d4d8c4 | 45 | motor1dir = 1; |
wbuysman | 3:e40763e01a80 | 46 | } |
wbuysman | 4:91b583d4d8c4 | 47 | scope.set(0, error1); |
wbuysman | 4:91b583d4d8c4 | 48 | scope.set(1, output1); |
wbuysman | 4:91b583d4d8c4 | 49 | scope.set(2, motor1.getSpeed()/omrekenfactor); |
wbuysman | 4:91b583d4d8c4 | 50 | scope.set(3, motor1.getPosition()/(omrekenfactor)); |
wbuysman | 4:91b583d4d8c4 | 51 | scope.send(); |
wbuysman | 3:e40763e01a80 | 52 | |
wbuysman | 0:c205dc094877 | 53 | } |
wbuysman | 0:c205dc094877 | 54 | } |