瀏覽代碼

servo: Added new module servo

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 年之前
父節點
當前提交
356b1dd43c
共有 10 個檔案被更改,包括 316 行新增0 行删除
  1. 3
    0
      examples/CMakeLists.txt
  2. 68
    0
      examples/es08a.cxx
  3. 1
    0
      src/CMakeLists.txt
  4. 4
    0
      src/servo/CMakeLists.txt
  5. 39
    0
      src/servo/es08a.cxx
  6. 40
    0
      src/servo/es08a.h
  7. 7
    0
      src/servo/jsupm_es08a.i
  8. 8
    0
      src/servo/pyupm_es08a.i
  9. 88
    0
      src/servo/servo.cxx
  10. 58
    0
      src/servo/servo.h

+ 3
- 0
examples/CMakeLists.txt 查看文件

@@ -8,6 +8,7 @@ add_executable (led-bar led-bar.cxx)
8 8
 add_executable (seg-lcd 4digitdisplay.cxx)
9 9
 add_executable (nrf_transmitter nrf_transmitter.cxx)
10 10
 add_executable (nrf_receiver nrf_receiver.cxx)
11
+add_executable (es08a es08a.cxx)
11 12
 
12 13
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
13 14
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -16,6 +17,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/buzzer)
16 17
 include_directories (${PROJECT_SOURCE_DIR}/src/ledbar)
17 18
 include_directories (${PROJECT_SOURCE_DIR}/src/4digitdisplay)
18 19
 include_directories (${PROJECT_SOURCE_DIR}/src/nrf24l01)
20
+include_directories (${PROJECT_SOURCE_DIR}/src/servo)
19 21
 
20 22
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
21 23
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -27,3 +29,4 @@ target_link_libraries (led-bar ledbar ${CMAKE_THREAD_LIBS_INIT})
27 29
 target_link_libraries (seg-lcd 4digitdisplay ${CMAKE_THREAD_LIBS_INIT})
