2014-06-03 16:23:26 +00:00
|
|
|
/*
|
|
|
|
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
|
|
|
*
|
|
|
|
* 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 <iostream>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2014-06-12 15:26:01 +00:00
|
|
|
#include <math.h>
|
2014-06-03 16:23:26 +00:00
|
|
|
|
|
|
|
#include "servo.h"
|
|
|
|
|
|
|
|
using namespace upm;
|
|
|
|
|
|
|
|
Servo::Servo (int pin) {
|
2015-02-12 16:28:13 -08:00
|
|
|
init(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, DEFAULT_WAIT_DISABLE_PWM);
|
|
|
|
}
|
2014-07-29 21:46:48 +04:00
|
|
|
|
2015-02-12 16:28:13 -08:00
|
|
|
Servo::Servo (int pin, int minPulseWidth, int maxPulseWidth) {
|
|
|
|
init(pin, minPulseWidth, maxPulseWidth, DEFAULT_WAIT_DISABLE_PWM);
|
|
|
|
}
|
2014-07-29 21:46:48 +04:00
|
|
|
|
2015-02-12 16:28:13 -08:00
|
|
|
Servo::Servo (int pin, int minPulseWidth, int maxPulseWidth, int waitAndDisablePwm) {
|
|
|
|
init(pin, minPulseWidth, maxPulseWidth, waitAndDisablePwm);
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Servo::~Servo () {
|
2015-02-12 16:28:13 -08:00
|
|
|
haltPwm();
|
2014-06-25 10:05:27 +01:00
|
|
|
mraa_pwm_close (m_pwmServoContext);
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X = between (MIN_PULSE_WIDTH , MAX_PULSE_WIDTH)
|
|
|
|
*
|
|
|
|
* X usec
|
|
|
|
* _______
|
|
|
|
* |_______________________________________
|
2015-02-12 16:28:13 -08:00
|
|
|
* m_period usec
|
2014-06-03 16:23:26 +00:00
|
|
|
*
|
|
|
|
* */
|
2014-06-25 10:05:27 +01:00
|
|
|
mraa_result_t Servo::setAngle (int angle) {
|
2014-06-03 16:23:26 +00:00
|
|
|
if (m_pwmServoContext == NULL) {
|
|
|
|
std::cout << "PWM context is NULL" << std::endl;
|
2014-06-25 10:05:27 +01:00
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|
2014-07-29 21:46:48 +04:00
|
|
|
|
2014-06-12 15:26:01 +00:00
|
|
|
if (angle > m_maxAngle || angle < 0) {
|
2014-06-25 10:05:27 +01:00
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2014-06-12 15:26:01 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 16:28:13 -08:00
|
|
|
mraa_pwm_enable (m_pwmServoContext, 1);
|
|
|
|
mraa_pwm_period_us (m_pwmServoContext, m_period);
|
|
|
|
mraa_pwm_pulsewidth_us (m_pwmServoContext, calcPulseTraveling(angle));
|
2014-07-29 21:46:48 +04:00
|
|
|
|
2015-02-12 16:28:13 -08:00
|
|
|
if (m_waitAndDisablePwm) {
|
|
|
|
sleep(1); // we must make sure that we don't turn off PWM before the servo is done moving.
|
|
|
|
haltPwm();
|
|
|
|
}
|
2014-06-12 15:26:01 +00:00
|
|
|
|
2015-02-12 16:28:13 -08:00
|
|
|
m_currAngle = angle;
|
|
|
|
}
|
2014-06-03 16:23:26 +00:00
|
|
|
|
2015-02-12 16:28:13 -08:00
|
|
|
mraa_result_t Servo::haltPwm () {
|
|
|
|
if (m_pwmServoContext == NULL) {
|
|
|
|
std::cout << "PWM context is NULL" << std::endl;
|
|
|
|
return MRAA_ERROR_UNSPECIFIED;
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 16:28:13 -08:00
|
|
|
return mraa_pwm_enable (m_pwmServoContext, 0);
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculating relative pulse time to the value.
|
|
|
|
* */
|
|
|
|
int Servo::calcPulseTraveling (int value) {
|
|
|
|
// if bigger than the boundaries
|
|
|
|
if (value > m_maxAngle) {
|
2014-06-12 15:26:01 +00:00
|
|
|
return m_maxPulseWidth;
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if less than the boundaries
|
|
|
|
if (value < 0) {
|
2014-06-12 15:26:01 +00:00
|
|
|
return m_minPulseWidth;
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the conversion
|
2014-06-12 15:26:01 +00:00
|
|
|
return (int) ((float)m_minPulseWidth + ((float)value / m_maxAngle) * ((float)m_maxPulseWidth - (float)m_minPulseWidth));
|
|
|
|
}
|
|
|
|
|
2014-07-29 21:46:48 +04:00
|
|
|
void
|
2014-06-12 15:26:01 +00:00
|
|
|
Servo::setMinPulseWidth (int width) {
|
|
|
|
m_minPulseWidth = width;
|
|
|
|
}
|
|
|
|
|
2014-07-29 21:46:48 +04:00
|
|
|
void
|
2014-06-12 15:26:01 +00:00
|
|
|
Servo::setMaxPulseWidth (int width) {
|
|
|
|
m_maxPulseWidth = width;
|
|
|
|
}
|
|
|
|
|
2014-07-29 21:46:48 +04:00
|
|
|
void
|
2015-02-12 16:28:13 -08:00
|
|
|
Servo::setPeriod (int period) {
|
|
|
|
m_period = period;
|
2014-06-12 15:26:01 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 21:46:48 +04:00
|
|
|
int
|
2014-06-12 15:26:01 +00:00
|
|
|
Servo::getMinPulseWidth () {
|
|
|
|
return m_minPulseWidth;
|
|
|
|
}
|
2014-07-29 21:46:48 +04:00
|
|
|
|
|
|
|
int
|
2014-06-12 15:26:01 +00:00
|
|
|
Servo::getMaxPulseWidth () {
|
|
|
|
return m_maxPulseWidth;
|
|
|
|
}
|
|
|
|
|
2014-07-29 21:46:48 +04:00
|
|
|
int
|
2015-02-12 16:28:13 -08:00
|
|
|
Servo::getPeriod () {
|
|
|
|
return m_period;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* private mathod: would like to use delegating constructors instead but that requires C++11
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Servo::init (int pin, int minPulseWidth, int maxPulseWidth, int waitAndDisablePwm) {
|
|
|
|
m_minPulseWidth = minPulseWidth;
|
|
|
|
m_maxPulseWidth = maxPulseWidth;
|
|
|
|
m_period = PERIOD;
|
|
|
|
|
|
|
|
m_waitAndDisablePwm = waitAndDisablePwm;
|
|
|
|
|
|
|
|
m_maxAngle = 180.0;
|
|
|
|
m_servoPin = pin;
|
|
|
|
m_pwmServoContext = mraa_pwm_init (m_servoPin);
|
|
|
|
|
|
|
|
m_currAngle = 180;
|
|
|
|
|
|
|
|
setAngle (0);
|
2014-06-03 16:23:26 +00:00
|
|
|
}
|