mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-18 09:00:35 +08:00
Make all battery sensor drivers report their sample interval
To make use of the time abstraction in the leaky integrator.
This commit is contained in:
@@ -15,8 +15,8 @@ static constexpr int DEFAULT_V_CHANNEL[1] = {0};
|
||||
static constexpr int DEFAULT_I_CHANNEL[1] = {0};
|
||||
#endif
|
||||
|
||||
AnalogBattery::AnalogBattery(int index, ModuleParams *parent) :
|
||||
Battery(index, parent)
|
||||
AnalogBattery::AnalogBattery(int index, ModuleParams *parent, const int sample_interval_us) :
|
||||
Battery(index, parent, sample_interval_us)
|
||||
{
|
||||
char param_name[17];
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
class AnalogBattery : public Battery
|
||||
{
|
||||
public:
|
||||
AnalogBattery(int index, ModuleParams *parent);
|
||||
AnalogBattery(int index, ModuleParams *parent, const int sample_interval_us);
|
||||
|
||||
/**
|
||||
* Update current battery status message.
|
||||
|
||||
@@ -103,6 +103,9 @@ private:
|
||||
uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)}; /**< notification of parameter updates */
|
||||
uORB::Subscription _adc_report_sub{ORB_ID(adc_report)};
|
||||
|
||||
static constexpr uint32_t SAMPLE_FREQUENCY_HZ = 100;
|
||||
static constexpr uint32_t SAMPLE_INTERVAL_US = 1_s / SAMPLE_FREQUENCY_HZ;
|
||||
|
||||
AnalogBattery _battery1;
|
||||
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
@@ -135,9 +138,9 @@ private:
|
||||
BatteryStatus::BatteryStatus() :
|
||||
ModuleParams(nullptr),
|
||||
ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default),
|
||||
_battery1(1, this),
|
||||
_battery1(1, this, SAMPLE_INTERVAL_US),
|
||||
#if BOARD_NUMBER_BRICKS > 1
|
||||
_battery2(2, this),
|
||||
_battery2(2, this, SAMPLE_INTERVAL_US),
|
||||
#endif
|
||||
_loop_perf(perf_alloc(PC_ELAPSED, MODULE_NAME))
|
||||
{
|
||||
@@ -277,7 +280,7 @@ BatteryStatus::task_spawn(int argc, char *argv[])
|
||||
bool
|
||||
BatteryStatus::init()
|
||||
{
|
||||
ScheduleOnInterval(10_ms); // 100 Hz
|
||||
ScheduleOnInterval(SAMPLE_INTERVAL_US);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ using namespace time_literals;
|
||||
EscBattery::EscBattery() :
|
||||
ModuleParams(nullptr),
|
||||
WorkItem(MODULE_NAME, px4::wq_configurations::lp_default),
|
||||
_battery(1, this)
|
||||
_battery(1, this, ESC_BATTERY_INTERVAL_US)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -50,6 +50,8 @@ EscBattery::init()
|
||||
return false;
|
||||
}
|
||||
|
||||
_esc_status_sub.set_interval_us(ESC_BATTERY_INTERVAL_US);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,8 @@
|
||||
#include <uORB/topics/actuator_controls.h>
|
||||
#include <battery/battery.h>
|
||||
|
||||
using namespace time_literals;
|
||||
|
||||
class EscBattery : public ModuleBase<EscBattery>, public ModuleParams, public px4::WorkItem
|
||||
{
|
||||
public:
|
||||
@@ -73,5 +75,6 @@ private:
|
||||
uORB::Subscription _actuator_ctrl_0_sub{ORB_ID(actuator_controls_0)};
|
||||
uORB::SubscriptionCallbackWorkItem _esc_status_sub{this, ORB_ID(esc_status)};
|
||||
|
||||
static constexpr uint32_t ESC_BATTERY_INTERVAL_US = 20_ms; // assume higher frequency esc feedback than 50Hz
|
||||
Battery _battery;
|
||||
};
|
||||
|
||||
@@ -81,6 +81,8 @@
|
||||
#include <v2.0/mavlink_types.h>
|
||||
#include <lib/battery/battery.h>
|
||||
|
||||
using namespace time_literals;
|
||||
|
||||
//! Enumeration to use on the bitmask in HIL_SENSOR
|
||||
enum class SensorSource {
|
||||
ACCEL = 0b111,
|
||||
@@ -193,7 +195,10 @@ private:
|
||||
class SimulatorBattery : public Battery
|
||||
{
|
||||
public:
|
||||
SimulatorBattery() : Battery(1, nullptr) {}
|
||||
static constexpr uint32_t SIMLATOR_BATTERY_SAMPLE_FREQUENCY_HZ = 100; // Hz
|
||||
static constexpr uint32_t SIMLATOR_BATTERY_SAMPLE_INTERVAL_US = 1_s / SIMLATOR_BATTERY_SAMPLE_FREQUENCY_HZ;
|
||||
|
||||
SimulatorBattery() : Battery(1, nullptr, SIMLATOR_BATTERY_SAMPLE_INTERVAL_US) {}
|
||||
|
||||
virtual void updateParams() override
|
||||
{
|
||||
|
||||
@@ -350,7 +350,7 @@ void Simulator::handle_message_hil_sensor(const mavlink_message_t *msg)
|
||||
static uint64_t last_integration_us = 0;
|
||||
|
||||
// battery simulation (limit update to 100Hz)
|
||||
if (hrt_elapsed_time(&_last_battery_timestamp) >= 10_ms) {
|
||||
if (hrt_elapsed_time(&_last_battery_timestamp) >= SimulatorBattery::SIMLATOR_BATTERY_SAMPLE_INTERVAL_US) {
|
||||
|
||||
const float discharge_interval_us = _param_sim_bat_drain.get() * 1000 * 1000;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user