Просмотр исходного кода

hcsr04: Added new sonar module (not working properly yet)

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 лет назад
Родитель
Сommit
23d847e380

+ 3
- 0
examples/CMakeLists.txt Просмотреть файл

@@ -9,6 +9,7 @@ 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 11
 add_executable (es08a es08a.cxx)
12
+add_executable (son-hcsr04 hcsr04.cxx)
12 13
 
13 14
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
14 15
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -18,6 +19,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ledbar)
18 19
 include_directories (${PROJECT_SOURCE_DIR}/src/4digitdisplay)
19 20
 include_directories (${PROJECT_SOURCE_DIR}/src/nrf24l01)
20 21
 include_directories (${PROJECT_SOURCE_DIR}/src/servo)
22
+include_directories (${PROJECT_SOURCE_DIR}/src/hcsr04)
21 23
 
22 24
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
23 25
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -30,3 +32,4 @@ target_link_libraries (seg-lcd 4digitdisplay ${CMAKE_THREAD_LIBS_INIT})
30 32
 target_link_libraries (nrf_transmitter nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
31 33
 target_link_libraries (nrf_receiver nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
32 34
 target_link_libraries (es08a servo ${CMAKE_THREAD_LIBS_INIT})
35
+target_link_libraries (son-hcsr04 hcsr04 ${CMAKE_THREAD_LIBS_INIT})

+ 61
- 0
examples/hcsr04.cxx Просмотреть файл

@@ -0,0 +1,61 @@
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 "hcsr04.h"
28
+#include <signal.h>
29
+#include <stdlib.h>
30
+#include <sys/time.h>
31
+
32
+upm::HCSR04 *sonar = NULL;
33
+
34
+void
35
+sig_handler(int signo)
36
+{
37
+    printf("got signal\n");
38
+    if (signo == SIGINT) {
39
+        printf("exiting application\n");
40
+        sonar->m_doWork = 1;
41
+    }
42
+}
43
+
44
+void
45
+interrupt (void) {
46
+    sonar->ackEdgeDetected ();
47
+}
48
+
49
+int
50
+main(int argc, char **argv)
51
+{
52
+    sonar = new upm::HCSR04(5, 7, &interrupt);
53
+    signal(SIGINT, sig_handler);
54
+
55
+    printf ("width = %d\n", sonar->getDistance());
56
+    std::cout << "exiting application" << std::endl;
57
+
58
+    delete sonar;
59
+
60
+    return 0;
61
+}

+ 1
- 0
src/CMakeLists.txt Просмотреть файл

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

+ 4
- 0
src/hcsr04/CMakeLists.txt Просмотреть файл

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

+ 94
- 0
src/hcsr04/hcsr04.cxx Просмотреть файл

@@ -0,0 +1,94 @@
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
+#include <functional>
29
+
30
+#include "hcsr04.h"
31
+
32
+using namespace upm;
33
+
34
+HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void)) {
35
+    maa_result_t error  = MAA_SUCCESS;
36
+    m_name              = "HCSR04";
37
+
38
+    m_pwmTriggerCtx     = maa_pwm_init (triggerPin);
39
+    if (m_pwmTriggerCtx == NULL) {
40
+        std::cout << "PWM context is NULL" << std::endl;
41
+        exit (1);
42
+    }
43
+
44
+    maa_init();
45
+    m_echoPinCtx = maa_gpio_init(echoPin);
46
+    if (m_echoPinCtx == NULL) {
47
+        fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", echoPin);
48
+        exit (1);
49
+    }
50
+
51
+    maa_gpio_dir(m_echoPinCtx, MAA_GPIO_IN);
52
+    gpio_edge_t edge = MAA_GPIO_EDGE_BOTH;
53
+    maa_gpio_isr (m_echoPinCtx, edge, fptr);
54
+}
55
+
56
+HCSR04::~HCSR04 () {
57
+    maa_result_t error = MAA_SUCCESS;
58
+
59
+    maa_pwm_close (m_pwmTriggerCtx);
60
+    error = maa_gpio_close (m_echoPinCtx);
61
+    if (error != MAA_SUCCESS) {
62
+        maa_result_print (error);
63
+    }
64
+}
65
+
66
+int
67
+HCSR04::getDistance () {
68
+    maa_pwm_enable (m_pwmTriggerCtx, 1);
69
+    maa_pwm_period_us (m_pwmTriggerCtx, MAX_PERIOD);
70
+    maa_pwm_pulsewidth_us (m_pwmTriggerCtx, TRIGGER_PULSE);
71
+    maa_pwm_enable (m_pwmTriggerCtx, 0);
72
+
73
+    m_doWork = 0;
74
+    m_InterruptCounter = 0;
75
+    while (!m_doWork) {
76
+        sleep (1);
77
+    }
78
+
79
+    return m_FallingTimeStamp - m_RisingTimeStamp;
80
+}
81
+
82
+void
83
+HCSR04::ackEdgeDetected () {
84
+    struct timeval timer;
85
+    gettimeofday(&timer, NULL);
86
+
87
+    ++m_InterruptCounter;
88
+    if (!(m_InterruptCounter % 2)) {
89
+        m_FallingTimeStamp  = 1000000 * timer.tv_sec + timer.tv_usec;
90
+        m_doWork = 1;
91
+    } else {
92
+        m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
93
+    }
94
+}

+ 66
- 0
src/hcsr04/hcsr04.h Просмотреть файл

@@ -0,0 +1,66 @@
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/aio.h>
28
+#include <maa/gpio.h>
29
+#include <maa/pwm.h>
30
+#include <sys/time.h>
31
+
32
+#define HIGH                   1
33
+#define LOW                    0
34
+
35
+#define MAX_PERIOD             7968
36
+#define TRIGGER_PULSE          10
37
+
38
+namespace upm {
39
+
40
+class HCSR04 {
41
+    public:
42
+        HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void));
43
+        ~HCSR04 ();
44
+        int getDistance ();
45
+        void ackEdgeDetected ();
46
+
47
+        uint8_t m_doWork;
48
+
49
+        std::string name()
50
+        {
51
+            return m_name;
52
+        }
53
+
54
+    private:
55
+        maa_pwm_context     m_pwmTriggerCtx;
56
+        maa_gpio_context    m_echoPinCtx;
57
+
58
+        uint8_t m_waitEcho;
59
+        long    m_RisingTimeStamp;
60
+        long    m_FallingTimeStamp;
61
+        uint8_t m_InterruptCounter;
62
+
63
+        std::string         m_name;
64
+};
65
+
66
+}

+ 7
- 0
src/hcsr04/jsupm_hcsr04.i Просмотреть файл

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

+ 8
- 0
src/hcsr04/pyupm_hcsr04.i Просмотреть файл

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