Library version for DC_Stepper_Controller_Lib with PWM speed control
Dependents: DR-ArmServoTest Auto_DC_pick_class MBed_TR1 ros_button_2021
main.cpp@2:6bc435f4ffec, 2021-03-12 (annotated)
- Committer:
- cheukming0607
- Date:
- Fri Mar 12 09:42:14 2021 +0000
- Revision:
- 2:6bc435f4ffec
- Parent:
- 1:30696e4d196b
- Child:
- 3:1229fe22fdcd
Basic DC stepper Motor Controller ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cheukming0607 | 2:6bc435f4ffec | 1 | #include "mbed.h" |
aberk | 0:bcff39fac858 | 2 | #include "QEI.h" |
cheukming0607 | 2:6bc435f4ffec | 3 | |
cheukming0607 | 2:6bc435f4ffec | 4 | DigitalOut out1(D2); |
cheukming0607 | 2:6bc435f4ffec | 5 | DigitalOut out2(D4); |
cheukming0607 | 2:6bc435f4ffec | 6 | DigitalIn home_button(D13); |
cheukming0607 | 2:6bc435f4ffec | 7 | QEI dc_motor (D8,D7,NC,792); |
cheukming0607 | 2:6bc435f4ffec | 8 | int counter = 0; |
aberk | 0:bcff39fac858 | 9 | |
cheukming0607 | 2:6bc435f4ffec | 10 | void reset(){ |
cheukming0607 | 2:6bc435f4ffec | 11 | while (home_button == 0){ |
cheukming0607 | 2:6bc435f4ffec | 12 | out1 = 1; |
cheukming0607 | 2:6bc435f4ffec | 13 | out2 = 0; |
cheukming0607 | 2:6bc435f4ffec | 14 | } |
cheukming0607 | 2:6bc435f4ffec | 15 | out1 = 0; |
cheukming0607 | 2:6bc435f4ffec | 16 | dc_motor.reset(); |
cheukming0607 | 2:6bc435f4ffec | 17 | wait(1); |
cheukming0607 | 2:6bc435f4ffec | 18 | }; |
cheukming0607 | 2:6bc435f4ffec | 19 | |
cheukming0607 | 2:6bc435f4ffec | 20 | void go_angle(int angle){ |
cheukming0607 | 2:6bc435f4ffec | 21 | int tar_pulse = ((double) angle / 360.0)* (2.0 * 792.0); |
cheukming0607 | 2:6bc435f4ffec | 22 | int cur_pulse = dc_motor.getPulses(); |
cheukming0607 | 2:6bc435f4ffec | 23 | |
cheukming0607 | 2:6bc435f4ffec | 24 | while ( tar_pulse != cur_pulse ){ |
aberk | 0:bcff39fac858 | 25 | |
cheukming0607 | 2:6bc435f4ffec | 26 | if (tar_pulse > cur_pulse){ |
cheukming0607 | 2:6bc435f4ffec | 27 | out1 = 1; |
cheukming0607 | 2:6bc435f4ffec | 28 | out2 = 0; |
cheukming0607 | 2:6bc435f4ffec | 29 | } |
cheukming0607 | 2:6bc435f4ffec | 30 | else { |
cheukming0607 | 2:6bc435f4ffec | 31 | out1 = 0; |
cheukming0607 | 2:6bc435f4ffec | 32 | out2 = 1; |
cheukming0607 | 2:6bc435f4ffec | 33 | } |
cheukming0607 | 2:6bc435f4ffec | 34 | cur_pulse = dc_motor.getPulses(); |
cheukming0607 | 2:6bc435f4ffec | 35 | } |
cheukming0607 | 2:6bc435f4ffec | 36 | out1 = 0; |
cheukming0607 | 2:6bc435f4ffec | 37 | out2 = 0; |
cheukming0607 | 2:6bc435f4ffec | 38 | }; |
cheukming0607 | 2:6bc435f4ffec | 39 | |
aberk | 0:bcff39fac858 | 40 | int main() { |
cheukming0607 | 2:6bc435f4ffec | 41 | reset(); |
cheukming0607 | 2:6bc435f4ffec | 42 | go_angle(90); |
cheukming0607 | 2:6bc435f4ffec | 43 | wait(1); |
cheukming0607 | 2:6bc435f4ffec | 44 | go_angle(180); |
cheukming0607 | 2:6bc435f4ffec | 45 | wait(1); |
cheukming0607 | 2:6bc435f4ffec | 46 | go_angle(45); |
cheukming0607 | 2:6bc435f4ffec | 47 | wait(1); |
cheukming0607 | 2:6bc435f4ffec | 48 | go_angle(30); |
cheukming0607 | 2:6bc435f4ffec | 49 | wait(1); |
cheukming0607 | 2:6bc435f4ffec | 50 | go_angle(1); |
cheukming0607 | 2:6bc435f4ffec | 51 | wait(1); |
aberk | 0:bcff39fac858 | 52 | |
cheukming0607 | 2:6bc435f4ffec | 53 | } |