Final code for our 4180 Drawing Robot!

Dependencies:   4DGL-uLCD-SE gCodeParser mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motor.h Source File

motor.h

00001 #ifndef MOTOR_H
00002 #define MOTOR_H
00003 
00004 #include "mbed.h"
00005 
00006 #define LEFT_MOTOR 0
00007 #define RIGHT_MOTOR 1
00008 
00009 class Motor {
00010   public: 
00011     
00012     Motor(PinName s, PinName d, PinName ms0, PinName ms1, PinName ms2, PinName en, char L_R);
00013     ~Motor();    
00014     
00015     inline void one_step();
00016     inline void one_step(char new_dir);
00017     inline void enable();
00018     inline void disable();
00019     inline void set_dir(char new_dir);
00020     inline void set_msLevel(char newLevel);
00021     inline char get_msLevel();
00022     
00023     enum {
00024         MS_ONE = 1,
00025         MS_TWO = 2,
00026         MS_FOUR = 4,
00027         MS_EIGHT = 8,
00028         MS_SIXTEEN = 16,
00029         MS_THIRTYTWO = 32
00030     };
00031     
00032     
00033   private:
00034   
00035     DigitalOut* step_pin;
00036     DigitalOut* dir_pin; 
00037     DigitalOut* ms0_pin;
00038     DigitalOut* ms1_pin;
00039     DigitalOut* ms2_pin;
00040     DigitalOut* enable_pin;
00041     
00042     char IN, OUT;
00043     char msLevel;
00044     
00045 };
00046 
00047 Motor::Motor(PinName s, PinName d, PinName ms0, PinName ms1, PinName ms2, PinName en, char L_R) {
00048     
00049     step_pin = new DigitalOut(s);
00050     dir_pin  = new DigitalOut(d);
00051     ms0_pin  = new DigitalOut(ms0);
00052     ms1_pin  = new DigitalOut(ms1);
00053     ms2_pin  = new DigitalOut(ms2);
00054     enable_pin = new DigitalOut(en);
00055     
00056     switch(L_R) {
00057         case LEFT_MOTOR:
00058             IN = 1; OUT = 0;
00059         break;
00060         case RIGHT_MOTOR:
00061             IN = 0; OUT = 1;
00062         break;   
00063         default:
00064             // ERROR
00065         break;
00066     }
00067     
00068     set_dir(IN);
00069     set_msLevel(MS_THIRTYTWO);
00070     enable();
00071 }
00072 
00073 Motor::~Motor() {
00074     delete step_pin;
00075     delete dir_pin;    
00076 }
00077 
00078 inline void Motor::enable() {
00079     *enable_pin = 0;
00080 }
00081 
00082 inline void Motor::disable() {
00083     *enable_pin = 1;
00084 }
00085 
00086 
00087 inline void Motor::set_dir(char new_dir) {
00088     *dir_pin = new_dir;
00089 }
00090 
00091 inline void Motor::one_step() {
00092     *step_pin = 0;
00093     *step_pin = 1;
00094     wait_us(6);
00095     *step_pin = 0;
00096 }
00097 
00098 inline void Motor::one_step(char new_dir) {
00099     set_dir(new_dir);
00100     one_step();
00101 }
00102 
00103 inline void Motor::set_msLevel(char newLevel) {
00104     
00105     int level = MS_ONE;
00106     
00107     switch(newLevel) {
00108         case MS_ONE:
00109             level = 0;
00110             msLevel = MS_ONE;
00111             break;
00112         case MS_TWO:
00113             level = 1;
00114             msLevel = MS_TWO;
00115             break;
00116         case MS_FOUR:
00117             level = 2;
00118             msLevel = MS_FOUR;
00119             break;
00120         case MS_EIGHT:
00121             level = 3;
00122             msLevel = MS_EIGHT;
00123             break;
00124         case MS_SIXTEEN:
00125             level = 4;
00126             msLevel = MS_SIXTEEN;
00127             break;
00128         case MS_THIRTYTWO:
00129             level = 5;
00130             msLevel = MS_THIRTYTWO;
00131             break;
00132         default:
00133             // Invalid microstep level!
00134             break;
00135     }
00136     
00137     *ms0_pin = level & 0x1;
00138     *ms1_pin = (level >> 1) & 0x1;
00139     *ms2_pin = (level >> 2) & 0x1;
00140 
00141 }
00142 
00143 inline char Motor::get_msLevel() {
00144     return msLevel;
00145 }
00146 
00147 #endif