AP_Scripting: fix PWMSource deletion crash

This commit is contained in:
Iampete1 2023-02-20 13:59:22 +00:00 committed by Andrew Tridgell
parent 2bb8294685
commit 54a9239e62
6 changed files with 53 additions and 12 deletions

View File

@ -244,6 +244,15 @@ void AP_Scripting::thread(void) {
} }
num_i2c_devices = 0; num_i2c_devices = 0;
// clear allocated PWM sources
for (uint8_t i=0; i<SCRIPTING_MAX_NUM_PWM_SOURCE; i++) {
if (_pwm_source[i] != nullptr) {
delete _pwm_source[i];
_pwm_source[i] = nullptr;
}
}
num_pwm_source = 0;
bool cleared = false; bool cleared = false;
while(true) { while(true) {
// 1hz check if we should restart // 1hz check if we should restart

View File

@ -28,6 +28,8 @@
#define SCRIPTING_MAX_NUM_I2C_DEVICE 4 #define SCRIPTING_MAX_NUM_I2C_DEVICE 4
#endif #endif
#define SCRIPTING_MAX_NUM_PWM_SOURCE 4
class AP_Scripting class AP_Scripting
{ {
public: public:
@ -89,6 +91,10 @@ public:
}; };
ObjectBuffer<struct scripting_mission_cmd> * mission_data; ObjectBuffer<struct scripting_mission_cmd> * mission_data;
// PWMSource storage
uint8_t num_pwm_source;
AP_HAL::PWMSource *_pwm_source[SCRIPTING_MAX_NUM_PWM_SOURCE];
private: private:
bool repl_start(void); bool repl_start(void);

View File

@ -309,24 +309,24 @@ function motor_factor_table_ud:roll(index, value) end
-- desc -- desc
---@class PWMSource_ud ---@class AP_HAL__PWMSource_ud
local PWMSource_ud = {} local AP_HAL__PWMSource_ud = {}
---@return PWMSource_ud ---@return AP_HAL__PWMSource_ud
function PWMSource() end function PWMSource() end
-- desc -- desc
---@return integer ---@return integer
function PWMSource_ud:get_pwm_avg_us() end function AP_HAL__PWMSource_ud:get_pwm_avg_us() end
-- desc -- desc
---@return integer ---@return integer
function PWMSource_ud:get_pwm_us() end function AP_HAL__PWMSource_ud:get_pwm_us() end
-- desc -- desc
---@param pin_number integer ---@param pin_number integer
---@return boolean ---@return boolean
function PWMSource_ud:set_pin(pin_number) end function AP_HAL__PWMSource_ud:set_pin(pin_number) end
-- desc -- desc

View File

@ -478,10 +478,11 @@ ap_object AP_HAL::AnalogSource method voltage_average float
ap_object AP_HAL::AnalogSource method voltage_latest float ap_object AP_HAL::AnalogSource method voltage_latest float
ap_object AP_HAL::AnalogSource method voltage_average_ratiometric float ap_object AP_HAL::AnalogSource method voltage_average_ratiometric float
userdata AP_HAL::PWMSource rename PWMSource global manual PWMSource lua_get_PWMSource 0
userdata AP_HAL::PWMSource method set_pin boolean uint8_t'skip_check "Scripting"'literal
userdata AP_HAL::PWMSource method get_pwm_us uint16_t ap_object AP_HAL::PWMSource method set_pin boolean uint8_t'skip_check "Scripting"'literal
userdata AP_HAL::PWMSource method get_pwm_avg_us uint16_t ap_object AP_HAL::PWMSource method get_pwm_us uint16_t
ap_object AP_HAL::PWMSource method get_pwm_avg_us uint16_t
singleton hal.gpio rename gpio singleton hal.gpio rename gpio
singleton hal.gpio literal singleton hal.gpio literal

View File

@ -500,6 +500,8 @@ int lua_get_CAN_device2(lua_State *L) {
directory listing, return table of files in a directory directory listing, return table of files in a directory
*/ */
int lua_dirlist(lua_State *L) { int lua_dirlist(lua_State *L) {
binding_argcheck(L, 1);
struct dirent *entry; struct dirent *entry;
int i; int i;
const char *path = luaL_checkstring(L, 1); const char *path = luaL_checkstring(L, 1);
@ -529,6 +531,7 @@ int lua_dirlist(lua_State *L) {
remove a file remove a file
*/ */
int lua_removefile(lua_State *L) { int lua_removefile(lua_State *L) {
binding_argcheck(L, 1);
const char *filename = luaL_checkstring(L, 1); const char *filename = luaL_checkstring(L, 1);
return luaL_fileresult(L, remove(filename) == 0, filename); return luaL_fileresult(L, remove(filename) == 0, filename);
} }
@ -540,3 +543,24 @@ int SRV_Channels_get_safety_state(lua_State *L) {
lua_pushboolean(L, data); lua_pushboolean(L, data);
return 1; return 1;
} }
int lua_get_PWMSource(lua_State *L) {
binding_argcheck(L, 0);
static_assert(SCRIPTING_MAX_NUM_PWM_SOURCE >= 0, "There cannot be a negative number of PWMSources");
if (AP::scripting()->num_pwm_source >= SCRIPTING_MAX_NUM_PWM_SOURCE) {
return luaL_argerror(L, 1, "no PWMSources available");
}
AP::scripting()->_pwm_source[AP::scripting()->num_pwm_source] = new AP_HAL::PWMSource;
if (AP::scripting()->_pwm_source[AP::scripting()->num_pwm_source] == nullptr) {
return luaL_argerror(L, 1, "PWMSources device nullptr");
}
new_AP_HAL__PWMSource(L);
*((AP_HAL::PWMSource**)luaL_checkudata(L, -1, "AP_HAL::PWMSource")) = AP::scripting()->_pwm_source[AP::scripting()->num_pwm_source];
AP::scripting()->num_pwm_source++;
return 1;
}

View File

@ -13,3 +13,4 @@ int lua_get_CAN_device2(lua_State *L);
int lua_dirlist(lua_State *L); int lua_dirlist(lua_State *L);
int lua_removefile(lua_State *L); int lua_removefile(lua_State *L);
int SRV_Channels_get_safety_state(lua_State *L); int SRV_Channels_get_safety_state(lua_State *L);
int lua_get_PWMSource(lua_State *L);