changes on drivers according to comments from DavidS

This commit is contained in:
Sergej Scheiermann
2017-04-11 16:01:35 +02:00
committed by Lorenz Meier
parent da31e6e0b5
commit 641a03510c
14 changed files with 422 additions and 1396 deletions
+22 -27
View File
@@ -180,42 +180,37 @@ fi
if ver hwcmp PX4FMU_V4 if ver hwcmp PX4FMU_V4
then then
# External I2C bus # External I2C bus
#if hmc5883 -C -T -X start if hmc5883 -C -T -X start
#then then
#fi fi
#if lis3mdl -R 2 start if lis3mdl -R 2 start
#then then
#fi fi
# Internal SPI bus is rotated 90 deg yaw # Internal SPI bus is rotated 90 deg yaw
#if hmc5883 -C -T -S -R 2 start if hmc5883 -C -T -S -R 2 start
#then then
#fi fi
# Internal SPI bus ICM-20608-G is rotated 90 deg yaw # Internal SPI bus ICM-20608-G is rotated 90 deg yaw
#if mpu6000 -R 2 -T 20608 start if mpu6000 -R 2 -T 20608 start
#then then
#fi fi
# Internal SPI bus ICM-20602-G is rotated 90 deg yaw
#if mpu6000 -R 2 -T 20602 start
#then
#fi
# Start either MPU9250 or BMI160. They are both connected to the same SPI bus and use the same # Start either MPU9250 or BMI160. They are both connected to the same SPI bus and use the same
# chip select pin. There are different boards with either one of them and the WHO_AM_I register # chip select pin. There are different boards with either one of them and the WHO_AM_I register
# will prevent the incorrect driver from a successful initialization. # will prevent the incorrect driver from a successful initialization.
# Internal SPI bus mpu9250 is rotated 90 deg yaw # Internal SPI bus mpu9250 is rotated 90 deg yaw
#if mpu9250 -R 2 start if mpu9250 -R 2 start
#then then
#fi fi
# Internal SPI bus BMI160 # Internal SPI bus BMI160
#if bmi160 start if bmi160 start
#then then
#fi fi
# Start either ICM2060X or BMI055. They are both connected to the same SPI bus and use the same # Start either ICM2060X or BMI055. They are both connected to the same SPI bus and use the same
# chip select pin. There are different boards with either one of them and the WHO_AM_I register # chip select pin. There are different boards with either one of them and the WHO_AM_I register
@@ -231,13 +226,13 @@ then
then then
fi fi
# expansion i2c used for BMM150 # expansion i2c used for BMM150 rotated by 90deg
if bmm150 start if bmm150 -R 2 start
then then
fi fi
# expansion i2c used for BMP285 # expansion i2c used for BMP280
if bmp285 start if bmp280 -I start
then then
fi fi
fi fi
@@ -53,7 +53,6 @@ set(config_module_list
drivers/bmi160 drivers/bmi160
drivers/bmi055 drivers/bmi055
drivers/bmm150 drivers/bmm150
drivers/bmp285
drivers/tap_esc drivers/tap_esc
drivers/iridiumsbd drivers/iridiumsbd
+123 -32
View File
@@ -71,7 +71,8 @@ void start(bool external_bus, enum Rotation rotation)
if (*g_dev_ptr != nullptr) if (*g_dev_ptr != nullptr)
/* if already started, the still command succeeded */ /* if already started, the still command succeeded */
{ {
errx(0, "already started"); PX4_ERR("already started");
exit(0);
} }
/* create the driver */ /* create the driver */
@@ -79,7 +80,8 @@ void start(bool external_bus, enum Rotation rotation)
#if defined(PX4_SPI_BUS_EXT) && defined(PX4_SPIDEV_EXT_BMI) #if defined(PX4_SPI_BUS_EXT) && defined(PX4_SPIDEV_EXT_BMI)
*g_dev_ptr = new BMM150(PX4_I2C_BUS_BMM150, path, external_bus, rotation); *g_dev_ptr = new BMM150(PX4_I2C_BUS_BMM150, path, external_bus, rotation);
#else #else
errx(0, "External I2C not available"); PX4_ERR("External I2C not available");
exit(0);
#endif #endif
} else { } else {
@@ -117,7 +119,9 @@ fail:
*g_dev_ptr = nullptr; *g_dev_ptr = nullptr;
} }
errx(1, "driver start failed"); PX4_ERR("driver start failed");
exit(1);
} }
@@ -132,37 +136,41 @@ void test(bool external_bus)
/* get the driver */ /* get the driver */
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd < 0) if (fd < 0) {
err(1, "%s open failed (try 'bmm150 start' if the driver is not running)", PX4_ERR("%s open failed (try 'bmm150 start' if the driver is not running)",
path); path);
exit(1);
}
/* reset to Max polling rate*/ /* reset to Max polling rate*/
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX) < 0) { if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX) < 0) {
err(1, "reset to Max polling rate"); PX4_ERR("reset to Max polling rate");
exit(1);
} }
/* do a simple demand read */ /* do a simple demand read */
sz = read(fd, &m_report, sizeof(m_report)); sz = read(fd, &m_report, sizeof(m_report));
if (sz != sizeof(m_report)) { if (sz != sizeof(m_report)) {
err(1, "immediate mag read failed"); PX4_ERR("immediate mag read failed");
exit(1);
} }
warnx("single read"); PX4_WARN("single read");
warnx("time: %lld", m_report.timestamp); PX4_WARN("time: %lld", m_report.timestamp);
warnx("mag x: \t%8.4f\t", (double)m_report.x); PX4_WARN("mag x: \t%8.4f\t", (double)m_report.x);
warnx("mag y: \t%8.4f\t", (double)m_report.y); PX4_WARN("mag y: \t%8.4f\t", (double)m_report.y);
warnx("mag z: \t%8.4f\t", (double)m_report.z); PX4_WARN("mag z: \t%8.4f\t", (double)m_report.z);
warnx("mag x: \t%d\traw 0x%0x", (short)m_report.x_raw, (unsigned short)m_report.x_raw); PX4_WARN("mag x: \t%d\traw 0x%0x", (short)m_report.x_raw, (unsigned short)m_report.x_raw);
warnx("mag y: \t%d\traw 0x%0x", (short)m_report.y_raw, (unsigned short)m_report.y_raw); PX4_WARN("mag y: \t%d\traw 0x%0x", (short)m_report.y_raw, (unsigned short)m_report.y_raw);
warnx("mag z: \t%d\traw 0x%0x", (short)m_report.z_raw, (unsigned short)m_report.z_raw); PX4_WARN("mag z: \t%d\traw 0x%0x", (short)m_report.z_raw, (unsigned short)m_report.z_raw);
errx(0, "PASS"); PX4_ERR("PASS");
exit(0);
} }
void void
reset(bool external_bus) reset(bool external_bus)
{ {
@@ -170,15 +178,18 @@ reset(bool external_bus)
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
err(1, "failed "); PX4_ERR("failed");
exit(1);
} }
if (ioctl(fd, SENSORIOCRESET, 0) < 0) { if (ioctl(fd, SENSORIOCRESET, 0) < 0) {
err(1, "driver reset failed"); PX4_ERR("driver reset failed");
exit(1);
} }
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
err(1, "driver poll restart failed"); PX4_ERR("driver poll restart failed");
exit(1);
} }
exit(0); exit(0);
@@ -191,7 +202,8 @@ info(bool external_bus)
BMM150 **g_dev_ptr = external_bus ? &g_dev_ext : &g_dev_int; BMM150 **g_dev_ptr = external_bus ? &g_dev_ext : &g_dev_int;
if (*g_dev_ptr == nullptr) { if (*g_dev_ptr == nullptr) {
errx(1, "driver not running"); PX4_ERR("driver not running");
exit(1);
} }
printf("state @ %p\n", *g_dev_ptr); printf("state @ %p\n", *g_dev_ptr);
@@ -210,7 +222,8 @@ regdump(bool external_bus)
BMM150 **g_dev_ptr = external_bus ? &g_dev_ext : &g_dev_int; BMM150 **g_dev_ptr = external_bus ? &g_dev_ext : &g_dev_int;
if (*g_dev_ptr == nullptr) { if (*g_dev_ptr == nullptr) {
errx(1, "driver not running"); PX4_ERR("driver not running");
exit(1);
} }
printf("regdump @ %p\n", *g_dev_ptr); printf("regdump @ %p\n", *g_dev_ptr);
@@ -222,9 +235,9 @@ regdump(bool external_bus)
void void
usage() usage()
{ {
warnx("missing command: try 'start', 'info', 'test', 'stop',\n'reset', 'regdump'"); PX4_WARN("missing command: try 'start', 'info', 'test', 'stop',\n'reset', 'regdump'");
warnx("options:"); PX4_WARN("options:");
warnx(" -X (external bus)"); PX4_WARN(" -X (external bus)");
} }
@@ -236,9 +249,11 @@ BMM150 :: BMM150(int bus, const char *path, bool external, enum Rotation rotatio
I2C("BMM150", path, bus, BMM150_SLAVE_ADDRESS, BMM150_BUS_SPEED), I2C("BMM150", path, bus, BMM150_SLAVE_ADDRESS, BMM150_BUS_SPEED),
_work{}, _work{},
_external(false), _external(false),
_running(false),
_call_interval(0), _call_interval(0),
_report{0}, _report{0},
_reports(nullptr), _reports(nullptr),
_collect_phase(false),
_scale{}, _scale{},
_range_scale(0.01), /* default range scale from from uT to gauss */ _range_scale(0.01), /* default range scale from from uT to gauss */
_topic(nullptr), _topic(nullptr),
@@ -261,6 +276,8 @@ BMM150 :: BMM150(int bus, const char *path, bool external, enum Rotation rotatio
_sample_perf(perf_alloc(PC_ELAPSED, "bmm150_read")), _sample_perf(perf_alloc(PC_ELAPSED, "bmm150_read")),
_bad_transfers(perf_alloc(PC_COUNT, "bmm150_bad_transfers")), _bad_transfers(perf_alloc(PC_COUNT, "bmm150_bad_transfers")),
_good_transfers(perf_alloc(PC_COUNT, "bmm150_good_transfers")), _good_transfers(perf_alloc(PC_COUNT, "bmm150_good_transfers")),
_measure_perf(perf_alloc(PC_ELAPSED, "bmp280_measure")),
_comms_errors(perf_alloc(PC_COUNT, "bmp280_comms_errors")),
_duplicates(perf_alloc(PC_COUNT, "bmm150_duplicates")), _duplicates(perf_alloc(PC_COUNT, "bmm150_duplicates")),
_rotation(rotation), _rotation(rotation),
_got_duplicate(false), _got_duplicate(false),
@@ -298,10 +315,17 @@ BMM150 :: ~BMM150()
unregister_class_devname(MAG_BASE_DEVICE_PATH, _class_instance); unregister_class_devname(MAG_BASE_DEVICE_PATH, _class_instance);
} }
if (_topic != nullptr) {
orb_unadvertise(_topic);
}
/* delete the perf counter */ /* delete the perf counter */
perf_free(_sample_perf); perf_free(_sample_perf);
perf_free(_bad_transfers); perf_free(_bad_transfers);
perf_free(_good_transfers); perf_free(_good_transfers);
perf_free(_measure_perf);
perf_free(_comms_errors);
perf_free(_duplicates); perf_free(_duplicates);
} }
@@ -333,7 +357,7 @@ int BMM150::init()
/* check id*/ /* check id*/
if (read_reg(BMM150_CHIP_ID_REG) != BMM150_CHIP_ID) { if (read_reg(BMM150_CHIP_ID_REG) != BMM150_CHIP_ID) {
warnx("id of magnetometer is not: 0x%02x", BMM150_CHIP_ID); PX4_WARN("id of magnetometer is not: 0x%02x", BMM150_CHIP_ID);
return -EIO; return -EIO;
} }
@@ -345,7 +369,15 @@ int BMM150::init()
_class_instance = register_class_devname(MAG_BASE_DEVICE_PATH); _class_instance = register_class_devname(MAG_BASE_DEVICE_PATH);
collect(); if (measure()) {
return -EIO;
}
up_udelay(10000);
if (collect()) {
return -EIO;
}
/* advertise sensor topic, measure manually to initialize valid report */ /* advertise sensor topic, measure manually to initialize valid report */
struct mag_report mrb; struct mag_report mrb;
@@ -356,7 +388,7 @@ int BMM150::init()
&_orb_class_instance, (is_external()) ? ORB_PRIO_HIGH : ORB_PRIO_MAX); &_orb_class_instance, (is_external()) ? ORB_PRIO_HIGH : ORB_PRIO_MAX);
if (_topic == nullptr) { if (_topic == nullptr) {
warnx("ADVERT FAIL"); PX4_WARN("ADVERT FAIL");
} }
out: out:
@@ -381,18 +413,21 @@ void
BMM150::start() BMM150::start()
{ {
/* reset the report ring and state machine */ /* reset the report ring and state machine */
_collect_phase = false;
_running = true;
_reports->flush(); _reports->flush();
/* schedule a cycle to start things */ /* schedule a cycle to start things */
work_queue(HPWORK, &_work, (worker_t)&BMM150::cycle_trampoline, this, 1); work_queue(HPWORK, &_work, (worker_t)&BMM150::cycle_trampoline, this, 1);
} }
void void
BMM150::stop() BMM150::stop()
{ {
_running = false;
work_cancel(HPWORK, &_work); work_cancel(HPWORK, &_work);
} }
ssize_t ssize_t
@@ -430,6 +465,15 @@ BMM150::read(struct file *filp, char *buffer, size_t buflen)
do { do {
_reports->flush(); _reports->flush();
/* trigger a measurement */
if (OK != measure()) {
ret = -EIO;
break;
}
/* wait for it to complete */
usleep(BMM150_CONVERSION_INTERVAL);
/* run the collection phase */ /* run the collection phase */
if (OK != collect()) { if (OK != collect()) {
ret = -EIO; ret = -EIO;
@@ -455,21 +499,65 @@ BMM150::cycle_trampoline(void *arg)
/* make measurement */ /* make measurement */
dev->cycle(); dev->cycle();
} }
void void
BMM150::cycle() BMM150::cycle()
{ {
if (_collect_phase) {
collect(); collect();
work_queue(HPWORK, &_work, (worker_t)&BMM150::cycle_trampoline, this, USEC2TICK(BMM150_CONVERSION_INTERVAL)); unsigned wait_gap = _call_interval - USEC2TICK(BMM150_CONVERSION_INTERVAL);
if ((wait_gap != 0) && (_running)) {
work_queue(HPWORK, &_work, (worker_t)&BMM150::cycle_trampoline, this,
wait_gap); //need to wait some time before new measurement
return;
}
} }
measure();
if ((_running)) {
/* schedule a fresh cycle call when the measurement is done */
work_queue(HPWORK,
&_work,
(worker_t)&BMM150::cycle_trampoline,
this,
USEC2TICK(BMM150_CONVERSION_INTERVAL));
}
}
int
BMM150::measure()
{
_collect_phase = true;
perf_begin(_measure_perf);
/* start measure */
int ret = set_power_mode(BMM150_FORCED_MODE);
if (ret != OK) {
perf_count(_comms_errors);
perf_cancel(_measure_perf);
return -EIO;
}
perf_end(_measure_perf);
return OK;
}
int int
BMM150::collect() BMM150::collect()
{ {
_collect_phase = false;
bool mag_notify = true; bool mag_notify = true;
uint8_t mag_data[8], status; uint8_t mag_data[8], status;
uint16_t resistance, lsb, msb, msblsb; uint16_t resistance, lsb, msb, msblsb;
@@ -537,6 +625,7 @@ BMM150::collect()
mrb.z_raw == 0 && mrb.z_raw == 0 &&
resistance == 0) { resistance == 0) {
// all zero data - probably a I2C bus error // all zero data - probably a I2C bus error
perf_count(_comms_errors);
perf_count(_bad_transfers); perf_count(_bad_transfers);
perf_end(_sample_perf); perf_end(_sample_perf);
return -EIO; return -EIO;
@@ -601,6 +690,9 @@ BMM150::collect()
// should use this sensor based on whether it has had failures // should use this sensor based on whether it has had failures
mrb.error_count = perf_event_count(_bad_transfers); mrb.error_count = perf_event_count(_bad_transfers);
// apply user specified rotation
rotate_3f(_rotation, mrb.x, mrb.y, mrb.z);
/* Scaling the data */ /* Scaling the data */
mrb.x = ((mrb.x * _range_scale) - _scale.x_offset) * _scale.x_scale; mrb.x = ((mrb.x * _range_scale) - _scale.x_offset) * _scale.x_scale;
@@ -1029,7 +1121,6 @@ BMM150::print_info()
(double)(1.0f / _range_scale)); (double)(1.0f / _range_scale));
printf("\n"); printf("\n");
} }
void void
+15 -7
View File
@@ -17,6 +17,7 @@
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <px4_log.h>
#include <systemlib/perf_counter.h> #include <systemlib/perf_counter.h>
#include <systemlib/err.h> #include <systemlib/err.h>
@@ -128,9 +129,10 @@
/* Preset modes - Data rates */ /* Preset modes - Data rates */
#define BMM150_LOWPOWER_DR BMM150_DATA_RATE_30HZ #define BMM150_LOWPOWER_DR BMM150_DATA_RATE_30HZ
#define BMM150_REGULAR_DR BMM150_DATA_RATE_30HZ #define BMM150_REGULAR_DR BMM150_DATA_RATE_30HZ
#define BMM150_HIGHACCURACY_DR BMM150_DATA_RATE_25HZ #define BMM150_HIGHACCURACY_DR BMM150_DATA_RATE_20HZ
#define BMM150_ENHANCED_DR BMM150_DATA_RATE_10HZ #define BMM150_ENHANCED_DR BMM150_DATA_RATE_10HZ
/* Power modes value definitions */ /* Power modes value definitions */
#define BMM150_NORMAL_MODE 0x00 #define BMM150_NORMAL_MODE 0x00
#define BMM150_FORCED_MODE 0x02 #define BMM150_FORCED_MODE 0x02
@@ -201,6 +203,11 @@ public:
virtual ssize_t read(struct file *filp, char *buffer, size_t buflen); virtual ssize_t read(struct file *filp, char *buffer, size_t buflen);
virtual int ioctl(struct file *filp, int cmd, unsigned long arg); virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
/**
* Stop automatic measurement.
*/
void stop();
/** /**
* Diagnostics - print some basic information about the driver. * Diagnostics - print some basic information about the driver.
*/ */
@@ -215,6 +222,8 @@ private:
work_s _work; work_s _work;
bool _external; bool _external;
bool _running;
/* altitude conversion calibration */ /* altitude conversion calibration */
unsigned _call_interval; unsigned _call_interval;
@@ -222,6 +231,8 @@ private:
struct mag_report _report; struct mag_report _report;
ringbuffer::RingBuffer *_reports; ringbuffer::RingBuffer *_reports;
bool _collect_phase;
struct mag_calibration_s _scale; struct mag_calibration_s _scale;
float _range_scale; float _range_scale;
@@ -251,6 +262,8 @@ private:
perf_counter_t _sample_perf; perf_counter_t _sample_perf;
perf_counter_t _bad_transfers; perf_counter_t _bad_transfers;
perf_counter_t _good_transfers; perf_counter_t _good_transfers;
perf_counter_t _measure_perf;
perf_counter_t _comms_errors;
perf_counter_t _duplicates; perf_counter_t _duplicates;
enum Rotation _rotation; enum Rotation _rotation;
@@ -265,12 +278,7 @@ private:
*/ */
void start(); void start();
/** int measure(); //start measure
* Stop automatic measurement.
*/
void stop();
int collect(); //get results and publish int collect(); //get results and publish
static void cycle_trampoline(void *arg); static void cycle_trampoline(void *arg);
+1
View File
@@ -37,6 +37,7 @@ px4_add_module(
COMPILE_FLAGS COMPILE_FLAGS
SRCS SRCS
bmp280_spi.cpp bmp280_spi.cpp
bmp280_i2c.cpp
bmp280.cpp bmp280.cpp
DEPENDS DEPENDS
platforms__common platforms__common
+94 -53
View File
@@ -51,6 +51,7 @@
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <px4_log.h>
#include <nuttx/arch.h> #include <nuttx/arch.h>
#include <nuttx/wqueue.h> #include <nuttx/wqueue.h>
@@ -104,6 +105,8 @@ public:
private: private:
bmp280::IBMP280 *_interface; bmp280::IBMP280 *_interface;
bool _running;
uint8_t _curr_ctrl; uint8_t _curr_ctrl;
struct work_s _work; struct work_s _work;
@@ -150,6 +153,7 @@ extern "C" __EXPORT int bmp280_main(int argc, char *argv[]);
BMP280::BMP280(bmp280::IBMP280 *interface, const char *path) : BMP280::BMP280(bmp280::IBMP280 *interface, const char *path) :
CDev("BMP280", path), CDev("BMP280", path),
_interface(interface), _interface(interface),
_running(false),
_report_ticks(0), _report_ticks(0),
_reports(nullptr), _reports(nullptr),
_collect_phase(false), _collect_phase(false),
@@ -161,8 +165,6 @@ BMP280::BMP280(bmp280::IBMP280 *interface, const char *path) :
_measure_perf(perf_alloc(PC_ELAPSED, "bmp280_measure")), _measure_perf(perf_alloc(PC_ELAPSED, "bmp280_measure")),
_comms_errors(perf_alloc(PC_COUNT, "bmp280_comms_errors")) _comms_errors(perf_alloc(PC_COUNT, "bmp280_comms_errors"))
{ {
_device_id.devid_s.devtype = DRV_BARO_DEVTYPE_BMP280;
// work_cancel in stop_cycle called from the dtor will explode if we don't do this... // work_cancel in stop_cycle called from the dtor will explode if we don't do this...
memset(&_work, 0, sizeof(_work)); memset(&_work, 0, sizeof(_work));
} }
@@ -181,6 +183,11 @@ BMP280::~BMP280()
delete _reports; delete _reports;
} }
if (_baro_topic != nullptr) {
orb_unadvertise(_baro_topic);
}
// free perf counters // free perf counters
perf_free(_sample_perf); perf_free(_sample_perf);
perf_free(_measure_perf); perf_free(_measure_perf);
@@ -221,7 +228,7 @@ BMP280::init()
/* check id*/ /* check id*/
if (_interface->get_reg(BPM280_ADDR_ID) != BPM280_VALUE_ID) { if (_interface->get_reg(BPM280_ADDR_ID) != BPM280_VALUE_ID) {
warnx("id of your baro is not: 0x%02x", BPM280_VALUE_ID); PX4_WARN("id of your baro is not: 0x%02x", BPM280_VALUE_ID);
return -EIO; return -EIO;
} }
@@ -270,7 +277,7 @@ BMP280::init()
&_orb_class_instance, _interface->is_external() ? ORB_PRIO_HIGH : ORB_PRIO_DEFAULT); &_orb_class_instance, _interface->is_external() ? ORB_PRIO_HIGH : ORB_PRIO_DEFAULT);
if (_baro_topic == nullptr) { if (_baro_topic == nullptr) {
warnx("failed to create sensor_baro publication"); PX4_WARN("failed to create sensor_baro publication");
return -ENOMEM; return -ENOMEM;
} }
@@ -440,6 +447,7 @@ BMP280::start_cycle()
/* reset the report ring and state machine */ /* reset the report ring and state machine */
_collect_phase = false; _collect_phase = false;
_running = true;
_reports->flush(); _reports->flush();
/* schedule a cycle to start things */ /* schedule a cycle to start things */
@@ -449,6 +457,7 @@ BMP280::start_cycle()
void void
BMP280::stop_cycle() BMP280::stop_cycle()
{ {
_running = false;
work_cancel(HPWORK, &_work); work_cancel(HPWORK, &_work);
} }
@@ -467,7 +476,7 @@ BMP280::cycle()
collect(); collect();
unsigned wait_gap = _report_ticks - _max_mesure_ticks; unsigned wait_gap = _report_ticks - _max_mesure_ticks;
if (wait_gap != 0) { if ((wait_gap != 0) && (_running)) {
work_queue(HPWORK, &_work, (worker_t)&BMP280::cycle_trampoline, this, work_queue(HPWORK, &_work, (worker_t)&BMP280::cycle_trampoline, this,
wait_gap); //need to wait some time before new measurement wait_gap); //need to wait some time before new measurement
return; return;
@@ -476,7 +485,10 @@ BMP280::cycle()
} }
measure(); measure();
if (_running) {
work_queue(HPWORK, &_work, (worker_t)&BMP280::cycle_trampoline, this, _max_mesure_ticks); work_queue(HPWORK, &_work, (worker_t)&BMP280::cycle_trampoline, this, _max_mesure_ticks);
}
} }
@@ -542,8 +554,6 @@ BMP280::collect()
report.temperature = _T; report.temperature = _T;
report.pressure = _P / 100.0f; // to mbar report.pressure = _P / 100.0f; // to mbar
/* Get device ID */
report.device_id = _device_id.devid;
/* altitude calculations based on http://www.kansasflyer.org/index.asp?nav=Avi&sec=Alti&tab=Theory&pg=1 */ /* altitude calculations based on http://www.kansasflyer.org/index.asp?nav=Avi&sec=Alti&tab=Theory&pg=1 */
@@ -620,10 +630,10 @@ struct bmp280_bus_option {
{ BMP280_BUS_SPI_INTERNAL, "/dev/bmp280_spi_int", &bmp280_spi_interface, PX4_SPI_BUS_SENSORS, PX4_SPIDEV_BARO, false, NULL }, { BMP280_BUS_SPI_INTERNAL, "/dev/bmp280_spi_int", &bmp280_spi_interface, PX4_SPI_BUS_SENSORS, PX4_SPIDEV_BARO, false, NULL },
#endif #endif
#ifdef PX4_I2C_OBDEV_BMP280 #ifdef PX4_I2C_OBDEV_BMP280
{ BMP280_BUS_I2C_INTERNAL, "/dev/bmp280_i2c_int", nullptr, PX4_I2C_BUS_ONBOARD, PX4_I2C_OBDEV_BMP280, false, NULL }, { BMP280_BUS_I2C_INTERNAL, "/dev/bmp280_i2c_int", &bmp280_i2c_interface, PX4_I2C_BUS_EXPANSION, PX4_I2C_OBDEV_BMP280, false, NULL },
#endif #endif
#if defined(PX4_I2C_BUS_EXPANSION) && defined(PX4_I2C_EXT_OBDEV_BMP280) #if defined(PX4_I2C_BUS_EXPANSION) && defined(PX4_I2C_EXT_OBDEV_BMP280)
{ BMP280_BUS_I2C_EXTERNAL, "/dev/bmp280_i2c_ext", nullptr, PX4_I2C_BUS_EXPANSION, PX4_I2C_EXT_OBDEV_BMP280, true, NULL }, { BMP280_BUS_I2C_EXTERNAL, "/dev/bmp280_i2c_ext", &bmp280_i2c_interface, PX4_I2C_BUS_EXPANSION, PX4_I2C_EXT_OBDEV_BMP280, true, NULL },
#endif #endif
}; };
#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0])) #define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
@@ -645,14 +655,15 @@ bool
start_bus(struct bmp280_bus_option &bus) start_bus(struct bmp280_bus_option &bus)
{ {
if (bus.dev != nullptr) { if (bus.dev != nullptr) {
errx(1, "bus option already started"); PX4_ERR("bus option already started");
exit(1);
} }
bmp280::IBMP280 *interface = bus.interface_constructor(bus.busnum, bus.device, bus.external); bmp280::IBMP280 *interface = bus.interface_constructor(bus.busnum, bus.device, bus.external);
if (interface->init() != OK) { if (interface->init() != OK) {
delete interface; delete interface;
warnx("no device on bus %u", (unsigned)bus.busid); PX4_WARN("no device on bus %u", (unsigned)bus.busid);
return false; return false;
} }
@@ -668,14 +679,17 @@ start_bus(struct bmp280_bus_option &bus)
/* set the poll rate to default, starts automatic data collection */ /* set the poll rate to default, starts automatic data collection */
if (fd == -1) { if (fd == -1) {
errx(1, "can't open baro device"); PX4_ERR("can't open baro device");
exit(1);
} }
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
close(fd); close(fd);
errx(1, "failed setting default poll rate"); PX4_ERR("failed setting default poll rate");
exit(1);
} }
close(fd); close(fd);
return true; return true;
} }
@@ -708,7 +722,9 @@ start(enum BMP280_BUS busid)
} }
if (!started) { if (!started) {
errx(1, "driver start failed"); PX4_WARN("bus option number is %d", i);
PX4_ERR("driver start failed");
exit(1);
} }
// one or more drivers started OK // one or more drivers started OK
@@ -728,7 +744,8 @@ struct bmp280_bus_option &find_bus(enum BMP280_BUS busid)
} }
} }
errx(1, "bus %u not started", (unsigned)busid); PX4_ERR("bus %u not started", (unsigned)busid);
exit(1);
} }
/** /**
@@ -749,30 +766,34 @@ test(enum BMP280_BUS busid)
fd = open(bus.devpath, O_RDONLY); fd = open(bus.devpath, O_RDONLY);
if (fd < 0) { if (fd < 0) {
err(1, "open failed (try 'bmp280 start' if the driver is not running)"); PX4_ERR("open failed (try 'bmp280 start' if the driver is not running)");
exit(1);
} }
/* do a simple demand read */ /* do a simple demand read */
sz = read(fd, &report, sizeof(report)); sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) { if (sz != sizeof(report)) {
err(1, "immediate read failed"); PX4_ERR("immediate read failed");
exit(1);
} }
warnx("single read"); PX4_WARN("single read");
warnx("pressure: %10.4f", (double)report.pressure); PX4_WARN("pressure: %10.4f", (double)report.pressure);
warnx("altitude: %11.4f", (double)report.altitude); PX4_WARN("altitude: %11.4f", (double)report.altitude);
warnx("temperature: %8.4f", (double)report.temperature); PX4_WARN("temperature: %8.4f", (double)report.temperature);
warnx("time: %lld", report.timestamp); PX4_WARN("time: %lld", report.timestamp);
/* set the queue depth to 10 */ /* set the queue depth to 10 */
if (OK != ioctl(fd, SENSORIOCSQUEUEDEPTH, 10)) { if (OK != ioctl(fd, SENSORIOCSQUEUEDEPTH, 10)) {
errx(1, "failed to set queue depth"); PX4_ERR("failed to set queue depth");
exit(1);
} }
/* start the sensor polling at 2Hz */ /* start the sensor polling at 2Hz */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, 2)) { if (OK != ioctl(fd, SENSORIOCSPOLLRATE, 2)) {
errx(1, "failed to set 2Hz poll rate"); PX4_ERR("failed to set 2Hz poll rate");
exit(1);
} }
/* read the sensor 5x and report each value */ /* read the sensor 5x and report each value */
@@ -785,25 +806,28 @@ test(enum BMP280_BUS busid)
ret = poll(&fds, 1, 2000); ret = poll(&fds, 1, 2000);
if (ret != 1) { if (ret != 1) {
errx(1, "timed out waiting for sensor data"); PX4_ERR("timed out waiting for sensor data");
exit(1);
} }
/* now go get it */ /* now go get it */
sz = read(fd, &report, sizeof(report)); sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) { if (sz != sizeof(report)) {
err(1, "periodic read failed"); PX4_ERR("periodic read failed");
exit(1);
} }
warnx("periodic read %u", i); PX4_WARN("periodic read %u", i);
warnx("pressure: %10.4f", (double)report.pressure); PX4_WARN("pressure: %10.4f", (double)report.pressure);
warnx("altitude: %11.4f", (double)report.altitude); PX4_WARN("altitude: %11.4f", (double)report.altitude);
warnx("temperature K: %8.4f", (double)report.temperature); PX4_WARN("temperature K: %8.4f", (double)report.temperature);
warnx("time: %lld", report.timestamp); PX4_WARN("time: %lld", report.timestamp);
} }
close(fd); close(fd);
errx(0, "PASS"); PX4_ERR("PASS");
exit(0);
} }
/** /**
@@ -818,15 +842,18 @@ reset(enum BMP280_BUS busid)
fd = open(bus.devpath, O_RDONLY); fd = open(bus.devpath, O_RDONLY);
if (fd < 0) { if (fd < 0) {
err(1, "failed "); PX4_ERR("failed ");
exit(1);
} }
if (ioctl(fd, SENSORIOCRESET, 0) < 0) { if (ioctl(fd, SENSORIOCRESET, 0) < 0) {
err(1, "driver reset failed"); PX4_ERR("driver reset failed");
exit(1);
} }
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) { if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
err(1, "driver poll restart failed"); PX4_ERR("driver poll restart failed");
exit(1);
} }
exit(0); exit(0);
@@ -842,7 +869,7 @@ info()
struct bmp280_bus_option &bus = bus_options[i]; struct bmp280_bus_option &bus = bus_options[i];
if (bus.dev != nullptr) { if (bus.dev != nullptr) {
warnx("%s", bus.devpath); PX4_WARN("%s", bus.devpath);
bus.dev->print_info(); bus.dev->print_info();
} }
} }
@@ -866,12 +893,14 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
fd = open(bus.devpath, O_RDONLY); fd = open(bus.devpath, O_RDONLY);
if (fd < 0) { if (fd < 0) {
err(1, "open failed (try 'bmp280 start' if the driver is not running)"); PX4_ERR("open failed (try 'bmp280 start' if the driver is not running)");
exit(1);
} }
/* start the sensor polling at max */ /* start the sensor polling at max */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX)) { if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX)) {
errx(1, "failed to set poll rate"); PX4_ERR("failed to set poll rate");
exit(1);
} }
/* average a few measurements */ /* average a few measurements */
@@ -888,14 +917,16 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
ret = poll(&fds, 1, 1000); ret = poll(&fds, 1, 1000);
if (ret != 1) { if (ret != 1) {
errx(1, "timed out waiting for sensor data"); PX4_ERR("timed out waiting for sensor data");
exit(1);
} }
/* now go get it */ /* now go get it */
sz = read(fd, &report, sizeof(report)); sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) { if (sz != sizeof(report)) {
err(1, "sensor read failed"); PX4_ERR("sensor read failed");
exit(1);
} }
pressure += report.pressure; pressure += report.pressure;
@@ -910,17 +941,18 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
const float g = 9.80665f; /* gravity constant in m/s/s */ const float g = 9.80665f; /* gravity constant in m/s/s */
const float R = 287.05f; /* ideal gas constant in J/kg/K */ const float R = 287.05f; /* ideal gas constant in J/kg/K */
warnx("averaged pressure %10.4fkPa at %um", (double)pressure, altitude); PX4_WARN("averaged pressure %10.4fkPa at %um", (double)pressure, altitude);
p1 = pressure * (powf(((T1 + (a * (float)altitude)) / T1), (g / (a * R)))); p1 = pressure * (powf(((T1 + (a * (float)altitude)) / T1), (g / (a * R))));
warnx("calculated MSL pressure %10.4fkPa", (double)p1); PX4_WARN("calculated MSL pressure %10.4fkPa", (double)p1);
/* save as integer Pa */ /* save as integer Pa */
p1 *= 1000.0f; p1 *= 1000.0f;
if (ioctl(fd, BAROIOCSMSLPRESSURE, (unsigned long)p1) != OK) { if (ioctl(fd, BAROIOCSMSLPRESSURE, (unsigned long)p1) != OK) {
err(1, "BAROIOCSMSLPRESSURE"); PX4_ERR("BAROIOCSMSLPRESSURE");
exit(1);
} }
close(fd); close(fd);
@@ -930,16 +962,23 @@ calibrate(unsigned altitude, enum BMP280_BUS busid)
void void
usage() usage()
{ {
warnx("missing command: try 'start', 'info', 'test', 'test2', 'reset', 'calibrate'"); PX4_WARN("missing command: try 'start', 'info', 'test', 'test2', 'reset', 'calibrate'");
warnx("options:"); PX4_WARN("options:");
warnx(" -X (external I2C bus TODO)"); PX4_WARN(" -X (external I2C bus TODO)");
warnx(" -I (internal I2C bus TODO)"); PX4_WARN(" -I (internal I2C bus TODO)");
warnx(" -S (external SPI bus)"); PX4_WARN(" -S (external SPI bus)");
warnx(" -s (internal SPI bus)"); PX4_WARN(" -s (internal SPI bus)");
} }
} // namespace } // namespace
bmp280::IBMP280::~IBMP280()
{
}
int int
bmp280_main(int argc, char *argv[]) bmp280_main(int argc, char *argv[])
{ {
@@ -951,12 +990,12 @@ bmp280_main(int argc, char *argv[])
switch (ch) { switch (ch) {
case 'X': case 'X':
busid = BMP280_BUS_I2C_EXTERNAL; busid = BMP280_BUS_I2C_EXTERNAL;
errx(1, "not supported yet");
break; break;
case 'I': case 'I':
busid = BMP280_BUS_I2C_INTERNAL; busid = BMP280_BUS_I2C_INTERNAL;
errx(1, "not supported yet"); //PX4_ERR("not supported yet");
//exit(1);
break; break;
case 'S': case 'S':
@@ -1008,7 +1047,8 @@ bmp280_main(int argc, char *argv[])
*/ */
if (!strcmp(verb, "calibrate")) { if (!strcmp(verb, "calibrate")) {
if (argc < 2) { if (argc < 2) {
errx(1, "missing altitude"); PX4_ERR("missing altitude");
exit(1);
} }
long altitude = strtol(argv[optind + 1], nullptr, 10); long altitude = strtol(argv[optind + 1], nullptr, 10);
@@ -1016,5 +1056,6 @@ bmp280_main(int argc, char *argv[])
bmp280::calibrate(altitude, busid); bmp280::calibrate(altitude, busid);
} }
errx(1, "unrecognized command, try 'start', 'test', 'reset' or 'info'"); PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'");
exit(1);
} }
+21 -19
View File
@@ -50,28 +50,28 @@
#define BPM280_VALUE_ID 0x58 /* chip id */ #define BPM280_VALUE_ID 0x58 /* chip id */
#define BPM280_VALUE_RESET 0xB6 /* reset */ #define BPM280_VALUE_RESET 0xB6 /* reset */
#define BPM280_STATUS_MEASURING 1<<3 /* if in process of measure */ #define BPM280_STATUS_MEASURING (1<<3) /* if in process of measure */
#define BPM280_STATUS_COPING 1<<0 /* if in process of data copy */ #define BPM280_STATUS_COPING (1<<0) /* if in process of data copy */
#define BPM280_CTRL_P0 0x0<<2 /* no p measure */ #define BPM280_CTRL_P0 (0x0<<2) /* no p measure */
#define BPM280_CTRL_P1 0x1<<2 #define BPM280_CTRL_P1 (0x1<<2)
#define BPM280_CTRL_P2 0x2<<2 #define BPM280_CTRL_P2 (0x2<<2)
#define BPM280_CTRL_P4 0x3<<2 #define BPM280_CTRL_P4 (0x3<<2)
#define BPM280_CTRL_P8 0x4<<2 #define BPM280_CTRL_P8 (0x4<<2)
#define BPM280_CTRL_P16 0x5<<2 #define BPM280_CTRL_P16 (0x5<<2)
#define BPM280_CTRL_T0 0x0<<5 /* no t measure */ #define BPM280_CTRL_T0 (0x0<<5) /* no t measure */
#define BPM280_CTRL_T1 0x1<<5 #define BPM280_CTRL_T1 (0x1<<5)
#define BPM280_CTRL_T2 0x2<<5 #define BPM280_CTRL_T2 (0x2<<5)
#define BPM280_CTRL_T4 0x3<<5 #define BPM280_CTRL_T4 (0x3<<5)
#define BPM280_CTRL_T8 0x4<<5 #define BPM280_CTRL_T8 (0x4<<5)
#define BPM280_CTRL_T16 0x5<<5 #define BPM280_CTRL_T16 (0x5<<5)
#define BPM280_CONFIG_F0 0x0<<2 /* no filter */ #define BPM280_CONFIG_F0 (0x0<<2) /* no filter */
#define BPM280_CONFIG_F2 0x1<<2 #define BPM280_CONFIG_F2 (0x1<<2)
#define BPM280_CONFIG_F4 0x2<<2 #define BPM280_CONFIG_F4 (0x2<<2)
#define BPM280_CONFIG_F8 0x3<<2 #define BPM280_CONFIG_F8 (0x3<<2)
#define BPM280_CONFIG_F16 0x4<<2 #define BPM280_CONFIG_F16 (0x4<<2)
#define BPM280_CTRL_MODE_SLEEP 0x0 #define BPM280_CTRL_MODE_SLEEP 0x0
@@ -146,6 +146,8 @@ public:
} /* namespace */ } /* namespace */
/* interface factories */ /* interface factories */
extern bmp280::IBMP280 *bmp280_spi_interface(uint8_t busnum, uint8_t device, bool external); extern bmp280::IBMP280 *bmp280_spi_interface(uint8_t busnum, uint8_t device, bool external);
extern bmp280::IBMP280 *bmp280_i2c_interface(uint8_t busnum, uint8_t device, bool external); extern bmp280::IBMP280 *bmp280_i2c_interface(uint8_t busnum, uint8_t device, bool external);
+142
View File
@@ -0,0 +1,142 @@
/****************************************************************************
*
* Copyright (c) 2016 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.
*
****************************************************************************/
/**
* @file bmp280_spi.cpp
*
* SPI interface for BMP280
*/
#include <px4_config.h>
#include <drivers/bmp280/bmp280.h>
#include <drivers/device/i2c.h>
#include "board_config.h"
#if defined(PX4_I2C_OBDEV_BMP280) || defined(PX4_I2C_EXT_OBDEV_BMP280)
class BMP280_I2C: public device::I2C, public bmp280::IBMP280
{
public:
BMP280_I2C(uint8_t bus, uint8_t device, bool external);
~BMP280_I2C();
bool is_external();
int init();
uint8_t get_reg(uint8_t addr);
int set_reg(uint8_t value, uint8_t addr);
bmp280::data_s *get_data(uint8_t addr);
bmp280::calibration_s *get_calibration(uint8_t addr);
private:
struct bmp280::calibration_s _cal;
struct bmp280::data_s _data;
bool _external;
};
bmp280::IBMP280 *bmp280_i2c_interface(uint8_t busnum, uint8_t device, bool external)
{
return new BMP280_I2C(busnum, device, external);
}
BMP280_I2C::BMP280_I2C(uint8_t bus, uint8_t device, bool external) :
I2C("BMP280_I2C", nullptr, bus, device, 100 * 1000)
{
_external = external;
}
BMP280_I2C::~BMP280_I2C()
{
}
bool BMP280_I2C::is_external()
{
return _external;
}
int BMP280_I2C::init()
{
return I2C::init();
}
uint8_t BMP280_I2C::get_reg(uint8_t addr)
{
uint8_t cmd[2] = { (uint8_t)(addr), 0};
transfer(&cmd[0], 1, &cmd[1], 1);
return cmd[1];
}
int BMP280_I2C::set_reg(uint8_t value, uint8_t addr)
{
uint8_t cmd[2] = { (uint8_t)(addr), value};
return transfer(cmd, sizeof(cmd), nullptr, 0);
}
bmp280::data_s *BMP280_I2C::get_data(uint8_t addr)
{
const uint8_t cmd = (uint8_t)(addr);
if (transfer(&cmd, sizeof(cmd), (uint8_t *)&_data, sizeof(struct bmp280::data_s)) == OK) {
return (&_data);
} else {
return nullptr;
}
}
bmp280::calibration_s *BMP280_I2C::get_calibration(uint8_t addr)
{
const uint8_t cmd = (uint8_t)(addr) ;
if (transfer(&cmd, sizeof(cmd), (uint8_t *)&_cal, sizeof(struct bmp280::calibration_s)) == OK) {
return &(_cal);
} else {
return nullptr;
}
}
#endif /* PX4_I2C_OBDEV_BMP280 || PX4_I2C_EXT_OBDEV_BMP280 */
-3
View File
@@ -93,9 +93,6 @@ BMP280_SPI::BMP280_SPI(uint8_t bus, spi_dev_e device, bool external) :
_external = external; _external = external;
} }
bmp280::IBMP280::~IBMP280()
{
}
BMP280_SPI::~BMP280_SPI() BMP280_SPI::~BMP280_SPI()
{ {
-44
View File
@@ -1,44 +0,0 @@
############################################################################
#
# Copyright (c) 2016 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__bmp285
MAIN bmp285
COMPILE_FLAGS
SRCS
bmp285_main.cpp
bmp285.cpp
DEPENDS
platforms__common
)
# vim: set noet ft=cmake fenc=utf-8 ff=unix :
File diff suppressed because it is too large Load Diff
-278
View File
@@ -1,278 +0,0 @@
/****************************************************************************
*
* Copyright (C) 2012-2016 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.
*
****************************************************************************/
/**
* @file bmp285.h
*
* Shared defines for the bmp285 driver.
*/
#ifndef BMP285_HPP_
#define BMP285_HPP_
#include <px4_config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>
#include <nuttx/arch.h>
#include <nuttx/wqueue.h>
#include <nuttx/clock.h>
#include <arch/board/board.h>
#include <board_config.h>
#include <drivers/device/device.h>
#include <drivers/drv_baro.h>
#include <drivers/drv_hrt.h>
#include <drivers/device/ringbuffer.h>
#include <drivers/device/i2c.h>
#include <systemlib/perf_counter.h>
#include <systemlib/err.h>
#pragma once
#define BMP285_DEVICE_PATH_PRESSURE "/dev/bmp285_i2c_int"
#define BMP285_DEVICE_PATH_PRESSURE_EXT "/dev/bmp285_i2c_ext"
#define BMP285_SLAVE_ADDRESS PX4_I2C_OBDEV_BMP285
#define BMP285_BUS_SPEED 1000*100
#define BPM285_ADDR_CAL 0x88 /* address of 12x 2 bytes calibration data */
#define BPM285_ADDR_DATA 0xF7 /* address of 2x 3 bytes p-t data */
#define BPM285_ADDR_CONFIG 0xF5 /* configuration */
#define BPM285_ADDR_CTRL 0xF4 /* controll */
#define BPM285_ADDR_STATUS 0xF3 /* state */
#define BPM285_ADDR_RESET 0xE0 /* reset */
#define BPM285_ADDR_ID 0xD0 /* id */
#define BPM285_VALUE_ID 0x58 /* chip id */
#define BPM285_VALUE_RESET 0xB6 /* reset */
#define BPM285_STATUS_MEASURING 1<<3 /* if in process of measure */
#define BPM285_STATUS_COPING 1<<0 /* if in process of data copy */
#define BPM285_CTRL_P0 0x0<<2 /* no p measure */
#define BPM285_CTRL_P1 0x1<<2
#define BPM285_CTRL_P2 0x2<<2
#define BPM285_CTRL_P4 0x3<<2
#define BPM285_CTRL_P8 0x4<<2
#define BPM285_CTRL_P16 0x5<<2
#define BPM285_CTRL_T0 0x0<<5 /* no t measure */
#define BPM285_CTRL_T1 0x1<<5
#define BPM285_CTRL_T2 0x2<<5
#define BPM285_CTRL_T4 0x3<<5
#define BPM285_CTRL_T8 0x4<<5
#define BPM285_CTRL_T16 0x5<<5
#define BPM285_CONFIG_F0 0x0<<2 /* no filter */
#define BPM285_CONFIG_F2 0x1<<2
#define BPM285_CONFIG_F4 0x2<<2
#define BPM285_CONFIG_F8 0x3<<2
#define BPM285_CONFIG_F16 0x4<<2
#define BMP285_CTRL_T_SB0 0x0<<5
#define BMP285_CTRL_T_SB1 0x1<<5
#define BMP285_CTRL_T_SB2 0x2<<5
#define BMP285_CTRL_T_SB3 0x3<<5
#define BMP285_CTRL_T_SB4 0x4<<5
#define BMP285_CTRL_T_SB5 0x5<<5
#define BMP285_CTRL_T_SB6 0x6<<5
#define BMP285_CTRL_T_SB7 0x7<<5
#define BPM285_CTRL_MODE_SLEEP 0x0
#define BPM285_CTRL_MODE_FORCE 0x1 /* on demand, goes to sleep after */
#define BPM285_CTRL_MODE_NORMAL 0x3
#define BPM285_MT_INIT 6400 /* max measure time of initial p + t in us */
#define BPM285_MT 2300 /* max measure time of p or t in us */
namespace bmp285
{
#pragma pack(push,1)
struct calibration_s {
uint16_t t1;
int16_t t2;
int16_t t3;
uint16_t p1;
int16_t p2;
int16_t p3;
int16_t p4;
int16_t p5;
int16_t p6;
int16_t p7;
int16_t p8;
int16_t p9;
}; //calibration data
struct data_s {
uint8_t p_msb;
uint8_t p_lsb;
uint8_t p_xlsb;
uint8_t t_msb;
uint8_t t_lsb;
uint8_t t_xlsb;
}; // data
#pragma pack(pop)
struct fcalibration_s {
float t1;
float t2;
float t3;
float p1;
float p2;
float p3;
float p4;
float p5;
float p6;
float p7;
float p8;
float p9;
};
} /* namespace */
/*
* BMP285 internal constants and data structures.
*/
class BMP285 : public device::I2C
{
public:
BMP285(int bus, uint16_t address, const char *path, bool external);
~BMP285();
bool is_external();
virtual int init();
virtual ssize_t read(struct file *filp, char *buffer, size_t buflen);
virtual int ioctl(struct file *filp, int cmd, unsigned long arg);
/**
* Diagnostics - print some basic information about the driver.
*/
void print_info();
bmp285::data_s *get_data(uint8_t reg);
bmp285::calibration_s *get_calibration(uint8_t addr);
private:
uint8_t _curr_ctrl;
struct work_s _work;
bool _external;
unsigned _report_ticks; // 0 - no cycling, otherwise period of sending a report
unsigned _max_mesure_ticks; //ticks needed to measure
ringbuffer::RingBuffer *_reports;
bool _collect_phase;
/* altitude conversion calibration */
unsigned _msl_pressure; /* in Pa */
orb_advert_t _baro_topic;
int _orb_class_instance;
int _class_instance;
perf_counter_t _sample_perf;
perf_counter_t _measure_perf;
perf_counter_t _comms_errors;
perf_counter_t _buffer_overflows;
struct bmp285::calibration_s _cal;
struct bmp285::data_s _data;
struct bmp285::fcalibration_s _fcal; //pre processed calibration constants
float _P; /* in Pa */
float _T; /* in K */
/* periodic execution helpers */
void start_cycle();
void stop_cycle();
void cycle(); //main execution
static void cycle_trampoline(void *arg);
/**
* Read a register from the BMP285
*
* @param The register to read.
* @return The value that was read.
*/
uint8_t read_reg(unsigned reg);
/**
* Write a register in the BMP285
*
* @param reg The register to write.
* @param value The new value to write.
* @return The value returned after transfer of data.
*/
int write_reg(unsigned reg, uint8_t value);
int measure(); //start measure
int collect(); //get results and publish
};
#endif
-370
View File
@@ -1,370 +0,0 @@
#include <drivers/bmp285/bmp285.hpp>
/** driver 'main' command */
extern "C" { __EXPORT int bmp285_main(int argc, char *argv[]); }
/**
* Local functions in support of the shell command.
*/
namespace bmp285
{
BMP285 *g_dev_int; // on internal bus
BMP285 *g_dev_ext; // on external bus
void start(bool);
void test(bool);
void reset(bool);
void info(bool);
void calibrate(bool, unsigned);
void usage();
/**
* Start the driver.
*
* This function only returns if the driver is up and running
* or failed to detect the sensor.
*/
void
start(bool external_bus)
{
int fd;
BMP285 **g_dev_ptr = external_bus ? &g_dev_ext : &g_dev_int;
const char *path = external_bus ? BMP285_DEVICE_PATH_PRESSURE_EXT : BMP285_DEVICE_PATH_PRESSURE;
if (*g_dev_ptr != nullptr)
/* if already started, the still command succeeded */
{
errx(0, "already started");
}
/* create the driver */
if (external_bus) {
#if defined(PX4_I2C_BUS_EXPANSION) && defined(PX4_I2C_EXT_OBDEV_BMP285)
*g_dev_ptr = new BMP285(PX4_I2C_BUS_EXPANSION, BMP285_SLAVE_ADDRESS, path, external_bus);
#else
errx(0, "External SPI not available");
#endif
} else {
*g_dev_ptr = new BMP285(PX4_I2C_BUS_BMP285, BMP285_SLAVE_ADDRESS, path, external_bus);
}
if (*g_dev_ptr == nullptr) {
goto fail;
}
if (OK != (*g_dev_ptr)->init()) {
goto fail;
}
/* set the poll rate to default, starts automatic data collection */
fd = open(path, O_RDONLY);
if (fd < 0) {
goto fail;
}
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
goto fail;
}
close(fd);
exit(0);
fail:
if (*g_dev_ptr != nullptr) {
delete (*g_dev_ptr);
*g_dev_ptr = 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(bool external_bus)
{
const char *path = external_bus ? BMP285_DEVICE_PATH_PRESSURE_EXT : BMP285_DEVICE_PATH_PRESSURE;
struct baro_report report;
ssize_t sz;
int ret;
/* get the driver */
int fd = open(path, O_RDONLY);
if (fd < 0) {
err(1, "open failed (try 'bmp285 start' if the driver is not running)");
}
/* do a simple demand read */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "immediate read failed");
}
warnx("single read");
warnx("pressure: %10.4f", (double)report.pressure);
warnx("altitude: %11.4f", (double)report.altitude);
warnx("temperature: %8.4f", (double)report.temperature);
warnx("time: %lld", report.timestamp);
/* set the queue depth to 10 */
if (OK != ioctl(fd, SENSORIOCSQUEUEDEPTH, 10)) {
errx(1, "failed to set queue depth");
}
/* start the sensor polling at 2Hz */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, 2)) {
errx(1, "failed to set 2Hz poll rate");
}
/* read the sensor 5x and report each value */
for (unsigned i = 0; i < 5; i++) {
struct pollfd fds;
/* wait for data to be ready */
fds.fd = fd;
fds.events = POLLIN;
ret = poll(&fds, 1, 2000);
if (ret != 1) {
errx(1, "timed out waiting for sensor data");
}
/* now go get it */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "periodic read failed");
}
warnx("periodic read %u", i);
warnx("pressure: %10.4f", (double)report.pressure);
warnx("altitude: %11.4f", (double)report.altitude);
warnx("temperature K: %8.4f", (double)report.temperature);
warnx("time: %lld", report.timestamp);
}
close(fd);
errx(0, "PASS");
}
/**
* Reset the driver.
*/
void
reset(bool external_bus)
{
const char *path = external_bus ? BMP285_DEVICE_PATH_PRESSURE_EXT : BMP285_DEVICE_PATH_PRESSURE;
int fd = open(path, O_RDONLY);
if (fd < 0) {
err(1, "failed ");
}
if (ioctl(fd, SENSORIOCRESET, 0) < 0) {
err(1, "driver reset failed");
}
if (ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT) < 0) {
err(1, "driver poll restart failed");
}
close(fd);
exit(0);
}
/**
* Print a little info about the driver.
*/
void
info(bool external_bus)
{
BMP285 **g_dev_ptr = external_bus ? &g_dev_ext : &g_dev_int;
if (*g_dev_ptr == nullptr) {
errx(1, "driver not running");
}
printf("state @ %p\n", *g_dev_ptr);
(*g_dev_ptr)->print_info();
exit(0);
}
/**
* Calculate actual MSL pressure given current altitude
*/
void
calibrate(bool external_bus, unsigned altitude)
{
const char *path = external_bus ? BMP285_DEVICE_PATH_PRESSURE_EXT : BMP285_DEVICE_PATH_PRESSURE;
struct baro_report report;
float pressure;
float p1;
/* get the driver */
int fd = open(path, O_RDONLY);
if (fd < 0) {
err(1, "open failed (try 'bmp285 start' if the driver is not running)");
}
/* start the sensor polling at max */
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_MAX)) {
errx(1, "failed to set poll rate");
}
/* average a few measurements */
pressure = 0.0f;
for (unsigned i = 0; i < 20; i++) {
struct pollfd fds;
int ret;
ssize_t sz;
/* wait for data to be ready */
fds.fd = fd;
fds.events = POLLIN;
ret = poll(&fds, 1, 1000);
if (ret != 1) {
errx(1, "timed out waiting for sensor data");
}
/* now go get it */
sz = read(fd, &report, sizeof(report));
if (sz != sizeof(report)) {
err(1, "sensor read failed");
}
pressure += report.pressure;
}
pressure /= 20; /* average */
pressure /= 10; /* scale from millibar to kPa */
/* tropospheric properties (0-11km) for standard atmosphere */
const float T1 = 15.0 + 273.15; /* temperature at base height in Kelvin */
const float a = -6.5 / 1000; /* temperature gradient in degrees per metre */
const float g = 9.80665f; /* gravity constant in m/s/s */
const float R = 287.05f; /* ideal gas constant in J/kg/K */
warnx("averaged pressure %10.4fkPa at %um", (double)pressure, altitude);
float value = (powf(((T1 + (a * (float)altitude)) / T1), (g / (a * R))));
warnx("power value is %10.4f", (double)value);
p1 = pressure * (powf(((T1 + (a * (float)altitude)) / T1), (g / (a * R))));
warnx("calculated MSL pressure %10.4fkPa", (double)p1);
/* save as integer Pa */
p1 *= 1000.0f;
warnx("p1 is %10.4fkPa at %um", (double)p1);
if (ioctl(fd, BAROIOCSMSLPRESSURE, (unsigned long)p1) != OK) {
err(1, "BAROIOCSMSLPRESSURE");
}
close(fd);
exit(0);
}
void
usage()
{
warnx("missing command: try 'start', 'test', 'info',\n'reset', 'calibrate'");
warnx("options:");
warnx(" -X (external bus)");
}
} // namespace
int
bmp285_main(int argc, char *argv[])
{
bool external_bus = false;
int ch;
/* jump over start/off/etc and look at options first */
while ((ch = getopt(argc, argv, "X:")) != EOF) {
switch (ch) {
case 'X':
external_bus = true;
break;
default:
bmp285::usage();
exit(0);
}
}
const char *verb = argv[optind];
/*
* Start/load the driver.
*/
if (!strcmp(verb, "start")) {
bmp285::start(external_bus);
}
/*
* Test the driver/device.
*/
if (!strcmp(verb, "test")) {
bmp285::test(external_bus);
}
/*
* Reset the driver.
*/
if (!strcmp(verb, "reset")) {
bmp285::reset(external_bus);
}
/*
* Print driver information.
*/
if (!strcmp(verb, "info")) {
bmp285::info(external_bus);
}
/*
* Perform MSL pressure calibration given an altitude in metres
*/
if (!strcmp(verb, "calibrate")) {
if (argc < 2) {
errx(1, "missing altitude");
}
long altitude = strtol(argv[optind + 1], nullptr, 10);
bmp285::calibrate(external_bus, altitude);
}
bmp285::usage();
exit(1);
}
+1 -2
View File
@@ -156,7 +156,6 @@
#define PX4_I2C_BUS_EXPANSION 1 #define PX4_I2C_BUS_EXPANSION 1
#define PX4_I2C_BUS_LED PX4_I2C_BUS_EXPANSION #define PX4_I2C_BUS_LED PX4_I2C_BUS_EXPANSION
#define PX4_I2C_BUS_BMM150 PX4_I2C_BUS_EXPANSION #define PX4_I2C_BUS_BMM150 PX4_I2C_BUS_EXPANSION
#define PX4_I2C_BUS_BMP285 PX4_I2C_BUS_EXPANSION
/* Devices on the external bus. /* Devices on the external bus.
* *
@@ -166,7 +165,7 @@
#define PX4_I2C_OBDEV_HMC5883 0x1e #define PX4_I2C_OBDEV_HMC5883 0x1e
#define PX4_I2C_OBDEV_LIS3MDL 0x1e #define PX4_I2C_OBDEV_LIS3MDL 0x1e
#define PX4_I2C_OBDEV_BMM150 0x10 #define PX4_I2C_OBDEV_BMM150 0x10
#define PX4_I2C_OBDEV_BMP285 0x76 #define PX4_I2C_OBDEV_BMP280 0x76
/* /*
* ADC channels * ADC channels