Dual DC Motor Drive using PWM for RenBuggy; there are two inputs to feed a hall sensor for distance / speed feedback.

Dependents:   RenBuggy

Revision:
0:34800070e9dc
Child:
1:0c8cb3439288
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DCMotorDrive.h	Tue Jan 14 10:45:19 2014 +0000
@@ -0,0 +1,68 @@
+/*******************************************************************************
+* RenBED DC Motor Drive for RenBuggy                                           *
+* Copyright (c) 2014 Jon Fuge                                                  *
+*                                                                              *
+* 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.                                                                *
+*                                                                              *
+* DCMotorDrive.h                                                               *
+*                                                                              *
+* V1.0 06/01/2014 First issue of code                      Jon Fuge            *
+*******************************************************************************/
+
+#ifndef _DCMOTORDRIVE_H
+#define _DCMOTORDRIVE_H
+
+#include "mbed.h"
+
+#define WHEEL_CIRCUMFERENCE 114.4
+#define COUNTS_PER_ROTATION 64
+#define PWM_SPEED_ADJUST 20
+#define ROTATION_LIMIT 20
+#define PWM_PERIOD 20
+#define PWM_PERIOD_US PWM_PERIOD * 1000
+#define MOTOR_STALL_TIME 2
+#define CAL_SPEED 80 // CalSpeed is 80 mm/s
+#define TWO_SECONDS 2000000
+#define US_IN_SECONDS 1000000
+
+class DCMotorDrive
+{
+public:
+    DCMotorDrive(PinName MotorOut, PinName SensorIn);
+    void    SetMotorPwm(int PwmValue);
+    int     GetAveragePulseTime(void);
+    void    ResetOdometer(void);
+    int     ReadOdometer(void);
+    void    ClearBuffer(void);
+
+private:
+    Ticker          timeout;
+    Timer           PulseClock;
+    volatile int    ClockBuffer[16];
+    volatile int    ClockPointer;
+    volatile int    RotationCounter;
+
+    PwmOut          _MotorPin;
+    InterruptIn     _SensorIn;
+    
+    void    Counter(void);
+    void    Stall(void);
+};
+
+#endif    // _DCMOTORDRIVE_H