28 30
 target_link_libraries (nrf_transmitter nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
29 31
 target_link_libraries (nrf_receiver nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
32
+target_link_libraries (es08a servo ${CMAKE_THREAD_LIBS_INIT})

+ 68
- 0
examples/es08a.cxx 查看文件

@@ -0,0 +1,68 @@
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
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include "es08a.h"
28
+#include <signal.h>
29
+#include <stdlib.h>
30
+
31
+int running = 0;
32
+
33
+void
34
+sig_handler(int signo)
35
+{
36
+    printf("got signal\n");
37
+    if (signo == SIGINT) {
38
+        printf("exiting application\n");
39
+        running = 1;
40
+    }
41
+}
42
+
43
+int
44
+main(int argc, char **argv)
45
+{
46
+    upm::ES08A *servo = new upm::ES08A(5);
47
+
48
+    signal(SIGINT, sig_handler);
49
+
50
+    int clock = 0;
51
+    while (!running) {
52
+        for (int i = 0; i < 18; i++) {
53
+            servo->setAngle (clock);
54
+            clock += 10;
55
+        }
56
+
57
+        for (int i = 0; i < 18; i++) {
58
+            servo->setAngle (clock);
59
+            clock -= 10;
60
+        }
61
+    }
62
+
63
+    std::cout << "exiting application" << std::endl;
64
+
65
+    delete servo;
66
+
67
+    return 0;
68
+}

+ 1
- 0
src/CMakeLists.txt 查看文件

@@ -5,3 +5,4 @@ add_subdirectory (buzzer)
5 5
 add_subdirectory (ledbar)
6 6
 add_subdirectory (4digitdisplay)
7 7
 add_subdirectory (nrf24l01)
8
+add_subdirectory (servo)

+ 4
- 0
src/servo/CMakeLists.txt 查看文件

@@ -0,0 +1,4 @@
1
+set (libname "servo")
2
+add_library (servo SHARED servo.cxx es08a.cxx)
3
+include_directories (${MAA_INCLUDE_DIR})
4
+target_link_libraries (servo ${MAA_LIBRARIES})

+ 39
- 0
src/servo/es08a.cxx 查看文件

@@ -0,0 +1,39 @@
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
+
25
+#include <iostream>
26
+#include <unistd.h>
27
+
28
+#include "es08a.h"
29
+
30
+using namespace upm;
31
+
32
+ES08A::ES08A (int pin) : Servo(pin) {
33
+    m_name     = "ES08A";
34
+    m_maxAngle = 180.0;
35
+}
36
+
37
+ES08A::~ES08A() {
38
+
39
+}

+ 40
- 0
src/servo/es08a.h 查看文件

@@ -0,0 +1,40 @@
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 "servo.h"
28
+
29
+namespace upm {
30
+
31
+#define MIN_PULSE_WIDTH      600
32
+#define MAX_PULSE_WIDTH      2500
33
+
34
+class ES08A : public Servo {
35
+    public:
36
+        ES08A (int pin);
37
+        ~ES08A ();
38
+};
39
+
40
+}

+ 7
- 0
src/servo/jsupm_es08a.i 查看文件

@@ -0,0 +1,7 @@
1
+%module jsupm_es08a
2
+
3
+%{
4
+    #include "es08a.h"
5
+%}
6
+
7
+%include "es08a.h"

+ 8
- 0
src/servo/pyupm_es08a.i 查看文件

@@ -0,0 +1,8 @@
1
+%module pyupm_es08a
2
+
3
+%feature("autodoc", "3");
4
+
5
+%include "es08a.h"
6
+%{
7
+    #include "es08a.h"
8
+%}

+ 88
- 0
src/servo/servo.cxx 查看文件

@@ -0,0 +1,88 @@
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
+
25
+#include <iostream>
26
+#include <unistd.h>
27
+#include <stdlib.h>
28
+
29
+#include "servo.h"
30
+
31
+using namespace upm;
32
+
33
+Servo::Servo (int pin) {
34
+    maa_result_t error = MAA_SUCCESS;
35
+
36
+    m_maxAngle        = 180.0;
37
+    m_servoPin        = pin;
38
+    m_pwmServoContext = maa_pwm_init (m_servoPin);
39
+}
40
+
41
+Servo::~Servo () {
42
+
43
+}
44
+
45
+/*
46
+ * X = between (MIN_PULSE_WIDTH , MAX_PULSE_WIDTH)
47
+ *
48
+ * X usec
49
+ * _______
50
+ *        |_______________________________________
51
+ *                      20000 usec
52
+ *
53
+ * Max period can be only 7968750(nses) which is ~8(msec)
54
+ * so the servo wil not work as expected.
55
+ * */
56
+maa_result_t Servo::setAngle (int angle) {
57
+    if (m_pwmServoContext == NULL) {
58
+        std::cout << "PWM context is NULL" << std::endl;
59
+        return MAA_ERROR_UNSPECIFIED;
60
+    }
61
+
62
+    maa_pwm_enable (m_pwmServoContext, 1);
63
+    for (int cycles = 0; cycles < 128; cycles++) {
64
+        maa_pwm_period_us (m_pwmServoContext, MAX_PERIOD);
65
+        maa_pwm_pulsewidth_us (m_pwmServoContext, calcPulseTraveling(angle));
66
+    }
67
+    maa_pwm_enable (m_pwmServoContext, 0);
68
+
69
+    std::cout << "angle = " << angle << " ,pulse = " << calcPulseTraveling(angle) << std::endl;
70
+}
71
+
72
+/*
73
+ * Calculating relative pulse time to the value.
74
+ * */
75
+int Servo::calcPulseTraveling (int value) {
76
+    // if bigger than the boundaries
77
+    if (value > m_maxAngle) {
78
+        return MAX_PULSE_WIDTH;
79
+    }
80
+
81
+    // if less than the boundaries
82
+    if (value  < 0) {
83
+        return MIN_PULSE_WIDTH;
84
+    }
85
+
86
+    // the conversion
87
+    return (int) ((float)MIN_PULSE_WIDTH + ((float)value / m_maxAngle) * ((float)MAX_PULSE_WIDTH - (float)MIN_PULSE_WIDTH));
88
+}

+ 58
- 0
src/servo/servo.h 查看文件

@@ -0,0 +1,58 @@
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 <maa/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
+class Servo {
39
+    public:
40
+        Servo (int pin);
41
+        ~Servo();
42
+        maa_result_t setAngle (int angle);
43
+
44
+        std::string name()
45
+        {
46
+            return m_name;
47
+        }
48
+    protected:
49
+        int calcPulseTraveling (int value);
50
+
51
+        std::string         m_name;
52
+        int                 m_servoPin;
53
+        float               m_maxAngle;
54
+        maa_pwm_context     m_pwmServoContext;
55
+        // maa_gpio_context    m_servoPinCtx;
56
+};
57
+
58
+}