Library for driving the IBT-2 H-bridge motor controller (with BTS 7960 or BTN 7971 half bridges).

See the Wiki tab for example code.

Committer:
rwunderl
Date:
Fri Jul 31 20:30:06 2015 +0000
Revision:
1:fe72c69ab361
Parent:
0:ea214158c2fb
Edited some documentation. No functional changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rwunderl 0:ea214158c2fb 1 /* mbed IBT-2 H-bridge motor controller
rwunderl 0:ea214158c2fb 2 * Copyright (c) 2015, rwunderl, http://mbed.org
rwunderl 0:ea214158c2fb 3 *
rwunderl 0:ea214158c2fb 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
rwunderl 0:ea214158c2fb 5 * of this software and associated documentation files (the "Software"), to deal
rwunderl 0:ea214158c2fb 6 * in the Software without restriction, including without limitation the rights
rwunderl 0:ea214158c2fb 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
rwunderl 0:ea214158c2fb 8 * copies of the Software, and to permit persons to whom the Software is
rwunderl 0:ea214158c2fb 9 * furnished to do so, subject to the following conditions:
rwunderl 0:ea214158c2fb 10 *
rwunderl 0:ea214158c2fb 11 * The above copyright notice and this permission notice shall be included in
rwunderl 0:ea214158c2fb 12 * all copies or substantial portions of the Software.
rwunderl 0:ea214158c2fb 13 *
rwunderl 0:ea214158c2fb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
rwunderl 0:ea214158c2fb 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
rwunderl 0:ea214158c2fb 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
rwunderl 0:ea214158c2fb 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
rwunderl 0:ea214158c2fb 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
rwunderl 0:ea214158c2fb 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
rwunderl 0:ea214158c2fb 20 * THE SOFTWARE.
rwunderl 0:ea214158c2fb 21 */
rwunderl 0:ea214158c2fb 22
rwunderl 0:ea214158c2fb 23 #include "IBT2.h"
rwunderl 0:ea214158c2fb 24
rwunderl 0:ea214158c2fb 25 IBT2::IBT2(PinName L_pwm, PinName R_pwm, PinName en, float freq):
rwunderl 0:ea214158c2fb 26 _L_pwm(L_pwm), _R_pwm(R_pwm), _en(en) {
rwunderl 0:ea214158c2fb 27 // Set initial conditions
rwunderl 0:ea214158c2fb 28 _period = 1.0f / freq; // in seconds
rwunderl 0:ea214158c2fb 29 _L_pwm.period(_period); // same period used for all PwmOuts
rwunderl 0:ea214158c2fb 30 _L_pwm = 0.0;
rwunderl 0:ea214158c2fb 31 //_R_pwm.period(_period);
rwunderl 0:ea214158c2fb 32 _R_pwm = 0.0;
rwunderl 0:ea214158c2fb 33 _en = 0;
rwunderl 0:ea214158c2fb 34 _speed = 0.0;
rwunderl 0:ea214158c2fb 35 }
rwunderl 0:ea214158c2fb 36
rwunderl 0:ea214158c2fb 37 void IBT2::setSpeed(float speed) {
rwunderl 0:ea214158c2fb 38 _speed = speed;
rwunderl 0:ea214158c2fb 39 if (_speed > 0.0f) {
rwunderl 0:ea214158c2fb 40 _en = 1;
rwunderl 0:ea214158c2fb 41 // forward
rwunderl 0:ea214158c2fb 42 _L_pwm = _speed;
rwunderl 0:ea214158c2fb 43 _R_pwm = 0.0;
rwunderl 0:ea214158c2fb 44 } else if (_speed < 0.0f) {
rwunderl 0:ea214158c2fb 45 _en = 1;
rwunderl 0:ea214158c2fb 46 // reverse
rwunderl 0:ea214158c2fb 47 _L_pwm = 0.0;
rwunderl 0:ea214158c2fb 48 _R_pwm = -_speed;
rwunderl 0:ea214158c2fb 49 } else /* _speed == 0.0 */ {
rwunderl 0:ea214158c2fb 50 _en = 0;
rwunderl 0:ea214158c2fb 51 _L_pwm = 0.0;
rwunderl 0:ea214158c2fb 52 _R_pwm = 0.0;
rwunderl 0:ea214158c2fb 53 }
rwunderl 0:ea214158c2fb 54 }
rwunderl 0:ea214158c2fb 55
rwunderl 0:ea214158c2fb 56 float IBT2::getSpeed(void) {
rwunderl 0:ea214158c2fb 57 return _speed;
rwunderl 0:ea214158c2fb 58 }