49 #include <px4_platform_common/px4_config.h> 50 #include <px4_platform_common/defines.h> 51 #include <px4_platform_common/getopt.h> 52 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> 56 #define PX4FLOW0_DEVICE_PATH "/dev/px4flow0" 59 #define I2C_FLOW_ADDRESS_DEFAULT 0x42 60 #define I2C_FLOW_ADDRESS_MIN 0x42
61 #define I2C_FLOW_ADDRESS_MAX 0x49
64 #define PX4FLOW_REG 0x16 66 #define PX4FLOW_CONVERSION_INTERVAL_DEFAULT 100000 67 #define PX4FLOW_CONVERSION_INTERVAL_MIN 10000
68 #define PX4FLOW_CONVERSION_INTERVAL_MAX 1000000
70 #define PX4FLOW_I2C_MAX_BUS_SPEED 400000 72 #define PX4FLOW_MAX_DISTANCE 5.0f 73 #define PX4FLOW_MIN_DISTANCE 0.3f 77 class PX4FLOW:
public device::I2C,
public px4::ScheduledWorkItem
82 uint8_t sonar_rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING);
156 ScheduledWorkItem(MODULE_NAME,
px4::device_bus_to_wq(get_device_id())),
193 PX4_ERR(
"failed to create distance_sensor object");
197 DEVICE_LOG(
"not primary range device, not advertising");
256 if (transfer(
nullptr, 0, &val[0], 22) !=
OK) {
271 int ret = transfer(&cmd, 1,
nullptr, 0);
331 report.sensor_id = 0;
337 float zeroval = 0.0f;
352 distance_report.
timestamp = report.timestamp;
355 distance_report.current_distance = report.ground_distance_m;
356 distance_report.variance = 0.0f;
357 distance_report.signal_quality = -1;
358 distance_report.type = distance_sensor_s::MAV_DISTANCE_SENSOR_ULTRASOUND;
360 distance_report.id = 0;
423 int start(
int argc,
char *argv[]);
435 if (start_in_progress) {
436 PX4_WARN(
"start already in progress");
440 if (g_dev !=
nullptr) {
441 start_in_progress =
false;
442 PX4_WARN(
"already started");
452 bool err_flag =
false;
455 const char *myoptarg =
nullptr;
456 uint8_t sonar_rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING;
458 while ((ch = px4_getopt(argc, argv,
"a:i:R:", &myoptind, &myoptarg)) != EOF) {
461 address = strtoul(myoptarg,
nullptr, 16);
464 PX4_WARN(
"invalid i2c address '%s'", myoptarg);
472 conversion_interval = strtoul(myoptarg,
nullptr, 10);
475 PX4_WARN(
"invalid conversion interval '%s'", myoptarg);
482 sonar_rotation = (uint8_t)atoi(myoptarg);
483 PX4_INFO(
"Setting sonar orientation to %d", (
int)sonar_rotation);
499 start_in_progress =
true;
500 PX4_INFO(
"scanning I2C buses for device..");
505 const int busses_to_try[] = {
506 PX4_I2C_BUS_EXPANSION,
507 #ifdef PX4_I2C_BUS_ESC 510 #ifdef PX4_I2C_BUS_ONBOARD 513 #ifdef PX4_I2C_BUS_EXPANSION1 514 PX4_I2C_BUS_EXPANSION1,
516 #ifdef PX4_I2C_BUS_EXPANSION2 517 PX4_I2C_BUS_EXPANSION2,
523 const int *cur_bus = busses_to_try;
525 while (*cur_bus != -1) {
528 g_dev =
new PX4FLOW(*cur_bus, address, (
enum Rotation)0, conversion_interval, sonar_rotation);
530 if (g_dev ==
nullptr) {
536 if (
OK == g_dev->
init()) {
550 if (*cur_bus != -1) {
553 if (g_dev ==
nullptr) {
558 start_in_progress =
false;
562 if (retry_nr < START_RETRY_COUNT) {
565 px4_usleep(START_RETRY_TIMEOUT * 1000);
573 if (g_dev !=
nullptr) {
578 start_in_progress =
false;
589 start_in_progress =
false;
591 if (g_dev !=
nullptr) {
598 PX4_WARN(
"driver not running");
610 if (g_dev ==
nullptr) {
611 PX4_WARN(
"driver not running");
622 PX4_INFO(
"usage: px4flow {start|info'}");
623 PX4_INFO(
" [-a i2c_address]");
624 PX4_INFO(
" [-i i2c_interval]");
640 if (!strcmp(argv[1],
"start")) {
647 if (!strcmp(argv[1],
"stop")) {
654 if (!strcmp(argv[1],
"info") || !strcmp(argv[1],
"status")) {
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
#define PX4FLOW_MAX_DISTANCE
void stop()
Stop the automatic measurement state machine.
#define RANGE_FINDER_BASE_DEVICE_PATH
orb_advert_t _px4flow_topic
measure the time elapsed performing an event
int info()
Print a little info about the driver.
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
#define PX4FLOW0_DEVICE_PATH
PX4FLOW(int bus, int address=I2C_FLOW_ADDRESS_DEFAULT, enum Rotation rotation=(enum Rotation) 0, int conversion_interval=PX4FLOW_CONVERSION_INTERVAL_DEFAULT, uint8_t sonar_rotation=distance_sensor_s::ROTATION_DOWNWARD_FACING)
int16_t pixel_flow_x_integral
accumulated flow in radians*10000 around x axis since last I2C readout [rad*10000] ...
void start()
Initialise the automatic measurement state machine and start it.
int start(int argc, char *argv[])
Start the driver.
orb_advert_t _distance_sensor_topic
__EXPORT void rotate_3f(enum Rotation rot, float &x, float &y, float &z)
rotate a 3 element float vector in-place
const int START_RETRY_COUNT
void usage(const char *reason)
Print the correct usage.
int stop()
Stop the driver.
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data)
void Run() override
Perform a poll cycle; collect from the previous measurement and start a new one.
count the number of times an event occurs
High-resolution timer with callouts and timekeeping.
int16_t gyro_temperature
Temperature * 100 in centi-degrees Celsius [degcelsius*100].
uint16_t frame_count_since_last_readout
number of flow measurements since last I2C readout [#frames]
Global flash based parameter store.
uint32_t sonar_timestamp
time since last sonar update [microseconds]
#define I2C_FLOW_ADDRESS_MAX
7-bit address.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
void perf_count(perf_counter_t handle)
Count a performance event.
#define PX4FLOW_MIN_DISTANCE
void perf_free(perf_counter_t handle)
Free a counter.
void init()
Activates/configures the hardware registers.
Definition of i2c frames.
uint32_t integration_timespan
accumulation timespan in microseconds since last I2C readout [microseconds]
#define DEVICE_LOG(FMT,...)
Rotation
Enum for board and external compass rotations.
__EXPORT int px4flow_main(int argc, char *argv[])
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
uint16_t ground_distance
Ground distance in meters*1000 [meters*1000].
void perf_end(perf_counter_t handle)
End a performance event.
int probe_address(uint8_t address)
Test whether the device supported by the driver is present at a specific address. ...
#define PX4FLOW_REG
Measure Register 22.
perf_counter_t _sample_perf
int16_t pixel_flow_y_integral
accumulated flow in radians*10000 around y axis since last I2C readout [rad*10000] ...
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
const int START_RETRY_TIMEOUT
enum Rotation _sensor_rotation
#define PX4FLOW_CONVERSION_INTERVAL_DEFAULT
in microseconds! = 10Hz
i2c_integral_frame _frame_integral
int16_t gyro_x_rate_integral
accumulated gyro x rates in radians*10000 since last I2C readout [rad*10000]
float _sensor_max_flow_rate
#define PX4FLOW_CONVERSION_INTERVAL_MAX
in microseconds! = 1 Hz
int16_t gyro_z_rate_integral
accumulated gyro z rates in radians*10000 since last I2C readout [rad*10000]
void perf_print_counter(perf_counter_t handle)
Print one performance counter to stdout.
perf_counter_t _comms_errors
int16_t gyro_y_rate_integral
accumulated gyro y rates in radians*10000 since last I2C readout [rad*10000]
#define I2C_FLOW_ADDRESS_DEFAULT
7-bit address. 8-bit address is 0x84, range 0x42 - 0x49
void usage()
Prints info about the driver argument usage.
uint8_t qual
averaged quality of accumulated flow values [0:bad quality;255: max quality]
orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance, int priority)
Local functions in support of the shell command.
void print_info()
Diagnostics - print some basic information about the driver.
void perf_begin(perf_counter_t handle)
Begin a performance event.
#define DEVICE_DEBUG(FMT,...)
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint32_t param_t
Parameter handle.
#define I2C_INTEGRAL_FRAME_SIZE
#define PX4FLOW_I2C_MAX_BUS_SPEED
400 KHz maximum speed
Performance measuring tools.
Base class for devices connected via I2C.