52 #include <px4_platform_common/getopt.h> 53 #include <px4_platform_common/module.h> 54 #include <px4_platform_common/module_params.h> 55 #include <px4_platform_common/posix.h> 56 #include <px4_platform_common/px4_config.h> 57 #include <px4_platform_common/tasks.h> 58 #include <px4_platform_common/time.h> 87 #define PCB_TEMP_ESTIMATE_DEG 5.0f 88 class Sensors :
public ModuleBase<Sensors>,
public ModuleParams
91 explicit Sensors(
bool hil_enabled);
95 static int task_spawn(
int argc,
char *argv[]);
98 static Sensors *instantiate(
int argc,
char *argv[]);
101 static int custom_command(
int argc,
char *argv[]);
104 static int print_usage(
const char *reason =
nullptr);
131 #ifdef ADC_AIRSPEED_VOLTAGE_CHANNEL 184 ModuleParams(nullptr),
185 _hil_enabled(hil_enabled),
223 #ifdef ADC_AIRSPEED_VOLTAGE_CHANNEL 229 if (!_h_adc.isValid()) {
235 #endif // ADC_AIRSPEED_VOLTAGE_CHANNEL 248 float air_temperature_celsius = (diff_pres.temperature > -300.0f) ? diff_pres.
temperature :
252 airspeed.
timestamp = diff_pres.timestamp;
255 float airspeed_input[3] = { diff_pres.differential_pressure_raw_pa, diff_pres.temperature, 0.0f };
264 switch ((diff_pres.device_id >> 16) & 0xFF) {
286 air_temperature_celsius);
289 air_temperature_celsius);
323 warn(
"WARNING: failed to set scale / offsets for airspeed sensor");
334 #ifdef ADC_AIRSPEED_VOLTAGE_CHANNEL 346 if (t - _last_adc >= 10000) {
350 int ret = _h_adc.read(&buf_adc,
sizeof(buf_adc));
352 if (ret >= (
int)
sizeof(buf_adc[0])) {
355 for (
unsigned i = 0; i < ret /
sizeof(buf_adc[0]); i++) {
356 if (ADC_AIRSPEED_VOLTAGE_CHANNEL == buf_adc[i].am_channel) {
359 const float voltage = (float)(buf_adc[i].am_data) * 3.3f / 4096.0f * 2.0f;
366 if (voltage > 0.4
f) {
369 _diff_pres.timestamp = t;
370 _diff_pres.differential_pressure_raw_pa = diff_pres_pa_raw;
371 _diff_pres.differential_pressure_filtered_pa = (_diff_pres.differential_pressure_filtered_pa * 0.9f) +
372 (diff_pres_pa_raw * 0.1
f);
373 _diff_pres.temperature = -1000.0f;
375 _diff_pres_pub.publish(_diff_pres);
409 px4_pollfd_struct_t poll_fds = {};
410 poll_fds.events = POLLIN;
414 while (!should_exit()) {
421 int pret =
px4_poll(&poll_fds, 1, 50);
445 _armed = vcontrol_mode.flag_armed;
450 const uint64_t airdata_prev_timestamp = airdata.
timestamp;
451 const uint64_t magnetometer_prev_timestamp = magnetometer.
timestamp;
466 if (airdata.
timestamp != airdata_prev_timestamp) {
470 if (magnetometer.
timestamp != magnetometer_prev_timestamp) {
511 _task_id = px4_task_spawn_cmd(
"sensors",
513 SCHED_PRIORITY_SENSOR_HUB,
515 (px4_main_t)&run_trampoline,
516 (
char *
const *)argv);
530 PX4_INFO(
"Airspeed status:");
547 PX4_WARN(
"%s\n", reason);
550 PRINT_MODULE_DESCRIPTION(
553 The sensors module is central to the whole system. It takes low-level output from drivers, turns 554 it into a more usable form, and publishes it for the rest of the system. 556 The provided functionality includes: 557 - Read the output from the sensor drivers (`sensor_gyro`, etc.). 558 If there are multiple of the same type, do voting and failover handling. 559 Then apply the board rotation and temperature calibration (if enabled). And finally publish the data; one of the 560 topics is `sensor_combined`, used by many parts of the system. 561 - Make sure the sensor drivers get the updated calibration parameters (scale & offset) when the parameters change or 562 on startup. The sensor drivers use the ioctl interface for parameter updates. For this to work properly, the 563 sensor drivers must already be running when `sensors` is started. 564 - Do preflight sensor consistency checks and publish the `sensor_preflight` topic. 567 It runs in its own thread and polls on the currently selected gyro topic. 571 PRINT_MODULE_USAGE_NAME("sensors",
"system");
572 PRINT_MODULE_USAGE_COMMAND(
"start");
573 PRINT_MODULE_USAGE_PARAM_FLAG(
'h',
"Start in HIL mode",
true);
574 PRINT_MODULE_USAGE_DEFAULT_COMMANDS();
581 bool hil_enabled =
false;
582 bool error_flag =
false;
586 const char *myoptarg =
nullptr;
588 while ((ch = px4_getopt(argc, argv,
"h", &myoptind, &myoptarg)) != EOF) {
599 PX4_WARN(
"unrecognized flag");
609 return new Sensors(hil_enabled);
ParameterHandles _parameter_handles
handles for interesting parameters
#define PCB_TEMP_ESTIMATE_DEG
HACK - true temperature is much less than indicated temperature in baro, subtract 5 degrees in an att...
void sensorsPoll(sensor_combined_s &raw, vehicle_air_data_s &airdata, vehicle_magnetometer_s &magnetometer)
read new sensor data
measure the time elapsed performing an event
float air_tube_diameter_mm
float calc_IAS_corrected(enum AIRSPEED_COMPENSATION_MODEL pmodel, enum AIRSPEED_SENSOR_MODEL smodel, float tube_len, float tube_dia_mm, float differential_pressure, float pressure_ambient, float temperature_celsius)
Calculate indicated airspeed.
struct uart_esc::@19 _parameter_handles
perf_counter_t _loop_perf
loop performance counter
static void parameter_update_poll()
poll parameter update
#define PX4_MAX_ADC_CHANNELS
void parameter_update_poll(bool forced=false)
Check for changes in parameters.
float calc_TAS_from_EAS(float speed_equivalent, float pressure_ambient, float temperature_celsius)
Calculate true airspeed (TAS) from equivalent airspeed (EAS).
uORB::Subscription _diff_pres_sub
raw differential pressure subscription
#define AIRSPEED0_DEVICE_PATH
void setRelativeTimestamps(sensor_combined_s &raw)
set the relative timestamps of each sensor timestamp, based on the last sensorsPoll, so that the data can be published.
Sensors(bool hil_enabled)
int main(int argc, char **argv)
int adc_init()
Do adc-related initialisation.
#define DRV_DIFF_PRESS_DEVTYPE_SDP33
uORB::Publication< sensor_preflight_s > _sensor_preflight
sensor preflight topic
float air_temperature_celsius
static int custom_command(int argc, char *argv[])
VehicleAcceleration _vehicle_acceleration
static void print_usage()
#define DRV_DIFF_PRESS_DEVTYPE_SDP31
uORB::Subscription _parameter_update_sub
notification of parameter updates
void initializeSensors()
This tries to find new sensor instances.
High-resolution timer with callouts and timekeeping.
int init(sensor_combined_s &raw)
initialize subscriptions etc.
void calcMagInconsistency(sensor_preflight_s &preflt)
Calculates the magnitude in Gauss of the largest difference between the primary and any other magneto...
Global flash based parameter store.
uORB::Subscription _vcontrol_mode_sub
vehicle control mode subscription
uORB::Publication< vehicle_magnetometer_s > _magnetometer_pub
combined sensor data topic
uORB::Publication< vehicle_air_data_s > _airdata_pub
combined sensor data topic
int print_status() override
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
float diff_pres_offset_pa
VehicleAngularVelocity _vehicle_angular_velocity
void diff_pres_poll(const vehicle_air_data_s &airdata)
Poll the differential pressure sensor for updated data.
bool publish(const T &data)
Publish the struct.
airspeed scaling factors; out = (in * Vscale) + offset
static void parameters_update()
update all parameters
DataValidator _airspeed_validator
data validator to monitor airspeed
void print()
Print the validator value.
void set_timeout(uint32_t timeout_interval_us)
Set the timeout value.
static int task_spawn(int argc, char *argv[])
static Sensors * instantiate(int argc, char *argv[])
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
float indicated_airspeed_m_s
uORB::Publication< airspeed_s > _airspeed_pub
airspeed
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.
static int print_usage(const char *reason=nullptr)
void initialize_parameter_handles(ParameterHandles ¶meter_handles)
initialize ParameterHandles struct
__EXPORT int sensors_main(int argc, char *argv[])
#define AIRSPEEDIOCSSCALE
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.
void set_equal_value_threshold(uint32_t threshold)
Set the equal count threshold.
void calcAccelInconsistency(sensor_preflight_s &preflt)
Calculates the magnitude in m/s/s of the largest difference between the primary and any other accel s...
VotedSensorsUpdate _voted_sensors_update
AIRSPEED_COMPENSATION_MODEL
bool _armed
arming status of the vehicle
void parametersUpdate()
call this whenever parameters got updated.
const bool _hil_enabled
if true, HIL is active
int parameters_update()
Update our local parameter cache.
Parameters _parameters
local copies of interesting parameters
void put(uint64_t timestamp, float val, uint64_t error_count, int priority)
Put an item into the validator.
Base publication multi wrapper class.
bool update(void *dst)
Update the struct.
uORB::Publication< sensor_combined_s > _sensor_pub
combined sensor data topic
void adc_poll()
Poll the ADC and update readings to suit.
float confidence(uint64_t timestamp)
Get the confidence of this validator.
Airspeed driver interface.
bool copy(void *dst)
Copy the struct.
int adc_init(void)
Sensors/misc inputs.
void deinit()
deinitialize the object (we cannot use the destructor because it is called on the wrong thread) ...
void checkFailover()
check if a failover event occured.
void perf_begin(perf_counter_t handle)
Begin a performance event.
void calcGyroInconsistency(sensor_preflight_s &preflt)
Calculates the magnitude in rad/s of the largest difference between the primary and any other gyro se...
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
Performance measuring tools.
#define DRV_DIFF_PRESS_DEVTYPE_SDP32