mirror of
https://github.com/eclipse/upm.git
synced 2025-07-01 09:21:12 +03:00
HCSR04: Modifying implementation and adding examples
Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
@ -29,15 +29,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
upm::HCSR04 *sonar = NULL;
|
||||
int shouldRun = true;
|
||||
|
||||
void
|
||||
sig_handler(int signo)
|
||||
{
|
||||
printf("got signal\n");
|
||||
if (signo == SIGINT) {
|
||||
printf("exiting application\n");
|
||||
sonar->m_doWork = 1;
|
||||
shouldRun = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,20 +43,20 @@ sig_handler(int signo)
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
sonar = new upm::HCSR04(5, 6);
|
||||
upm::HCSR04 *sonar = new upm::HCSR04(2, 4);
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
sleep(1);
|
||||
|
||||
for(;;){
|
||||
while(shouldRun){
|
||||
std::cout << "get distance" << std::endl;
|
||||
double distance = sonar->getDistance(CM);
|
||||
double distance = sonar->getDistance(HCSR04_CM);
|
||||
std::cout << "distance " << distance << std::endl;
|
||||
sleep(5);
|
||||
sleep(2);
|
||||
}
|
||||
std::cout << "Exiting... " << std::endl;
|
||||
|
||||
delete sonar;
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [Interesting]
|
||||
|
48
examples/c/hcsr04.c
Normal file
48
examples/c/hcsr04.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Author: Abhishek Malik <abhishek.malik@intel.com>
|
||||
* Copyright (c) 2017 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "upm_utilities.h"
|
||||
#include "hcsr04.h"
|
||||
|
||||
int main() {
|
||||
hcsr04_context dev = hcsr04_init(2,4);
|
||||
if(dev == NULL) {
|
||||
printf("Unable to intialize the sensor\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
double distance;
|
||||
while(1) {
|
||||
distance = hcsr04_get_distance(dev, HCSR04_CM);
|
||||
printf("Distance detected: %f\n", distance);
|
||||
upm_delay(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -23,17 +23,19 @@
|
||||
*/
|
||||
|
||||
//NOT TESTED!!!
|
||||
import upm_hcsr04.*;
|
||||
|
||||
public class HCSR04Sample {
|
||||
|
||||
// ! [Interesting]
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
upm_hcsr04.HCSR04 sonar = new upm_hcsr04.HCSR04((short) 5, (short) 6);
|
||||
HCSR04 sonar = new HCSR04((short) 2, (short) 4);
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
while (true) {
|
||||
System.out.println("Get distance");
|
||||
double distance = sonar.getDistance(upm_hcsr04.javaupm_hcsr04Constants.CM);
|
||||
double distance = sonar.getDistance(HCSR04_U.swigToEnum(0));
|
||||
System.out.println("Distance: " + distance);
|
||||
|
||||
Thread.sleep(5000);
|
||||
|
41
examples/javascript/hcsr04.js
Normal file
41
examples/javascript/hcsr04.js
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Author: Abhishek Malik <abhishek.malik@intel.com>
|
||||
* Copyright (c) 2017 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.
|
||||
*/
|
||||
|
||||
var HCSR04 = require('jsupm_hcsr04');
|
||||
|
||||
// Instantiate a HCSR04 ultrasonic distance sensor on digital pins D2 and D4
|
||||
var myHCSR04 = new HCSR04.HCSR04(2, 4);
|
||||
|
||||
// Check every second for the presence of a magnetic field (south polarity)
|
||||
setInterval(function()
|
||||
{
|
||||
console.log("Distance: " + myHCSR04.getDistance(0));
|
||||
}, 1000);
|
||||
|
||||
// Print message when exiting
|
||||
process.on('SIGINT', function()
|
||||
{
|
||||
console.log("Exiting...");
|
||||
process.exit(0);
|
||||
});
|
52
examples/python/hcsr04.py
Normal file
52
examples/python/hcsr04.py
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/python
|
||||
# Author: Abhishek Malik <abhishek.malik@intel.com>
|
||||
# Copyright (c) 2017 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.
|
||||
|
||||
from __future__ import print_function
|
||||
import time, sys, signal, atexit
|
||||
from upm import pyupm_hcsr04 as hcsr04
|
||||
|
||||
def main():
|
||||
# Instantiate a HCSR04 Distance sensor on digital pins 2 and 4
|
||||
hcsr04_sensor = hcsr04.HCSR04(2, 4);
|
||||
|
||||
## Exit handlers ##
|
||||
# This function stops python from printing a stacktrace when you hit control-C
|
||||
def SIGINTHandler(signum, frame):
|
||||
raise SystemExit
|
||||
|
||||
# This function lets you run code on exit
|
||||
def exitHandler():
|
||||
print("Exiting")
|
||||
sys.exit(0)
|
||||
|
||||
# Register exit handlers
|
||||
atexit.register(exitHandler)
|
||||
signal.signal(signal.SIGINT, SIGINTHandler)
|
||||
|
||||
# Read the value every second and detect the pressure
|
||||
while(1):
|
||||
print("Distance: {0}".format(hcsr04_sensor.getDistance(hcsr04.HCSR04_CM)))
|
||||
time.sleep(1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user