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/ArduSub/sensors.cpp
Views: 1798
1
#include "Sub.h"
2
3
// return barometric altitude in centimeters
4
void Sub::read_barometer()
5
{
6
barometer.update();
7
// If we are reading a positive altitude, the sensor needs calibration
8
// Even a few meters above the water we should have no significant depth reading
9
if(barometer.get_altitude() > 0) {
10
barometer.update_calibration();
11
}
12
13
if (ap.depth_sensor_present) {
14
sensor_health.depth = barometer.healthy(depth_sensor_idx);
15
}
16
}
17
18
void Sub::init_rangefinder()
19
{
20
#if AP_RANGEFINDER_ENABLED
21
rangefinder.set_log_rfnd_bit(MASK_LOG_CTUN);
22
rangefinder.init(ROTATION_PITCH_270);
23
rangefinder_state.alt_cm_filt.set_cutoff_frequency(RANGEFINDER_WPNAV_FILT_HZ);
24
rangefinder_state.enabled = rangefinder.has_orientation(ROTATION_PITCH_270);
25
#endif
26
}
27
28
// return rangefinder altitude in centimeters
29
void Sub::read_rangefinder()
30
{
31
#if AP_RANGEFINDER_ENABLED
32
rangefinder.update();
33
34
// signal quality ranges from 0 (worst) to 100 (perfect), -1 means n/a
35
int8_t signal_quality_pct = rangefinder.signal_quality_pct_orient(ROTATION_PITCH_270);
36
37
rangefinder_state.alt_healthy =
38
(rangefinder.status_orient(ROTATION_PITCH_270) == RangeFinder::Status::Good) &&
39
(rangefinder.range_valid_count_orient(ROTATION_PITCH_270) >= RANGEFINDER_HEALTH_MAX) &&
40
(signal_quality_pct == -1 || signal_quality_pct >= g.rangefinder_signal_min);
41
42
int16_t temp_alt = rangefinder.distance_cm_orient(ROTATION_PITCH_270);
43
44
#if RANGEFINDER_TILT_CORRECTION
45
// correct alt for angle of the rangefinder
46
temp_alt = (float)temp_alt * MAX(0.707f, ahrs.get_rotation_body_to_ned().c.z);
47
#endif
48
49
rangefinder_state.alt_cm = temp_alt;
50
rangefinder_state.inertial_alt_cm = inertial_nav.get_position_z_up_cm();
51
rangefinder_state.min_cm = rangefinder.min_distance_cm_orient(ROTATION_PITCH_270);
52
rangefinder_state.max_cm = rangefinder.max_distance_cm_orient(ROTATION_PITCH_270);
53
54
// calculate rangefinder_terrain_offset_cm
55
if (rangefinder_state.alt_healthy) {
56
uint32_t now = AP_HAL::millis();
57
if (now - rangefinder_state.last_healthy_ms > RANGEFINDER_TIMEOUT_MS) {
58
// reset filter if we haven't used it within the last second
59
rangefinder_state.alt_cm_filt.reset(rangefinder_state.alt_cm);
60
} else {
61
rangefinder_state.alt_cm_filt.apply(rangefinder_state.alt_cm, 0.05f);
62
}
63
rangefinder_state.last_healthy_ms = now;
64
rangefinder_state.rangefinder_terrain_offset_cm =
65
sub.rangefinder_state.inertial_alt_cm - sub.rangefinder_state.alt_cm_filt.get();
66
}
67
68
// send rangefinder altitude and health to waypoint navigation library
69
wp_nav.set_rangefinder_terrain_offset(
70
rangefinder_state.enabled,
71
rangefinder_state.alt_healthy,
72
rangefinder_state.rangefinder_terrain_offset_cm);
73
circle_nav.set_rangefinder_terrain_offset(
74
rangefinder_state.enabled && wp_nav.rangefinder_used(),
75
rangefinder_state.alt_healthy,
76
rangefinder_state.rangefinder_terrain_offset_cm);
77
#else
78
rangefinder_state.enabled = false;
79
rangefinder_state.alt_healthy = false;
80
rangefinder_state.alt_cm = 0;
81
rangefinder_state.inertial_alt_cm = 0;
82
rangefinder_state.rangefinder_terrain_offset_cm = 0;
83
#endif
84
}
85
86
// return true if rangefinder_alt can be used
87
bool Sub::rangefinder_alt_ok() const
88
{
89
uint32_t now = AP_HAL::millis();
90
return (rangefinder_state.enabled && rangefinder_state.alt_healthy && now - rangefinder_state.last_healthy_ms < RANGEFINDER_TIMEOUT_MS);
91
}
92
93