this is part of the pill dispenser prototype model. the servo motor here controls the gate of the cartridge to drop one pill at a time to the channel.

Dependencies:   mbed

Committer:
khp007
Date:
Fri Mar 23 22:25:11 2018 +0000
Revision:
0:e31980f4132f
to run the servo motor to move the gate of the dispenser cartridge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
khp007 0:e31980f4132f 1 /* mbed R/C Servo Library
khp007 0:e31980f4132f 2 *
khp007 0:e31980f4132f 3 * Copyright (c) 2007-2010 sford, cstyles
khp007 0:e31980f4132f 4 *
khp007 0:e31980f4132f 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
khp007 0:e31980f4132f 6 * of this software and associated documentation files (the "Software"), to deal
khp007 0:e31980f4132f 7 * in the Software without restriction, including without limitation the rights
khp007 0:e31980f4132f 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
khp007 0:e31980f4132f 9 * copies of the Software, and to permit persons to whom the Software is
khp007 0:e31980f4132f 10 * furnished to do so, subject to the following conditions:
khp007 0:e31980f4132f 11 *
khp007 0:e31980f4132f 12 * The above copyright notice and this permission notice shall be included in
khp007 0:e31980f4132f 13 * all copies or substantial portions of the Software.
khp007 0:e31980f4132f 14 *
khp007 0:e31980f4132f 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
khp007 0:e31980f4132f 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
khp007 0:e31980f4132f 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
khp007 0:e31980f4132f 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
khp007 0:e31980f4132f 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
khp007 0:e31980f4132f 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
khp007 0:e31980f4132f 21 * THE SOFTWARE.
khp007 0:e31980f4132f 22 */
khp007 0:e31980f4132f 23
khp007 0:e31980f4132f 24 #include "Servo.h"
khp007 0:e31980f4132f 25 #include "mbed.h"
khp007 0:e31980f4132f 26
khp007 0:e31980f4132f 27 static float clamp(float value, float min, float max) {
khp007 0:e31980f4132f 28 if(value < min) {
khp007 0:e31980f4132f 29 return min;
khp007 0:e31980f4132f 30 } else if(value > max) {
khp007 0:e31980f4132f 31 return max;
khp007 0:e31980f4132f 32 } else {
khp007 0:e31980f4132f 33 return value;
khp007 0:e31980f4132f 34 }
khp007 0:e31980f4132f 35 }
khp007 0:e31980f4132f 36
khp007 0:e31980f4132f 37 Servo::Servo(PinName pin) : _pwm(pin) {
khp007 0:e31980f4132f 38 calibrate();
khp007 0:e31980f4132f 39 write(0.5);
khp007 0:e31980f4132f 40 }
khp007 0:e31980f4132f 41
khp007 0:e31980f4132f 42 void Servo::write(float percent) {
khp007 0:e31980f4132f 43 float offset = _range * 2.0 * (percent - 0.5);
khp007 0:e31980f4132f 44 _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
khp007 0:e31980f4132f 45 _p = clamp(percent, 0.0, 1.0);
khp007 0:e31980f4132f 46 }
khp007 0:e31980f4132f 47
khp007 0:e31980f4132f 48 void Servo::position(float degrees) {
khp007 0:e31980f4132f 49 float offset = _range * (degrees / _degrees);
khp007 0:e31980f4132f 50 _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
khp007 0:e31980f4132f 51 }
khp007 0:e31980f4132f 52
khp007 0:e31980f4132f 53 void Servo::calibrate(float range, float degrees) {
khp007 0:e31980f4132f 54 _range = range;
khp007 0:e31980f4132f 55 _degrees = degrees;
khp007 0:e31980f4132f 56 }
khp007 0:e31980f4132f 57
khp007 0:e31980f4132f 58 float Servo::read() {
khp007 0:e31980f4132f 59 return _p;
khp007 0:e31980f4132f 60 }
khp007 0:e31980f4132f 61
khp007 0:e31980f4132f 62 Servo& Servo::operator= (float percent) {
khp007 0:e31980f4132f 63 write(percent);
khp007 0:e31980f4132f 64 return *this;
khp007 0:e31980f4132f 65 }
khp007 0:e31980f4132f 66
khp007 0:e31980f4132f 67 Servo& Servo::operator= (Servo& rhs) {
khp007 0:e31980f4132f 68 write(rhs.read());
khp007 0:e31980f4132f 69 return *this;
khp007 0:e31980f4132f 70 }
khp007 0:e31980f4132f 71
khp007 0:e31980f4132f 72 Servo::operator float() {
khp007 0:e31980f4132f 73 return read();
khp007 0:e31980f4132f 74 }