L9110S H-Bridge Driver
Fork of L9110S by
L9110S.cpp
- Committer:
- HMFK03LST1
- Date:
- 2015-10-31
- Revision:
- 1:4c6c89e42fa4
- Parent:
- 0:1a9b998e0ec1
- Child:
- 2:5d4f1e2b27fe
File content as of revision 1:4c6c89e42fa4:
/* L9110S H-Brigh Driver Interface * * By Sebastian Donner * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #include "mbed.h" #include "L9110S.h" /** Create a new interface for an L9110S * * @param cw_r is the pin for clockwise rotate * @param ccw_r is the pin for counter clockwise rotate */ L9110S::L9110S(PinName cw_r, PinName ccw_r): cw_min(0), cw_max(100), ccw_min(0), ccw_max(100), _cw(cw_r), _ccw(ccw_r), periode(500) { _cw.period_us( periode); _ccw.period_us(periode); _cw.pulsewidth_us( 0); _ccw.pulsewidth_us(0); } /** sets Pulswidth * * @param power positiv in clockwise Dir * @param power negativ in counter clockwise Dir * * in % of range from min_(cw/ccw) to max_(cw/ccw) * */ void L9110S::drive(int power) { // Calc PWM in us if (power > 0) {power = ((power * (cw_max - cw_min )) + (cw_min * 100)) * periode / 100000;} else { if (power < 0) {power = ((power * (ccw_max- ccw_min)) - (ccw_min * 100)) * periode / 100000;} else power = 0;} // Limit PWM -100 to 100 if ((power > 100) || (power < -100)) power = 0; //cw or ccw Rotate if (power >= 0){_ccw.pulsewidth_us(0); _cw.pulsewidth_us(power);} else {_cw.pulsewidth_us(0); _ccw.pulsewidth_us(-power);} } /** sets Pulswidth * * @param dir positiv dir is clockwise direction * @param dir negativ dir is counter clockwise direction * @param dir zero is stop * * @param power in % of range from min_(cw/ccw) to max_(cw/ccw) allways positiv * */ void L9110S::drive(int dir, int power) { // Calc PWM in us if (dir > 0) {power = ((power * (cw_max - cw_min )) + (cw_min * 100)) * periode / 100000;} else { if (dir < 0) {power = ((power * (ccw_max- ccw_min)) + (ccw_min * 100)) * periode / 100000;} else power = 0;} // Limit PWM 0 to 100 and Stop if ((power > 100) || (power < 0) || (dir == 0)) power = 0; //cw or ccw Rotate if (dir > 0){_ccw.pulsewidth_us(0); _cw.pulsewidth_us(power);} else {_cw.pulsewidth_us(0); _ccw.pulsewidth_us(power);} } /** sets the PWM frequency * * @param hz is the PWM frequency (default 2000 Hz) */ void L9110S::frequency(int hz) { periode = 1000000/hz; }