samples: sensor: mpu6050: convert to devicetree

Add a sample for the MPU6050 that demonstrates on-demand and triggered
display of all sensor data.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2019-12-22 08:48:34 -06:00 committed by Carles Cufí
commit 649500aeca
6 changed files with 229 additions and 0 deletions

View file

@ -0,0 +1,12 @@
#
# Copyright (c) 2019 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(mpu6050)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,59 @@
.. _mpu6050:
MPU6050: Invensense Motion Tracking Device
##########################################
Description
***********
This sample application periodically (0.5 Hz) measures the sensor
temperature, acceleration, and angular velocity, displaying the values
on the console along with a timestamp since startup.
When triggered mode is enabled the measurements are displayed at the
rate they are produced by the sensor.
Wiring
*******
This sample uses an external breakout for the sensor. A devicetree
overlay must be provided to identify the I2C bus and GPIO used to
control the sensor.
Building and Running
********************
After providing a devicetree overlay that specifies the sensor location,
build this sample app using:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/mpu6050
:board: nrf52_pca10040
:goals: build flash
Sample Output
=============
.. code-block:: console
*** Booting Zephyr OS build zephyr-v2.1.0-576-g4b38659b0661 ***
[0:00:00.008]:23.6359 Cel
accel -5.882554 -6.485893 5.868188 m/s/s
gyro 0.014522 0.002264 -0.036905 rad/s
[0:00:02.020]:23.6359 Cel
accel -5.841853 -6.435615 5.911283 m/s/s
gyro 0.017852 0.001199 -0.034640 rad/s
[0:00:04.032]:23.6829 Cel
accel -5.930438 -6.461951 6.009446 m/s/s
gyro 0.012923 0.002131 -0.037171 rad/s
[0:00:06.044]:23.6359 Cel
accel -5.884948 -6.524200 5.961562 m/s/s
gyro 0.012390 -0.001732 -0.045964 rad/s
[0:00:08.056]:35.7712 Cel
accel -5.863400 -12.872426 -0.154427 m/s/s
gyro -0.034373 -0.034373 -0.034373 rad/s
[0:00:10.068]:23.6829 Cel
accel -5.906496 -6.461951 5.899312 m/s/s
gyro 0.015321 -0.000399 -0.039169 rad/s
<repeats endlessly>

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
mpu6050@68 {
compatible = "invensense,mpu6050";
reg = <0x68>;
status = "okay";
label = "MPU6050";
int-gpios = <&gpio0 11 0>;
};
};

View file

@ -0,0 +1,10 @@
#
# Copyright (c) 2019 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_MPU6050=y
CONFIG_MPU6050_TRIGGER_NONE=y

View file

@ -0,0 +1,13 @@
#
# Copyright (c) 2019 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#
sample:
name: MPU6050 Sensor Sample
tests:
sample.sensor.mpu6050:
build_only: true
platform_whitelist: nrf52_pca10040
tags: sensors

View file

@ -0,0 +1,120 @@
/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <stdio.h>
static const char *now_str(void)
{
static char buf[16]; /* ...HH:MM:SS.MMM */
u32_t now = k_uptime_get_32();
unsigned int ms = now % MSEC_PER_SEC;
unsigned int s;
unsigned int min;
unsigned int h;
now /= MSEC_PER_SEC;
s = now % 60U;
now /= 60U;
min = now % 60U;
now /= 60U;
h = now;
snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u",
h, min, s, ms);
return buf;
}
static int process_mpu6050(struct device *dev)
{
struct sensor_value temperature;
struct sensor_value accel[3];
struct sensor_value gyro[3];
int rc = sensor_sample_fetch(dev);
if (rc == 0) {
rc = sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ,
accel);
}
if (rc == 0) {
rc = sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ,
gyro);
}
if (rc == 0) {
rc = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP,
&temperature);
}
if (rc == 0) {
printf("[%s]:%g Cel\n"
" accel % f % f % f m/s/s\n"
" gyro % f % f % f rad/s\n",
now_str(),
sensor_value_to_double(&temperature),
sensor_value_to_double(&accel[0]),
sensor_value_to_double(&accel[1]),
sensor_value_to_double(&accel[2]),
sensor_value_to_double(&gyro[0]),
sensor_value_to_double(&gyro[1]),
sensor_value_to_double(&gyro[2]));
} else {
printf("sample fetch/get failed: %d\n", rc);
}
return rc;
}
#ifdef CONFIG_MPU6050_TRIGGER
static struct sensor_trigger trigger;
static void handle_mpu6050_drdy(struct device *dev,
struct sensor_trigger *trig)
{
int rc = process_mpu6050(dev);
if (rc != 0) {
printf("cancelling trigger due to failure: %d\n", rc);
(void)sensor_trigger_set(dev, trig, NULL);
return;
}
}
#endif /* CONFIG_MPU6050_TRIGGER */
void main(void)
{
const char *const label = DT_INST_0_INVENSENSE_MPU6050_LABEL;
struct device *mpu6050 = device_get_binding(label);
if (!mpu6050) {
printf("Failed to find sensor %s\n", label);
return;
}
#ifdef CONFIG_MPU6050_TRIGGER
trigger = (struct sensor_trigger) {
.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_ALL,
};
if (sensor_trigger_set(mpu6050, &trigger,
handle_mpu6050_drdy) < 0) {
printf("Cannot configure trigger\n");
return;
};
printk("Configured for triggered sampling.\n");
#endif
while (!IS_ENABLED(CONFIG_MPU6050_TRIGGER)) {
int rc = process_mpu6050(mpu6050);
if (rc != 0) {
break;
}
k_sleep(K_SECONDS(2));
}
/* triggered runs with its own thread after exit */
}