Team repo

Dependencies:   mbed QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Motor.cpp Source File

Motor.cpp

00001 #include "Motor.h"
00002 #include "mbed.h"
00003   
00004 
00005 Motor::Motor(PinName _M1PWM, PinName _M2PWM, PinName _M1DIR, PinName _M2DIR, PinName _D2, int default_delay) :    
00006                     M1PWM(_M1PWM),
00007                     M2PWM(_M2PWM), 
00008                     M1DIR(_M1DIR), 
00009                     M2DIR(_M2DIR),
00010                     D2(_D2){
00011     
00012     delay=default_delay;
00013     dir=true;    
00014     length = 5;
00015     
00016     M1PWM=0;
00017     M2PWM=0;
00018     M1DIR=0;
00019     M2DIR=0;
00020     D2=0;
00021     wait_us(10);
00022     D2=1; 
00023     steps = 0;   
00024 }
00025 
00026 void Motor::run(bool t){
00027     if(t){
00028         D2=1;
00029         if(dir) clockwise(delay);   
00030         else anticlockwise(delay);
00031     }else{
00032         D2=0;
00033     }     
00034 }
00035 
00036 void Motor::run2(bool t){
00037     if(t){
00038         D2=1;
00039         if(dir) step_clockwise(length, delay);   
00040         else step_anticlockwise(length, delay);
00041     }else{
00042         D2=0;
00043     }     
00044 }
00045 
00046 void Motor::initialize(int middle){
00047     step_clockwise(middle, 1000);
00048 }
00049 
00050 void Motor::anticlockwise(int delay){
00051     for (int i = 0; i < 8; i++) { 
00052         switch (i) { 
00053             case 0: {
00054                 M1PWM=1;
00055                 M1DIR=0;
00056                 M2PWM=0;
00057                 M2DIR=0;
00058             }
00059             break;
00060             case 1: {
00061                 M1PWM=1;
00062                 M1DIR=0;
00063                 M2PWM=1;
00064                 M2DIR=0;
00065             }
00066             break;
00067             case 2: {
00068                 M1PWM=0;
00069                 M1DIR=0;
00070                 M2PWM=1;
00071                 M2DIR=0;
00072             }
00073             break;
00074             case 3: {
00075                 M1PWM=1;
00076                 M1DIR=1;
00077                 M2PWM=1;
00078                 M2DIR=0;
00079             }
00080             break;
00081             case 4: {
00082                 M1PWM=1;
00083                 M1DIR=1;
00084                 M2PWM=0;
00085                 M2DIR=0;
00086             }
00087             break;
00088             case 5: {
00089                 M1PWM=1;
00090                 M1DIR=1;
00091                 M2PWM=1;
00092                 M2DIR=1;
00093             }
00094             break;
00095             case 6: {
00096                 M1PWM=0;
00097                 M1DIR=0;
00098                 M2PWM=1;
00099                 M2DIR=1;
00100             }
00101             break;
00102             case 7: {
00103                 M1PWM=1;
00104                 M1DIR=0;
00105                 M2PWM=1;
00106                 M2DIR=1;
00107             }
00108             break;
00109         }    
00110     wait_us(delay);
00111     }
00112     steps -= 1;
00113     //printf("%i\r\n",delay);     
00114 }
00115 
00116 void Motor::clockwise(int delay){
00117     for (int i = 0; i < 8; i++) { 
00118         switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
00119             case 0: {
00120                 M1PWM=1;
00121                 M1DIR=0;
00122                 M2PWM=1;
00123                 M2DIR=1;
00124             }
00125             break;
00126             case 1: {
00127                 M1PWM=0;
00128                 M1DIR=0;
00129                 M2PWM=1;
00130                 M2DIR=1;
00131             }
00132             break;
00133             case 2: {
00134                 M1PWM=1;
00135                 M1DIR=1;
00136                 M2PWM=1;
00137                 M2DIR=1;
00138             }
00139             break;
00140             case 3: {
00141                 M1PWM=1;
00142                 M1DIR=1;
00143                 M2PWM=0;
00144                 M2DIR=0;
00145             }
00146             break;
00147             case 4: {
00148                 M1PWM=1;
00149                 M1DIR=1;
00150                 M2PWM=1;
00151                 M2DIR=0;
00152             }
00153             break;
00154             case 5: {
00155                 M1PWM=0;
00156                 M1DIR=0;
00157                 M2PWM=1;
00158                 M2DIR=0;
00159             }
00160             break;
00161             case 6: {
00162                 M1PWM=1;
00163                 M1DIR=0;
00164                 M2PWM=1;
00165                 M2DIR=0;
00166             }
00167             break;
00168             case 7: {
00169                 M1PWM=1;
00170                 M1DIR=0;
00171                 M2PWM=0;
00172                 M2DIR=0;
00173             }
00174             break;
00175         }
00176         //printf("%i\r\n",delay);
00177         wait_us(delay);
00178     }
00179     steps += 1;
00180 }
00181 
00182 void Motor::anticlockwise(){
00183     anticlockwise(delay);
00184 }
00185     
00186 void Motor::clockwise(){
00187     clockwise(delay);
00188 }
00189 
00190 void Motor::step_clockwise(int steps){
00191     for (int i = 0; i < steps; i++){
00192         clockwise();
00193     }
00194 } 
00195 
00196 void Motor::step_anticlockwise(int steps){
00197     for (int i = 0; i < steps; i++){
00198         anticlockwise();
00199     }   
00200 }
00201 
00202 void Motor::step_clockwise(int steps, int delay){
00203     for (int i = 0; i < steps; i++){
00204         clockwise(delay);
00205     }
00206 } 
00207 
00208 void Motor::step_anticlockwise(int steps, int delay){
00209     for (int i = 0; i < steps; i++){
00210         anticlockwise(delay);
00211     }   
00212 }