2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 Servo::Servo (int pin) {
35 mraa_result_t error = MRAA_SUCCESS;
37 m_minPulseWidth = MIN_PULSE_WIDTH;
38 m_maxPulseWidth = MAX_PULSE_WIDTH;
39 m_maxPeriod = MAX_PERIOD;
43 m_pwmServoContext = mraa_pwm_init (m_servoPin);
51 mraa_pwm_close (m_pwmServoContext);
55 * X = between (MIN_PULSE_WIDTH , MAX_PULSE_WIDTH)
59 * |_______________________________________
62 * Max period can be only 7968750(nses) which is ~8(msec)
63 * so the servo wil not work as expected.
65 mraa_result_t Servo::setAngle (int angle) {
66 if (m_pwmServoContext == NULL) {
67 std::cout << "PWM context is NULL" << std::endl;
68 return MRAA_ERROR_UNSPECIFIED;
71 if (angle > m_maxAngle || angle < 0) {
72 return MRAA_ERROR_UNSPECIFIED;
75 int period = (m_maxPulseWidth - m_minPulseWidth) / m_maxAngle;
77 int cycles = (int)(100.0 * (abs (m_currAngle - angle) / m_maxAngle));
79 // int cycles = (int)(100.0 * ((float)angle / (float)m_maxAngle));
81 mraa_pwm_enable (m_pwmServoContext, 1);
82 for (int cycle = 0; cycle < cycles; cycle++) {
83 mraa_pwm_period_us (m_pwmServoContext, m_maxPeriod);
84 mraa_pwm_pulsewidth_us (m_pwmServoContext, calcPulseTraveling(angle));
86 mraa_pwm_enable (m_pwmServoContext, 0);
88 std::cout << "angle = " << angle << " ,pulse = " << calcPulseTraveling(angle) << ", cycles " << cycles << std::endl;
94 * Calculating relative pulse time to the value.
96 int Servo::calcPulseTraveling (int value) {
97 // if bigger than the boundaries
98 if (value > m_maxAngle) {
99 return m_maxPulseWidth;
102 // if less than the boundaries
104 return m_minPulseWidth;
108 return (int) ((float)m_minPulseWidth + ((float)value / m_maxAngle) * ((float)m_maxPulseWidth - (float)m_minPulseWidth));
112 Servo::setMinPulseWidth (int width) {
113 m_minPulseWidth = width;
117 Servo::setMaxPulseWidth (int width) {
118 m_maxPulseWidth = width;
122 Servo::setMaxPeriod (int width) {
127 Servo::getMinPulseWidth () {
128 return m_minPulseWidth;
132 Servo::getMaxPulseWidth () {
133 return m_maxPulseWidth;
137 Servo::getMaxPeriod () {