adis16477 driver

This commit is contained in:
Daniel Agar
2018-04-27 13:12:11 -04:00
committed by Lorenz Meier
parent 5de5d6ea49
commit ffbd75d1b9
8 changed files with 1332 additions and 0 deletions
+2
View File
@@ -104,6 +104,8 @@
#define DRV_MAG_DEVTYPE_ADIS16448 0x56
#define DRV_GYR_DEVTYPE_ADIS16448 0x57
#define DRV_BARO_DEVTYPE_LPS22HB 0x58
#define DRV_ACC_DEVTYPE_ADIS16477 0x59
#define DRV_GYR_DEVTYPE_ADIS16477 0x60
/*
* ioctl() definitions
+1
View File
@@ -32,6 +32,7 @@
############################################################################
add_subdirectory(adis16448)
add_subdirectory(adis16477)
add_subdirectory(bma180)
add_subdirectory(bmi055)
add_subdirectory(bmi160)
File diff suppressed because it is too large Load Diff
+230
View File
@@ -0,0 +1,230 @@
/****************************************************************************
*
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
*
* 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 PX4 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.
*
****************************************************************************/
/*
* ADIS16477.hpp
*
*/
#ifndef DRIVERS_IMU_ADIS16477_ADIS16477_HPP_
#define DRIVERS_IMU_ADIS16477_ADIS16477_HPP_
#include <drivers/device/ringbuffer.h>
#include <drivers/device/spi.h>
#include <drivers/drv_hrt.h>
#include <drivers/drv_accel.h>
#include <drivers/drv_gyro.h>
#include <mathlib/math/filter/LowPassFilter2p.hpp>
#include <drivers/device/integrator.h>
#include <lib/conversion/rotation.h>
#include <perf/perf_counter.h>
#include <ecl/geo/geo.h>
#define ADIS16477_GYRO_DEFAULT_RATE 250
#define ADIS16477_GYRO_DEFAULT_DRIVER_FILTER_FREQ 30
#define ADIS16477_ACCEL_DEFAULT_RATE 250
#define ADIS16477_ACCEL_DEFAULT_DRIVER_FILTER_FREQ 30
#define ADIS16477_ACCEL_MAX_OUTPUT_RATE 1221
#define ADIS16477_GYRO_MAX_OUTPUT_RATE 1221
class ADIS16477_gyro;
class ADIS16477 : public device::SPI
{
public:
ADIS16477(int bus, const char *path_accel, const char *path_gyro, uint32_t device, enum Rotation rotation);
virtual ~ADIS16477();
virtual int init();
virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
void print_info();
protected:
virtual int probe();
friend class ADIS16477_gyro;
virtual int gyro_ioctl(struct file *filp, int cmd, unsigned long arg);
private:
ADIS16477_gyro *_gyro{nullptr};
uint16_t _product{0}; /** product code */
struct hrt_call _call {};
unsigned _call_interval{0};
struct gyro_calibration_s _gyro_scale {};
// gyro 0.025 °/sec/LSB
float _gyro_range_scale{0.025f};
float _gyro_range_rad_s{math::radians(500.0f)};
struct accel_calibration_s _accel_scale {};
// accel 1.25 mg/LSB
float _accel_range_scale{1.25f * CONSTANTS_ONE_G / 1000.0f};
float _accel_range_m_s2{40.0f * CONSTANTS_ONE_G};
orb_advert_t _accel_topic{nullptr};
int _accel_orb_class_instance{-1};
int _accel_class_instance{-1};
unsigned _sample_rate{100};
perf_counter_t _sample_perf;
perf_counter_t _bad_transfers;
math::LowPassFilter2p _gyro_filter_x{ADIS16477_GYRO_DEFAULT_RATE, ADIS16477_GYRO_DEFAULT_DRIVER_FILTER_FREQ};
math::LowPassFilter2p _gyro_filter_y{ADIS16477_GYRO_DEFAULT_RATE, ADIS16477_GYRO_DEFAULT_DRIVER_FILTER_FREQ};
math::LowPassFilter2p _gyro_filter_z{ADIS16477_GYRO_DEFAULT_RATE, ADIS16477_GYRO_DEFAULT_DRIVER_FILTER_FREQ};
math::LowPassFilter2p _accel_filter_x{ADIS16477_ACCEL_DEFAULT_RATE, ADIS16477_ACCEL_DEFAULT_DRIVER_FILTER_FREQ};
math::LowPassFilter2p _accel_filter_y{ADIS16477_ACCEL_DEFAULT_RATE, ADIS16477_ACCEL_DEFAULT_DRIVER_FILTER_FREQ};
math::LowPassFilter2p _accel_filter_z{ADIS16477_ACCEL_DEFAULT_RATE, ADIS16477_ACCEL_DEFAULT_DRIVER_FILTER_FREQ};
Integrator _accel_int{1000000 / ADIS16477_ACCEL_MAX_OUTPUT_RATE, false};
Integrator _gyro_int{1000000 / ADIS16477_GYRO_MAX_OUTPUT_RATE, true};
enum Rotation _rotation;
perf_counter_t _controller_latency_perf;
#pragma pack(push, 1)
/**
* Report conversation with in the ADIS16477, including command byte and interrupt status.
*/
struct ADISReport {
uint16_t cmd;
uint16_t diag_stat;
int16_t gyro_x;
int16_t gyro_y;
int16_t gyro_z;
int16_t accel_x;
int16_t accel_y;
int16_t accel_z;
uint16_t temp;
uint16_t DATA_CNTR;
uint16_t checksum;
ADISReport():
cmd(0),
diag_stat(0),
gyro_x(0),
gyro_y(0),
gyro_z(0),
accel_x(0),
accel_y(0),
accel_z(0),
temp(0),
DATA_CNTR(0)
{}
};
#pragma pack(pop)
/**
* Start automatic measurement.
*/
void start();
/**
* Stop automatic measurement.
*/
void stop();
/**
* Reset chip.
*
* Resets the chip and measurements ranges, but not scale and offset.
*/
int reset();
/**
* Static trampoline from the hrt_call context; because we don't have a
* generic hrt wrapper yet.
*
* Called by the HRT in interrupt context at the specified rate if
* automatic polling is enabled.
*
* @param arg Instance pointer for the driver that is polling.
*/
static void measure_trampoline(void *arg);
/**
* Fetch measurements from the sensor and update the report buffers.
*/
int measure();
bool publish_accel(const ADISReport &report);
bool publish_gyro(const ADISReport &report);
uint16_t read_reg(uint8_t reg);
void write_reg(uint8_t reg, uint8_t val);
/**
* Measurement self test
*
* @return 0 on success, 1 on failure
*/
int self_test();
/*
set low pass filter frequency
*/
void _set_dlpf_filter(uint16_t frequency_hz);
/*
set IMU to factory default
*/
void _set_factory_default();
/*
set sample rate (approximate) - 1kHz to 5Hz
*/
void _set_sample_rate(uint16_t desired_sample_rate_hz);
/*
set the gyroscope dynamic range
*/
void _set_gyro_dyn_range(uint16_t desired_gyro_dyn_range);
ADIS16477(const ADIS16477 &);
ADIS16477 operator=(const ADIS16477 &);
};
#endif /* DRIVERS_IMU_ADIS16477_ADIS16477_HPP_ */
@@ -0,0 +1,79 @@
/****************************************************************************
*
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
*
* 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 PX4 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 "ADIS16477_gyro.hpp"
ADIS16477_gyro::ADIS16477_gyro(ADIS16477 *parent, const char *path) :
CDev("ADIS16477_gyro", path),
_parent(parent),
_gyro_topic(nullptr),
_gyro_orb_class_instance(-1),
_gyro_class_instance(-1)
{
}
ADIS16477_gyro::~ADIS16477_gyro()
{
if (_gyro_class_instance != -1) {
unregister_class_devname(GYRO_BASE_DEVICE_PATH, _gyro_class_instance);
}
}
int
ADIS16477_gyro::init()
{
int ret = CDev::init();
/* if probe/setup failed, bail now */
if (ret != OK) {
DEVICE_DEBUG("gyro init failed");
return ret;
}
_gyro_class_instance = register_class_devname(GYRO_BASE_DEVICE_PATH);
return ret;
}
int
ADIS16477_gyro::ioctl(struct file *filp, int cmd, unsigned long arg)
{
switch (cmd) {
case DEVIOCGDEVICEID:
return (int)CDev::ioctl(filp, cmd, arg);
break;
default:
return _parent->gyro_ioctl(filp, cmd, arg);
}
}
@@ -0,0 +1,71 @@
/****************************************************************************
*
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
*
* 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 PX4 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.
*
****************************************************************************/
#ifndef DRIVERS_IMU_ADIS16477_ADIS16477_GYRO_HPP_
#define DRIVERS_IMU_ADIS16477_ADIS16477_GYRO_HPP_
#include "ADIS16477.hpp"
#include <drivers/device/CDev.hpp>
#include <drivers/drv_gyro.h>
/**
* Helper class implementing the gyro driver node.
*/
class ADIS16477_gyro : public device::CDev
{
public:
ADIS16477_gyro(ADIS16477 *parent, const char *path);
virtual ~ADIS16477_gyro();
virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
virtual int init();
protected:
friend class ADIS16477;
private:
ADIS16477 *_parent{nullptr};
orb_advert_t _gyro_topic{nullptr};
int _gyro_orb_class_instance{-1};
int _gyro_class_instance{-1};
/* do not allow to copy this class due to pointer data members */
ADIS16477_gyro(const ADIS16477_gyro &);
ADIS16477_gyro operator=(const ADIS16477_gyro &);
};
#endif /* DRIVERS_IMU_ADIS16477_ADIS16477_GYRO_HPP_ */
@@ -0,0 +1,266 @@
/****************************************************************************
*
* Copyright (c) 2018 PX4 Development Team. All rights reserved.
*
* 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 PX4 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 "ADIS16477.hpp"
#define ADIS16477_DEVICE_PATH_ACCEL "/dev/adis16477_accel"
#define ADIS16477_DEVICE_PATH_GYRO "/dev/adis16477_gyro"
extern "C" { __EXPORT int adis16477_main(int argc, char *argv[]); }
/**
* Local functions in support of the shell command.
*/
namespace adis16477
{
ADIS16477 *g_dev;
void start(enum Rotation rotation);
void test();
void reset();
void info();
void usage();
/**
* Start the driver.
*/
void
start(enum Rotation rotation)
{
int fd = -1;
if (g_dev != nullptr)
/* if already started, the still command succeeded */
{
errx(0, "already started");
}
/* create the driver */
#if defined(PX4_SPIDEV_ADIS16477)
g_dev = new ADIS16477(PX4_SPI_BUS_SENSOR1, ADIS16477_DEVICE_PATH_ACCEL, ADIS16477_DEVICE_PATH_GYRO,
PX4_SPIDEV_ADIS16477, rotation);
#else
PX4_ERR("External SPI not available");
exit(0);
#endif
if (g_dev == nullptr) {
goto fail;
}
if (OK != (g_dev)->init()) {
goto fail;
}
/* set the poll rate to default, starts automatic data collection */
fd = px4_open(ADIS16477_DEVICE_PATH_ACCEL, O_RDONLY);
if (fd < 0) {
goto fail;
}
if (px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
goto fail;
}
px4_close(fd);
exit(0);
fail:
if (g_dev != nullptr) {
delete g_dev;
g_dev = nullptr;
}
errx(1, "driver start failed");
}
/**
* Perform some basic functional tests on the driver;
* make sure we can collect data from the sensor in polled
* and automatic modes.
*/
void
test()
{
accel_report a_report;
gyro_report g_report;
ssize_t sz;
/* get the driver */
int fd = px4_open(ADIS16477_DEVICE_PATH_ACCEL, O_RDONLY);
if (fd < 0) {
err(1, "%s open failed", ADIS16477_DEVICE_PATH_ACCEL);
}
/* get the gyro driver */
int fd_gyro = px4_open(ADIS16477_DEVICE_PATH_GYRO, O_RDONLY);
if (fd_gyro < 0) {
err(1, "%s open failed", ADIS16477_DEVICE_PATH_GYRO);
}
/* do a simple demand read */
sz = read(fd, &a_report, sizeof(a_report));
if (sz != sizeof(a_report)) {
PX4_ERR("ret: %d, expected: %d", sz, sizeof(a_report));
err(1, "immediate acc read failed");
}
print_message(a_report);
/* do a simple demand read */
sz = px4_read(fd_gyro, &g_report, sizeof(g_report));
if (sz != sizeof(g_report)) {
warnx("ret: %d, expected: %d", sz, sizeof(g_report));
err(1, "immediate gyro read failed");
}
print_message(g_report);
px4_close(fd_gyro);
px4_close(fd);
reset();
errx(0, "PASS");
}
/**
* Reset the driver.
*/
void
reset()
{
int fd = px4_open(ADIS16477_DEVICE_PATH_ACCEL, O_RDONLY);
if (fd < 0) {
err(1, "open failed");
}
if (px4_ioctl(fd, SENSORIOCRESET, 0) < 0) {
err(1, "driver reset failed");
}
if (px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
err(1, "driver poll restart failed");
}
px4_close(fd);
exit(0);
}
/**
* Print a little info about the driver.
*/
void
info()
{
if (g_dev == nullptr) {
errx(1, "driver not running");
}
printf("state @ %p\n", g_dev);
g_dev->print_info();
exit(0);
}
void
usage()
{
PX4_INFO("missing command: try 'start', 'test', 'info', 'reset'");
PX4_INFO("options:");
PX4_INFO(" -R rotation");
}
}
// namespace
int
adis16477_main(int argc, char *argv[])
{
enum Rotation rotation = ROTATION_NONE;
int ch;
/* start options */
while ((ch = getopt(argc, argv, "R:")) != EOF) {
switch (ch) {
case 'R':
rotation = (enum Rotation)atoi(optarg);
break;
default:
adis16477::usage();
exit(0);
}
}
const char *verb = argv[optind];
/*
* Start/load the driver.
*/
if (!strcmp(verb, "start")) {
adis16477::start(rotation);
}
/*
* Test the driver/device.
*/
if (!strcmp(verb, "test")) {
adis16477::test();
}
/*
* Reset the driver.
*/
if (!strcmp(verb, "reset")) {
adis16477::reset();
}
/*
* Print driver information.
*/
if (!strcmp(verb, "info")) {
adis16477::info();
}
adis16477::usage();
exit(1);
}
+40
View File
@@ -0,0 +1,40 @@
############################################################################
#
# Copyright (c) 2018 PX4 Development Team. All rights reserved.
#
# 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 PX4 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.
#
############################################################################
px4_add_module(
MODULE drivers__imu__adis16477
MAIN adis16477
SRCS
ADIS16477.cpp
ADIS16477_gyro.cpp
ADIS16477_main.cpp
)