Gaëtan Andrieu / Motordriver_Tennis
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motordriver.cpp Source File

motordriver.cpp

00001 /*motor driver libary modified from the following libary,
00002 *
00003 * mbed simple H-bridge motor controller
00004 * Copyright (c) 2007-2010, sford
00005 *
00006 * by Christopher Hasler.
00007 *
00008 * from sford's libary,
00009 *
00010 * Permission is hereby granted, free of charge, to any person obtaining a copy
00011 * of this software and associated documentation files (the "Software"), to deal
00012 * in the Software without restriction, including without limitation the rights
00013 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00014 * copies of the Software, and to permit persons to whom the Software is
00015 * furnished to do so, subject to the following conditions:
00016 *
00017 * The above copyright notice and this permission notice shall be included in
00018 * all copies or substantial portions of the Software.
00019 *
00020 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00021 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00022 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00023 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00024 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00025 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00026 * THE SOFTWARE.
00027 */
00028 
00029 #include "motordriver.h"
00030 
00031 #include "mbed.h"
00032 
00033 Motor::Motor(PinName pwm, PinName fwd, PinName rev, int brakeable):
00034         _pwm(pwm), _fwd(fwd), _rev(rev) {
00035 
00036     // Set initial condition of PWM
00037     _pwm.period(0.001);
00038     _pwm = 0;
00039 
00040     // Initial condition of output enables
00041     _fwd = 0;
00042     _rev = 0;
00043 
00044     //set if the motor dirver is capable of braking. (addition)
00045     Brakeable= brakeable;
00046     sign = 0;//i.e nothing.
00047 }
00048 float Motor::speed(float speed) {
00049     float temp = 0;
00050     _fwd = (speed > 0.0);
00051     _rev = (speed < 0.0);
00052     temp = abs(speed);
00053     _pwm = temp;
00054     return temp;
00055 }
00056 /*
00057 float Motor::speed(float speed) {
00058     float temp = 0;
00059     if (sign == 0) {
00060         _fwd = (speed > 0.0);
00061         _rev = (speed < 0.0);
00062         temp = abs(speed);
00063         _pwm = temp;
00064     } else if (sign == 1) {
00065         if (speed < 0) {
00066             _fwd = (speed > 0.0);
00067             _rev = (speed < 0.0);
00068             _pwm = 0;
00069             temp = 0;
00070        } else {
00071             _fwd = (speed > 0.0);
00072             _rev = (speed < 0.0);
00073             temp = abs(speed);
00074             _pwm = temp;
00075         }
00076     } else if (sign == -1) {
00077         if (speed > 0) {
00078             _fwd = (speed > 0.0);
00079             _rev = (speed < 0.0);
00080             _pwm = 0;
00081             temp = 0;
00082         } else {
00083             _fwd = (speed > 0.0);
00084             _rev = (speed < 0.0);
00085             temp = abs(speed);
00086             _pwm = temp;
00087         }
00088     }
00089     if (speed > 0)
00090         sign = 1;
00091     else if (speed < 0) {
00092         sign = -1;
00093     } else if (speed == 0) {
00094         sign = 0;
00095     }
00096     return temp;
00097 }*/
00098 //  (additions)
00099 void Motor::coast(void) {
00100     _fwd = 0;
00101     _rev = 0;
00102     _pwm = 0;
00103     sign = 0;
00104 }
00105 
00106 float Motor::stop(float duty) {
00107     if (Brakeable == 1) {
00108         _fwd = 1;
00109         _rev = 1;
00110         _pwm = duty;
00111         sign = 0;
00112         return duty;
00113     } else
00114         Motor::coast();
00115         return -1;
00116 }
00117 
00118 float Motor::state(void) {
00119     if ((_fwd == _rev) && (_pwm > 0)) {
00120         return -2;//braking
00121     } else if (_pwm == 0) {
00122         return 2;//coasting
00123     } else if ((_fwd == 0) && (_rev == 1)) {
00124         return -(_pwm);//reversing
00125     }  else if ((_fwd == 1) && (_rev == 0)) {
00126         return _pwm;//fowards
00127     } else
00128         return -3;//error
00129 }
00130 
00131 float Motor::direction(void) {
00132     if ((_fwd == _rev) && (_pwm > 0)) {
00133         return -2;//braking
00134     } else if (_pwm == 0) {
00135         return 2;//coasting
00136     } else if ((_fwd == 0) && (_rev == 1)) {
00137         return -1;//reversing
00138     }  else if ((_fwd == 1) && (_rev == 0)) {
00139         return 1;//fowards
00140     } else
00141         return -3;//error
00142 }
00143 
00144 /*
00145  test code, this demonstrates working motor drivers.
00146 
00147 Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can break
00148 Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can break
00149 int main() {
00150     for (float s=-1.0; s < 1.0 ; s += 0.01) {
00151        A.speed(s);
00152        B.speed(s);
00153        wait(0.02);
00154     }
00155     A.stop();
00156     B.stop();
00157     wait(1);
00158     A.coast();
00159     B.coast();
00160 }
00161 */