Browse Source

hcsr04: fixes driver and addresses issue #207

Signed-off-by: Rafael Neri <rafael.neri@gmail.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Rafael Neri 9 years ago
parent
commit
5a62602a11
4 changed files with 56 additions and 32 deletions
  1. 2
    2
      examples/c++/CMakeLists.txt
  2. 9
    3
      examples/c++/hcsr04.cxx
  3. 33
    13
      src/hcsr04/hcsr04.cxx
  4. 12
    14
      src/hcsr04/hcsr04.h

+ 2
- 2
examples/c++/CMakeLists.txt View File

@@ -18,7 +18,7 @@ add_executable (nrf24l01-transmitter-example nrf24l01-transmitter.cxx)
18 18
 add_executable (nrf24l01-receiver-example nrf24l01-receiver.cxx)
19 19
 add_executable (nrf24l01-broadcast-example nrf24l01-broadcast.cxx)
20 20
 add_executable (es08a-example es08a.cxx)
21
-add_executable (son-hcsr04-example hcsr04.cxx)
21
+add_executable (hcsr04-example hcsr04.cxx)
22 22
 add_executable (ssd1308-oled-example ssd1308-oled.cxx)
23 23
 add_executable (ssd1327-oled-example ssd1327-oled.cxx)
24 24
 add_executable (max44000-example max44000.cxx)
