samples: sensor: Add distance measurement sensor sample
Added sample to measure distance Signed-off-by: Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
This commit is contained in:
parent
94a6c1f0ff
commit
fa732ece86
9
samples/sensor/distance_polling/CMakeLists.txt
Normal file
9
samples/sensor/distance_polling/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Copyright (c) 2025 Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.20.0)
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
project(distance_polling)
|
||||||
|
|
||||||
|
FILE(GLOB app_sources src/*.c)
|
||||||
|
target_sources(app PRIVATE ${app_sources})
|
||||||
43
samples/sensor/distance_polling/README.rst
Normal file
43
samples/sensor/distance_polling/README.rst
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
.. zephyr:code-sample:: distance_polling
|
||||||
|
:name: Generic distance measurement
|
||||||
|
:relevant-api: sensor_interface
|
||||||
|
|
||||||
|
Measure distance to an object using a distance sensor
|
||||||
|
|
||||||
|
Overview
|
||||||
|
********
|
||||||
|
|
||||||
|
This sample application periodically measures the distance of an object and
|
||||||
|
display it, via the console.
|
||||||
|
|
||||||
|
Building and Running
|
||||||
|
********************
|
||||||
|
|
||||||
|
This sample supports up to 5 distance sensors. Each sensor needs to be aliased
|
||||||
|
as ``distanceN`` where ``N`` goes from ``0`` to ``4``. For example:
|
||||||
|
|
||||||
|
.. code-block:: devicetree
|
||||||
|
|
||||||
|
/ {
|
||||||
|
aliases {
|
||||||
|
distance0 = &vl53l1x;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Make sure the aliases are in devicetree, then build and run with:
|
||||||
|
|
||||||
|
.. zephyr-app-commands::
|
||||||
|
:zephyr-app: samples/sensor/distance_polling
|
||||||
|
:board: <board to use>
|
||||||
|
:goals: build flash
|
||||||
|
:compact:
|
||||||
|
|
||||||
|
Sample Output
|
||||||
|
=============
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
vl53l1x: 0.153m
|
||||||
|
vl53l1x: 0.154m
|
||||||
|
vl53l1x: 0.154m
|
||||||
|
vl53l1x: 0.153m
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/ {
|
||||||
|
aliases {
|
||||||
|
distance0 = &vl53l1x;
|
||||||
|
};
|
||||||
|
};
|
||||||
3
samples/sensor/distance_polling/prj.conf
Normal file
3
samples/sensor/distance_polling/prj.conf
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
CONFIG_LOG=y
|
||||||
|
CONFIG_GPIO=y
|
||||||
|
CONFIG_SENSOR=y
|
||||||
14
samples/sensor/distance_polling/sample.yaml
Normal file
14
samples/sensor/distance_polling/sample.yaml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
sample:
|
||||||
|
name: Distance measurement sample
|
||||||
|
common:
|
||||||
|
tags: sensors
|
||||||
|
harness: console
|
||||||
|
harness_config:
|
||||||
|
type: one_line
|
||||||
|
regex:
|
||||||
|
- "^[a-zA-Z0-9_,+-.]*: [0-9\\.]+m$"
|
||||||
|
tests:
|
||||||
|
sample.sensor.distance_polling:
|
||||||
|
filter: dt_alias_exists("distance0")
|
||||||
|
integration_platforms:
|
||||||
|
- arduino_nicla_vision/stm32h747xx/m7 # vl53l1x
|
||||||
56
samples/sensor/distance_polling/src/main.c
Normal file
56
samples/sensor/distance_polling/src/main.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Thiyagarajan Pandiyan <psvthiyagarajan@gmail.com>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/sensor.h>
|
||||||
|
|
||||||
|
#define DISTANCE_ALIAS(i) DT_ALIAS(_CONCAT(distance, i))
|
||||||
|
#define DISTANCE_SENSOR(i, _) \
|
||||||
|
IF_ENABLED(DT_NODE_EXISTS(DISTANCE_ALIAS(i)), (DEVICE_DT_GET(DISTANCE_ALIAS(i)),))
|
||||||
|
|
||||||
|
/* support up to 5 distance sensors */
|
||||||
|
static const struct device *const sensors[] = {LISTIFY(5, DISTANCE_SENSOR, ())};
|
||||||
|
|
||||||
|
static void fetch_and_display(const struct device *sensor)
|
||||||
|
{
|
||||||
|
struct sensor_value distance;
|
||||||
|
int rc = sensor_sample_fetch(sensor);
|
||||||
|
|
||||||
|
if (rc < 0) {
|
||||||
|
printf("ERROR: Fetch failed: %d\n", rc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = sensor_channel_get(sensor, SENSOR_CHAN_DISTANCE, &distance);
|
||||||
|
if (rc < 0) {
|
||||||
|
printf("ERROR: get failed: %d\n", rc);
|
||||||
|
} else {
|
||||||
|
printf("%s: %d.%03dm\n", sensor->name, distance.val1, distance.val2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < ARRAY_SIZE(sensors); i++) {
|
||||||
|
if (!device_is_ready(sensors[i])) {
|
||||||
|
printf("Error: Device \"%s\" is not ready\n", sensors[i]->name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
for (i = 0; i < ARRAY_SIZE(sensors); i++) {
|
||||||
|
fetch_and_display(sensors[i]);
|
||||||
|
k_sleep(K_MSEC(500));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user