• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Author: Rafael Neri <rafael.neri@gmail.com>
4  * Copyright (c) 2014-2015 Intel Corporation.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <iostream>
27 #include <string>
28 #include <stdexcept>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <functional>
32 
33 #include "hcsr04.h"
34 
35 using namespace upm;
36 
37 #ifdef JAVACALLBACK
HCSR04(uint8_t triggerPin,uint8_t echoPin,IsrCallback * cb)38 HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, IsrCallback *cb)
39 {
40         HCSR04 (triggerPin, echoPin, generic_callback_isr);
41 }
42 #endif
43 
HCSR04(uint8_t triggerPin,uint8_t echoPin,void (* fptr)(void *))44 HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void *)) {
45     mraa_result_t error  = MRAA_SUCCESS;
46     m_name              = "HCSR04";
47 
48     m_triggerPinCtx     = mraa_gpio_init (triggerPin);
49     if (m_triggerPinCtx == NULL) {
50         throw std::invalid_argument(std::string(__FUNCTION__) +
51                                     ": mraa_pwm_init() failed, invalid pin?");
52         return;
53     }
54 
55     mraa_gpio_dir(m_triggerPinCtx, MRAA_GPIO_OUT);
56     mraa_gpio_write (m_triggerPinCtx, 0);
57 
58     m_echoPinCtx = mraa_gpio_init(echoPin);
59     if (m_echoPinCtx == NULL) {
60         throw std::invalid_argument(std::string(__FUNCTION__) +
61                                     ": mraa_gpio_init() failed, invalid pin?");
62         return;
63     }
64 
65     mraa_gpio_dir(m_echoPinCtx, MRAA_GPIO_IN);
66     mraa_gpio_isr(m_echoPinCtx, MRAA_GPIO_EDGE_BOTH, fptr, NULL);
67 }
68 
~HCSR04()69 HCSR04::~HCSR04 () {
70     mraa_result_t error = MRAA_SUCCESS;
71 
72     error = mraa_gpio_close (m_triggerPinCtx);
73     if (error != MRAA_SUCCESS) {
74         mraa_result_print (error);
75     }
76 
77     error = mraa_gpio_close (m_echoPinCtx);
78     if (error != MRAA_SUCCESS) {
79         mraa_result_print (error);
80     }
81 }
82 
83 double
timing()84 HCSR04::timing() {
85     mraa_gpio_write (m_triggerPinCtx, 1);
86     usleep(10);
87     mraa_gpio_write (m_triggerPinCtx, 0);
88 
89     m_doWork = 0;
90     m_InterruptCounter = 0;
91     while (!m_doWork) {
92         usleep (5);
93     }
94 
95     return m_FallingTimeStamp - m_RisingTimeStamp;
96 }
97 
98 void
ackEdgeDetected()99 HCSR04::ackEdgeDetected () {
100     struct timeval timer;
101     gettimeofday(&timer, NULL);
102 
103     ++m_InterruptCounter;
104     if (!(m_InterruptCounter % 2)) {
105         m_FallingTimeStamp  = 1000000 * timer.tv_sec + timer.tv_usec;
106         m_doWork = 1;
107     } else {
108         m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
109     }
110 }
111 
112 double
getDistance(int sys)113 HCSR04::getDistance(int sys)
114 {
115     double _timing = timing();
116     if (sys)
117     {
118         return (_timing/2) / 29.1;
119     } else {
120         return (_timing/2) / 74.1;
121     }
122 }
123