-
Notifications
You must be signed in to change notification settings - Fork 20.3k
feat(rangefinder): 添加对 Benewake TFA1500 激光测距仪的支持 #32117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -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 | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(not needed when it's in the common.inc) |
||||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
|
|
||||||
| if (frame.packet.header != TFA1500_FRAME_HEADER) { | ||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| const uint8_t expected_crc_sum_of_bytes = ~(frame.packet.dist_low + | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So not really a sum-of-bytes if we're taking the
Suggested change
|
||||||
| 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); | ||||||
| } | ||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
| } | ||||||
| } | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use something like
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DONE.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
| 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 | ||||||||||||||
|
|
||||||||||||||
peterbarker marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+1
to
+4
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| #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; | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| 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 | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :-)