L9110S H-Bridge Driver
Fork of L9110S by
L9110S.cpp@0:1a9b998e0ec1, 2015-10-31 (annotated)
- Committer:
- HMFK03LST1
- Date:
- Sat Oct 31 15:22:33 2015 +0000
- Revision:
- 0:1a9b998e0ec1
- Child:
- 1:4c6c89e42fa4
L9110S H-Bridge driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HMFK03LST1 | 0:1a9b998e0ec1 | 1 | /** L9110S H-Brigh Driver Interface |
HMFK03LST1 | 0:1a9b998e0ec1 | 2 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 3 | * By Sebastian Donner |
HMFK03LST1 | 0:1a9b998e0ec1 | 4 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 5 | * Permission is hereby granted, free of charge, to any person |
HMFK03LST1 | 0:1a9b998e0ec1 | 6 | * obtaining a copy of this software and associated documentation |
HMFK03LST1 | 0:1a9b998e0ec1 | 7 | * files (the "Software"), to deal in the Software without |
HMFK03LST1 | 0:1a9b998e0ec1 | 8 | * restriction, including without limitation the rights to use, |
HMFK03LST1 | 0:1a9b998e0ec1 | 9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
HMFK03LST1 | 0:1a9b998e0ec1 | 10 | * copies of the Software, and to permit persons to whom the |
HMFK03LST1 | 0:1a9b998e0ec1 | 11 | * Software is furnished to do so, subject to the following |
HMFK03LST1 | 0:1a9b998e0ec1 | 12 | * conditions: |
HMFK03LST1 | 0:1a9b998e0ec1 | 13 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 14 | * The above copyright notice and this permission notice shall be |
HMFK03LST1 | 0:1a9b998e0ec1 | 15 | * included in all copies or substantial portions of the Software. |
HMFK03LST1 | 0:1a9b998e0ec1 | 16 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
HMFK03LST1 | 0:1a9b998e0ec1 | 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
HMFK03LST1 | 0:1a9b998e0ec1 | 19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
HMFK03LST1 | 0:1a9b998e0ec1 | 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
HMFK03LST1 | 0:1a9b998e0ec1 | 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
HMFK03LST1 | 0:1a9b998e0ec1 | 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
HMFK03LST1 | 0:1a9b998e0ec1 | 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
HMFK03LST1 | 0:1a9b998e0ec1 | 24 | * OTHER DEALINGS IN THE SOFTWARE. |
HMFK03LST1 | 0:1a9b998e0ec1 | 25 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 26 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 27 | #include "mbed.h" |
HMFK03LST1 | 0:1a9b998e0ec1 | 28 | #include "L9110S.h" |
HMFK03LST1 | 0:1a9b998e0ec1 | 29 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 30 | /** Create a new interface for an L9110S |
HMFK03LST1 | 0:1a9b998e0ec1 | 31 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 32 | * @param cw_r is the pin for clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 33 | * @param ccw_r is the pin for counter clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 34 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 35 | L9110S::L9110S(PinName cw_r, PinName ccw_r): |
HMFK03LST1 | 0:1a9b998e0ec1 | 36 | cw_min(0), cw_max(100), ccw_min(0), ccw_max(100), _cw(cw_r), _ccw(ccw_r), periode(500) |
HMFK03LST1 | 0:1a9b998e0ec1 | 37 | { |
HMFK03LST1 | 0:1a9b998e0ec1 | 38 | _cw.period_us( periode); |
HMFK03LST1 | 0:1a9b998e0ec1 | 39 | _ccw.period_us(periode); |
HMFK03LST1 | 0:1a9b998e0ec1 | 40 | _cw.pulsewidth_us( 0); |
HMFK03LST1 | 0:1a9b998e0ec1 | 41 | _ccw.pulsewidth_us(0); |
HMFK03LST1 | 0:1a9b998e0ec1 | 42 | } |
HMFK03LST1 | 0:1a9b998e0ec1 | 43 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 44 | /** sets Pulswidth |
HMFK03LST1 | 0:1a9b998e0ec1 | 45 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 46 | * positiv in clockwise Dir |
HMFK03LST1 | 0:1a9b998e0ec1 | 47 | * negativ in counter clockwise Dir |
HMFK03LST1 | 0:1a9b998e0ec1 | 48 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 49 | * in % of range from min_(cw/ccw) to max_(cw/ccw) |
HMFK03LST1 | 0:1a9b998e0ec1 | 50 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 51 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 52 | void L9110S::drive(int power) |
HMFK03LST1 | 0:1a9b998e0ec1 | 53 | { |
HMFK03LST1 | 0:1a9b998e0ec1 | 54 | // Calc PWM in us |
HMFK03LST1 | 0:1a9b998e0ec1 | 55 | if (power > 0) {power = ((power * (cw_max - cw_min )) + (cw_min * 100)) * periode / 100000;} |
HMFK03LST1 | 0:1a9b998e0ec1 | 56 | else { if (power < 0) {power = ((power * (ccw_max- ccw_min)) - (ccw_min * 100)) * periode / 100000;} |
HMFK03LST1 | 0:1a9b998e0ec1 | 57 | else power = 0;} |
HMFK03LST1 | 0:1a9b998e0ec1 | 58 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 59 | // Limit PWM -100 to 100 |
HMFK03LST1 | 0:1a9b998e0ec1 | 60 | if ((power > 100) || (power < -100)) power = 0; |
HMFK03LST1 | 0:1a9b998e0ec1 | 61 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 62 | //cw or ccw Rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 63 | if (power >= 0){_ccw.pulsewidth_us(0); _cw.pulsewidth_us(power);} |
HMFK03LST1 | 0:1a9b998e0ec1 | 64 | else {_cw.pulsewidth_us(0); _ccw.pulsewidth_us(-power);} |
HMFK03LST1 | 0:1a9b998e0ec1 | 65 | } |
HMFK03LST1 | 0:1a9b998e0ec1 | 66 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 67 | /** sets Pulswidth |
HMFK03LST1 | 0:1a9b998e0ec1 | 68 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 69 | * positiv dir is clockwise direction |
HMFK03LST1 | 0:1a9b998e0ec1 | 70 | * negativ dir is counter clockwise direction |
HMFK03LST1 | 0:1a9b998e0ec1 | 71 | * zero is stop |
HMFK03LST1 | 0:1a9b998e0ec1 | 72 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 73 | * power in % of range from min_(cw/ccw) to max_(cw/ccw) |
HMFK03LST1 | 0:1a9b998e0ec1 | 74 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 75 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 76 | void L9110S::drive(int dir, int power) |
HMFK03LST1 | 0:1a9b998e0ec1 | 77 | { |
HMFK03LST1 | 0:1a9b998e0ec1 | 78 | // Calc PWM in us |
HMFK03LST1 | 0:1a9b998e0ec1 | 79 | if (dir > 0) {power = ((power * (cw_max - cw_min )) + (cw_min * 100)) * periode / 100000;} |
HMFK03LST1 | 0:1a9b998e0ec1 | 80 | else { if (dir < 0) {power = ((power * (ccw_max- ccw_min)) + (ccw_min * 100)) * periode / 100000;} |
HMFK03LST1 | 0:1a9b998e0ec1 | 81 | else power = 0;} |
HMFK03LST1 | 0:1a9b998e0ec1 | 82 | // Limit PWM 0 to 100 and Stop |
HMFK03LST1 | 0:1a9b998e0ec1 | 83 | if ((power > 100) || (power < 0) || (dir == 0)) power = 0; |
HMFK03LST1 | 0:1a9b998e0ec1 | 84 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 85 | //cw or ccw Rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 86 | if (dir > 0){_ccw.pulsewidth_us(0); _cw.pulsewidth_us(power);} |
HMFK03LST1 | 0:1a9b998e0ec1 | 87 | else {_cw.pulsewidth_us(0); _ccw.pulsewidth_us(power);} |
HMFK03LST1 | 0:1a9b998e0ec1 | 88 | } |
HMFK03LST1 | 0:1a9b998e0ec1 | 89 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 90 | /** sets the PWM frequency |
HMFK03LST1 | 0:1a9b998e0ec1 | 91 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 92 | * @param hz is the PWM frequency (default 2000 Hz) |
HMFK03LST1 | 0:1a9b998e0ec1 | 93 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 94 | void L9110S::frequency(int hz) |
HMFK03LST1 | 0:1a9b998e0ec1 | 95 | { |
HMFK03LST1 | 0:1a9b998e0ec1 | 96 | periode = 1000000/hz; |
HMFK03LST1 | 0:1a9b998e0ec1 | 97 | } |
HMFK03LST1 | 0:1a9b998e0ec1 | 98 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 99 |