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

Dependents:   RenBuggy

Committer:
jf1452
Date:
Tue Jan 14 10:45:19 2014 +0000
Revision:
0:34800070e9dc
Child:
1:0c8cb3439288
Dual DC Motor Drive using PWM for RenBuggy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jf1452 0:34800070e9dc 1 /*******************************************************************************
jf1452 0:34800070e9dc 2 * RenBED DC Motor Drive for RenBuggy *
jf1452 0:34800070e9dc 3 * Copyright (c) 2014 Jon Fuge *
jf1452 0:34800070e9dc 4 * *
jf1452 0:34800070e9dc 5 * Permission is hereby granted, free of charge, to any person obtaining a copy *
jf1452 0:34800070e9dc 6 * of this software and associated documentation files (the "Software"), to deal*
jf1452 0:34800070e9dc 7 * in the Software without restriction, including without limitation the rights *
jf1452 0:34800070e9dc 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
jf1452 0:34800070e9dc 9 * copies of the Software, and to permit persons to whom the Software is *
jf1452 0:34800070e9dc 10 * furnished to do so, subject to the following conditions: *
jf1452 0:34800070e9dc 11 * *
jf1452 0:34800070e9dc 12 * The above copyright notice and this permission notice shall be included in *
jf1452 0:34800070e9dc 13 * all copies or substantial portions of the Software. *
jf1452 0:34800070e9dc 14 * *
jf1452 0:34800070e9dc 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
jf1452 0:34800070e9dc 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
jf1452 0:34800070e9dc 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
jf1452 0:34800070e9dc 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
jf1452 0:34800070e9dc 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,*
jf1452 0:34800070e9dc 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
jf1452 0:34800070e9dc 21 * THE SOFTWARE. *
jf1452 0:34800070e9dc 22 * *
jf1452 0:34800070e9dc 23 * DCMotorDrive.h *
jf1452 0:34800070e9dc 24 * *
jf1452 0:34800070e9dc 25 * V1.0 06/01/2014 First issue of code Jon Fuge *
jf1452 0:34800070e9dc 26 *******************************************************************************/
jf1452 0:34800070e9dc 27
jf1452 0:34800070e9dc 28 #ifndef _DCMOTORDRIVE_H
jf1452 0:34800070e9dc 29 #define _DCMOTORDRIVE_H
jf1452 0:34800070e9dc 30
jf1452 0:34800070e9dc 31 #include "mbed.h"
jf1452 0:34800070e9dc 32
jf1452 0:34800070e9dc 33 #define WHEEL_CIRCUMFERENCE 114.4
jf1452 0:34800070e9dc 34 #define COUNTS_PER_ROTATION 64
jf1452 0:34800070e9dc 35 #define PWM_SPEED_ADJUST 20
jf1452 0:34800070e9dc 36 #define ROTATION_LIMIT 20
jf1452 0:34800070e9dc 37 #define PWM_PERIOD 20
jf1452 0:34800070e9dc 38 #define PWM_PERIOD_US PWM_PERIOD * 1000
jf1452 0:34800070e9dc 39 #define MOTOR_STALL_TIME 2
jf1452 0:34800070e9dc 40 #define CAL_SPEED 80 // CalSpeed is 80 mm/s
jf1452 0:34800070e9dc 41 #define TWO_SECONDS 2000000
jf1452 0:34800070e9dc 42 #define US_IN_SECONDS 1000000
jf1452 0:34800070e9dc 43
jf1452 0:34800070e9dc 44 class DCMotorDrive
jf1452 0:34800070e9dc 45 {
jf1452 0:34800070e9dc 46 public:
jf1452 0:34800070e9dc 47 DCMotorDrive(PinName MotorOut, PinName SensorIn);
jf1452 0:34800070e9dc 48 void SetMotorPwm(int PwmValue);
jf1452 0:34800070e9dc 49 int GetAveragePulseTime(void);
jf1452 0:34800070e9dc 50 void ResetOdometer(void);
jf1452 0:34800070e9dc 51 int ReadOdometer(void);
jf1452 0:34800070e9dc 52 void ClearBuffer(void);
jf1452 0:34800070e9dc 53
jf1452 0:34800070e9dc 54 private:
jf1452 0:34800070e9dc 55 Ticker timeout;
jf1452 0:34800070e9dc 56 Timer PulseClock;
jf1452 0:34800070e9dc 57 volatile int ClockBuffer[16];
jf1452 0:34800070e9dc 58 volatile int ClockPointer;
jf1452 0:34800070e9dc 59 volatile int RotationCounter;
jf1452 0:34800070e9dc 60
jf1452 0:34800070e9dc 61 PwmOut _MotorPin;
jf1452 0:34800070e9dc 62 InterruptIn _SensorIn;
jf1452 0:34800070e9dc 63
jf1452 0:34800070e9dc 64 void Counter(void);
jf1452 0:34800070e9dc 65 void Stall(void);
jf1452 0:34800070e9dc 66 };
jf1452 0:34800070e9dc 67
jf1452 0:34800070e9dc 68 #endif // _DCMOTORDRIVE_H