Motor class for the robot

Dependencies:   mbed

Dependents:   TEST_ENCODER

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Motorslam.cpp Source File

Motorslam.cpp

00001 //libraries//
00002 #include "mbed.h"
00003 #include "Motorslam.h"
00004 
00005 #define SIG_HIGH    (1)
00006 #define SIG_LOW     (0)
00007 
00008 Motorslam::Motorslam(PinName pinPwmLeft, PinName pinLeftIn1, PinName pinLeftIn2,
00009                      PinName pinPwmRight, PinName pinRightIn1, PinName pinRightIn2,
00010                      PinName pinNStby):
00011                    
00012         pwmLeft(pinPwmLeft),
00013         leftIn1(pinLeftIn1),
00014         leftIn2(pinLeftIn2),
00015         pwmRight(pinPwmRight),
00016         rightIn1(pinRightIn1),
00017         rightIn2(pinRightIn2),  
00018         nStby(pinNStby)
00019         {
00020          leftIn1 = SIG_LOW;
00021          leftIn2 = SIG_LOW;
00022          rightIn1 = SIG_LOW;
00023          rightIn2 = SIG_LOW;
00024          nStby = SIG_LOW;
00025          pwmLeft.period(DEFAULT_PWM_PERIOD);
00026          pwmLeft = DEFAULT_PWM_PULSEWIDTH;
00027          pwmRight.period(DEFAULT_PWM_PERIOD);
00028          pwmLeft = DEFAULT_PWM_PULSEWIDTH;  
00029             }
00030         
00031         void Motorslam::init()
00032             {
00033          leftIn1 = SIG_LOW;
00034          leftIn2 = SIG_LOW;
00035          rightIn1 = SIG_LOW;
00036          rightIn2 = SIG_LOW;
00037          nStby = SIG_LOW;
00038          pwmLeft.period(DEFAULT_PWM_PERIOD);
00039          pwmLeft = DEFAULT_PWM_PULSEWIDTH;
00040          pwmRight.period(DEFAULT_PWM_PERIOD);
00041          pwmLeft = DEFAULT_PWM_PULSEWIDTH; 
00042                 }
00043             
00044         void Motorslam::setPwmLeft(float fPeriod, float fPulsewidth)
00045         {
00046             pwmLeft.period(fPeriod);
00047             pwmLeft = fPulsewidth;
00048             }
00049             
00050         void Motorslam::setPwmLeftperiod(float fPeriod)
00051         {
00052             pwmLeft.period(fPeriod);
00053             }
00054             
00055         void Motorslam::setPwmLeftpulsewidth(float fPulsewidth)
00056         {
00057             pwmLeft = fPulsewidth;
00058             }
00059          
00060         void Motorslam::setPwmRight(float fPeriod, float fPulsewidth)
00061         {
00062             pwmRight.period(fPeriod);
00063             pwmRight = fPulsewidth;
00064             }
00065             
00066         void Motorslam::setPwmRightperiod(float fPeriod)
00067         {
00068             pwmRight.period(fPeriod);
00069             }
00070             
00071         void Motorslam::setPwmRightpulsewidth(float fPulsewidth)
00072         {
00073             pwmRight = fPulsewidth;
00074             }
00075          
00076         void Motorslam::standby(void)
00077         {
00078             nStby = SIG_LOW;
00079             }
00080          
00081         void Motorslam::motorLeft_stop(void)
00082         {
00083             leftIn1 = SIG_LOW;
00084             leftIn2 = SIG_LOW;
00085             }
00086             
00087         void Motorslam::motorLeft_cw(void)
00088         {
00089             leftIn1 = SIG_HIGH;
00090             leftIn2 = SIG_LOW;
00091             nStby = SIG_HIGH;
00092             }
00093         void Motorslam::motorLeft_ccw(void)
00094         {
00095             leftIn1 = SIG_LOW;
00096             leftIn2 = SIG_HIGH;
00097             nStby = SIG_HIGH;
00098             }
00099          
00100         void Motorslam::motorRight_stop(void)
00101         {
00102             rightIn1 = SIG_LOW;
00103             rightIn2 = SIG_LOW;
00104             }
00105         void Motorslam::motorRight_cw(void)
00106         {
00107             rightIn1 = SIG_HIGH;
00108             rightIn2 = SIG_LOW;
00109             nStby = SIG_HIGH;
00110             }
00111         void Motorslam::motorRight_ccw(void)
00112         {
00113             rightIn1 = SIG_LOW;
00114             rightIn2 = SIG_HIGH;
00115             nStby = SIG_HIGH;
00116             }
00117      
00118         void Motorslam::moveStop(void)
00119         {
00120            Motorslam::standby(); 
00121             }
00122         void Motorslam::moveForward(void)
00123         {
00124             Motorslam::motorLeft_cw();
00125             Motorslam::motorRight_cw();
00126             }
00127         void Motorslam::moveBackward(void)
00128         {
00129             Motorslam::motorLeft_ccw();
00130             Motorslam::motorRight_ccw();
00131             }
00132         void Motorslam::moveLeft(void)
00133         {
00134             Motorslam::motorLeft_ccw();
00135             Motorslam::motorRight_cw();
00136             wait(0.5);//trial; (0.5) for low friction(wooden floor); (0.7) for high friction
00137             Motorslam::moveStop();//trial
00138             }
00139         void Motorslam::moveRight(void)
00140         {
00141             Motorslam::motorLeft_cw();
00142             Motorslam::motorRight_ccw();
00143             wait(0.5);//trial; (0.5) for low friction(wooden floor); (0.7) for high friction
00144             Motorslam::moveStop();//trial
00145             }
00146