Dual DC Motor Drive using PWM for RenBuggy; there are two inputs to feed a hall sensor for distance / speed feedback.
DCMotorDrive.cpp@0:34800070e9dc, 2014-01-14 (annotated)
- 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?
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_C |
jf1452 | 0:34800070e9dc | 29 | #define _DCMOTORDRIVE_C |
jf1452 | 0:34800070e9dc | 30 | |
jf1452 | 0:34800070e9dc | 31 | #include "mbed.h" |
jf1452 | 0:34800070e9dc | 32 | #include "DCMotorDrive.h" |
jf1452 | 0:34800070e9dc | 33 | |
jf1452 | 0:34800070e9dc | 34 | DCMotorDrive::DCMotorDrive(PinName MotorOut, PinName SensorIn): |
jf1452 | 0:34800070e9dc | 35 | _MotorPin(MotorOut), _SensorIn(SensorIn) |
jf1452 | 0:34800070e9dc | 36 | { |
jf1452 | 0:34800070e9dc | 37 | _MotorPin.period_ms(PWM_PERIOD); |
jf1452 | 0:34800070e9dc | 38 | SetMotorPwm(0); |
jf1452 | 0:34800070e9dc | 39 | _SensorIn.mode(PullUp); |
jf1452 | 0:34800070e9dc | 40 | _SensorIn.fall(this, &DCMotorDrive::Counter); |
jf1452 | 0:34800070e9dc | 41 | PulseClock.reset(); |
jf1452 | 0:34800070e9dc | 42 | ResetOdometer(); |
jf1452 | 0:34800070e9dc | 43 | ClearBuffer(); |
jf1452 | 0:34800070e9dc | 44 | } |
jf1452 | 0:34800070e9dc | 45 | |
jf1452 | 0:34800070e9dc | 46 | void DCMotorDrive::SetMotorPwm(int PwmValue) |
jf1452 | 0:34800070e9dc | 47 | { |
jf1452 | 0:34800070e9dc | 48 | _MotorPin.pulsewidth_us(PwmValue); |
jf1452 | 0:34800070e9dc | 49 | } |
jf1452 | 0:34800070e9dc | 50 | |
jf1452 | 0:34800070e9dc | 51 | int DCMotorDrive::GetAveragePulseTime(void) |
jf1452 | 0:34800070e9dc | 52 | { |
jf1452 | 0:34800070e9dc | 53 | uint32_t Average = 0; |
jf1452 | 0:34800070e9dc | 54 | int BufferPointer; |
jf1452 | 0:34800070e9dc | 55 | int ValueCount = 0; |
jf1452 | 0:34800070e9dc | 56 | |
jf1452 | 0:34800070e9dc | 57 | for (BufferPointer = 0; BufferPointer <= 15; BufferPointer++ ) |
jf1452 | 0:34800070e9dc | 58 | { |
jf1452 | 0:34800070e9dc | 59 | if (ClockBuffer[BufferPointer] != 0) |
jf1452 | 0:34800070e9dc | 60 | ValueCount ++; |
jf1452 | 0:34800070e9dc | 61 | Average += ClockBuffer[BufferPointer]; |
jf1452 | 0:34800070e9dc | 62 | } |
jf1452 | 0:34800070e9dc | 63 | return((int)(Average / ValueCount)); |
jf1452 | 0:34800070e9dc | 64 | } |
jf1452 | 0:34800070e9dc | 65 | |
jf1452 | 0:34800070e9dc | 66 | void DCMotorDrive::ResetOdometer(void) |
jf1452 | 0:34800070e9dc | 67 | { |
jf1452 | 0:34800070e9dc | 68 | RotationCounter = 0; |
jf1452 | 0:34800070e9dc | 69 | } |
jf1452 | 0:34800070e9dc | 70 | |
jf1452 | 0:34800070e9dc | 71 | int DCMotorDrive::ReadOdometer(void) |
jf1452 | 0:34800070e9dc | 72 | { |
jf1452 | 0:34800070e9dc | 73 | return(RotationCounter); |
jf1452 | 0:34800070e9dc | 74 | } |
jf1452 | 0:34800070e9dc | 75 | |
jf1452 | 0:34800070e9dc | 76 | void DCMotorDrive::ClearBuffer(void) |
jf1452 | 0:34800070e9dc | 77 | { |
jf1452 | 0:34800070e9dc | 78 | int BufferPointer; |
jf1452 | 0:34800070e9dc | 79 | |
jf1452 | 0:34800070e9dc | 80 | ClockBuffer[ClockPointer = 0] = 0; |
jf1452 | 0:34800070e9dc | 81 | for (BufferPointer = 1; BufferPointer <= 15; BufferPointer++ ) |
jf1452 | 0:34800070e9dc | 82 | ClockBuffer[BufferPointer] = 0; |
jf1452 | 0:34800070e9dc | 83 | } |
jf1452 | 0:34800070e9dc | 84 | void DCMotorDrive::Counter(void) |
jf1452 | 0:34800070e9dc | 85 | { |
jf1452 | 0:34800070e9dc | 86 | ClockBuffer[ClockPointer] = PulseClock.read_us(); |
jf1452 | 0:34800070e9dc | 87 | PulseClock.reset(); |
jf1452 | 0:34800070e9dc | 88 | timeout.attach(this, &DCMotorDrive::Stall, MOTOR_STALL_TIME); |
jf1452 | 0:34800070e9dc | 89 | RotationCounter++; |
jf1452 | 0:34800070e9dc | 90 | if (++ClockPointer > 15) |
jf1452 | 0:34800070e9dc | 91 | ClockPointer = 0; |
jf1452 | 0:34800070e9dc | 92 | } |
jf1452 | 0:34800070e9dc | 93 | |
jf1452 | 0:34800070e9dc | 94 | void DCMotorDrive::Stall(void) |
jf1452 | 0:34800070e9dc | 95 | { |
jf1452 | 0:34800070e9dc | 96 | ClearBuffer(); |
jf1452 | 0:34800070e9dc | 97 | timeout.detach(); |
jf1452 | 0:34800070e9dc | 98 | } |
jf1452 | 0:34800070e9dc | 99 | |
jf1452 | 0:34800070e9dc | 100 | #endif |