CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_BattMonitor/AP_BattMonitor_FuelLevel_PWM.cpp
Views: 1798
1
#include "AP_BattMonitor_config.h"
2
3
#if AP_BATTERY_FUELLEVEL_PWM_ENABLED
4
5
#include <AP_HAL/AP_HAL.h>
6
7
#include "AP_BattMonitor_FuelLevel_PWM.h"
8
9
/*
10
"battery" monitor for liquid fuel level systems that give a PWM value indicating quantity of remaining fuel.
11
12
Output is:
13
14
- mAh remaining is fuel level in millilitres
15
- consumed mAh is in consumed millilitres
16
- fixed 1.0v voltage
17
*/
18
extern const AP_HAL::HAL& hal;
19
20
/// Constructor
21
AP_BattMonitor_FuelLevel_PWM::AP_BattMonitor_FuelLevel_PWM(AP_BattMonitor &mon, AP_BattMonitor::BattMonitor_State &mon_state, AP_BattMonitor_Params &params) :
22
AP_BattMonitor_Analog(mon, mon_state, params)
23
{
24
_state.voltage = 1.0; // show a fixed voltage of 1v
25
26
// need to add check
27
_state.healthy = false;
28
}
29
30
/*
31
read - read the "voltage" and "current"
32
*/
33
void AP_BattMonitor_FuelLevel_PWM::read()
34
{
35
if (!pwm_source.set_pin(_curr_pin, "FuelLevelPWM")) {
36
_state.healthy = false;
37
return;
38
}
39
40
uint16_t pulse_width = pwm_source.get_pwm_us();
41
42
/*
43
this driver assumes that CAPACITY is set to tank volume in millilitres.
44
*/
45
const uint16_t pwm_empty = 1100;
46
const uint16_t pwm_full = 1900;
47
const uint16_t pwm_buffer = 20;
48
49
uint32_t now_us = AP_HAL::micros();
50
51
// check for invalid pulse
52
if (pulse_width <= (pwm_empty - pwm_buffer)|| pulse_width >= (pwm_full + pwm_buffer)) {
53
_state.healthy = (now_us - _state.last_time_micros) < 250000U;
54
return;
55
}
56
pulse_width = constrain_int16(pulse_width, pwm_empty, pwm_full);
57
float proportion_full = (pulse_width - pwm_empty) / float(pwm_full - pwm_empty);
58
float proportion_used = 1.0 - proportion_full;
59
60
_state.last_time_micros = now_us;
61
_state.healthy = true;
62
63
// map consumed_mah to consumed millilitres
64
_state.consumed_mah = proportion_used * _params._pack_capacity;
65
66
// map consumed_wh using fixed voltage of 1
67
_state.consumed_wh = _state.consumed_mah;
68
}
69
70
#endif // AP_BATTERY_FUELLEVEL_PWM_ENABLED
71
72