907e160cadf1926efda77533f613a41a16e51e53
[contrib/upm.git] / src / servo / servo.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24 #pragma once
25
26 #include <string>
27 #include <mraa/pwm.h>
28
29 namespace upm {
30
31 #define MIN_PULSE_WIDTH      600
32 #define MAX_PULSE_WIDTH      2500
33 #define MAX_PERIOD           7968
34
35 #define HIGH                  1
36 #define LOW                   0
37
38 /**
39  * @brief Base class for other servo components
40  *
41  * PMOD pins for MAX44000PMB1 board
42  *
43  */
44 class Servo {
45     public:
46         /**
47          * Instanciates a servo object
48          *
49          * @param pin servo pin number
50          */
51         Servo (int pin);
52
53         /**
54          * Servo object destructor.
55          */
56         ~Servo();
57
58         /**
59          * Set the of the servo engine.
60          *
61          * X = between (MIN_PULSE_WIDTH , MAX_PULSE_WIDTH)
62          *
63          * X usec
64          * _______
65          *        |_______________________________________
66          *                      20000 usec
67          *
68          * Max period can be only 7968750(nses) which is ~8(msec)
69          * so the servo will not work as expected.
70          *
71          * @param angle number between 0 and 180
72          */
73         mraa_result_t setAngle (int angle);
74
75         /**
76          * Return name of the component
77          */
78         std::string name()
79         {
80             return m_name;
81         }
82         
83         /**
84          * Set min pulse width
85          *
86          * @param width HIGH signal width
87          */
88         void setMinPulseWidth (int width);
89         
90         /**
91          * Set max pulse width
92          *
93          * @param width HIGH signal width
94          */
95         void setMaxPulseWidth (int width);
96         
97         /**
98          * Set max period width
99          *
100          * @param width PWM period width
101          */
102         void setMaxPeriod (int width);
103         
104         /**
105          * Return min pulse width
106          */
107         int getMinPulseWidth ();
108         
109         /**
110          * Return max pulse width
111          */
112         int getMaxPulseWidth ();
113         
114         /**
115          * Return max PWM period width
116          */
117         int getMaxPeriod ();
118         
119     protected:
120         int calcPulseTraveling (int value);
121
122         std::string         m_name;
123         int                 m_servoPin;
124         float               m_maxAngle;
125         mraa_pwm_context     m_pwmServoContext;
126         int                 m_currAngle;
127         
128         int                 m_minPulseWidth;
129         int                 m_maxPulseWidth;
130         int                 m_maxPeriod;
131 };
132
133 }