mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-26 05:33:33 +08:00
mb12xx driver: add -b and -a flags to choose i2c busses and remove nuttx stuff such as exit functions
This commit is contained in:
committed by
Beat Küng
parent
fed6c9ae34
commit
8f4e9228bf
@@ -53,14 +53,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <perf/perf_counter.h>
|
#include <perf/perf_counter.h>
|
||||||
#include <systemlib/err.h>
|
|
||||||
|
|
||||||
#include <drivers/drv_hrt.h>
|
#include <drivers/drv_hrt.h>
|
||||||
#include <drivers/drv_range_finder.h>
|
#include <drivers/drv_range_finder.h>
|
||||||
@@ -72,7 +70,7 @@
|
|||||||
#include <board_config.h>
|
#include <board_config.h>
|
||||||
|
|
||||||
/* Configuration Constants */
|
/* Configuration Constants */
|
||||||
#define MB12XX_BUS PX4_I2C_BUS_EXPANSION
|
#define MB12XX_BUS_DEFAULT PX4_I2C_BUS_EXPANSION
|
||||||
#define MB12XX_BASEADDR 0x70 /* 7-bit address. 8-bit address is 0xE0 */
|
#define MB12XX_BASEADDR 0x70 /* 7-bit address. 8-bit address is 0xE0 */
|
||||||
#define MB12XX_DEVICE_PATH "/dev/mb12xx"
|
#define MB12XX_DEVICE_PATH "/dev/mb12xx"
|
||||||
|
|
||||||
@@ -97,7 +95,7 @@ class MB12XX : public device::I2C
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MB12XX(uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING,
|
MB12XX(uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING,
|
||||||
int bus = MB12XX_BUS, int address = MB12XX_BASEADDR);
|
int bus = MB12XX_BUS_DEFAULT, int address = MB12XX_BASEADDR);
|
||||||
virtual ~MB12XX();
|
virtual ~MB12XX();
|
||||||
|
|
||||||
virtual int init();
|
virtual int init();
|
||||||
@@ -685,26 +683,74 @@ namespace mb12xx
|
|||||||
|
|
||||||
MB12XX *g_dev;
|
MB12XX *g_dev;
|
||||||
|
|
||||||
void start(uint8_t rotation);
|
int bus_options[] = {
|
||||||
void stop();
|
#ifdef PX4_I2C_BUS_EXPANSION
|
||||||
void test();
|
PX4_I2C_BUS_EXPANSION,
|
||||||
void reset();
|
#endif
|
||||||
void info();
|
#ifdef PX4_I2C_BUS_EXPANSION1
|
||||||
|
PX4_I2C_BUS_EXPANSION1,
|
||||||
|
#endif
|
||||||
|
#ifdef PX4_I2C_BUS_EXPANSION2
|
||||||
|
PX4_I2C_BUS_EXPANSION2,
|
||||||
|
#endif
|
||||||
|
#ifdef PX4_I2C_BUS_ONBOARD
|
||||||
|
PX4_I2C_BUS_ONBOARD,
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#define NUM_BUS_OPTIONS (sizeof(bus_options)/sizeof(bus_options[0]))
|
||||||
|
|
||||||
|
int start(uint8_t rotation);
|
||||||
|
int start_bus(uint8_t rotation, int i2c_bus);
|
||||||
|
int stop();
|
||||||
|
int test();
|
||||||
|
int reset();
|
||||||
|
int info();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the driver.
|
*
|
||||||
|
* Attempt to start driver on all available I2C busses.
|
||||||
|
*
|
||||||
|
* This function will return as soon as the first sensor
|
||||||
|
* is detected on one of the available busses or if no
|
||||||
|
* sensors are detected.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
void
|
int
|
||||||
start(uint8_t rotation)
|
start(uint8_t rotation)
|
||||||
{
|
{
|
||||||
int fd;
|
if (g_dev != nullptr) {
|
||||||
|
PX4_ERR("already started");
|
||||||
|
return PX4_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) {
|
||||||
|
if (start_bus(rotation, bus_options[i]) == PX4_OK) {
|
||||||
|
return PX4_OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return PX4_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start the driver on a specific bus.
|
||||||
|
*
|
||||||
|
* This function only returns if the sensor is up and running
|
||||||
|
* or could not be detected successfully.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
start_bus(uint8_t rotation, int i2c_bus)
|
||||||
|
{
|
||||||
|
int fd = -1;
|
||||||
|
|
||||||
if (g_dev != nullptr) {
|
if (g_dev != nullptr) {
|
||||||
errx(1, "already started");
|
PX4_ERR("already started");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create the driver */
|
/* create the driver */
|
||||||
g_dev = new MB12XX(rotation, MB12XX_BUS);
|
g_dev = new MB12XX(rotation, i2c_bus);
|
||||||
|
|
||||||
if (g_dev == nullptr) {
|
if (g_dev == nullptr) {
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -725,32 +771,40 @@ start(uint8_t rotation)
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0);
|
close(fd);
|
||||||
|
return PX4_OK;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
|
|
||||||
|
if (fd >= 0) {
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
if (g_dev != nullptr) {
|
if (g_dev != nullptr) {
|
||||||
delete g_dev;
|
delete g_dev;
|
||||||
g_dev = nullptr;
|
g_dev = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
errx(1, "driver start failed");
|
PX4_ERR("not started on bus %d", i2c_bus);
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop the driver
|
* Stop the driver
|
||||||
*/
|
*/
|
||||||
void stop()
|
int
|
||||||
|
stop()
|
||||||
{
|
{
|
||||||
if (g_dev != nullptr) {
|
if (g_dev != nullptr) {
|
||||||
delete g_dev;
|
delete g_dev;
|
||||||
g_dev = nullptr;
|
g_dev = nullptr;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
errx(1, "driver not running");
|
PX4_ERR("driver not running");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0);
|
return PX4_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -758,7 +812,7 @@ void stop()
|
|||||||
* make sure we can collect data from the sensor in polled
|
* make sure we can collect data from the sensor in polled
|
||||||
* and automatic modes.
|
* and automatic modes.
|
||||||
*/
|
*/
|
||||||
void
|
int
|
||||||
test()
|
test()
|
||||||
{
|
{
|
||||||
struct distance_sensor_s report;
|
struct distance_sensor_s report;
|
||||||
@@ -768,21 +822,24 @@ test()
|
|||||||
int fd = open(MB12XX_DEVICE_PATH, O_RDONLY);
|
int fd = open(MB12XX_DEVICE_PATH, O_RDONLY);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
err(1, "%s open failed (try 'mb12xx start' if the driver is not running", MB12XX_DEVICE_PATH);
|
PX4_ERR("%s open failed (try 'mb12xx start' if the driver is not running)", MB12XX_DEVICE_PATH);
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
print_message(report);
|
print_message(report);
|
||||||
|
|
||||||
/* 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");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read the sensor 5x and report each value */
|
/* read the sensor 5x and report each value */
|
||||||
@@ -795,14 +852,16 @@ test()
|
|||||||
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");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
print_message(report);
|
print_message(report);
|
||||||
@@ -810,53 +869,73 @@ test()
|
|||||||
|
|
||||||
/* reset the sensor polling to default rate */
|
/* reset the sensor polling to default rate */
|
||||||
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT)) {
|
if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT)) {
|
||||||
errx(1, "failed to set default poll rate");
|
PX4_ERR("failed to set default poll rate");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
errx(0, "PASS");
|
PX4_INFO("PASS");
|
||||||
|
return PX4_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the driver.
|
* Reset the driver.
|
||||||
*/
|
*/
|
||||||
void
|
int
|
||||||
reset()
|
reset()
|
||||||
{
|
{
|
||||||
int fd = open(MB12XX_DEVICE_PATH, O_RDONLY);
|
int fd = open(MB12XX_DEVICE_PATH, O_RDONLY);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
err(1, "failed ");
|
PX4_ERR("failed");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ioctl(fd, SENSORIOCRESET, 0) < 0) {
|
if (ioctl(fd, SENSORIOCRESET, 0) < 0) {
|
||||||
err(1, "driver reset failed");
|
PX4_ERR("driver reset failed");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0);
|
close(fd);
|
||||||
|
return PX4_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print a little info about the driver.
|
* Print a little info about the driver.
|
||||||
*/
|
*/
|
||||||
void
|
int
|
||||||
info()
|
info()
|
||||||
{
|
{
|
||||||
if (g_dev == nullptr) {
|
if (g_dev == nullptr) {
|
||||||
errx(1, "driver not running");
|
PX4_ERR("driver poll restart failed");
|
||||||
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("state @ %p\n", g_dev);
|
printf("state @ %p\n", g_dev);
|
||||||
g_dev->print_info();
|
g_dev->print_info();
|
||||||
|
|
||||||
exit(0);
|
return PX4_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace */
|
} /* namespace */
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
mb12xx2_usage()
|
||||||
|
{
|
||||||
|
PX4_INFO("usage: mb12xx command [options]");
|
||||||
|
PX4_INFO("options:");
|
||||||
|
PX4_INFO("\t-b --bus i2cbus (%d)", MB12XX_BUS_DEFAULT);
|
||||||
|
PX4_INFO("\t-a --all");
|
||||||
|
PX4_INFO("\t-R --rotation (%d)", distance_sensor_s::ROTATION_DOWNWARD_FACING);
|
||||||
|
PX4_INFO("command:");
|
||||||
|
PX4_INFO("\tstart|stop|test|reset|info");
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
mb12xx_main(int argc, char *argv[])
|
mb12xx_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -864,16 +943,27 @@ mb12xx_main(int argc, char *argv[])
|
|||||||
int myoptind = 1;
|
int myoptind = 1;
|
||||||
const char *myoptarg = nullptr;
|
const char *myoptarg = nullptr;
|
||||||
uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING;
|
uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING;
|
||||||
|
bool start_all = false;
|
||||||
|
|
||||||
while ((ch = px4_getopt(argc, argv, "R:", &myoptind, &myoptarg)) != EOF) {
|
int i2c_bus = MB12XX_BUS_DEFAULT;
|
||||||
|
|
||||||
|
while ((ch = px4_getopt(argc, argv, "ab:R:", &myoptind, &myoptarg)) != EOF) {
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case 'R':
|
case 'R':
|
||||||
rotation = (uint8_t)atoi(myoptarg);
|
rotation = (uint8_t)atoi(myoptarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'b':
|
||||||
|
i2c_bus = atoi(myoptarg);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'a':
|
||||||
|
start_all = true;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
PX4_WARN("Unknown option!");
|
PX4_WARN("Unknown option!");
|
||||||
return -1;
|
goto out_error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -885,38 +975,43 @@ mb12xx_main(int argc, char *argv[])
|
|||||||
* Start/load the driver.
|
* Start/load the driver.
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "start")) {
|
if (!strcmp(argv[myoptind], "start")) {
|
||||||
mb12xx::start(rotation);
|
if (start_all) {
|
||||||
|
return mb12xx::start(rotation);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return mb12xx::start_bus(rotation, i2c_bus);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Stop the driver
|
* Stop the driver
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "stop")) {
|
if (!strcmp(argv[myoptind], "stop")) {
|
||||||
mb12xx::stop();
|
return mb12xx::stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Test the driver/device.
|
* Test the driver/device.
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "test")) {
|
if (!strcmp(argv[myoptind], "test")) {
|
||||||
mb12xx::test();
|
return mb12xx::test();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Reset the driver.
|
* Reset the driver.
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "reset")) {
|
if (!strcmp(argv[myoptind], "reset")) {
|
||||||
mb12xx::reset();
|
return mb12xx::reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Print driver information.
|
* Print driver information.
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "info") || !strcmp(argv[myoptind], "status")) {
|
if (!strcmp(argv[myoptind], "info") || !strcmp(argv[myoptind], "status")) {
|
||||||
mb12xx::info();
|
return mb12xx::info();
|
||||||
}
|
}
|
||||||
|
|
||||||
out_error:
|
out_error:
|
||||||
PX4_ERR("unrecognized command, try 'start', 'test', 'reset' or 'info'");
|
mb12xx2_usage();
|
||||||
return PX4_ERROR;
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -903,6 +903,7 @@ reset()
|
|||||||
return PX4_ERROR;
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
close(fd);
|
||||||
return PX4_OK;
|
return PX4_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user