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 by
main.cpp@13:7ec4762ec4ce, 2014-10-17 (annotated)
- Committer:
- Hooglugt
- Date:
- Fri Oct 17 08:47:27 2014 +0000
- Revision:
- 13:7ec4762ec4ce
- Parent:
- 11:4f65e70290ac
- Child:
- 14:98925c1ed440
onnodige stukken code verwijdert voor testen motor (step function)
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); |
| vsluiter | 6:0832c6c6bcba | 10 | |
| vsluiter | 0:c9e647421e54 | 11 | void setlooptimerflag(void) |
| vsluiter | 0:c9e647421e54 | 12 | { |
| vsluiter | 0:c9e647421e54 | 13 | looptimerflag = true; |
| vsluiter | 0:c9e647421e54 | 14 | } |
| vsluiter | 0:c9e647421e54 | 15 | |
| Hooglugt | 13:7ec4762ec4ce | 16 | void clamp(float * in, float min, float max) |
| Hooglugt | 13:7ec4762ec4ce | 17 | { |
| Hooglugt | 13:7ec4762ec4ce | 18 | *in > min ? *in < max? : *in = max: *in = min; |
| Hooglugt | 13:7ec4762ec4ce | 19 | } |
| vsluiter | 1:5ac85aad9da4 | 20 | |
| vsluiter | 4:1a53b06eeb7f | 21 | int main() |
| vsluiter | 4:1a53b06eeb7f | 22 | { |
| Hooglugt | 13:7ec4762ec4ce | 23 | Encoder motor1(PTD2,PTD0); |
| Hooglugt | 13:7ec4762ec4ce | 24 | PwmOut pwm_motor(PTC8); |
| Hooglugt | 13:7ec4762ec4ce | 25 | pwm_motor.period_us(100); //10kHz PWM frequency |
| Hooglugt | 13:7ec4762ec4ce | 26 | DigitalOut motordir(PTC9); |
| vsluiter | 0:c9e647421e54 | 27 | Ticker looptimer; |
| vsluiter | 5:06381e54b94a | 28 | looptimer.attach(setlooptimerflag,TSAMP); |
| Hooglugt | 13:7ec4762ec4ce | 29 | |
| Hooglugt | 13:7ec4762ec4ce | 30 | wait(10); |
| Hooglugt | 13:7ec4762ec4ce | 31 | |
| vsluiter | 0:c9e647421e54 | 32 | while(1) { |
| Hooglugt | 13:7ec4762ec4ce | 33 | float new_pwm; |
| Hooglugt | 13:7ec4762ec4ce | 34 | |
| vsluiter | 0:c9e647421e54 | 35 | while(!looptimerflag); |
| vsluiter | 8:15c6cb82c725 | 36 | looptimerflag = false; //clear flag |
| Hooglugt | 13:7ec4762ec4ce | 37 | |
| Hooglugt | 13:7ec4762ec4ce | 38 | new_pwm = 1; |
| vsluiter | 8:15c6cb82c725 | 39 | clamp(&new_pwm, -1,1); |
| Hooglugt | 13:7ec4762ec4ce | 40 | |
| Hooglugt | 13:7ec4762ec4ce | 41 | scope.set(0, new_pwm); |
| Hooglugt | 13:7ec4762ec4ce | 42 | scope.set(1, motor1.getPosition()); |
| vsluiter | 6:0832c6c6bcba | 43 | scope.send(); |
| Hooglugt | 13:7ec4762ec4ce | 44 | |
| vsluiter | 0:c9e647421e54 | 45 | if(new_pwm > 0) |
| vsluiter | 7:37b06688b449 | 46 | motordir = 0; |
| vsluiter | 7:37b06688b449 | 47 | else |
| vsluiter | 0:c9e647421e54 | 48 | motordir = 1; |
| Hooglugt | 13:7ec4762ec4ce | 49 | |
| vsluiter | 0:c9e647421e54 | 50 | pwm_motor.write(abs(new_pwm)); |
| vsluiter | 0:c9e647421e54 | 51 | } |
| Hooglugt | 13:7ec4762ec4ce | 52 | } |
