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@0:c205dc094877, 2014-10-23 (annotated)
- Committer:
- wbuysman
- Date:
- Thu Oct 23 11:58:51 2014 +0000
- Revision:
- 0:c205dc094877
- Child:
- 2:302174acdac7
Poging tot regelaar
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 | 0:c205dc094877 | 6 | |
wbuysman | 0:c205dc094877 | 7 | HIDScope scope(3); |
wbuysman | 0:c205dc094877 | 8 | |
wbuysman | 0:c205dc094877 | 9 | int main() |
wbuysman | 0:c205dc094877 | 10 | { |
wbuysman | 0:c205dc094877 | 11 | //motor 1, 25D |
wbuysman | 0:c205dc094877 | 12 | Encoder motor1(PTD3, PTD5); |
wbuysman | 0:c205dc094877 | 13 | DigitalOut motor1dir(PTC9); |
wbuysman | 0:c205dc094877 | 14 | PwmOut pwm_motor1(PTC8); |
wbuysman | 0:c205dc094877 | 15 | pwm_motor1.period_us(100); //10kHz PWM frequency |
wbuysman | 0:c205dc094877 | 16 | |
wbuysman | 0:c205dc094877 | 17 | //motor 2, 25D |
wbuysman | 0:c205dc094877 | 18 | Encoder motor2(PTD2,PTD0); |
wbuysman | 0:c205dc094877 | 19 | DigitalOut motor2dir(PTA4); |
wbuysman | 0:c205dc094877 | 20 | PwmOut pwm_motor2(PTA5); |
wbuysman | 0:c205dc094877 | 21 | pwm_motor2.period_us(100); //10kHz PWM frequency |
wbuysman | 0:c205dc094877 | 22 | |
wbuysman | 0:c205dc094877 | 23 | |
wbuysman | 0:c205dc094877 | 24 | integral1 = 0; |
wbuysman | 0:c205dc094877 | 25 | setpoint1 = 9.5; |
wbuysman | 0:c205dc094877 | 26 | dt1 = 0.01; |
wbuysman | 0:c205dc094877 | 27 | Kp1 = 0.01; |
wbuysman | 0:c205dc094877 | 28 | Ki1 = 0.01; |
wbuysman | 0:c205dc094877 | 29 | while(1) { |
wbuysman | 0:c205dc094877 | 30 | start: |
wbuysman | 0:c205dc094877 | 31 | error1 = setpoint1 - motor1.getposition/(4480*6.28)/dt1; //motorpositie omgerekend naar rad/s |
wbuysman | 0:c205dc094877 | 32 | integral1 = integral1 + error1*dt1; |
wbuysman | 0:c205dc094877 | 33 | output1 = Kp1*error1 + Ki1*integral1; |
wbuysman | 0:c205dc094877 | 34 | pwm_motor1.write(abs(output1); |
wbuysman | 0:c205dc094877 | 35 | wait(dt1) |
wbuysman | 0:c205dc094877 | 36 | goto start |
wbuysman | 0:c205dc094877 | 37 | |
wbuysman | 0:c205dc094877 | 38 | if(new_pwm > 0) { |
wbuysman | 0:c205dc094877 | 39 | motor1dir = 0; |
wbuysman | 0:c205dc094877 | 40 | } else { |
wbuysman | 0:c205dc094877 | 41 | motor1dir = 1; |
wbuysman | 0:c205dc094877 | 42 | } |
wbuysman | 0:c205dc094877 | 43 | |
wbuysman | 0:c205dc094877 | 44 | scope.set(0, new_pwm); |
wbuysman | 0:c205dc094877 | 45 | scope.set(1, motor1.getPosition()); |
wbuysman | 0:c205dc094877 | 46 | scope.set(2, motor1.getposition()/(4480*6.28)/dt1); |
wbuysman | 0:c205dc094877 | 47 | scope.send(); |
wbuysman | 0:c205dc094877 | 48 | } |
wbuysman | 0:c205dc094877 | 49 | } |