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 MODSERIAL mbed-dsp mbed
Fork of BMT-M9_motorcontrol_groep3 by
main.cpp@16:1869377fbb73, 2014-10-17 (annotated)
- Committer:
- Hooglugt
- Date:
- Fri Oct 17 10:57:44 2014 +0000
- Revision:
- 16:1869377fbb73
- Parent:
- 15:979011e26ca3
- Child:
- 17:055edbd6ba9d
pwm waarden in de looper (2 en -2), werking clamp
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| vsluiter | 0:c9e647421e54 | 1 | #include "mbed.h" |
| vsluiter | 0:c9e647421e54 | 2 | #include "encoder.h" |
| vsluiter | 6:0832c6c6bcba | 3 | #include "HIDScope.h" |
| vsluiter | 6:0832c6c6bcba | 4 | |
| vsluiter | 5:06381e54b94a | 5 | #define TSAMP 0.01 |
| vsluiter | 6:0832c6c6bcba | 6 | |
| vsluiter | 8:15c6cb82c725 | 7 | void clamp(float * in, float min, float max); |
| vsluiter | 0:c9e647421e54 | 8 | volatile bool looptimerflag; |
| Hooglugt | 11:4f65e70290ac | 9 | HIDScope scope(2); |
| Hooglugt | 15:979011e26ca3 | 10 | float new_pwm = 2; |
| vsluiter | 6:0832c6c6bcba | 11 | |
| vsluiter | 0:c9e647421e54 | 12 | void setlooptimerflag(void) |
| vsluiter | 0:c9e647421e54 | 13 | { |
| vsluiter | 0:c9e647421e54 | 14 | looptimerflag = true; |
| vsluiter | 0:c9e647421e54 | 15 | } |
| vsluiter | 0:c9e647421e54 | 16 | |
| Hooglugt | 13:7ec4762ec4ce | 17 | void clamp(float * in, float min, float max) |
| Hooglugt | 13:7ec4762ec4ce | 18 | { |
| Hooglugt | 13:7ec4762ec4ce | 19 | *in > min ? *in < max? : *in = max: *in = min; |
| Hooglugt | 13:7ec4762ec4ce | 20 | } |
| vsluiter | 1:5ac85aad9da4 | 21 | |
| Hooglugt | 14:98925c1ed440 | 22 | void looper() |
| Hooglugt | 14:98925c1ed440 | 23 | { |
| Hooglugt | 16:1869377fbb73 | 24 | if (new_pwm==2) { // 2 en -2 om aan te tonen dat clamp function ook werkt |
| Hooglugt | 15:979011e26ca3 | 25 | new_pwm = -2; |
| Hooglugt | 14:98925c1ed440 | 26 | } else { |
| Hooglugt | 15:979011e26ca3 | 27 | new_pwm = 2; |
| Hooglugt | 14:98925c1ed440 | 28 | } |
| Hooglugt | 14:98925c1ed440 | 29 | } |
| Hooglugt | 14:98925c1ed440 | 30 | |
| Hooglugt | 14:98925c1ed440 | 31 | |
| vsluiter | 4:1a53b06eeb7f | 32 | int main() |
| vsluiter | 4:1a53b06eeb7f | 33 | { |
| Hooglugt | 16:1869377fbb73 | 34 | //motor 1, 25D |
| Hooglugt | 15:979011e26ca3 | 35 | Encoder motor1(PTD3, PTD5); |
| Hooglugt | 15:979011e26ca3 | 36 | DigitalOut motor1dir(PTC9); |
| Hooglugt | 15:979011e26ca3 | 37 | PwmOut pwm_motor1(PTC8); |
| Hooglugt | 16:1869377fbb73 | 38 | pwm_motor.period_us(100); //10kHz PWM frequency |
| Hooglugt | 16:1869377fbb73 | 39 | |
| Hooglugt | 16:1869377fbb73 | 40 | //motor 2, 25D |
| Hooglugt | 15:979011e26ca3 | 41 | Encoder motor2(PTD2,PTD0); |
| Hooglugt | 15:979011e26ca3 | 42 | DigitalOut motor2dir(PTA4); |
| Hooglugt | 15:979011e26ca3 | 43 | PwmOut pwm_motor2(PTA5); |
| Hooglugt | 16:1869377fbb73 | 44 | |
| Hooglugt | 16:1869377fbb73 | 45 | pwm_motor.period_us(100); //10kHz PWM frequency |
| vsluiter | 0:c9e647421e54 | 46 | Ticker looptimer; |
| vsluiter | 5:06381e54b94a | 47 | looptimer.attach(setlooptimerflag,TSAMP); |
| Hooglugt | 14:98925c1ed440 | 48 | Ticker flip_switch; |
| Hooglugt | 15:979011e26ca3 | 49 | flip_switch.attach(looper, 2); |
| Hooglugt | 13:7ec4762ec4ce | 50 | |
| vsluiter | 0:c9e647421e54 | 51 | while(1) { |
| vsluiter | 0:c9e647421e54 | 52 | while(!looptimerflag); |
| vsluiter | 8:15c6cb82c725 | 53 | looptimerflag = false; //clear flag |
| Hooglugt | 13:7ec4762ec4ce | 54 | |
| vsluiter | 8:15c6cb82c725 | 55 | clamp(&new_pwm, -1,1); |
| Hooglugt | 13:7ec4762ec4ce | 56 | |
| Hooglugt | 13:7ec4762ec4ce | 57 | scope.set(0, new_pwm); |
| Hooglugt | 15:979011e26ca3 | 58 | scope.set(1, motor2.getPosition()); |
| vsluiter | 6:0832c6c6bcba | 59 | scope.send(); |
| Hooglugt | 14:98925c1ed440 | 60 | |
| vsluiter | 0:c9e647421e54 | 61 | if(new_pwm > 0) |
| Hooglugt | 15:979011e26ca3 | 62 | motor2dir = 0; |
| vsluiter | 7:37b06688b449 | 63 | else |
| Hooglugt | 15:979011e26ca3 | 64 | motor2dir = 1; |
| Hooglugt | 14:98925c1ed440 | 65 | |
| Hooglugt | 15:979011e26ca3 | 66 | pwm_motor2.write(abs(new_pwm)); |
| vsluiter | 0:c9e647421e54 | 67 | } |
| Hooglugt | 13:7ec4762ec4ce | 68 | } |
