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
Scripting/AP_RangeFinder: Add lua scripting based RangeFinder #20015
base: master
Are you sure you want to change the base?
Changes from all commits
0d9bd1e
621d7d5
bb6d9d0
4864873
02a8f0b
ded77bf
a46007b
801cba0
08243e4
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 |
---|---|---|
|
@@ -51,6 +51,7 @@ | |
#include "AP_RangeFinder_MSP.h" | ||
#include "AP_RangeFinder_USD1_CAN.h" | ||
#include "AP_RangeFinder_Benewake_CAN.h" | ||
#include "AP_RangeFinder_Lua.h" | ||
|
||
#include <AP_BoardConfig/AP_BoardConfig.h> | ||
#include <AP_Logger/AP_Logger.h> | ||
|
@@ -159,7 +160,7 @@ const AP_Param::GroupInfo RangeFinder::var_info[] = { | |
// @Path: AP_RangeFinder_Wasp.cpp,AP_RangeFinder_Benewake_CAN.cpp | ||
AP_SUBGROUPVARPTR(drivers[9], "A_", 44, RangeFinder, backend_var_info[9]), | ||
#endif | ||
|
||
AP_GROUPEND | ||
}; | ||
|
||
|
@@ -524,8 +525,15 @@ void RangeFinder::detect_instance(uint8_t instance, uint8_t& serial_instance) | |
case Type::Benewake_CAN: | ||
#if AP_RANGEFINDER_BENEWAKE_CAN_ENABLED | ||
_add_backend(new AP_RangeFinder_Benewake_CAN(state[instance], params[instance]), instance); | ||
#endif | ||
break; | ||
|
||
case Type::Lua_Scripting: | ||
#if AP_SCRIPTING_ENABLED | ||
_add_backend(new AP_RangeFinder_Lua(state[instance], params[instance]), instance); | ||
#endif | ||
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. break after endif |
||
break; | ||
|
||
case Type::NONE: | ||
break; | ||
} | ||
|
@@ -603,12 +611,13 @@ bool RangeFinder::has_orientation(enum Rotation orientation) const | |
// find first range finder instance with the specified orientation | ||
AP_RangeFinder_Backend *RangeFinder::find_instance(enum Rotation orientation) const | ||
{ | ||
// first try for a rangefinder that is in range | ||
// first try for a rangefinder that is in range and also in use | ||
for (uint8_t i=0; i<num_instances; i++) { | ||
rishabsingh3003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AP_RangeFinder_Backend *backend = get_backend(i); | ||
if (backend != nullptr && | ||
backend->orientation() == orientation && | ||
backend->status() == Status::Good) { | ||
backend->status() == Status::Good && | ||
backend->use_sensor()) { | ||
return backend; | ||
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. need to check all uses of get_backend() to ensure they obey use setting |
||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
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_RangeFinder_Lua.h" | ||
#include <AP_HAL/AP_HAL.h> | ||
|
||
#if AP_SCRIPTING_ENABLED | ||
|
||
// constructor | ||
AP_RangeFinder_Lua::AP_RangeFinder_Lua(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) : | ||
AP_RangeFinder_Backend(_state, _params) | ||
{ | ||
set_status(RangeFinder::Status::NoData); | ||
} | ||
|
||
|
||
// Set the distance based on a Lua Script | ||
bool AP_RangeFinder_Lua::handle_script_msg(float dist_m) | ||
{ | ||
// only accept distances for the configured orentation | ||
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. Comment is wrong. |
||
state.last_reading_ms = AP_HAL::millis(); | ||
_distance_m = dist_m; | ||
return true; | ||
} | ||
|
||
|
||
// update the state of the sensor | ||
void AP_RangeFinder_Lua::update(void) | ||
{ | ||
//Time out on incoming data; if we don't get new | ||
//data in 500ms, dump it | ||
if (AP_HAL::millis() - state.last_reading_ms > AP_RANGEFINDER_LUA_TIMEOUT_MS) { | ||
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. I quite like this sort of timeout, however we did not implement one for EFI, there the script sets last reading itself. That does mean the script can report that rangefinder is lost, eg it can still set the distance without updating the timeout. Here we cant tell the difference between the script stopping and the script loosing connection with the sensor. However it does require that the script fill that last time in correctly. |
||
set_status(RangeFinder::Status::NoData); | ||
state.distance_m = 0.0f; | ||
} else { | ||
state.distance_m = _distance_m; | ||
update_status(); | ||
} | ||
} | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "AP_RangeFinder.h" | ||
#include "AP_RangeFinder_Backend.h" | ||
|
||
#if AP_SCRIPTING_ENABLED | ||
|
||
// Data timeout | ||
#define AP_RANGEFINDER_LUA_TIMEOUT_MS 500 | ||
|
||
class AP_RangeFinder_Lua : public AP_RangeFinder_Backend | ||
{ | ||
public: | ||
|
||
// constructor | ||
AP_RangeFinder_Lua(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params); | ||
|
||
// update state | ||
void update(void) override; | ||
|
||
// Get update from Lua script | ||
bool handle_script_msg(float dist_m) override; | ||
|
||
MAV_DISTANCE_SENSOR _get_mav_distance_sensor_type() const override { | ||
return MAV_DISTANCE_SENSOR_UNKNOWN; | ||
} | ||
|
||
private: | ||
|
||
float _distance_m; // stored data from lua script: | ||
}; | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1870,11 +1870,44 @@ function terrain:status() end | |
---@return boolean | ||
function terrain:enabled() end | ||
|
||
-- RangeFinder backend | ||
---@class AP_RangeFinder_Backend_ud | ||
local AP_RangeFinder_Backend_ud = {} | ||
|
||
-- Send distance to lua rangefinder backend. Returns false if failed | ||
---@param distance number | ||
---@return boolean | ||
function AP_RangeFinder_Backend_ud:handle_script_msg(distance) end | ||
|
||
-- Status of this rangefinder instance | ||
---@return integer | ||
function AP_RangeFinder_Backend_ud:status() end | ||
|
||
-- Type of rangefinder of this instance | ||
---@return integer | ||
function AP_RangeFinder_Backend_ud:type() end | ||
|
||
-- Orintation of the rangefinder of this instance | ||
---@return integer | ||
function AP_RangeFinder_Backend_ud:orientation() end | ||
|
||
-- Current distance of the sensor instance | ||
---@return number | ||
function AP_RangeFinder_Backend_ud:distance() end | ||
|
||
-- Returns true if backend has new data | ||
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. don't need has_data(), use status() |
||
---@return boolean | ||
function AP_RangeFinder_Backend_ud:has_data() end | ||
|
||
-- desc | ||
---@class rangefinder | ||
rangefinder = {} | ||
|
||
-- get backend based on rangefinder instance provided | ||
---@param rangefinder_instance integer | ||
---@return AP_RangeFinder_Backend_ud | ||
function rangefinder:get_backend(rangefinder_instance) end | ||
|
||
-- desc | ||
---@param orientation integer | ||
---@return Vector3f_ud | ||
|
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 really is a mess, a function that does this would make it a lot cleaner:
that would return the first in-range rangefinder with the given orient that has USE=1