Semi-Functional EtchSketch Smartbot code. Takes x/y values and draws with polar coords.

Committer:
theschrade54
Date:
Sat Dec 15 01:48:19 2012 +0000
Revision:
0:eae9712515a6
Semi-Functional EtchSketch Smartbot code. Takes x/y values and draws with polar coords.

Who changed what in which revision?

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