diff --git a/ArduCopter/Generator_test/scripts/gentest.lua b/ArduCopter/Generator_test/scripts/gentest.lua new file mode 100644 index 0000000000000..c38f437d4d209 --- /dev/null +++ b/ArduCopter/Generator_test/scripts/gentest.lua @@ -0,0 +1,10 @@ + +function update() + + local errorcode = generator:get_errorcode() + gcs:send_text(0, "LUA: Error code: " .. errorcode) + +return update, 1000 +end + +return update() diff --git a/libraries/AP_Generator/AP_Generator.cpp b/libraries/AP_Generator/AP_Generator.cpp index f1906bdc8fb50..a6ff3f08f07dd 100644 --- a/libraries/AP_Generator/AP_Generator.cpp +++ b/libraries/AP_Generator/AP_Generator.cpp @@ -173,6 +173,15 @@ bool AP_Generator::run() return _driver_ptr->run(); } +int AP_Generator::get_errorcode() +{ + // Return 0 if no driver + if (_driver_ptr == nullptr) { + return 0; + } + return _driver_ptr->get_errorcode(); +} + // Get the AP_Generator singleton AP_Generator *AP_Generator::get_singleton() { diff --git a/libraries/AP_Generator/AP_Generator.h b/libraries/AP_Generator/AP_Generator.h index be36d09e20091..d160077892cb7 100644 --- a/libraries/AP_Generator/AP_Generator.h +++ b/libraries/AP_Generator/AP_Generator.h @@ -57,6 +57,9 @@ class AP_Generator bool idle(void); bool run(void); + // error code returns 0 if no error or the error code return function is not implemented + int get_errorcode(void); + void send_generator_status(const class GCS_MAVLINK &channel); // Parameter block diff --git a/libraries/AP_Generator/AP_Generator_Backend.h b/libraries/AP_Generator/AP_Generator_Backend.h index eb095977ac041..cb987a5371123 100644 --- a/libraries/AP_Generator/AP_Generator_Backend.h +++ b/libraries/AP_Generator/AP_Generator_Backend.h @@ -32,6 +32,9 @@ class AP_Generator_Backend virtual bool idle(void) { return false; } virtual bool run(void) { return false; } + // Error code returns 0 if no error or the error code return function is not implemented + virtual int get_errorcode(void) {return 0;} + // Use generator mavlink message virtual void send_generator_status(const GCS_MAVLINK &channel) {} diff --git a/libraries/AP_Generator/AP_Generator_IE_FuelCell.h b/libraries/AP_Generator/AP_Generator_IE_FuelCell.h index 7d09d8e2080fd..a5e65350bf187 100644 --- a/libraries/AP_Generator/AP_Generator_IE_FuelCell.h +++ b/libraries/AP_Generator/AP_Generator_IE_FuelCell.h @@ -26,6 +26,9 @@ class AP_Generator_IE_FuelCell : public AP_Generator_Backend // Update fuel cell, expected to be called at 20hz void update(void) override; + // Allow generator error code to pass up to frontend and export to LUA + int get_errorcode(void) override {return _err_code;} + protected: // Pointer to serial uart diff --git a/libraries/AP_Scripting/docs/docs.lua b/libraries/AP_Scripting/docs/docs.lua index 7171b241c5296..aa88fd91b4716 100644 --- a/libraries/AP_Scripting/docs/docs.lua +++ b/libraries/AP_Scripting/docs/docs.lua @@ -3571,3 +3571,11 @@ function visual_odom:healthy() end -- visual odometry quality as a percentage from 1 to 100 or 0 if unknown ---@return integer function visual_odom:quality() end + +--Gnerator object +--@class generator +generator = {} + +-- returns the generator error code +--- @return integer +function generator:get_errorcode() end \ No newline at end of file diff --git a/libraries/AP_Scripting/generator/description/bindings.desc b/libraries/AP_Scripting/generator/description/bindings.desc index 40910361296c5..337253d314047 100644 --- a/libraries/AP_Scripting/generator/description/bindings.desc +++ b/libraries/AP_Scripting/generator/description/bindings.desc @@ -955,3 +955,8 @@ singleton AP_VisualOdom depends HAL_VISUALODOM_ENABLED singleton AP_VisualOdom rename visual_odom singleton AP_VisualOdom method healthy boolean singleton AP_VisualOdom method quality int8_t + +include AP_Generator/AP_Generator.h +singleton AP_Generator depends HAL_GENERATOR_ENABLED +singleton AP_Generator rename generator +singleton AP_Generator method get_errorcode int8_t