L9110S H-Bridge Driver
Fork of L9110S by
L9110S.h@5:5d8bf5144d8f, 2016-07-20 (annotated)
- Committer:
- HMFK03LST1
- Date:
- Wed Jul 20 14:12:29 2016 +0000
- Revision:
- 5:5d8bf5144d8f
- Parent:
- 4:d2fb1359716e
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HMFK03LST1 | 0:1a9b998e0ec1 | 1 | #include "mbed.h" |
HMFK03LST1 | 0:1a9b998e0ec1 | 2 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 3 | /** L9110S H-Brigh Driver Interface |
HMFK03LST1 | 0:1a9b998e0ec1 | 4 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 5 | * By Sebastian Donner |
HMFK03LST1 | 0:1a9b998e0ec1 | 6 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 7 | * Permission is hereby granted, free of charge, to any person |
HMFK03LST1 | 0:1a9b998e0ec1 | 8 | * obtaining a copy of this software and associated documentation |
HMFK03LST1 | 0:1a9b998e0ec1 | 9 | * files (the "Software"), to deal in the Software without |
HMFK03LST1 | 0:1a9b998e0ec1 | 10 | * restriction, including without limitation the rights to use, |
HMFK03LST1 | 0:1a9b998e0ec1 | 11 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
HMFK03LST1 | 0:1a9b998e0ec1 | 12 | * copies of the Software, and to permit persons to whom the |
HMFK03LST1 | 0:1a9b998e0ec1 | 13 | * Software is furnished to do so, subject to the following |
HMFK03LST1 | 0:1a9b998e0ec1 | 14 | * conditions: |
HMFK03LST1 | 0:1a9b998e0ec1 | 15 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 16 | * The above copyright notice and this permission notice shall be |
HMFK03LST1 | 0:1a9b998e0ec1 | 17 | * included in all copies or substantial portions of the Software. |
HMFK03LST1 | 0:1a9b998e0ec1 | 18 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
HMFK03LST1 | 0:1a9b998e0ec1 | 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
HMFK03LST1 | 0:1a9b998e0ec1 | 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
HMFK03LST1 | 0:1a9b998e0ec1 | 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
HMFK03LST1 | 0:1a9b998e0ec1 | 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
HMFK03LST1 | 0:1a9b998e0ec1 | 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
HMFK03LST1 | 0:1a9b998e0ec1 | 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
HMFK03LST1 | 0:1a9b998e0ec1 | 26 | * OTHER DEALINGS IN THE SOFTWARE. |
HMFK03LST1 | 0:1a9b998e0ec1 | 27 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 28 | * @code |
HMFK03LST1 | 0:1a9b998e0ec1 | 29 | * #include "mbed.h" |
HMFK03LST1 | 0:1a9b998e0ec1 | 30 | * #include "L9110S.h" |
HMFK03LST1 | 0:1a9b998e0ec1 | 31 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 32 | * Serial debug(USBTX,USBRX); |
HMFK03LST1 | 0:1a9b998e0ec1 | 33 | * L9119S motor_x(p21, p22); |
HMFK03LST1 | 0:1a9b998e0ec1 | 34 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 35 | * int main() |
HMFK03LST1 | 0:1a9b998e0ec1 | 36 | * { |
HMFK03LST1 | 0:1a9b998e0ec1 | 37 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 38 | * int power = 0; |
HMFK03LST1 | 0:1a9b998e0ec1 | 39 | * int step = 1; |
HMFK03LST1 | 0:1a9b998e0ec1 | 40 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 41 | * debug.baud(115200); |
HMFK03LST1 | 0:1a9b998e0ec1 | 42 | * debug.printf("L9110S Motor Test"); |
HMFK03LST1 | 0:1a9b998e0ec1 | 43 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 44 | * while(1) |
HMFK03LST1 | 0:1a9b998e0ec1 | 45 | * { |
HMFK03LST1 | 0:1a9b998e0ec1 | 46 | * power += step; |
HMFK03LST1 | 0:1a9b998e0ec1 | 47 | * motor_x.drive(power); |
HMFK03LST1 | 0:1a9b998e0ec1 | 48 | * debug.printf("Power: %i\n\r", power ); |
HMFK03LST1 | 0:1a9b998e0ec1 | 49 | * wait(0.10); |
HMFK03LST1 | 0:1a9b998e0ec1 | 50 | * if ((power > 99)||(power < -99)) step *= -1; |
HMFK03LST1 | 4:d2fb1359716e | 51 | * } |
HMFK03LST1 | 0:1a9b998e0ec1 | 52 | * } |
HMFK03LST1 | 0:1a9b998e0ec1 | 53 | * @endcode |
HMFK03LST1 | 0:1a9b998e0ec1 | 54 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 55 | class L9110S { |
HMFK03LST1 | 0:1a9b998e0ec1 | 56 | public: |
HMFK03LST1 | 0:1a9b998e0ec1 | 57 | //! Min Power in PWM% for clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 58 | int cw_min; |
HMFK03LST1 | 0:1a9b998e0ec1 | 59 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 60 | //! Max Power in PWM% for clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 61 | int cw_max; |
HMFK03LST1 | 0:1a9b998e0ec1 | 62 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 63 | //! Min Power in PWM% for counter clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 64 | int ccw_min; |
HMFK03LST1 | 0:1a9b998e0ec1 | 65 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 66 | //! Max Power in PWM% for counter clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 67 | int ccw_max; |
HMFK03LST1 | 0:1a9b998e0ec1 | 68 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 69 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 70 | /** Create a new interface for an L9110S |
HMFK03LST1 | 0:1a9b998e0ec1 | 71 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 72 | * @param cw_r is the pin for clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 73 | * @param ccw_r is the pin for counter clockwise rotate |
HMFK03LST1 | 0:1a9b998e0ec1 | 74 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 75 | L9110S(PinName cw_r, PinName ccw_r); |
HMFK03LST1 | 0:1a9b998e0ec1 | 76 | |
HMFK03LST1 | 3:695e0c6881c5 | 77 | /** sets Pulswidth cw(+) ccw(-) |
HMFK03LST1 | 0:1a9b998e0ec1 | 78 | * |
HMFK03LST1 | 3:695e0c6881c5 | 79 | * @param power positiv in clockwise Dir |
HMFK03LST1 | 3:695e0c6881c5 | 80 | * @param power negativ in counter clockwise Dir |
HMFK03LST1 | 0:1a9b998e0ec1 | 81 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 82 | * in % of range from min_(cw/ccw) to max_(cw/ccw) |
HMFK03LST1 | 0:1a9b998e0ec1 | 83 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 84 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 85 | void drive(int power); |
HMFK03LST1 | 0:1a9b998e0ec1 | 86 | |
HMFK03LST1 | 3:695e0c6881c5 | 87 | /** sets Pulswidth and Dir |
HMFK03LST1 | 0:1a9b998e0ec1 | 88 | * |
HMFK03LST1 | 3:695e0c6881c5 | 89 | * @param dir positiv dir is clockwise direction |
HMFK03LST1 | 3:695e0c6881c5 | 90 | * @param dir negativ dir is counter clockwise direction |
HMFK03LST1 | 3:695e0c6881c5 | 91 | * @param dir zero is stop |
HMFK03LST1 | 0:1a9b998e0ec1 | 92 | * |
HMFK03LST1 | 3:695e0c6881c5 | 93 | * @param power in % of range from min_(cw/ccw) to max_(cw/ccw) allways positiv |
HMFK03LST1 | 0:1a9b998e0ec1 | 94 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 95 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 96 | void drive(int dir, int power); |
HMFK03LST1 | 0:1a9b998e0ec1 | 97 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 98 | /** sets the PWM frequency |
HMFK03LST1 | 0:1a9b998e0ec1 | 99 | * |
HMFK03LST1 | 0:1a9b998e0ec1 | 100 | * @param hz is the PWM frequency (default 2000 Hz) |
HMFK03LST1 | 0:1a9b998e0ec1 | 101 | */ |
HMFK03LST1 | 0:1a9b998e0ec1 | 102 | void frequency(int hz); |
HMFK03LST1 | 0:1a9b998e0ec1 | 103 | |
HMFK03LST1 | 4:d2fb1359716e | 104 | void drive_diff(float Soll, float Ist, int kp ,int i_lim, int trash); |
HMFK03LST1 | 4:d2fb1359716e | 105 | /** sets the PWM frequency |
HMFK03LST1 | 4:d2fb1359716e | 106 | * |
HMFK03LST1 | 4:d2fb1359716e | 107 | * @param Soll target value |
HMFK03LST1 | 4:d2fb1359716e | 108 | * @param Ist actual value |
HMFK03LST1 | 4:d2fb1359716e | 109 | * @param kp prop Faktor |
HMFK03LST1 | 4:d2fb1359716e | 110 | * @param i_lim I Limiter |
HMFK03LST1 | 4:d2fb1359716e | 111 | * @param trash Deathband wide in value * 10 |
HMFK03LST1 | 4:d2fb1359716e | 112 | * |
HMFK03LST1 | 4:d2fb1359716e | 113 | */ |
HMFK03LST1 | 4:d2fb1359716e | 114 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 115 | private: |
HMFK03LST1 | 0:1a9b998e0ec1 | 116 | PwmOut _cw; |
HMFK03LST1 | 0:1a9b998e0ec1 | 117 | PwmOut _ccw; |
HMFK03LST1 | 0:1a9b998e0ec1 | 118 | int periode; |
HMFK03LST1 | 5:5d8bf5144d8f | 119 | int power_act; |
HMFK03LST1 | 5:5d8bf5144d8f | 120 | float drive_i; |
HMFK03LST1 | 4:d2fb1359716e | 121 | float deg_diff(float Soll, float Ist); |
HMFK03LST1 | 0:1a9b998e0ec1 | 122 | |
HMFK03LST1 | 0:1a9b998e0ec1 | 123 | }; |