forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AP_Rangefinder: add generic Chinese Sonar sensor
- Loading branch information
Showing
6 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
libraries/AP_RangeFinder/AP_RangeFinder_GenericCSonar_Serial.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/* | ||
driver for GenericCSonar_Serial : ME007YS; A02YYUW; | ||
*/ | ||
#include "AP_RangeFinder_GenericCSonar_Serial.h" | ||
|
||
#if AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED | ||
|
||
#include <AP_HAL/AP_HAL.h> | ||
#include <AP_Math/crc.h> | ||
#include <stdio.h> | ||
|
||
extern const AP_HAL::HAL& hal; | ||
|
||
static constexpr float GENERICCSONAR_SERIAL_MAX_RANGE_M = 4.5; | ||
static constexpr uint8_t GENERICCSONAR_HEADER = 0xFF; | ||
|
||
// read - return last value measured by sensor | ||
bool AP_RangeFinder_GenericCSonar_Serial::get_reading(float &reading_m) | ||
{ | ||
if (uart == nullptr) { | ||
return false; | ||
} | ||
|
||
// format is: [ 0xFF | DATA_H | DATA_L | SUM ] | ||
|
||
// read any available lines from the lidar | ||
for (auto i=0; i<8192; i++) { | ||
uint8_t b; | ||
if (!uart->read(b)) { | ||
break; | ||
} | ||
if (buf_len == 0 && b != GENERICCSONAR_HEADER) { | ||
// discard | ||
continue; | ||
} | ||
buf[buf_len++] = b; | ||
if (buf_len == sizeof(buf)) { | ||
buf_len = 0; | ||
const uint16_t crc = (buf[0] + buf[1] + buf[2]) & 0x00FF; | ||
if (crc != buf[3]) { | ||
// bad CRC, discard | ||
continue; | ||
} | ||
reading_m = ((buf[1] << 8) + buf[2]) * 0.001; | ||
return reading_m <= GENERICCSONAR_SERIAL_MAX_RANGE_M; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
#endif // AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED |
38 changes: 38 additions & 0 deletions
38
libraries/AP_RangeFinder/AP_RangeFinder_GenericCSonar_Serial.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include "AP_RangeFinder_config.h" | ||
|
||
#if AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED | ||
|
||
#include "AP_RangeFinder.h" | ||
#include "AP_RangeFinder_Backend_Serial.h" | ||
|
||
class AP_RangeFinder_GenericCSonar_Serial : public AP_RangeFinder_Backend_Serial | ||
{ | ||
|
||
public: | ||
|
||
static AP_RangeFinder_Backend_Serial *create( | ||
RangeFinder::RangeFinder_State &_state, | ||
AP_RangeFinder_Params &_params) { | ||
return NEW_NOTHROW AP_RangeFinder_GenericCSonar_Serial(_state, _params); | ||
} | ||
|
||
protected: | ||
|
||
MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override { | ||
return MAV_DISTANCE_SENSOR_ULTRASOUND; | ||
} | ||
|
||
private: | ||
|
||
using AP_RangeFinder_Backend_Serial::AP_RangeFinder_Backend_Serial; | ||
|
||
// get a reading | ||
bool get_reading(float &reading_m) override; | ||
|
||
uint8_t buf[4]; | ||
uint8_t buf_len = 0; | ||
}; | ||
|
||
#endif // AP_RANGEFINDER_GENERICCSONAR_SERIAL_ENABLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters