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:
Wed Jan 15 11:48:01 2014 +0000
Revision:
1:0c8cb3439288
Parent:
0:34800070e9dc
Child:
2:5239659649d1
Preliminary code - work in progress 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_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 1:0c8cb3439288 38 SetMotorPwmAndRevolutions(0,0);
jf1452 0:34800070e9dc 39 _SensorIn.mode(PullUp);
jf1452 0:34800070e9dc 40 _SensorIn.fall(this, &DCMotorDrive::Counter);
jf1452 1:0c8cb3439288 41 PulseClock.start();
jf1452 0:34800070e9dc 42 PulseClock.reset();
jf1452 0:34800070e9dc 43 ResetOdometer();
jf1452 0:34800070e9dc 44 ClearBuffer();
jf1452 0:34800070e9dc 45 }
jf1452 0:34800070e9dc 46
jf1452 1:0c8cb3439288 47 void DCMotorDrive::SetMotorPwmAndRevolutions(int PwmValue, int MaxRevolutions)
jf1452 0:34800070e9dc 48 {
jf1452 0:34800070e9dc 49 _MotorPin.pulsewidth_us(PwmValue);
jf1452 1:0c8cb3439288 50 RevolutionLimit = MaxRevolutions;
jf1452 1:0c8cb3439288 51 CaptureTime = 0;
jf1452 0:34800070e9dc 52 }
jf1452 0:34800070e9dc 53
jf1452 0:34800070e9dc 54 int DCMotorDrive::GetAveragePulseTime(void)
jf1452 0:34800070e9dc 55 {
jf1452 0:34800070e9dc 56 uint32_t Average = 0;
jf1452 0:34800070e9dc 57 int BufferPointer;
jf1452 1:0c8cb3439288 58 int Count = 0;
jf1452 0:34800070e9dc 59
jf1452 1:0c8cb3439288 60 for (BufferPointer = 0; BufferPointer < BUFFER_SIZE; BufferPointer++ )
jf1452 0:34800070e9dc 61 {
jf1452 0:34800070e9dc 62 if (ClockBuffer[BufferPointer] != 0)
jf1452 1:0c8cb3439288 63 Count += 1;
jf1452 0:34800070e9dc 64 Average += ClockBuffer[BufferPointer];
jf1452 0:34800070e9dc 65 }
jf1452 1:0c8cb3439288 66 if (Count == 0)
jf1452 1:0c8cb3439288 67 return (0);
jf1452 1:0c8cb3439288 68 else
jf1452 1:0c8cb3439288 69 return((int)(Average / Count));
jf1452 0:34800070e9dc 70 }
jf1452 0:34800070e9dc 71
jf1452 1:0c8cb3439288 72 int DCMotorDrive::GetLastPulseTime(void)
jf1452 1:0c8cb3439288 73 {
jf1452 1:0c8cb3439288 74 return(LastPulseTime);
jf1452 1:0c8cb3439288 75 }
jf1452 0:34800070e9dc 76 void DCMotorDrive::ResetOdometer(void)
jf1452 0:34800070e9dc 77 {
jf1452 0:34800070e9dc 78 RotationCounter = 0;
jf1452 0:34800070e9dc 79 }
jf1452 0:34800070e9dc 80
jf1452 0:34800070e9dc 81 int DCMotorDrive::ReadOdometer(void)
jf1452 0:34800070e9dc 82 {
jf1452 0:34800070e9dc 83 return(RotationCounter);
jf1452 0:34800070e9dc 84 }
jf1452 0:34800070e9dc 85
jf1452 1:0c8cb3439288 86 int DCMotorDrive::ReadCaptureTime(void)
jf1452 1:0c8cb3439288 87 {
jf1452 1:0c8cb3439288 88 return(CaptureTime);
jf1452 1:0c8cb3439288 89 }
jf1452 0:34800070e9dc 90 void DCMotorDrive::ClearBuffer(void)
jf1452 0:34800070e9dc 91 {
jf1452 0:34800070e9dc 92 int BufferPointer;
jf1452 0:34800070e9dc 93
jf1452 1:0c8cb3439288 94 ClockPointer = 0;
jf1452 1:0c8cb3439288 95 for (BufferPointer = 0; BufferPointer < BUFFER_SIZE; BufferPointer++ )
jf1452 0:34800070e9dc 96 ClockBuffer[BufferPointer] = 0;
jf1452 0:34800070e9dc 97 }
jf1452 0:34800070e9dc 98 void DCMotorDrive::Counter(void)
jf1452 0:34800070e9dc 99 {
jf1452 1:0c8cb3439288 100 LastPulseTime = PulseClock.read_us();
jf1452 0:34800070e9dc 101 PulseClock.reset();
jf1452 1:0c8cb3439288 102 ClockBuffer[ClockPointer] = LastPulseTime;
jf1452 0:34800070e9dc 103 timeout.attach(this, &DCMotorDrive::Stall, MOTOR_STALL_TIME);
jf1452 0:34800070e9dc 104 RotationCounter++;
jf1452 1:0c8cb3439288 105 if (++ClockPointer == BUFFER_SIZE)
jf1452 0:34800070e9dc 106 ClockPointer = 0;
jf1452 1:0c8cb3439288 107 if (RevolutionLimit != 0)
jf1452 1:0c8cb3439288 108 if (--RevolutionLimit == 0)
jf1452 1:0c8cb3439288 109 {
jf1452 1:0c8cb3439288 110 SetMotorPwmAndRevolutions(0,0);
jf1452 1:0c8cb3439288 111 CaptureTime = GetAveragePulseTime();
jf1452 1:0c8cb3439288 112 }
jf1452 0:34800070e9dc 113 }
jf1452 0:34800070e9dc 114
jf1452 0:34800070e9dc 115 void DCMotorDrive::Stall(void)
jf1452 0:34800070e9dc 116 {
jf1452 0:34800070e9dc 117 ClearBuffer();
jf1452 0:34800070e9dc 118 timeout.detach();
jf1452 0:34800070e9dc 119 }
jf1452 0:34800070e9dc 120
jf1452 0:34800070e9dc 121 #endif