44 #include <px4_platform_common/px4_config.h> 45 #include <px4_platform_common/module.h> 46 #include <px4_platform_common/module_params.h> 47 #include <px4_platform_common/getopt.h> 48 #include <px4_platform_common/posix.h> 49 #include <px4_platform_common/tasks.h> 50 #include <px4_platform_common/time.h> 51 #include <px4_platform_common/log.h> 65 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> 102 #ifndef BOARD_NUMBER_BRICKS 103 #error "battery_status module requires power bricks" 106 #if BOARD_NUMBER_BRICKS == 0 107 #error "battery_status module requires power bricks" 110 class BatteryStatus :
public ModuleBase<BatteryStatus>,
public ModuleParams,
public px4::ScheduledWorkItem
117 static int task_spawn(
int argc,
char *argv[]);
120 static int custom_command(
int argc,
char *argv[]);
123 static int print_usage(
const char *reason =
nullptr);
143 #if BOARD_NUMBER_BRICKS > 1 144 int _battery_pub_intance0ndx {0};
179 ModuleParams(nullptr),
180 ScheduledWorkItem(MODULE_NAME,
px4::wq_configurations::hp_default),
185 for (
int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
243 int ret =
_h_adc.read(&buf_adc,
sizeof(buf_adc));
256 int bat_voltage_v_chan[BOARD_NUMBER_BRICKS] = BOARD_BATT_V_LIST;
257 int bat_voltage_i_chan[BOARD_NUMBER_BRICKS] = BOARD_BATT_I_LIST;
264 bool valid_chan[BOARD_NUMBER_BRICKS] = BOARD_BRICK_VALID_LIST;
267 float bat_current_a[BOARD_NUMBER_BRICKS] = {0.0f};
268 float bat_voltage_v[BOARD_NUMBER_BRICKS] = {0.0f};
275 int selected_source = -1;
277 if (ret >= (
int)
sizeof(buf_adc[0])) {
280 for (
unsigned i = 0; i < ret /
sizeof(buf_adc[0]); i++) {
282 for (
int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
287 if (
_battery_pub[b] !=
nullptr && selected_source < 0 && valid_chan[b]) {
293 # if BOARD_NUMBER_BRICKS > 1 296 if (_battery_pub_intance0ndx != selected_source) {
301 _battery_pub_intance0ndx = selected_source;
309 if (bat_voltage_v_chan[b] == buf_adc[i].am_channel) {
313 }
else if (bat_voltage_i_chan[b] == buf_adc[i].am_channel) {
322 for (
int b = 0; b < BOARD_NUMBER_BRICKS; b++) {
325 bool connected = bat_voltage_v[b] > BOARD_ADC_OPEN_CIRCUIT_V;
331 if (BOARD_ADC_OPEN_CIRCUIT_V > BOARD_VALID_UV) {
332 connected &= valid_chan[b];
340 connected, selected_source == b, b,
341 ctrl.control[actuator_controls_s::INDEX_THROTTLE],
368 _armed = vcontrol_mode.flag_armed;
386 _object.store(instance);
387 _task_id = task_id_is_work_queue;
389 if (instance->
init()) {
394 PX4_ERR(
"alloc failed");
398 _object.store(
nullptr);
407 ScheduleOnInterval(10_ms);
425 PX4_WARN(
"%s\n", reason);
428 PRINT_MODULE_DESCRIPTION(
432 The provided functionality includes: 433 - Read the output from the ADC driver (via ioctl interface) and publish `battery_status`. 437 It runs in its own thread and polls on the currently selected gyro topic. 441 PRINT_MODULE_USAGE_NAME("battery_status",
"system");
442 PRINT_MODULE_USAGE_COMMAND(
"start");
443 PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
static int custom_command(int argc, char *argv[])
Battery _battery[BOARD_NUMBER_BRICKS]
Helper lib to publish battery_status topic.
uORB::Subscription _actuator_ctrl_0_sub
attitude controls sub
int print_status() override
__EXPORT int battery_status_main(int argc, char *argv[])
Analog layout: FMU: IN2 - battery voltage IN3 - battery current IN4 - 5V sense IN10 - spare (we could...
measure the time elapsed performing an event
struct uart_esc::@19 _parameter_handles
static void parameter_update_poll()
poll parameter update
#define PX4_MAX_ADC_CHANNELS
int main(int argc, char **argv)
int adc_init()
Do adc-related initialisation.
static void print_usage()
float battery_current_offset
High-resolution timer with callouts and timekeeping.
Library calls for battery functionality.
Global flash based parameter store.
DevHandle _h_adc
ADC driver handle.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
int32_t battery_adc_channel
static void parameters_update()
update all parameters
void parameter_update_poll(bool forced=false)
Check for changes in parameters.
static int task_spawn(int argc, char *argv[])
void init()
Activates/configures the hardware registers.
uORB::Subscription _parameter_update_sub
notification of parameter updates
void perf_end(perf_counter_t handle)
End a performance event.
bool updated()
Check if there is a new update.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Parameters _parameters
local copies of interesting parameters
float battery_current_scaling
void initialize_parameter_handles(ParameterHandles ¶meter_handles)
initialize ParameterHandles struct
void updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float current_a, bool connected, bool selected_source, int priority, float throttle_normalized, bool armed, battery_status_s *status)
Update current battery status message.
perf_counter_t _loop_perf
loop performance counter
static int print_usage(const char *reason=nullptr)
struct uart_esc::@18 _parameters
int update_parameters(const ParameterHandles ¶meter_handles, Parameters ¶meters)
Read out the parameters using the handles into the parameters struct.
~BatteryStatus() override
int parameters_update()
Update our local parameter cache.
void adc_poll()
Poll the ADC and update readings to suit.
bool _armed
arming status of the vehicle
orb_advert_t _battery_pub[BOARD_NUMBER_BRICKS]
battery status
bool copy(void *dst)
Copy the struct.
int adc_init(void)
Sensors/misc inputs.
ParameterHandles _parameter_handles
handles for interesting parameters
void perf_begin(perf_counter_t handle)
Begin a performance event.
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
static int orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data, int *instance, int priority)
Advertise as the publisher of a topic.
float battery_voltage_scaling
uORB::Subscription _vcontrol_mode_sub
vehicle control mode subscription
Performance measuring tools.