@@ -269,7 +269,7 @@ target_link_libraries (nrf24l01-transmitter-example nrf24l01 ${CMAKE_THREAD_LIBS
269 269
 target_link_libraries (nrf24l01-receiver-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
270 270
 target_link_libraries (nrf24l01-broadcast-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT})
271 271
 target_link_libraries (es08a-example servo ${CMAKE_THREAD_LIBS_INIT})
272
-target_link_libraries (son-hcsr04-example hcsr04 ${CMAKE_THREAD_LIBS_INIT})
272
+target_link_libraries (hcsr04-example hcsr04 ${CMAKE_THREAD_LIBS_INIT})
273 273
 target_link_libraries (ssd1308-oled-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
274 274
 target_link_libraries (ssd1327-oled-example i2clcd ${CMAKE_THREAD_LIBS_INIT})
275 275
 target_link_libraries (max44000-example max44000 ${CMAKE_THREAD_LIBS_INIT})

+ 9
- 3
examples/c++/hcsr04.cxx View File

@@ -50,11 +50,17 @@ interrupt (void * args) {
50 50
 int
51 51
 main(int argc, char **argv)
52 52
 {
53
-    sonar = new upm::HCSR04(5, 7, &interrupt);
53
+    sonar = new upm::HCSR04(5, 6, &interrupt);
54 54
     signal(SIGINT, sig_handler);
55 55
 
56
-    printf ("width = %d\n", sonar->getDistance());
57
-    std::cout << "exiting application" << std::endl;
56
+    sleep(1);
57
+
58
+    for(;;){
59
+        std::cout << "get distance" << std::endl;
60
+        double distance = sonar->getDistance(CM);
61
+        std::cout << "distance " << distance << std::endl;
62
+        sleep(5);
63
+    }
58 64
 
59 65
     delete sonar;
60 66
 

+ 33
- 13
src/hcsr04/hcsr04.cxx View File

@@ -1,6 +1,7 @@
1 1
 /*
2 2
  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
- * Copyright (c) 2014 Intel Corporation.
3
+ * Author: Rafael Neri <rafael.neri@gmail.com>
4
+ * Copyright (c) 2014-2015 Intel Corporation.
4 5
  *
5 6
  * Permission is hereby granted, free of charge, to any person obtaining
6 7
  * a copy of this software and associated documentation files (the
@@ -42,13 +43,17 @@ HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) {
42 43
     mraa_result_t error  = MRAA_SUCCESS;
43 44
     m_name              = "HCSR04";
44 45
 
45
-    m_pwmTriggerCtx     = mraa_pwm_init (triggerPin);
46
-    if (m_pwmTriggerCtx == NULL) {
47
-        std::cout << "PWM context is NULL" << std::endl;
46
+    mraa_init();
47
+
48
+    m_triggerPinCtx     = mraa_gpio_init (triggerPin);
49
+    if (m_triggerPinCtx == NULL) {
50
+        fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", triggerPin);
48 51
         exit (1);
49 52
     }
50 53
 
51
-    mraa_init();
54
+    mraa_gpio_dir(m_triggerPinCtx, MRAA_GPIO_OUT);
55
+    mraa_gpio_write (m_triggerPinCtx, 0);
56
+
52 57
     m_echoPinCtx = mraa_gpio_init(echoPin);
53 58
     if (m_echoPinCtx == NULL) {
54 59
         fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", echoPin);
@@ -62,24 +67,27 @@ HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) {
62 67
 HCSR04::~HCSR04 () {
63 68
     mraa_result_t error = MRAA_SUCCESS;
64 69
 
65
-    mraa_pwm_close (m_pwmTriggerCtx);
70
+    error = mraa_gpio_close (m_triggerPinCtx);
71
+    if (error != MRAA_SUCCESS) {
72
+        mraa_result_print (error);
73
+    }
74
+
66 75
     error = mraa_gpio_close (m_echoPinCtx);
67 76
     if (error != MRAA_SUCCESS) {
68 77
         mraa_result_print (error);
69 78
     }
70 79
 }
71 80
 
72
-int
73
-HCSR04::getDistance () {
74
-    mraa_pwm_enable (m_pwmTriggerCtx, 1);
75
-    mraa_pwm_period_us (m_pwmTriggerCtx, MAX_PERIOD);
76
-    mraa_pwm_pulsewidth_us (m_pwmTriggerCtx, TRIGGER_PULSE);
77
-    mraa_pwm_enable (m_pwmTriggerCtx, 0);
81
+double
82
+HCSR04::timing() {
83
+    mraa_gpio_write (m_triggerPinCtx, 1);
84
+    usleep(10);
85
+    mraa_gpio_write (m_triggerPinCtx, 0);
78 86
 
79 87
     m_doWork = 0;
80 88
     m_InterruptCounter = 0;
81 89
     while (!m_doWork) {
82
-        sleep (1);
90
+        usleep (5);
83 91
     }
84 92
 
85 93
     return m_FallingTimeStamp - m_RisingTimeStamp;
@@ -98,3 +106,15 @@ HCSR04::ackEdgeDetected () {
98 106
         m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
99 107
     }
100 108
 }
109
+
110
+double
111
+HCSR04::getDistance(int sys)
112
+{
113
+    double _timing = timing();
114
+    if (sys)
115
+    {
116
+        return (_timing/2) / 29.1;
117
+    } else {
118
+        return (_timing/2) / 74.1;
119
+    }
120
+}

+ 12
- 14
src/hcsr04/hcsr04.h View File

@@ -1,6 +1,7 @@
1
- /*
1
+/*
2 2
  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
- * Copyright (c) 2014 Intel Corporation.
3
+ * Author: Rafael Neri <rafael.neri@gmail.com>
4
+ * Copyright (c) 2014-2015 Intel Corporation.
4 5
  *
5 6
  * Permission is hereby granted, free of charge, to any person obtaining
6 7
  * a copy of this software and associated documentation files (the
@@ -33,11 +34,8 @@
33 34
 #include "../IsrCallback.h"
34 35
 #endif
35 36
 
36
-#define HIGH                   1
37
-#define LOW                    0
38
-
39
-#define MAX_PERIOD             7968
40
-#define TRIGGER_PULSE          10
37
+#define CM 1
38
+#define INC 0
41 39
 
42 40
 namespace upm {
43 41
 /**
@@ -62,9 +60,6 @@ namespace upm {
62 60
  */
63 61
 class HCSR04 {
64 62
     public:
65
-#if defined(SWIGJAVA) || defined(JAVACALLBACK)
66
-        HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
67
-#else 
68 63
         /**
69 64
          * Instantiates an HCSR04 object
70 65
          *
@@ -73,6 +68,9 @@ class HCSR04 {
73 68
          * @param fptr Function pointer to handle rising-edge and
74 69
          * falling-edge interrupts
75 70
          */
71
+#if defined(SWIGJAVA) || defined(JAVACALLBACK)
72
+        HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb);
73
+#else
76 74
         HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
77 75
 #endif
78 76
         /**
@@ -83,7 +81,7 @@ class HCSR04 {
83 81
         /**
84 82
          * Gets the distance from the sensor
85 83
          */
86
-        int getDistance ();
84
+        double getDistance (int sys);
87 85
 
88 86
         /**
89 87
          * On each interrupt, this function detects if the interrupt
@@ -106,10 +104,10 @@ class HCSR04 {
106 104
 #if defined(SWIGJAVA) || defined(JAVACALLBACK)
107 105
         HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *));
108 106
 #endif
109
-        mraa_pwm_context     m_pwmTriggerCtx;
110
-        mraa_gpio_context    m_echoPinCtx;
107
+        double timing();
108
+        mraa_gpio_context   m_triggerPinCtx;
109
+        mraa_gpio_context   m_echoPinCtx;
111 110
 
112
-        uint8_t m_waitEcho;
113 111
         long    m_RisingTimeStamp;
114 112
         long    m_FallingTimeStamp;
115 113
         uint8_t m_InterruptCounter;