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@3:1229fe22fdcd, 2021-03-12 (annotated)
- Committer:
- cheukming0607
- Date:
- Fri Mar 12 09:53:53 2021 +0000
- Revision:
- 3:1229fe22fdcd
- Parent:
- 2:6bc435f4ffec
- Child:
- 4:b242191ed0cf
Add commend for main.cpp; ;
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 | 3:1229fe22fdcd | 4 | DigitalOut out1(D2); // Motor direction control pin 1 |
cheukming0607 | 3:1229fe22fdcd | 5 | DigitalOut out2(D4); // Motor direction control pin 2 |
cheukming0607 | 3:1229fe22fdcd | 6 | DigitalIn home_button(D13); // Button for setting the home button |
cheukming0607 | 3:1229fe22fdcd | 7 | QEI dc_motor (D8,D7,NC,792); // Create QEI object for increment encoder |
cheukming0607 | 3:1229fe22fdcd | 8 | int counter = 0; // Dummy counter for sample program |
aberk | 0:bcff39fac858 | 9 | |
cheukming0607 | 3:1229fe22fdcd | 10 | void reset(){ // Setting home point for increment encoder |
cheukming0607 | 3:1229fe22fdcd | 11 | while (home_button == 0){ // Continue to turn clockwise until home button pressed |
cheukming0607 | 2:6bc435f4ffec | 12 | out1 = 1; |
cheukming0607 | 2:6bc435f4ffec | 13 | out2 = 0; |
cheukming0607 | 2:6bc435f4ffec | 14 | } |
cheukming0607 | 2:6bc435f4ffec | 15 | out1 = 0; |
cheukming0607 | 3:1229fe22fdcd | 16 | dc_motor.reset(); // Reset pulse_ |
cheukming0607 | 2:6bc435f4ffec | 17 | wait(1); |
cheukming0607 | 2:6bc435f4ffec | 18 | }; |
cheukming0607 | 2:6bc435f4ffec | 19 | |
cheukming0607 | 3:1229fe22fdcd | 20 | void go_angle(int angle){ // Move motor to specific angle from home point |
cheukming0607 | 3:1229fe22fdcd | 21 | int tar_pulse = ((double) angle / 360.0)* (2.0 * 792.0); // Calculating target pulse number |
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 | 3:1229fe22fdcd | 26 | if (tar_pulse > cur_pulse){ // Rotate motor clockwise |
cheukming0607 | 2:6bc435f4ffec | 27 | out1 = 1; |
cheukming0607 | 2:6bc435f4ffec | 28 | out2 = 0; |
cheukming0607 | 2:6bc435f4ffec | 29 | } |
cheukming0607 | 3:1229fe22fdcd | 30 | else { // Rotate motor counter clockwrise |
cheukming0607 | 2:6bc435f4ffec | 31 | out1 = 0; |
cheukming0607 | 2:6bc435f4ffec | 32 | out2 = 1; |
cheukming0607 | 2:6bc435f4ffec | 33 | } |
cheukming0607 | 3:1229fe22fdcd | 34 | cur_pulse = dc_motor.getPulses(); // Get increment encoder current pulse number |
cheukming0607 | 2:6bc435f4ffec | 35 | } |
cheukming0607 | 3:1229fe22fdcd | 36 | out1 = 0; // Stop motor |
cheukming0607 | 2:6bc435f4ffec | 37 | out2 = 0; |
cheukming0607 | 2:6bc435f4ffec | 38 | }; |
cheukming0607 | 2:6bc435f4ffec | 39 | |
cheukming0607 | 3:1229fe22fdcd | 40 | int main() { // Sample Testing program |
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 | } |