yashirou shimogamo / Mbed 2 deprecated BIRD2019

Dependencies:   mbed mbed-rtos SPI_MX25R

Control/Servo/Servo.cpp

Committer:
shimogamo
Date:
2018-12-01
Revision:
0:d6402bcd58f7

File content as of revision 0:d6402bcd58f7:

/* mbed R/C Servo Library
 *  
 * Copyright (c) 2007-2010 sford, cstyles
 *
 * 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.
 */
 
#include "Servo.h"
#include "mbed.h"


static float clamp(float value, float min, float max) {
    if(value < min) {
        return min;
    } else if(value > max) {
        return max;
    } else {
        return value;
    }
}

Servo::Servo(PinName pin) : _pwm(pin) {
    calibrate();
    write(0.5);
}
 
void Servo::write(float percent) {
    float offset = _range * 2.0 * (percent - 0.5);
    _pwm.pulsewidth(_center + clamp(offset, -_range, _range));
    _p = clamp(percent, 0.0, 1.0);
    _angle = p2angle(_p);
}


void Servo::rawwrite(int width_us){
    _pwm.pulsewidth_us(width_us);
    _p = (((float)width_us * 0.001 * 0.001) - _center) / (_range * 2.0) + 0.5;
    _angle = p2angle(_p);
}


void Servo::position(float degrees) {
    float offset = _range * (degrees / _degrees);
    _pwm.pulsewidth(_center + clamp(offset, -_range, _range));
    _p = clamp(degrees * 0.5 / _degrees + 0.5, 0.0, 1.0);
    _angle = clamp(degrees, -_degrees, _degrees);
}

//calibrateで_range(パルス幅の値域)、_degree(角度の値域)、_center(パルス幅の中心値)、_period_ms(パルス周期)を設定する
void Servo::calibrate(float range_us, float degrees, float center_us, int period_ms_) {
    _range = range_us * 0.001 * 0.001;
    _degrees = degrees;
    _center = center_us * 0.001 * 0.001;
    _period_ms = period_ms_;
    _pwm.period_ms(_period_ms);
}

float Servo::read() {
    return _p;
}

float Servo::readangle() {
    return _angle;
}

Servo& Servo::operator= (float percent) { 
    write(percent);
    return *this;
}

Servo& Servo::operator= (Servo& rhs) {
    write(rhs.read());
    return *this;
}

Servo::operator float() {
    return read();
}

float Servo::p2angle(float p){
    return (p - 0.5) * 2.0 * _degrees;
}