35 #include "generated/airframe.h"
36 #include "generated/modules.h"
39 #ifdef MILLIAMP_PER_PERCENT
40 #warning "deprecated MILLIAMP_PER_PERCENT --> Please use MILLIAMP_AT_FULL_THROTTLE"
42 #if defined BATTERY_SENS || defined BATTERY_OFFSET
43 #warning "BATTERY_SENS and BATTERY_OFFSET are deprecated, please remove them --> if you want to change the default use VoltageOfAdc"
46 #if defined COMMAND_THROTTLE
47 #define COMMAND_CURRENT_ESTIMATION COMMAND_THROTTLE
48 #elif defined COMMAND_THRUST
49 #define COMMAND_CURRENT_ESTIMATION COMMAND_THRUST
52 #ifndef BAT_CHECKER_DELAY
53 #define BAT_CHECKER_DELAY 5
57 #define MIN_BAT_LEVEL 3
60 #ifndef TAKEOFF_BAT_LEVEL
61 #define TAKEOFF_BAT_LEVEL LOW_BAT_LEVEL
70 #define VoltageOfAdc(adc) DefaultVoltageOfAdc(adc)
73 #define VBoardOfAdc(adc) DefaultVBoardOfAdc(adc)
75 #ifndef MilliAmpereOfAdc
76 #define MilliAmpereOfAdc(adc) DefaultMilliAmpereOfAdc(adc)
79 #ifndef CURRENT_ESTIMATION_NONLINEARITY
80 #define CURRENT_ESTIMATION_NONLINEARITY 1.2
84 #if defined MILLIAMP_AT_FULL_THROTTLE && !defined MILLIAMP_AT_IDLE_THROTTLE
86 #define MILLIAMP_AT_IDLE_THROTTLE 0
94 #if defined ADC_CHANNEL_VSUPPLY || (defined ADC_CHANNEL_CURRENT && !defined SITL) || defined MILLIAMP_AT_FULL_THROTTLE
96 #ifdef ADC_CHANNEL_VSUPPLY
99 #if defined ADC_CHANNEL_VBOARD
102 #if defined ADC_CHANNEL_CURRENT && !defined SITL
103 struct adc_buf current_adc_buf;
105 #if defined ADC_CHANNEL_CURRENT2 && !defined SITL
106 struct adc_buf current2_adc_buf;
108 #ifdef MILLIAMP_AT_FULL_THROTTLE
114 #ifdef PREFLIGHT_CHECKS
141 #if defined ADC_CHANNEL_VSUPPLY
145 #if defined ADC_CHANNEL_VBOARD
150 #if defined ADC_CHANNEL_CURRENT && !defined SITL
153 #if defined ADC_CHANNEL_CURRENT2 && !defined SITL
156 #elif defined MILLIAMP_AT_FULL_THROTTLE
168 static uint32_t bat_low_counter = 0;
169 static uint32_t bat_critical_counter = 0;
170 static bool vsupply_check_started =
false;
172 #if defined(ADC_CHANNEL_VSUPPLY) && !defined(SITL) && !USE_BATTERY_MONITOR
174 electrical_priv.vsupply_adc_buf.av_nb_sample));
177 #if defined(ADC_CHANNEL_VBOARD) && !defined(SITL)
179 electrical_priv.vboard_adc_buf.av_nb_sample));
182 #ifdef ADC_CHANNEL_CURRENT
184 int32_t current_adc = electrical_priv.current_adc_buf.sum / electrical_priv.current_adc_buf.av_nb_sample;
187 #ifdef ADC_CHANNEL_CURRENT2
188 current_adc = electrical_priv.current2_adc_buf.sum / electrical_priv.current2_adc_buf.av_nb_sample;
192 #elif defined MILLIAMP_AT_FULL_THROTTLE && defined COMMAND_CURRENT_ESTIMATION
203 static float full_current = (float)MILLIAMP_AT_FULL_THROTTLE / 1000.f;
204 static float idle_current = (float)MILLIAMP_AT_IDLE_THROTTLE / 1000.f;
206 float x = ((float)
commands[COMMAND_CURRENT_ESTIMATION]) / ((float)
MAX_PPRZ);
222 powf((powf(full_current - idle_current, electrical_priv.nonlin_factor) -
223 powf(((full_current - idle_current) * x), electrical_priv.nonlin_factor)),
224 (1.f / electrical_priv.nonlin_factor));
230 static const float period_to_hour = 1 / 3600.f / ELECTRICAL_PERIODIC_FREQ;
238 vsupply_check_started =
true;
241 if (vsupply_check_started) {
243 if (bat_low_counter > 0) {
246 if (bat_low_counter == 0) {
256 if (bat_critical_counter > 0) {
257 bat_critical_counter--;
259 if (bat_critical_counter == 0) {
arch independent ADC (Analog to Digital Converter) API
#define DEFAULT_AV_NB_SAMPLE
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
#define ADC_CHANNEL_VSUPPLY
bool autopilot_throttle_killed(void)
get kill status
Core autopilot interface common to all firmwares.
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Link between ChibiOS ADC drivers and Paparazzi adc_buffers.
pprz_t commands[COMMANDS_NB]
Hardware independent code for commands handling.
#define ADC_CHANNEL_CURRENT
#define ADC_CHANNEL_VBOARD
void electrical_avg_reset(float var)
#define BAT_CHECKER_DELAY
#define VoltageOfAdc(adc)
#define MilliAmpereOfAdc(adc)
struct Electrical electrical
#define TAKEOFF_BAT_LEVEL
#define CURRENT_ESTIMATION_NONLINEARITY
void electrical_init(void)
void electrical_periodic(void)
Interface for electrical status: supply voltage, current, battery status, etc.
uint32_t avg_power
average power sum
#define LOW_BAT_LEVEL
low battery level in Volts (for 3S LiPo)
float energy
consumed energy in Wh
float current
current in A
uint32_t avg_cnt
average power counter
#define CRITIC_BAT_LEVEL
critical battery level in Volts (for 3S LiPo)
bool bat_low
battery low status
float charge
consumed electric charge in Ah
float vboard
board voltage in V
float vsupply
supply voltage in V
bool bat_critical
battery critical status
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
void preflight_error(struct preflight_result_t *result, const char *fmt,...)
Register a preflight error used inside the preflight checking functions.
void preflight_success(struct preflight_result_t *result, const char *fmt,...)
Register a preflight success used inside the preflight checking functions.
void preflight_check_register(struct preflight_check_t *check, preflight_check_f func)
Register a preflight check and add it to the linked list.
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.