mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 06:58:11 +01:00
Support MaxBotix XL in addition to HRXL (#4510)
This commit is contained in:
parent
ac5c6ec288
commit
82c1988a2d
1 changed files with 19 additions and 8 deletions
|
@ -1,9 +1,10 @@
|
|||
// Official Datasheet:
|
||||
// https://www.maxbotix.com/documents/HRXL-MaxSonar-WR_Datasheet.pdf
|
||||
// HRXL: https://www.maxbotix.com/documents/HRXL-MaxSonar-WR_Datasheet.pdf
|
||||
// XL: https://www.maxbotix.com/documents/XL-MaxSonar-WR_Datasheet.pdf
|
||||
//
|
||||
// This implementation is designed to work with the TTL Versions of the
|
||||
// MaxBotix HRXL MaxSonar WR sensor series. The sensor's TTL Pin (5) should be
|
||||
// wired to one of the ESP's input pins and configured as uart rx_pin.
|
||||
// MaxBotix HRXL and XL MaxSonar WR sensor series. The sensor's TTL Pin (5)
|
||||
// should be wired to one of the ESP's input pins and configured as uart rx_pin.
|
||||
|
||||
#include "hrxl_maxsonar_wr.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
@ -17,8 +18,10 @@ static const uint8_t ASCII_NBSP = 0xFF;
|
|||
static const int MAX_DATA_LENGTH_BYTES = 6;
|
||||
|
||||
/**
|
||||
* The sensor outputs something like "R1234\r" at a fixed rate of 6 Hz. Where
|
||||
* 1234 means a distance of 1,234 m.
|
||||
* HRXL sensors output the format "R1234\r" at 6Hz
|
||||
* The 1234 means 1234mm
|
||||
* XL sensors output the format "R123\r" at 5 to 10Hz
|
||||
* The 123 means 123cm
|
||||
*/
|
||||
void HrxlMaxsonarWrComponent::loop() {
|
||||
uint8_t data;
|
||||
|
@ -42,9 +45,17 @@ void HrxlMaxsonarWrComponent::check_buffer_() {
|
|||
if (this->buffer_.back() == static_cast<char>(ASCII_CR) || this->buffer_.length() >= MAX_DATA_LENGTH_BYTES) {
|
||||
ESP_LOGV(TAG, "Read from serial: %s", this->buffer_.c_str());
|
||||
|
||||
if (this->buffer_.length() == MAX_DATA_LENGTH_BYTES && this->buffer_[0] == 'R' &&
|
||||
this->buffer_.back() == static_cast<char>(ASCII_CR)) {
|
||||
int millimeters = parse_number<int>(this->buffer_.substr(1, MAX_DATA_LENGTH_BYTES - 2)).value_or(0);
|
||||
size_t rpos = this->buffer_.find(static_cast<char>(ASCII_CR));
|
||||
|
||||
if (this->buffer_.length() <= MAX_DATA_LENGTH_BYTES && this->buffer_[0] == 'R' && rpos != std::string::npos) {
|
||||
std::string distance = this->buffer_.substr(1, rpos - 1);
|
||||
int millimeters = parse_number<int>(distance).value_or(0);
|
||||
|
||||
// XL reports in cm instead of mm and reports 3 digits instead of 4
|
||||
if (distance.length() == 3) {
|
||||
millimeters = millimeters * 10;
|
||||
}
|
||||
|
||||
float meters = float(millimeters) / 1000.0;
|
||||
ESP_LOGV(TAG, "Distance from sensor: %d mm, %f m", millimeters, meters);
|
||||
this->publish_state(meters);
|
||||
|
|
Loading…
Reference in a new issue