Skip to content

Commit

Permalink
sf1xx: move to PX4Rangefinder and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dagar authored and BazookaJoe1900 committed Feb 3, 2020
1 parent b8856a2 commit dcedfc6
Show file tree
Hide file tree
Showing 8 changed files with 540 additions and 918 deletions.
4 changes: 2 additions & 2 deletions src/drivers/distance_sensor/sf0x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
############################################################################
#
# Copyright (c) 2015 PX4 Development Team. All rights reserved.
# Copyright (c) 2015-2020 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
Expand Down Expand Up @@ -31,7 +31,7 @@
#
############################################################################
px4_add_module(
MODULE drivers__sf0x
MODULE drivers__distance_sensor__sf0x
MAIN sf0x
COMPILE_FLAGS
-Wno-cast-align # TODO: fix and enable
Expand Down
21 changes: 14 additions & 7 deletions src/drivers/distance_sensor/sf0x/SF0X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@

#include <termios.h>

#include <lib/parameters/param.h>

using namespace time_literals;

/* Configuration Constants */
#define SF0X_TAKE_RANGE_REG 'd'

Expand All @@ -61,32 +65,35 @@ SF0X::~SF0X()
perf_free(_comms_errors);
}

int
SF0X::init()
int SF0X::init()
{
int32_t hw_model = 0;
param_get(param_find("SENS_EN_SF0X"), &hw_model);

switch (hw_model) {
case 1: /* SF02 (40m, 12 Hz)*/
case 1:
/* SF02 (40m, 12 Hz)*/
_px4_rangefinder.set_min_distance(0.30f);
_px4_rangefinder.set_max_distance(40.0f);
_interval = 83334;
break;

case 2: /* SF10/a (25m 32Hz) */
case 2:
/* SF10/a (25m 32Hz) */
_px4_rangefinder.set_min_distance(0.01f);
_px4_rangefinder.set_max_distance(25.0f);
_interval = 31250;
break;

case 3: /* SF10/b (50m 32Hz) */
case 3:
/* SF10/b (50m 32Hz) */
_px4_rangefinder.set_min_distance(0.01f);
_px4_rangefinder.set_max_distance(50.0f);
_interval = 31250;
break;

case 4: /* SF10/c (100m 16Hz) */
case 4:
/* SF10/c (100m 16Hz) */
_px4_rangefinder.set_min_distance(0.01f);
_px4_rangefinder.set_max_distance(100.0f);
_interval = 62500;
Expand Down Expand Up @@ -263,7 +270,7 @@ void SF0X::Run()
if (OK != collect_ret) {

/* we know the sensor needs about four seconds to initialize */
if (hrt_absolute_time() > 5 * 1000 * 1000LL && _consecutive_fail_count < 5) {
if (hrt_absolute_time() > 5_s && _consecutive_fail_count < 5) {
PX4_ERR("collection error #%u", _consecutive_fail_count);
}

Expand Down
3 changes: 1 addition & 2 deletions src/drivers/distance_sensor/sf0x/SF0X.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <lib/drivers/rangefinder/PX4Rangefinder.hpp>
#include <drivers/drv_hrt.h>
#include <lib/parameters/param.h>
#include <lib/perf/perf_counter.h>

#include "sf0x_parser.h"
Expand Down Expand Up @@ -79,7 +78,7 @@ class SF0X : public px4::ScheduledWorkItem
enum SF0X_PARSE_STATE _parse_state {SF0X_PARSE_STATE0_UNSYNC};
hrt_abstime _last_read{0};

unsigned _consecutive_fail_count;
unsigned _consecutive_fail_count{0};

perf_counter_t _sample_perf;
perf_counter_t _comms_errors;
Expand Down
13 changes: 7 additions & 6 deletions src/drivers/distance_sensor/sf1xx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
############################################################################
#
# Copyright (c) 2015 PX4 Development Team. All rights reserved.
# Copyright (c) 2015-2020 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
Expand Down Expand Up @@ -31,12 +31,13 @@
#
############################################################################
px4_add_module(
MODULE drivers__sf1xx
MODULE drivers__distance_sensor__sf1xx
MAIN sf1xx
COMPILE_FLAGS
-Wno-cast-align # TODO: fix and enable
SRCS
sf1xx.cpp
SF1XX.cpp
SF1XX.hpp
sf1xx_main.cpp
DEPENDS
drivers_rangefinder
px4_work_queue
)

246 changes: 246 additions & 0 deletions src/drivers/distance_sensor/sf1xx/SF1XX.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
/****************************************************************************
*
* Copyright (c) 2013-2020 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 "SF1XX.hpp"

#include <lib/parameters/param.h>

using namespace time_literals;

/* Configuration Constants */
#define SF1XX_TAKE_RANGE_REG 0

SF1XX::SF1XX(int bus, int address, uint8_t rotation) :
I2C("SF1XX", nullptr, bus, address, 400000),
ScheduledWorkItem(MODULE_NAME, px4::device_bus_to_wq(get_device_id())),
_px4_rangefinder(0 /* device id not yet used */, ORB_PRIO_DEFAULT, rotation),
_sample_perf(perf_alloc(PC_ELAPSED, MODULE_NAME": read")),
_comms_errors(perf_alloc(PC_COUNT, MODULE_NAME": com_err"))
{
}

SF1XX::~SF1XX()
{
stop();

perf_free(_sample_perf);
perf_free(_comms_errors);
}

int SF1XX::init()
{
int32_t hw_model = 0;
param_get(param_find("SENS_EN_SF1XX"), &hw_model);

switch (hw_model) {
case 1:
/* SF10/a (25m 32Hz) */
_px4_rangefinder.set_min_distance(0.01f);
_px4_rangefinder.set_max_distance(25.0f);
_interval = 31250;
break;

case 2:
/* SF10/b (50m 32Hz) */
_px4_rangefinder.set_min_distance(0.01f);
_px4_rangefinder.set_max_distance(50.0f);
_interval = 31250;
break;

case 3:
/* SF10/c (100m 16Hz) */
_px4_rangefinder.set_min_distance(0.01f);
_px4_rangefinder.set_max_distance(100.0f);
_interval = 62500;
break;

case 4:
/* SF11/c (120m 20Hz) */
_px4_rangefinder.set_min_distance(0.01f);
_px4_rangefinder.set_max_distance(120.0f);
_interval = 50000;
break;

case 5:
/* SF/LW20/b (50m 48-388Hz) */
_px4_rangefinder.set_min_distance(0.001f);
_px4_rangefinder.set_max_distance(50.0f);
_interval = 20834;
break;

case 6:
/* SF/LW20/c (100m 48-388Hz) */
_px4_rangefinder.set_min_distance(0.001f);
_px4_rangefinder.set_max_distance(100.0f);
_interval = 20834;
break;

default:
PX4_ERR("invalid HW model %d.", hw_model);
return -1;
}

/* do I2C init (and probe) first */
if (I2C::init() != OK) {
return PX4_ERROR;
}

return PX4_OK;
}

int SF1XX::measure()
{
// Send the command to begin a measurement.
uint8_t cmd = SF1XX_TAKE_RANGE_REG;
int ret = transfer(&cmd, 1, nullptr, 0);

if (OK != ret) {
perf_count(_comms_errors);
PX4_DEBUG("i2c::transfer returned %d", ret);
return ret;
}

return PX4_OK;
}

int SF1XX::collect()
{
perf_begin(_sample_perf);

/* clear buffer if last read was too long ago */
int64_t read_elapsed = hrt_elapsed_time(&_last_read);

/* read from the sensor (i2c) */
uint8_t val[2] {};
const hrt_abstime timestamp_sample = hrt_absolute_time();
int ret = transfer(nullptr, 0, &val[0], 2);

if (ret < 0) {
PX4_DEBUG("read err: %d", ret);
perf_count(_comms_errors);
perf_end(_sample_perf);

/* only throw an error if we time out */
if (read_elapsed > (_interval * 2)) {
return ret;

} else {
return -EAGAIN;
}

} else if (ret == 0) {
return -EAGAIN;
}

_last_read = hrt_absolute_time();

uint16_t distance_cm = val[0] << 8 | val[1];
float distance_m = float(distance_cm) * 1e-2f;

_px4_rangefinder.update(timestamp_sample, distance_m);

perf_end(_sample_perf);

return PX4_OK;
}

void SF1XX::start()
{
/* reset the report ring and state machine */
_collect_phase = false;

/* schedule a cycle to start things */
ScheduleNow();
}

void SF1XX::stop()
{
ScheduleClear();
}

void SF1XX::Run()
{
/* collection phase? */
if (_collect_phase) {

/* perform collection */
int collect_ret = collect();

if (collect_ret == -EAGAIN) {
/* reschedule to grab the missing bits, time to transmit 8 bytes @ 9600 bps */
ScheduleDelayed(1042 * 8);

return;
}

if (OK != collect_ret) {

/* we know the sensor needs about four seconds to initialize */
if (hrt_absolute_time() > 5_s && _consecutive_fail_count < 5) {
PX4_ERR("collection error #%u", _consecutive_fail_count);
}

_consecutive_fail_count++;

/* restart the measurement state machine */
start();
return;

} else {
/* apparently success */
_consecutive_fail_count = 0;
}

/* next phase is measurement */
_collect_phase = false;
}

/* measurement phase */
if (OK != measure()) {
PX4_DEBUG("measure error");
}

/* next phase is collection */
_collect_phase = true;

/* schedule a fresh cycle call when the measurement is done */
ScheduleDelayed(_interval);
}

void SF1XX::print_info()
{
perf_print_counter(_sample_perf);
perf_print_counter(_comms_errors);

_px4_rangefinder.print_status();
}
Loading

0 comments on commit dcedfc6

Please sign in to comment.