![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final code for our 4180 Drawing Robot!
Dependencies: 4DGL-uLCD-SE gCodeParser mbed
motor.h@2:ba15545a4ccf, 2014-04-30 (annotated)
- Committer:
- jford38
- Date:
- Wed Apr 30 16:40:10 2014 +0000
- Revision:
- 2:ba15545a4ccf
- Parent:
- 0:40576dfac535
Added Visual Studio Source files and README.; Alejandro.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 0:40576dfac535 | 1 | #ifndef MOTOR_H |
jford38 | 0:40576dfac535 | 2 | #define MOTOR_H |
jford38 | 0:40576dfac535 | 3 | |
jford38 | 0:40576dfac535 | 4 | #include "mbed.h" |
jford38 | 0:40576dfac535 | 5 | |
jford38 | 0:40576dfac535 | 6 | #define LEFT_MOTOR 0 |
jford38 | 0:40576dfac535 | 7 | #define RIGHT_MOTOR 1 |
jford38 | 0:40576dfac535 | 8 | |
jford38 | 0:40576dfac535 | 9 | class Motor { |
jford38 | 0:40576dfac535 | 10 | public: |
jford38 | 0:40576dfac535 | 11 | |
jford38 | 0:40576dfac535 | 12 | Motor(PinName s, PinName d, PinName ms0, PinName ms1, PinName ms2, PinName en, char L_R); |
jford38 | 0:40576dfac535 | 13 | ~Motor(); |
jford38 | 0:40576dfac535 | 14 | |
jford38 | 0:40576dfac535 | 15 | inline void one_step(); |
jford38 | 0:40576dfac535 | 16 | inline void one_step(char new_dir); |
jford38 | 0:40576dfac535 | 17 | inline void enable(); |
jford38 | 0:40576dfac535 | 18 | inline void disable(); |
jford38 | 0:40576dfac535 | 19 | inline void set_dir(char new_dir); |
jford38 | 0:40576dfac535 | 20 | inline void set_msLevel(char newLevel); |
jford38 | 0:40576dfac535 | 21 | inline char get_msLevel(); |
jford38 | 0:40576dfac535 | 22 | |
jford38 | 0:40576dfac535 | 23 | enum { |
jford38 | 0:40576dfac535 | 24 | MS_ONE = 1, |
jford38 | 0:40576dfac535 | 25 | MS_TWO = 2, |
jford38 | 0:40576dfac535 | 26 | MS_FOUR = 4, |
jford38 | 0:40576dfac535 | 27 | MS_EIGHT = 8, |
jford38 | 0:40576dfac535 | 28 | MS_SIXTEEN = 16, |
jford38 | 0:40576dfac535 | 29 | MS_THIRTYTWO = 32 |
jford38 | 0:40576dfac535 | 30 | }; |
jford38 | 0:40576dfac535 | 31 | |
jford38 | 0:40576dfac535 | 32 | |
jford38 | 0:40576dfac535 | 33 | private: |
jford38 | 0:40576dfac535 | 34 | |
jford38 | 0:40576dfac535 | 35 | DigitalOut* step_pin; |
jford38 | 0:40576dfac535 | 36 | DigitalOut* dir_pin; |
jford38 | 0:40576dfac535 | 37 | DigitalOut* ms0_pin; |
jford38 | 0:40576dfac535 | 38 | DigitalOut* ms1_pin; |
jford38 | 0:40576dfac535 | 39 | DigitalOut* ms2_pin; |
jford38 | 0:40576dfac535 | 40 | DigitalOut* enable_pin; |
jford38 | 0:40576dfac535 | 41 | |
jford38 | 0:40576dfac535 | 42 | char IN, OUT; |
jford38 | 0:40576dfac535 | 43 | char msLevel; |
jford38 | 0:40576dfac535 | 44 | |
jford38 | 0:40576dfac535 | 45 | }; |
jford38 | 0:40576dfac535 | 46 | |
jford38 | 0:40576dfac535 | 47 | Motor::Motor(PinName s, PinName d, PinName ms0, PinName ms1, PinName ms2, PinName en, char L_R) { |
jford38 | 0:40576dfac535 | 48 | |
jford38 | 0:40576dfac535 | 49 | step_pin = new DigitalOut(s); |
jford38 | 0:40576dfac535 | 50 | dir_pin = new DigitalOut(d); |
jford38 | 0:40576dfac535 | 51 | ms0_pin = new DigitalOut(ms0); |
jford38 | 0:40576dfac535 | 52 | ms1_pin = new DigitalOut(ms1); |
jford38 | 0:40576dfac535 | 53 | ms2_pin = new DigitalOut(ms2); |
jford38 | 0:40576dfac535 | 54 | enable_pin = new DigitalOut(en); |
jford38 | 0:40576dfac535 | 55 | |
jford38 | 0:40576dfac535 | 56 | switch(L_R) { |
jford38 | 0:40576dfac535 | 57 | case LEFT_MOTOR: |
jford38 | 0:40576dfac535 | 58 | IN = 1; OUT = 0; |
jford38 | 0:40576dfac535 | 59 | break; |
jford38 | 0:40576dfac535 | 60 | case RIGHT_MOTOR: |
jford38 | 0:40576dfac535 | 61 | IN = 0; OUT = 1; |
jford38 | 0:40576dfac535 | 62 | break; |
jford38 | 0:40576dfac535 | 63 | default: |
jford38 | 0:40576dfac535 | 64 | // ERROR |
jford38 | 0:40576dfac535 | 65 | break; |
jford38 | 0:40576dfac535 | 66 | } |
jford38 | 0:40576dfac535 | 67 | |
jford38 | 0:40576dfac535 | 68 | set_dir(IN); |
jford38 | 0:40576dfac535 | 69 | set_msLevel(MS_THIRTYTWO); |
jford38 | 0:40576dfac535 | 70 | enable(); |
jford38 | 0:40576dfac535 | 71 | } |
jford38 | 0:40576dfac535 | 72 | |
jford38 | 0:40576dfac535 | 73 | Motor::~Motor() { |
jford38 | 0:40576dfac535 | 74 | delete step_pin; |
jford38 | 0:40576dfac535 | 75 | delete dir_pin; |
jford38 | 0:40576dfac535 | 76 | } |
jford38 | 0:40576dfac535 | 77 | |
jford38 | 0:40576dfac535 | 78 | inline void Motor::enable() { |
jford38 | 0:40576dfac535 | 79 | *enable_pin = 0; |
jford38 | 0:40576dfac535 | 80 | } |
jford38 | 0:40576dfac535 | 81 | |
jford38 | 0:40576dfac535 | 82 | inline void Motor::disable() { |
jford38 | 0:40576dfac535 | 83 | *enable_pin = 1; |
jford38 | 0:40576dfac535 | 84 | } |
jford38 | 0:40576dfac535 | 85 | |
jford38 | 0:40576dfac535 | 86 | |
jford38 | 0:40576dfac535 | 87 | inline void Motor::set_dir(char new_dir) { |
jford38 | 0:40576dfac535 | 88 | *dir_pin = new_dir; |
jford38 | 0:40576dfac535 | 89 | } |
jford38 | 0:40576dfac535 | 90 | |
jford38 | 0:40576dfac535 | 91 | inline void Motor::one_step() { |
jford38 | 0:40576dfac535 | 92 | *step_pin = 0; |
jford38 | 0:40576dfac535 | 93 | *step_pin = 1; |
jford38 | 0:40576dfac535 | 94 | wait_us(6); |
jford38 | 0:40576dfac535 | 95 | *step_pin = 0; |
jford38 | 0:40576dfac535 | 96 | } |
jford38 | 0:40576dfac535 | 97 | |
jford38 | 0:40576dfac535 | 98 | inline void Motor::one_step(char new_dir) { |
jford38 | 0:40576dfac535 | 99 | set_dir(new_dir); |
jford38 | 0:40576dfac535 | 100 | one_step(); |
jford38 | 0:40576dfac535 | 101 | } |
jford38 | 0:40576dfac535 | 102 | |
jford38 | 0:40576dfac535 | 103 | inline void Motor::set_msLevel(char newLevel) { |
jford38 | 0:40576dfac535 | 104 | |
jford38 | 0:40576dfac535 | 105 | int level = MS_ONE; |
jford38 | 0:40576dfac535 | 106 | |
jford38 | 0:40576dfac535 | 107 | switch(newLevel) { |
jford38 | 0:40576dfac535 | 108 | case MS_ONE: |
jford38 | 0:40576dfac535 | 109 | level = 0; |
jford38 | 0:40576dfac535 | 110 | msLevel = MS_ONE; |
jford38 | 0:40576dfac535 | 111 | break; |
jford38 | 0:40576dfac535 | 112 | case MS_TWO: |
jford38 | 0:40576dfac535 | 113 | level = 1; |
jford38 | 0:40576dfac535 | 114 | msLevel = MS_TWO; |
jford38 | 0:40576dfac535 | 115 | break; |
jford38 | 0:40576dfac535 | 116 | case MS_FOUR: |
jford38 | 0:40576dfac535 | 117 | level = 2; |
jford38 | 0:40576dfac535 | 118 | msLevel = MS_FOUR; |
jford38 | 0:40576dfac535 | 119 | break; |
jford38 | 0:40576dfac535 | 120 | case MS_EIGHT: |
jford38 | 0:40576dfac535 | 121 | level = 3; |
jford38 | 0:40576dfac535 | 122 | msLevel = MS_EIGHT; |
jford38 | 0:40576dfac535 | 123 | break; |
jford38 | 0:40576dfac535 | 124 | case MS_SIXTEEN: |
jford38 | 0:40576dfac535 | 125 | level = 4; |
jford38 | 0:40576dfac535 | 126 | msLevel = MS_SIXTEEN; |
jford38 | 0:40576dfac535 | 127 | break; |
jford38 | 0:40576dfac535 | 128 | case MS_THIRTYTWO: |
jford38 | 0:40576dfac535 | 129 | level = 5; |
jford38 | 0:40576dfac535 | 130 | msLevel = MS_THIRTYTWO; |
jford38 | 0:40576dfac535 | 131 | break; |
jford38 | 0:40576dfac535 | 132 | default: |
jford38 | 0:40576dfac535 | 133 | // Invalid microstep level! |
jford38 | 0:40576dfac535 | 134 | break; |
jford38 | 0:40576dfac535 | 135 | } |
jford38 | 0:40576dfac535 | 136 | |
jford38 | 0:40576dfac535 | 137 | *ms0_pin = level & 0x1; |
jford38 | 0:40576dfac535 | 138 | *ms1_pin = (level >> 1) & 0x1; |
jford38 | 0:40576dfac535 | 139 | *ms2_pin = (level >> 2) & 0x1; |
jford38 | 0:40576dfac535 | 140 | |
jford38 | 0:40576dfac535 | 141 | } |
jford38 | 0:40576dfac535 | 142 | |
jford38 | 0:40576dfac535 | 143 | inline char Motor::get_msLevel() { |
jford38 | 0:40576dfac535 | 144 | return msLevel; |
jford38 | 0:40576dfac535 | 145 | } |
jford38 | 0:40576dfac535 | 146 | |
jford38 | 0:40576dfac535 | 147 | #endif |