From 94ac7dceab1bcbd52ede16dc05328abb9e88cb5b Mon Sep 17 00:00:00 2001 From: Juan Flores Date: Fri, 28 Jun 2019 15:07:13 -0600 Subject: [PATCH] examples/vl53l1x: Add VL53L1X example demo. --- examples/vl53l1x/.gitignore | 12 +++ examples/vl53l1x/Kconfig | 30 +++++++ examples/vl53l1x/Make.defs | 39 +++++++++ examples/vl53l1x/Makefile | 55 +++++++++++++ examples/vl53l1x/vl53l1x_main.c | 135 ++++++++++++++++++++++++++++++++ 5 files changed, 271 insertions(+) create mode 100644 examples/vl53l1x/.gitignore create mode 100644 examples/vl53l1x/Kconfig create mode 100644 examples/vl53l1x/Make.defs create mode 100644 examples/vl53l1x/Makefile create mode 100644 examples/vl53l1x/vl53l1x_main.c diff --git a/examples/vl53l1x/.gitignore b/examples/vl53l1x/.gitignore new file mode 100644 index 000000000..caa9bdef2 --- /dev/null +++ b/examples/vl53l1x/.gitignore @@ -0,0 +1,12 @@ +/hello +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/vl53l1x/Kconfig b/examples/vl53l1x/Kconfig new file mode 100644 index 000000000..e012ccb6a --- /dev/null +++ b/examples/vl53l1x/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_VL53L1X + tristate "\"VL53L1X\" example" + default n + ---help--- + Enable the \"VL53L1X\" example + +if EXAMPLES_VL53L1X + +config EXAMPLES_VL53L1X_PROGNAME + string "Program name" + default "vl53l1x" + depends on BUILD_LOADABLE + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config EXAMPLES_VL53L1X_PRIORITY + int "VL53L1X priority" + default 100 + +config EXAMPLES_VL53L1X_STACKSIZE + int "VL53L1X stack size" + default 2048 + +endif diff --git a/examples/vl53l1x/Make.defs b/examples/vl53l1x/Make.defs new file mode 100644 index 000000000..71a67479e --- /dev/null +++ b/examples/vl53l1x/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/vl53l1x/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2019 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_VL53L1X),) +CONFIGURED_APPS += examples/vl53l1x +endif diff --git a/examples/vl53l1x/Makefile b/examples/vl53l1x/Makefile new file mode 100644 index 000000000..f0e160172 --- /dev/null +++ b/examples/vl53l1x/Makefile @@ -0,0 +1,55 @@ +############################################################################ +# apps/examples/vl53l1x/Makefile +# +# Copyright (C) 2019 Acutronics Robotics. All rights reserved. +# Author: Acutronics Robotics (Juan Flores Muñoz) +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/Make.defs + +CONFIG_EXAMPLES_VL53L1X_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_EXAMPLES_VL53L1X_STACKSIZE ?= 2048 + +APPNAME = vl53l1x + +PRIORITY = $(CONFIG_EXAMPLES_VL53L1X_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_VL53L1X_STACKSIZE) + +ASRCS = +CSRCS = +MAINSRC = vl53l1x_main.c + +CONFIG_EXAMPLES_VL53L1X_PROGNAME ?= vl53l1x$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_VL53L1X_PROGNAME) + +MODULE = CONFIG_EXAMPLES_VL53L1X + +include $(APPDIR)/Application.mk diff --git a/examples/vl53l1x/vl53l1x_main.c b/examples/vl53l1x/vl53l1x_main.c new file mode 100644 index 000000000..88866e91d --- /dev/null +++ b/examples/vl53l1x/vl53l1x_main.c @@ -0,0 +1,135 @@ +/**************************************************************************** + * examples/vl53l1x/vl53l1x_main.c + * + * Copyright (C) 2019 Acutronics Robotics. All rights reserved. + * Author: Acutronics Robotics (Juan Flores Muñoz) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include + +#include +#include + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void error_message() +{ + printf("VL53L1X help usage:\n"); + printf("vl53l1x -m -c \n "); + printf("measure_mode:\n"); + printf(" short-> Up to 1.3 mtr and higher precision\n"); + printf(" long-> Up to 4 mtr and lower precision\n"); + printf("calibration_distance:\n"); + printf(" Set a object to a known distance. " + "Valid value between 10mm and 400mm or none\n"); +} + +/**************************************************************************** + * hello_main + ****************************************************************************/ + +#if defined(BUILD_MODULE) +int main(int argc, FAR char *argv[]) +#else +int vl53l1x_main(int argc, char *argv[]) +#endif +{ + uint16_t calib_value = 0; /* Variable to save the calibration value */ + int fd; /* VL53L1X file descriptor */ + uint16_t result; /* Variable to save the return data of the + * read. + */ + + if (argc == 1 || (argc == 2) && !strcmp(argv[1], "-h") || argc > 5 || + argc < 4) + { + error_message(); /* Just show the instructions of how to set-up + * this App + */ + return 0; + } + + /* Opening the sensor with read only permission */ + + fd = open("/dev/tof0", O_RDONLY); + + /* Setting the measure mode. */ + + if (!strcmp(argv[2], "long")) + { + ioctl(fd, SNIOC_DISTANCELONG); + } + else if (!strcmp(argv[2], "short")) + { + ioctl(fd, SNIOC_DISTANCESHORT); + } + else + { + error_message(); /* Just show the instructions of how to set-up + * this App + */ + close(fd); + return 0; + } + + /* Set the calibration value */ + + if (argc == 5) + { + calib_value = atoi(argv[4]); + if (calib_value > 10 || calib_value < 4000) + { + ioctl(fd, SNIOC_CALIBRATE, calib_value); + } + else + { + printf("Error: Calibration not set, value out of range\n"); + } + } + + while (1) + { + /* Read the distance every 100 mS. */ + + read(fd, &result, sizeof(result)); + printf("Distance %i mm \n", result); + usleep(100000); + } +}