You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
5.2 KiB
140 lines
5.2 KiB
5 years ago
|
/*
|
||
|
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/>.
|
||
|
*/
|
||
|
|
||
|
#include <AP_HAL/AP_HAL.h>
|
||
|
#include "AP_Proximity_NanaRadar_MR72.h"
|
||
|
#include <AP_SerialManager/AP_SerialManager.h>
|
||
|
#include <AP_Math/crc.h>
|
||
|
#include <ctype.h>
|
||
|
#include <stdio.h>
|
||
|
#include <GCS_MAVLink/GCS.h>
|
||
|
|
||
|
extern const AP_HAL::HAL& hal;
|
||
|
|
||
|
/*
|
||
|
The constructor also initialises the proximity sensor. Note that this
|
||
|
constructor is not called until detect() returns true, so we
|
||
|
already know that we should setup the proximity sensor
|
||
|
*/
|
||
|
AP_Proximity_NanaRadar_MR72::AP_Proximity_NanaRadar_MR72(
|
||
|
AP_Proximity &_frontend,
|
||
|
AP_Proximity::Proximity_State &_state) :
|
||
|
AP_Proximity_Backend(_frontend, _state)
|
||
|
{
|
||
|
const AP_SerialManager &serial_manager = AP::serialmanager();
|
||
|
|
||
|
uart = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Lidar360, 0);
|
||
|
if (uart != nullptr) {
|
||
|
uart->begin(serial_manager.find_baudrate(AP_SerialManager::SerialProtocol_Lidar360, 0));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// detect if a TeraRanger Tower proximity sensor is connected by looking for a configured serial port
|
||
|
bool AP_Proximity_NanaRadar_MR72::detect()
|
||
|
{
|
||
|
AP_HAL::UARTDriver *uart = nullptr;
|
||
|
uart = AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_Lidar360, 0);
|
||
|
return uart != nullptr;
|
||
|
}
|
||
|
|
||
|
// update the state of the sensor
|
||
|
void AP_Proximity_NanaRadar_MR72::update(void)
|
||
|
{
|
||
|
if (uart == nullptr) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// process incoming messages
|
||
|
read_sensor_data();
|
||
|
|
||
|
// check for timeout and set health status
|
||
|
if ((_last_distance_received_ms == 0) || (AP_HAL::millis() - _last_distance_received_ms > PROXIMITY_TRTOWER_TIMEOUT_MS)) {
|
||
|
set_status(AP_Proximity::Status::NoData);
|
||
|
} else {
|
||
|
set_status(AP_Proximity::Status::Good);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// get maximum and minimum distances (in meters) of primary sensor
|
||
|
float AP_Proximity_NanaRadar_MR72::distance_max() const
|
||
|
{
|
||
|
return 40.0f;
|
||
|
}
|
||
|
float AP_Proximity_NanaRadar_MR72::distance_min() const
|
||
|
{
|
||
|
return 0.20f;
|
||
|
}
|
||
|
|
||
|
// check for replies from sensor, returns true if at least one message was processed
|
||
|
bool AP_Proximity_NanaRadar_MR72::read_sensor_data()
|
||
|
{
|
||
|
if (uart == nullptr) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
uint16_t message_count = 0;
|
||
|
int16_t nbytes = uart->available();
|
||
|
while (nbytes-- > 0) {
|
||
|
// uint8_t c = uart->read();
|
||
|
// gcs().send_text(MAV_SEVERITY_INFO, "getc 0x%02x",c);
|
||
|
if (uart->read() == Head1)
|
||
|
{ //判断数据包帧头0x54
|
||
|
buffer[0] = Head1;
|
||
|
// gcs().send_text(MAV_SEVERITY_INFO, "getc 0x%02x",buffer[0]);
|
||
|
if (uart->read() == Head2)
|
||
|
{ //判断数据包帧头0X48
|
||
|
buffer[1] = Head2;
|
||
|
// gcs().send_text(MAV_SEVERITY_INFO, "getc 0x%02x",buffer[1]);
|
||
|
for (int i = 2; i < 19; i++)
|
||
|
{ //存储数据到数组
|
||
|
buffer[i] = uart->read();
|
||
|
// gcs().send_text(MAV_SEVERITY_INFO, "%02d: 0x%02x",i,buffer[i]);
|
||
|
}
|
||
|
CheckSum = crc_crc8(buffer,18);
|
||
|
// gcs().send_text(MAV_SEVERITY_INFO, "crc 0x%02x, rec:0x%02x",CheckSum,buffer[18]);
|
||
|
if (buffer[18] == CheckSum)
|
||
|
{ //按照协议对收到的数据进行校验
|
||
|
update_sector_data(0, UINT16_VALUE(buffer[2], buffer[3])); // d1
|
||
|
update_sector_data(45, UINT16_VALUE(buffer[4], buffer[5])); // d8
|
||
|
update_sector_data(90, UINT16_VALUE(buffer[6], buffer[7])); // d7
|
||
|
update_sector_data(135, UINT16_VALUE(buffer[8], buffer[9])); // d6
|
||
|
update_sector_data(180, UINT16_VALUE(buffer[10], buffer[11])); // d5
|
||
|
update_sector_data(225, UINT16_VALUE(buffer[12], buffer[13])); // d4
|
||
|
update_sector_data(270, UINT16_VALUE(buffer[14], buffer[15])); // d3
|
||
|
update_sector_data(315, UINT16_VALUE(buffer[16], buffer[17])); // d2
|
||
|
|
||
|
message_count++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
return (message_count > 0);
|
||
|
}
|
||
|
|
||
|
// process reply
|
||
|
void AP_Proximity_NanaRadar_MR72::update_sector_data(int16_t angle_deg, uint16_t distance_cm)
|
||
|
{
|
||
|
uint8_t sector;
|
||
|
if (convert_angle_to_sector(angle_deg, sector)) {
|
||
|
_angle[sector] = angle_deg;
|
||
|
_distance[sector] = ((float) distance_cm) / 100;
|
||
|
_distance_valid[sector] = distance_cm != 0xffff;
|
||
|
_last_distance_received_ms = AP_HAL::millis();
|
||
|
// update boundary used for avoidance
|
||
|
update_boundary_for_sector(sector, true);
|
||
|
}
|
||
|
}
|