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@2:302174acdac7, 2014-10-23 (annotated)
- Committer:
- wbuysman
- Date:
- Thu Oct 23 12:05:16 2014 +0000
- Revision:
- 2:302174acdac7
- Parent:
- 0:c205dc094877
- Child:
- 3:e40763e01a80
Fouten eruit gehaald, identifiers toegevoegd
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 | 2:302174acdac7 | 24 | float integral1 = 0; |
wbuysman | 2:302174acdac7 | 25 | float setpoint1 = 9.5; |
wbuysman | 2:302174acdac7 | 26 | float dt1 = 0.01; |
wbuysman | 2:302174acdac7 | 27 | float Kp1 = 0.01; |
wbuysman | 2:302174acdac7 | 28 | float Ki1 = 0.01; |
wbuysman | 2:302174acdac7 | 29 | float error1 = 0; |
wbuysman | 2:302174acdac7 | 30 | float output1 = 0; |
wbuysman | 2:302174acdac7 | 31 | |
wbuysman | 0:c205dc094877 | 32 | while(1) { |
wbuysman | 0:c205dc094877 | 33 | start: |
wbuysman | 2:302174acdac7 | 34 | float error1 = setpoint1 - motor1.getPosition/(4480*6.28)/dt1; //motorpositie omgerekend naar rad/s |
wbuysman | 0:c205dc094877 | 35 | integral1 = integral1 + error1*dt1; |
wbuysman | 0:c205dc094877 | 36 | output1 = Kp1*error1 + Ki1*integral1; |
wbuysman | 2:302174acdac7 | 37 | pwm_motor1.write(abs(output1)); |
wbuysman | 0:c205dc094877 | 38 | wait(dt1) |
wbuysman | 2:302174acdac7 | 39 | goto start; |
wbuysman | 0:c205dc094877 | 40 | |
wbuysman | 0:c205dc094877 | 41 | if(new_pwm > 0) { |
wbuysman | 0:c205dc094877 | 42 | motor1dir = 0; |
wbuysman | 0:c205dc094877 | 43 | } else { |
wbuysman | 0:c205dc094877 | 44 | motor1dir = 1; |
wbuysman | 0:c205dc094877 | 45 | } |
wbuysman | 0:c205dc094877 | 46 | |
wbuysman | 2:302174acdac7 | 47 | scope.set(0, output1); |
wbuysman | 0:c205dc094877 | 48 | scope.set(1, motor1.getPosition()); |
wbuysman | 2:302174acdac7 | 49 | scope.set(2, motor1.getPosition()/(4480*6.28)/dt1); |
wbuysman | 0:c205dc094877 | 50 | scope.send(); |
wbuysman | 0:c205dc094877 | 51 | } |
wbuysman | 0:c205dc094877 | 52 | } |