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 21 13:27:27 2014 +0000
Revision:
4:b9aafce708b5
Parent:
2:5239659649d1
Update

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 2:5239659649d1 34 DCMotorDrive::DCMotorDrive(PinName MotorOut, PinName BrakeOut, PinName SensorIn):
jf1452 2:5239659649d1 35 _MotorPin(MotorOut), _BrakePin(BrakeOut), _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 2:5239659649d1 41 _BrakePin = 1;
jf1452 1:0c8cb3439288 42 PulseClock.start();
jf1452 0:34800070e9dc 43 PulseClock.reset();
jf1452 0:34800070e9dc 44 ResetOdometer();
jf1452 2:5239659649d1 45 }
jf1452 2:5239659649d1 46
jf1452 2:5239659649d1 47 void DCMotorDrive::SetMotorPwm(int PwmValue)
jf1452 2:5239659649d1 48 {
jf1452 2:5239659649d1 49 if (PwmValue <= 0)
jf1452 2:5239659649d1 50 {
jf1452 2:5239659649d1 51 _MotorPin.pulsewidth_us(0);
jf1452 2:5239659649d1 52 _BrakePin = 1;
jf1452 2:5239659649d1 53 } else
jf1452 2:5239659649d1 54 {
jf1452 2:5239659649d1 55 _BrakePin = 0;
jf1452 2:5239659649d1 56 timeout.attach(this, &DCMotorDrive::Stall, MOTOR_STALL_TIME);
jf1452 2:5239659649d1 57 _MotorPin.pulsewidth_us(PwmValue);
jf1452 2:5239659649d1 58 }
jf1452 0:34800070e9dc 59 }
jf1452 0:34800070e9dc 60
jf1452 1:0c8cb3439288 61 void DCMotorDrive::SetMotorPwmAndRevolutions(int PwmValue, int MaxRevolutions)
jf1452 0:34800070e9dc 62 {
jf1452 2:5239659649d1 63 SetMotorPwm(PwmValue);
jf1452 1:0c8cb3439288 64 RevolutionLimit = MaxRevolutions;
jf1452 0:34800070e9dc 65 }
jf1452 0:34800070e9dc 66
jf1452 1:0c8cb3439288 67 int DCMotorDrive::GetLastPulseTime(void)
jf1452 1:0c8cb3439288 68 {
jf1452 1:0c8cb3439288 69 return(LastPulseTime);
jf1452 1:0c8cb3439288 70 }
jf1452 2:5239659649d1 71
jf1452 2:5239659649d1 72 int DCMotorDrive::GetRevolutionsLeft(void)
jf1452 2:5239659649d1 73 {
jf1452 2:5239659649d1 74 return(RevolutionLimit);
jf1452 2:5239659649d1 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 0:34800070e9dc 86 void DCMotorDrive::Counter(void)
jf1452 0:34800070e9dc 87 {
jf1452 1:0c8cb3439288 88 LastPulseTime = PulseClock.read_us();
jf1452 0:34800070e9dc 89 PulseClock.reset();
jf1452 2:5239659649d1 90 timeout.detach();
jf1452 0:34800070e9dc 91 timeout.attach(this, &DCMotorDrive::Stall, MOTOR_STALL_TIME);
jf1452 0:34800070e9dc 92 RotationCounter++;
jf1452 1:0c8cb3439288 93 if (RevolutionLimit != 0)
jf1452 1:0c8cb3439288 94 if (--RevolutionLimit == 0)
jf1452 1:0c8cb3439288 95 {
jf1452 1:0c8cb3439288 96 SetMotorPwmAndRevolutions(0,0);
jf1452 2:5239659649d1 97 _BrakePin = 1;
jf1452 2:5239659649d1 98 timeout.detach();
jf1452 1:0c8cb3439288 99 }
jf1452 0:34800070e9dc 100 }
jf1452 0:34800070e9dc 101
jf1452 0:34800070e9dc 102 void DCMotorDrive::Stall(void)
jf1452 0:34800070e9dc 103 {
jf1452 2:5239659649d1 104 SetMotorPwmAndRevolutions(0,0);
jf1452 2:5239659649d1 105 _BrakePin = 1;
jf1452 0:34800070e9dc 106 timeout.detach();
jf1452 0:34800070e9dc 107 }
jf1452 0:34800070e9dc 108
jf1452 0:34800070e9dc 109 #endif