motor drive libary to use speed control, coasting and dynamic braking. NOTE, dynamic braking my result in large currents. this may or may not set the motor driver on fire/break it/other undesired effects. so read the data sheet folks.
Dependents: motordrivertestprogram Initialmbedrobotprogram mbedrobot ipod ... more
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 00049 float Motor::speed(float speed) { 00050 float temp = 0; 00051 if (sign == 0) { 00052 _fwd = (speed > 0.0); 00053 _rev = (speed < 0.0); 00054 temp = abs(speed); 00055 _pwm = temp; 00056 } else if (sign == 1) { 00057 if (speed < 0) { 00058 _fwd = (speed > 0.0); 00059 _rev = (speed < 0.0); 00060 _pwm = 0; 00061 temp = 0; 00062 } else { 00063 _fwd = (speed > 0.0); 00064 _rev = (speed < 0.0); 00065 temp = abs(speed); 00066 _pwm = temp; 00067 } 00068 } else if (sign == -1) { 00069 if (speed > 0) { 00070 _fwd = (speed > 0.0); 00071 _rev = (speed < 0.0); 00072 _pwm = 0; 00073 temp = 0; 00074 } else { 00075 _fwd = (speed > 0.0); 00076 _rev = (speed < 0.0); 00077 temp = abs(speed); 00078 _pwm = temp; 00079 } 00080 } 00081 if (speed > 0) 00082 sign = 1; 00083 else if (speed < 0) { 00084 sign = -1; 00085 } else if (speed == 0) { 00086 sign = 0; 00087 } 00088 return temp; 00089 } 00090 // (additions) 00091 void Motor::coast(void) { 00092 _fwd = 0; 00093 _rev = 0; 00094 _pwm = 0; 00095 sign = 0; 00096 } 00097 00098 float Motor::stop(float duty) { 00099 if (Brakeable == 1) { 00100 _fwd = 1; 00101 _rev = 1; 00102 _pwm = duty; 00103 sign = 0; 00104 return duty; 00105 } else 00106 Motor::coast(); 00107 return -1; 00108 } 00109 00110 float Motor::state(void) { 00111 if ((_fwd == _rev) && (_pwm > 0)) { 00112 return -2;//braking 00113 } else if (_pwm == 0) { 00114 return 2;//coasting 00115 } else if ((_fwd == 0) && (_rev == 1)) { 00116 return -(_pwm);//reversing 00117 } else if ((_fwd == 1) && (_rev == 0)) { 00118 return _pwm;//fowards 00119 } else 00120 return -3;//error 00121 } 00122 00123 /* 00124 test code, this demonstrates working motor drivers. 00125 00126 Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can break 00127 Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can break 00128 int main() { 00129 for (float s=-1.0; s < 1.0 ; s += 0.01) { 00130 A.speed(s); 00131 B.speed(s); 00132 wait(0.02); 00133 } 00134 A.stop(); 00135 B.stop(); 00136 wait(1); 00137 A.coast(); 00138 B.coast(); 00139 } 00140 */
Generated on Tue Jul 12 2022 14:24:23 by
