Dual DC Motor Drive using PWM for RenBuggy; there are two inputs to feed a hall sensor for distance / speed feedback.
DCMotorDrive.cpp
- Committer:
- jf1452
- Date:
- 2014-01-15
- Revision:
- 1:0c8cb3439288
- Parent:
- 0:34800070e9dc
- Child:
- 2:5239659649d1
File content as of revision 1:0c8cb3439288:
/******************************************************************************* * 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_C #define _DCMOTORDRIVE_C #include "mbed.h" #include "DCMotorDrive.h" DCMotorDrive::DCMotorDrive(PinName MotorOut, PinName SensorIn): _MotorPin(MotorOut), _SensorIn(SensorIn) { _MotorPin.period_ms(PWM_PERIOD); SetMotorPwmAndRevolutions(0,0); _SensorIn.mode(PullUp); _SensorIn.fall(this, &DCMotorDrive::Counter); PulseClock.start(); PulseClock.reset(); ResetOdometer(); ClearBuffer(); } void DCMotorDrive::SetMotorPwmAndRevolutions(int PwmValue, int MaxRevolutions) { _MotorPin.pulsewidth_us(PwmValue); RevolutionLimit = MaxRevolutions; CaptureTime = 0; } int DCMotorDrive::GetAveragePulseTime(void) { uint32_t Average = 0; int BufferPointer; int Count = 0; for (BufferPointer = 0; BufferPointer < BUFFER_SIZE; BufferPointer++ ) { if (ClockBuffer[BufferPointer] != 0) Count += 1; Average += ClockBuffer[BufferPointer]; } if (Count == 0) return (0); else return((int)(Average / Count)); } int DCMotorDrive::GetLastPulseTime(void) { return(LastPulseTime); } void DCMotorDrive::ResetOdometer(void) { RotationCounter = 0; } int DCMotorDrive::ReadOdometer(void) { return(RotationCounter); } int DCMotorDrive::ReadCaptureTime(void) { return(CaptureTime); } void DCMotorDrive::ClearBuffer(void) { int BufferPointer; ClockPointer = 0; for (BufferPointer = 0; BufferPointer < BUFFER_SIZE; BufferPointer++ ) ClockBuffer[BufferPointer] = 0; } void DCMotorDrive::Counter(void) { LastPulseTime = PulseClock.read_us(); PulseClock.reset(); ClockBuffer[ClockPointer] = LastPulseTime; timeout.attach(this, &DCMotorDrive::Stall, MOTOR_STALL_TIME); RotationCounter++; if (++ClockPointer == BUFFER_SIZE) ClockPointer = 0; if (RevolutionLimit != 0) if (--RevolutionLimit == 0) { SetMotorPwmAndRevolutions(0,0); CaptureTime = GetAveragePulseTime(); } } void DCMotorDrive::Stall(void) { ClearBuffer(); timeout.detach(); } #endif