mirror of
https://github.com/eclipse/upm.git
synced 2025-07-02 01:41:12 +03:00
hcsr04: Added new sonar module (not working properly yet)
Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
This commit is contained in:
4
src/hcsr04/CMakeLists.txt
Normal file
4
src/hcsr04/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
set (libname "hcsr04")
|
||||
add_library (hcsr04 SHARED hcsr04.cxx)
|
||||
include_directories (${MAA_INCLUDE_DIR})
|
||||
target_link_libraries (hcsr04 ${MAA_LIBRARIES})
|
94
src/hcsr04/hcsr04.cxx
Normal file
94
src/hcsr04/hcsr04.cxx
Normal file
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <functional>
|
||||
|
||||
#include "hcsr04.h"
|
||||
|
||||
using namespace upm;
|
||||
|
||||
HCSR04::HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void)) {
|
||||
maa_result_t error = MAA_SUCCESS;
|
||||
m_name = "HCSR04";
|
||||
|
||||
m_pwmTriggerCtx = maa_pwm_init (triggerPin);
|
||||
if (m_pwmTriggerCtx == NULL) {
|
||||
std::cout << "PWM context is NULL" << std::endl;
|
||||
exit (1);
|
||||
}
|
||||
|
||||
maa_init();
|
||||
m_echoPinCtx = maa_gpio_init(echoPin);
|
||||
if (m_echoPinCtx == NULL) {
|
||||
fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", echoPin);
|
||||
exit (1);
|
||||
}
|
||||
|
||||
maa_gpio_dir(m_echoPinCtx, MAA_GPIO_IN);
|
||||
gpio_edge_t edge = MAA_GPIO_EDGE_BOTH;
|
||||
maa_gpio_isr (m_echoPinCtx, edge, fptr);
|
||||
}
|
||||
|
||||
HCSR04::~HCSR04 () {
|
||||
maa_result_t error = MAA_SUCCESS;
|
||||
|
||||
maa_pwm_close (m_pwmTriggerCtx);
|
||||
error = maa_gpio_close (m_echoPinCtx);
|
||||
if (error != MAA_SUCCESS) {
|
||||
maa_result_print (error);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
HCSR04::getDistance () {
|
||||
maa_pwm_enable (m_pwmTriggerCtx, 1);
|
||||
maa_pwm_period_us (m_pwmTriggerCtx, MAX_PERIOD);
|
||||
maa_pwm_pulsewidth_us (m_pwmTriggerCtx, TRIGGER_PULSE);
|
||||
maa_pwm_enable (m_pwmTriggerCtx, 0);
|
||||
|
||||
m_doWork = 0;
|
||||
m_InterruptCounter = 0;
|
||||
while (!m_doWork) {
|
||||
sleep (1);
|
||||
}
|
||||
|
||||
return m_FallingTimeStamp - m_RisingTimeStamp;
|
||||
}
|
||||
|
||||
void
|
||||
HCSR04::ackEdgeDetected () {
|
||||
struct timeval timer;
|
||||
gettimeofday(&timer, NULL);
|
||||
|
||||
++m_InterruptCounter;
|
||||
if (!(m_InterruptCounter % 2)) {
|
||||
m_FallingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
m_doWork = 1;
|
||||
} else {
|
||||
m_RisingTimeStamp = 1000000 * timer.tv_sec + timer.tv_usec;
|
||||
}
|
||||
}
|
66
src/hcsr04/hcsr04.h
Normal file
66
src/hcsr04/hcsr04.h
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <maa/aio.h>
|
||||
#include <maa/gpio.h>
|
||||
#include <maa/pwm.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define HIGH 1
|
||||
#define LOW 0
|
||||
|
||||
#define MAX_PERIOD 7968
|
||||
#define TRIGGER_PULSE 10
|
||||
|
||||
namespace upm {
|
||||
|
||||
class HCSR04 {
|
||||
public:
|
||||
HCSR04 (uint8_t triggerPin, uint8_t echoPin, void (*fptr)(void));
|
||||
~HCSR04 ();
|
||||
int getDistance ();
|
||||
void ackEdgeDetected ();
|
||||
|
||||
uint8_t m_doWork;
|
||||
|
||||
std::string name()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
private:
|
||||
maa_pwm_context m_pwmTriggerCtx;
|
||||
maa_gpio_context m_echoPinCtx;
|
||||
|
||||
uint8_t m_waitEcho;
|
||||
long m_RisingTimeStamp;
|
||||
long m_FallingTimeStamp;
|
||||
uint8_t m_InterruptCounter;
|
||||
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
}
|
7
src/hcsr04/jsupm_hcsr04.i
Normal file
7
src/hcsr04/jsupm_hcsr04.i
Normal file
@ -0,0 +1,7 @@
|
||||
%module jsupm_hcsr04
|
||||
|
||||
%{
|
||||
#include "hcsr04.h"
|
||||
%}
|
||||
|
||||
%include "hcsr04.h"
|
8
src/hcsr04/pyupm_hcsr04.i
Normal file
8
src/hcsr04/pyupm_hcsr04.i
Normal file
@ -0,0 +1,8 @@
|
||||
%module pyupm_hcsr04
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "hcsr04.h"
|
||||
%{
|
||||
#include "hcsr04.h"
|
||||
%}
|
Reference in New Issue
Block a user