mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-04 21:23:57 +08:00
tfmini: fix format and fix driver start / stop logic so it works when running PX4 as threads on Linux.
This commit is contained in:
@@ -47,6 +47,7 @@
|
|||||||
#include <px4_getopt.h>
|
#include <px4_getopt.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -537,7 +538,7 @@ TFMINI::collect()
|
|||||||
int bytes_available = 0;
|
int bytes_available = 0;
|
||||||
::ioctl(_fd, FIONREAD, (unsigned long)&bytes_available);
|
::ioctl(_fd, FIONREAD, (unsigned long)&bytes_available);
|
||||||
|
|
||||||
if(!bytes_available) {
|
if (!bytes_available) {
|
||||||
return -EAGAIN;
|
return -EAGAIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -552,11 +553,12 @@ TFMINI::collect()
|
|||||||
perf_end(_sample_perf);
|
perf_end(_sample_perf);
|
||||||
|
|
||||||
/* only throw an error if we time out */
|
/* only throw an error if we time out */
|
||||||
if (read_elapsed > (_conversion_interval * 2)) {
|
if (read_elapsed > (_conversion_interval * 2)) {
|
||||||
/* flush anything in RX buffer */
|
/* flush anything in RX buffer */
|
||||||
tcflush(_fd, TCIFLUSH);
|
tcflush(_fd, TCIFLUSH);
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
|
||||||
|
} else {
|
||||||
return -EAGAIN;
|
return -EAGAIN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -704,23 +706,23 @@ namespace tfmini
|
|||||||
|
|
||||||
TFMINI *g_dev;
|
TFMINI *g_dev;
|
||||||
|
|
||||||
void start(const char *port, uint8_t rotation);
|
int start(const char *port, uint8_t rotation);
|
||||||
void stop();
|
int stop();
|
||||||
void test();
|
int test();
|
||||||
void info();
|
int info();
|
||||||
void usage();
|
void usage();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the driver.
|
* Start the driver.
|
||||||
*/
|
*/
|
||||||
void
|
int
|
||||||
start(const char *port, uint8_t rotation)
|
start(const char *port, uint8_t rotation)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if (g_dev != nullptr) {
|
if (g_dev != nullptr) {
|
||||||
PX4_ERR("already started");
|
PX4_ERR("already started");
|
||||||
exit(1);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create the driver */
|
/* create the driver */
|
||||||
@@ -746,7 +748,7 @@ start(const char *port, uint8_t rotation)
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0);
|
return 0;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
|
|
||||||
@@ -756,13 +758,13 @@ fail:
|
|||||||
}
|
}
|
||||||
|
|
||||||
PX4_ERR("driver start failed");
|
PX4_ERR("driver start failed");
|
||||||
exit(1);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop the driver
|
* Stop the driver
|
||||||
*/
|
*/
|
||||||
void stop()
|
int stop()
|
||||||
{
|
{
|
||||||
if (g_dev != nullptr) {
|
if (g_dev != nullptr) {
|
||||||
PX4_INFO("stopping driver");
|
PX4_INFO("stopping driver");
|
||||||
@@ -772,10 +774,10 @@ void stop()
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
PX4_ERR("driver not running");
|
PX4_ERR("driver not running");
|
||||||
exit(1);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit(0);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -783,7 +785,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;
|
||||||
@@ -792,14 +794,17 @@ test()
|
|||||||
int fd = px4_open(RANGE_FINDER0_DEVICE_PATH, O_RDONLY);
|
int fd = px4_open(RANGE_FINDER0_DEVICE_PATH, O_RDONLY);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
err(1, "%s open failed (try 'tfmini start' if the driver is not running", RANGE_FINDER0_DEVICE_PATH);
|
PX4_ERR("%s open failed (try 'tfmini start' if the driver is not running", RANGE_FINDER0_DEVICE_PATH);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* do a simple demand read */
|
/* do a simple demand read */
|
||||||
sz = px4_read(fd, &report, sizeof(report));
|
sz = px4_read(fd, &report, sizeof(report));
|
||||||
|
|
||||||
if (sz != sizeof(report)) {
|
if (sz != sizeof(report)) {
|
||||||
err(1, "immediate read failed");
|
PX4_ERR("immediate read failed");
|
||||||
|
close(fd);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
print_message(report);
|
print_message(report);
|
||||||
@@ -807,7 +812,7 @@ test()
|
|||||||
/* start the sensor polling at 2 Hz rate */
|
/* start the sensor polling at 2 Hz rate */
|
||||||
if (OK != px4_ioctl(fd, SENSORIOCSPOLLRATE, 2)) {
|
if (OK != px4_ioctl(fd, SENSORIOCSPOLLRATE, 2)) {
|
||||||
PX4_ERR("failed to set 2Hz poll rate");
|
PX4_ERR("failed to set 2Hz poll rate");
|
||||||
exit(1);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read the sensor 5x and report each value */
|
/* read the sensor 5x and report each value */
|
||||||
@@ -838,27 +843,28 @@ test()
|
|||||||
/* reset the sensor polling to the default rate */
|
/* reset the sensor polling to the default rate */
|
||||||
if (OK != px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT)) {
|
if (OK != px4_ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT)) {
|
||||||
PX4_ERR("failed to set default poll rate");
|
PX4_ERR("failed to set default poll rate");
|
||||||
exit(1);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
PX4_INFO("PASS");
|
PX4_INFO("PASS");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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) {
|
||||||
PX4_ERR("driver not running");
|
PX4_ERR("driver not running");
|
||||||
exit(1);
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("state @ %p\n", g_dev);
|
printf("state @ %p\n", g_dev);
|
||||||
g_dev->print_info();
|
g_dev->print_info();
|
||||||
|
|
||||||
exit(0);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -907,7 +913,7 @@ tfmini_main(int argc, char *argv[])
|
|||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "start")) {
|
if (!strcmp(argv[myoptind], "start")) {
|
||||||
if (strcmp(device_path, "") != 0) {
|
if (strcmp(device_path, "") != 0) {
|
||||||
tfmini::start(device_path, rotation);
|
return tfmini::start(device_path, rotation);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
PX4_WARN("Please specify device path!");
|
PX4_WARN("Please specify device path!");
|
||||||
@@ -920,14 +926,14 @@ tfmini_main(int argc, char *argv[])
|
|||||||
* Stop the driver
|
* Stop the driver
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "stop")) {
|
if (!strcmp(argv[myoptind], "stop")) {
|
||||||
tfmini::stop();
|
return tfmini::stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Test the driver/device.
|
* Test the driver/device.
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "test")) {
|
if (!strcmp(argv[myoptind], "test")) {
|
||||||
tfmini::test();
|
return tfmini::test();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -935,6 +941,7 @@ tfmini_main(int argc, char *argv[])
|
|||||||
*/
|
*/
|
||||||
if (!strcmp(argv[myoptind], "info") || !strcmp(argv[myoptind], "status")) {
|
if (!strcmp(argv[myoptind], "info") || !strcmp(argv[myoptind], "status")) {
|
||||||
tfmini::info();
|
tfmini::info();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
out_error:
|
out_error:
|
||||||
|
|||||||
Reference in New Issue
Block a user