Dual DC Motor Drive using PWM for RenBuggy; there are two inputs to feed a hall sensor for distance / speed feedback.
DCMotorDrive.h@4:b9aafce708b5, 2014-01-21 (annotated)
- Committer:
- jf1452
- Date:
- Tue Jan 21 13:27:27 2014 +0000
- Revision:
- 4:b9aafce708b5
- Parent:
- 3:50495c3c3c37
- Parent:
- 2:5239659649d1
Update
Who changed what in which revision?
User | Revision | Line number | New 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 PWM_PERIOD 20 |
jf1452 | 0:34800070e9dc | 34 | #define MOTOR_STALL_TIME 2 |
jf1452 | 0:34800070e9dc | 35 | |
jf1452 | 0:34800070e9dc | 36 | class DCMotorDrive |
jf1452 | 0:34800070e9dc | 37 | { |
jf1452 | 0:34800070e9dc | 38 | public: |
jf1452 | 2:5239659649d1 | 39 | DCMotorDrive(PinName MotorOut, PinName BrakeOut, PinName SensorIn); |
jf1452 | 2:5239659649d1 | 40 | void SetMotorPwm(int PwmValue); |
jf1452 | 1:0c8cb3439288 | 41 | void SetMotorPwmAndRevolutions(int PwmValue, int MaxRevolutions); |
jf1452 | 0:34800070e9dc | 42 | int GetAveragePulseTime(void); |
jf1452 | 1:0c8cb3439288 | 43 | int GetLastPulseTime(void); |
jf1452 | 2:5239659649d1 | 44 | int GetRevolutionsLeft(void); |
jf1452 | 0:34800070e9dc | 45 | void ResetOdometer(void); |
jf1452 | 0:34800070e9dc | 46 | int ReadOdometer(void); |
jf1452 | 1:0c8cb3439288 | 47 | int ReadCaptureTime(void); |
jf1452 | 0:34800070e9dc | 48 | |
jf1452 | 0:34800070e9dc | 49 | private: |
jf1452 | 0:34800070e9dc | 50 | Ticker timeout; |
jf1452 | 0:34800070e9dc | 51 | Timer PulseClock; |
jf1452 | 0:34800070e9dc | 52 | volatile int ClockPointer; |
jf1452 | 1:0c8cb3439288 | 53 | volatile int LastPulseTime; |
jf1452 | 0:34800070e9dc | 54 | volatile int RotationCounter; |
jf1452 | 1:0c8cb3439288 | 55 | volatile int RevolutionLimit; |
jf1452 | 0:34800070e9dc | 56 | |
jf1452 | 0:34800070e9dc | 57 | PwmOut _MotorPin; |
jf1452 | 2:5239659649d1 | 58 | DigitalOut _BrakePin; |
jf1452 | 0:34800070e9dc | 59 | InterruptIn _SensorIn; |
jf1452 | 0:34800070e9dc | 60 | |
jf1452 | 0:34800070e9dc | 61 | void Counter(void); |
jf1452 | 0:34800070e9dc | 62 | void Stall(void); |
jf1452 | 0:34800070e9dc | 63 | }; |
jf1452 | 0:34800070e9dc | 64 | |
jf1452 | 3:50495c3c3c37 | 65 | #endif // _DCMOTORDRIVE_H |