Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/AP_HAL_ChibiOS/hwdef/include/minimize_common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,6 @@ define AP_SERVO_TELEM_ENABLED 0

# don't send extra flight information on small boards
define AP_MAVLINK_MSG_FLIGHT_INFORMATION_ENABLED 0

# Enable Benewake TFA1500
define AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED 1
Copy link
Contributor

@Hwurzburg Hwurzburg Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be enabled in minimize_common in my opinion...I dont see any other RF enabled

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be set to zero in here, not one :-)

Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ define AP_RANGEFINDER_BACKEND_DEFAULT_ENABLED 0
define AP_RANGEFINDER_BENEWAKE_TF02_ENABLED 1
define AP_RANGEFINDER_BENEWAKE_TF03_ENABLED 1
define AP_RANGEFINDER_BENEWAKE_TFMINI_ENABLED 1
define AP_RANGEFINDER_BENEWAKE_TFMINIPLUS_ENABLED 1
define AP_RANGEFINDER_BENEWAKE_TFMINIPLUS_ENABLED 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
define AP_RANGEFINDER_BENEWAKE_TFMINIPLUS_ENABLED 1

(not needed when it's in the common.inc)

6 changes: 6 additions & 0 deletions libraries/AP_RangeFinder/AP_RangeFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "AP_RangeFinder_Wasp.h"
#include "AP_RangeFinder_Benewake_TF02.h"
#include "AP_RangeFinder_Benewake_TF03.h"
#include "AP_RangeFinder_Benewake_TFA1500.h"
#include "AP_RangeFinder_Benewake_TFMini.h"
#include "AP_RangeFinder_Benewake_TFMiniPlus.h"
#include "AP_RangeFinder_Benewake_TFS20L.h"
Expand Down Expand Up @@ -532,6 +533,11 @@ __INITFUNC__ void RangeFinder::detect_instance(uint8_t instance, uint8_t& serial
serial_create_fn = AP_RangeFinder_TeraRanger_Serial::create;
break;
#endif
#if AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED
case Type::BenewakeTFA1500:
serial_create_fn = AP_RangeFinder_Benewake_TFA1500::create;
break;
#endif
#if AP_RANGEFINDER_PWM_ENABLED
case Type::PWM:
if (AP_RangeFinder_PWM::detect()) {
Expand Down
3 changes: 3 additions & 0 deletions libraries/AP_RangeFinder/AP_RangeFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class RangeFinder
#if AP_RANGEFINDER_BENEWAKE_TFS20L_ENABLED
BenewakeTFS20L = 46,
#endif // AP_RANGEFINDER_BENEWAKE_TFS20L_ENABLED
#if AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED
BenewakeTFA1500 = 47,
#endif // AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED
#if AP_RANGEFINDER_SIM_ENABLED
SIM = 100,
#endif
Expand Down
131 changes: 131 additions & 0 deletions libraries/AP_RangeFinder/AP_RangeFinder_Benewake_TFA1500.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "AP_RangeFinder_config.h"

#if AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED

#include "AP_RangeFinder_Benewake_TFA1500.h"

#include <AP_HAL/AP_HAL.h>
#include <AP_HAL/utility/sparse-endian.h>

extern const AP_HAL::HAL &hal;

static constexpr uint8_t TFA1500_FRAME_HEADER = 0x5CU;
static constexpr uint8_t TFA1500_FRAME_LENGTH = 5U;
static constexpr uint32_t TFA1500_DIST_MAX_CM = 130000U;

static const uint8_t TFA1500_CMD_START[] = {
0x55U, 0xAAU, 0xCBU, 0xCCU, 0xCCU, 0xCCU, 0xCCU, 0xFBU
};

static bool parse_frame(const uint8_t *buffer, uint32_t &dist_cm)
{
union {
uint8_t bytes[TFA1500_FRAME_LENGTH];
struct PACKED {
uint8_t header;
uint8_t dist_low;
uint8_t dist_mid;
uint8_t dist_high;
uint8_t crc_sum_of_bytes;
} packet;
} frame;

memcpy(frame.bytes, buffer, sizeof(frame.bytes));
Comment on lines +22 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was not clear.

The union goes in the header, you don't memcpy into a separate thing.

e.g. https://github.com/ardupilot/ardupilot/blob/master/libraries/AP_RangeFinder/AP_RangeFinder_RDS02UF.h#L97


if (frame.packet.header != TFA1500_FRAME_HEADER) {
return false;
}

const uint8_t expected_crc_sum_of_bytes = ~(frame.packet.dist_low +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So not really a sum-of-bytes if we're taking the ~ of it...

Suggested change
const uint8_t expected_crc_sum_of_bytes = ~(frame.packet.dist_low +
const uint8_t expected_crc = ~(frame.packet.dist_low +

frame.packet.dist_mid +
frame.packet.dist_high);

if (expected_crc_sum_of_bytes != frame.packet.crc_sum_of_bytes) {
return false;
}

dist_cm = (frame.packet.dist_high << 16) |
(frame.packet.dist_mid << 8) |
frame.packet.dist_low;
return true;
}

void AP_RangeFinder_Benewake_TFA1500::init_serial(uint8_t serial_instance)
{
AP_RangeFinder_Backend_Serial::init_serial(serial_instance);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

bool AP_RangeFinder_Benewake_TFA1500::get_reading(float &reading_m)
{
if (uart == nullptr) {
return false;
}

const uint32_t now = AP_HAL::millis();
if (last_init_ms == 0 ||
((now - last_init_ms > 1000) && (now - state.last_reading_ms > 1000))) {
last_init_ms = now;
uart->write(TFA1500_CMD_START, sizeof(TFA1500_CMD_START));
}

float sum_cm = 0.0f;
uint16_t count = 0;
uint16_t count_out_of_range = 0;

// read limit to prevent consuming too much CPU
for (auto j = 0U; j < 8192; j++) {
uint8_t c;
if (!uart->read(c)) {
break;
}

tf_linebuf[tf_linebuf_len++] = c;

if (tf_linebuf_len < TFA1500_FRAME_LENGTH) {
continue;
}

uint32_t dist_cm = 0U;
if (parse_frame(tf_linebuf, dist_cm)) {

if ((dist_cm >= TFA1500_DIST_MAX_CM) || (dist_cm == (uint32_t)model_dist_max_cm())) {
count_out_of_range++;
} else {
sum_cm += dist_cm;
count++;
}

tf_linebuf_len = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You wouldn't usually refer to a binary structure as a "line", but I'm not that fussed.


} else {

// shift buffer until a header byte is found or buffer is empty
uint8_t i;
for (i = 1; i < tf_linebuf_len; i++) {
if (tf_linebuf[i] == TFA1500_FRAME_HEADER) {
break;
}
}
if (i < tf_linebuf_len) {
memmove(&tf_linebuf[0], &tf_linebuf[i], tf_linebuf_len - i);
tf_linebuf_len -= i;
} else {
tf_linebuf_len = 0;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use something like find_signature_in_buffer or move_signature_in_buffer instead of rolling your own.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a method rather than in-lining the code. I want to factor this stuff at some stage.

}

if (count > 0) {
reading_m = (sum_cm * 0.01f) / count;
return true;
}

if (count_out_of_range > 0) {
reading_m = MAX(model_dist_max_cm() * 0.01f, max_distance());
return true;
}

return false;
}

#endif
31 changes: 31 additions & 0 deletions libraries/AP_RangeFinder/AP_RangeFinder_Benewake_TFA1500.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Datasheet: https://en.benewake.com/DataDownload/index_pid_20_lcid_104.html

#pragma once

Comment on lines +1 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Datasheet: https://en.benewake.com/DataDownload/index_pid_20_lcid_104.html
#pragma once
#pragma once
// Datasheet: https://en.benewake.com/DataDownload/index_pid_20_lcid_104.html

#if AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED

#include "AP_RangeFinder_Benewake.h"

class AP_RangeFinder_Benewake_TFA1500 : public AP_RangeFinder_Benewake
{
public:
static AP_RangeFinder_Backend_Serial *create(
RangeFinder::RangeFinder_State &_state,
AP_RangeFinder_Params &_params)
{
return NEW_NOTHROW AP_RangeFinder_Benewake_TFA1500(_state, _params);
}
void init_serial(uint8_t serial_instance) override;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void init_serial(uint8_t serial_instance) override;


protected:
float model_dist_max_cm() const override { return 0x3FFFFF; }

private:
using AP_RangeFinder_Benewake::AP_RangeFinder_Benewake;
bool get_reading(float &reading_m) override;
uint8_t tf_linebuf[5];
uint8_t tf_linebuf_len;
uint32_t last_init_ms = 0;
};

#endif // AP_RANGEFINDER_BENEWAKE_TFA1500
1 change: 1 addition & 0 deletions libraries/AP_RangeFinder/AP_RangeFinder_Params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const AP_Param::GroupInfo AP_RangeFinder_Params::var_info[] = {
// @Values: 44:HexsoonRadar
// @Values: 45:LightWare-GRF
// @Values: 46:BenewakeTFS20L
// @Values: 47:BenewakeTFA1500
// @Values: 100:SITL
// @User: Standard
AP_GROUPINFO_FLAGS("TYPE", 1, AP_RangeFinder_Params, type, 0, AP_PARAM_FLAG_ENABLE),
Expand Down
4 changes: 4 additions & 0 deletions libraries/AP_RangeFinder/AP_RangeFinder_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
#define AP_RANGEFINDER_BENEWAKE_TFMINI_ENABLED (AP_RANGEFINDER_BENEWAKE_ENABLED && AP_RANGEFINDER_BACKEND_DEFAULT_ENABLED)
#endif

#ifndef AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED
#define AP_RANGEFINDER_BENEWAKE_TFA1500_ENABLED (AP_RANGEFINDER_BENEWAKE_ENABLED && AP_RANGEFINDER_BACKEND_DEFAULT_ENABLED)
#endif

#ifndef AP_RANGEFINDER_BENEWAKE_TFMINIPLUS_ENABLED
#define AP_RANGEFINDER_BENEWAKE_TFMINIPLUS_ENABLED AP_RANGEFINDER_BACKEND_DEFAULT_ENABLED
#endif
Expand Down
Loading