L9110S H-Bridge Driver

Fork of L9110S by Hochschule München

Revision:
0:1a9b998e0ec1
Child:
1:4c6c89e42fa4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/L9110S.cpp	Sat Oct 31 15:22:33 2015 +0000
@@ -0,0 +1,99 @@
+/** 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 
+   * 
+   *  positiv in clockwise Dir
+   *  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 
+   * 
+   *  positiv dir is clockwise direction
+   *  negativ dir is counter clockwise direction
+   *  zero is stop 
+   *
+   *  power in % of range from min_(cw/ccw) to max_(cw/ccw)
+   *
+   */
+   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; 
+   }
+    
+        
\ No newline at end of file