@@ -57,21 +57,52 @@ LandDetector::~LandDetector()
57
57
58
58
void LandDetector::start ()
59
59
{
60
- _update_params ( );
61
- ScheduleOnInterval (LAND_DETECTOR_UPDATE_INTERVAL );
60
+ ScheduleDelayed (50_ms );
61
+ _vehicle_local_position_sub. registerCallback ( );
62
62
}
63
63
64
64
void LandDetector::Run ()
65
65
{
66
+ // push backup schedule
67
+ ScheduleDelayed (50_ms);
68
+
66
69
perf_begin (_cycle_perf);
67
70
68
- if (_parameter_update_sub.updated ()) {
71
+ if (_parameter_update_sub.updated () || (_land_detected.timestamp == 0 )) {
72
+ parameter_update_s param_update;
73
+ _parameter_update_sub.copy (¶m_update);
74
+
75
+ updateParams ();
69
76
_update_params ();
77
+
78
+ _total_flight_time = static_cast <uint64_t >(_param_total_flight_time_high.get ()) << 32 ;
79
+ _total_flight_time |= static_cast <uint32_t >(_param_total_flight_time_low.get ());
80
+ }
81
+
82
+ actuator_armed_s actuator_armed;
83
+
84
+ if (_actuator_armed_sub.update (&actuator_armed)) {
85
+ _armed = actuator_armed.armed ;
70
86
}
71
87
72
- _actuator_armed_sub.update (&_actuator_armed);
88
+ vehicle_acceleration_s vehicle_acceleration;
89
+
90
+ if (_vehicle_acceleration_sub.update (&vehicle_acceleration)) {
91
+ _acceleration = matrix::Vector3f{vehicle_acceleration.xyz };
92
+ }
93
+
94
+ _vehicle_local_position_sub.update (&_vehicle_local_position);
95
+ _vehicle_status_sub.update (&_vehicle_status);
96
+
73
97
_update_topics ();
74
- _update_state ();
98
+
99
+ const hrt_abstime now_us = hrt_absolute_time ();
100
+
101
+ _freefall_hysteresis.set_state_and_update (_get_freefall_state (), now_us);
102
+ _ground_contact_hysteresis.set_state_and_update (_get_ground_contact_state (), now_us);
103
+ _maybe_landed_hysteresis.set_state_and_update (_get_maybe_landed_state (), now_us);
104
+ _landed_hysteresis.set_state_and_update (_get_landed_state (), now_us);
105
+ _ground_effect_hysteresis.set_state_and_update (_get_ground_effect_state (), now_us);
75
106
76
107
const bool freefallDetected = _freefall_hysteresis.get_state ();
77
108
const bool ground_contactDetected = _ground_contact_hysteresis.get_state ();
@@ -80,8 +111,6 @@ void LandDetector::Run()
80
111
const float alt_max = _get_max_altitude () > 0 .0f ? _get_max_altitude () : INFINITY;
81
112
const bool in_ground_effect = _ground_effect_hysteresis.get_state ();
82
113
83
- const hrt_abstime now = hrt_absolute_time ();
84
-
85
114
// publish at 1 Hz, very first time, or when the result has changed
86
115
if ((hrt_elapsed_time (&_land_detected.timestamp ) >= 1_s) ||
87
116
(_land_detected.landed != landDetected) ||
@@ -93,24 +122,23 @@ void LandDetector::Run()
93
122
94
123
if (!landDetected && _land_detected.landed && _takeoff_time == 0 ) { /* only set take off time once, until disarming */
95
124
// We did take off
96
- _takeoff_time = now ;
125
+ _takeoff_time = now_us ;
97
126
}
98
127
99
- _land_detected.timestamp = hrt_absolute_time ();
100
128
_land_detected.landed = landDetected;
101
129
_land_detected.freefall = freefallDetected;
102
130
_land_detected.maybe_landed = maybe_landedDetected;
103
131
_land_detected.ground_contact = ground_contactDetected;
104
132
_land_detected.alt_max = alt_max;
105
133
_land_detected.in_ground_effect = in_ground_effect;
106
-
134
+ _land_detected. timestamp = hrt_absolute_time ();
107
135
_vehicle_land_detected_pub.publish (_land_detected);
108
136
}
109
137
110
138
// set the flight time when disarming (not necessarily when landed, because all param changes should
111
139
// happen on the same event and it's better to set/save params while not in armed state)
112
- if (_takeoff_time != 0 && !_actuator_armed. armed && _previous_armed_state) {
113
- _total_flight_time += now - _takeoff_time;
140
+ if (_takeoff_time != 0 && !_armed && _previous_armed_state) {
141
+ _total_flight_time += now_us - _takeoff_time;
114
142
_takeoff_time = 0 ;
115
143
116
144
uint32_t flight_time = (_total_flight_time >> 32 ) & 0xffffffff ;
@@ -124,7 +152,7 @@ void LandDetector::Run()
124
152
_param_total_flight_time_low.commit_no_notification ();
125
153
}
126
154
127
- _previous_armed_state = _actuator_armed. armed ;
155
+ _previous_armed_state = _armed ;
128
156
129
157
perf_end (_cycle_perf);
130
158
@@ -134,36 +162,4 @@ void LandDetector::Run()
134
162
}
135
163
}
136
164
137
- void LandDetector::_update_params ()
138
- {
139
- parameter_update_s param_update;
140
- _parameter_update_sub.copy (¶m_update);
141
-
142
- updateParams ();
143
- _update_total_flight_time ();
144
- }
145
-
146
- void LandDetector::_update_state ()
147
- {
148
- const hrt_abstime now_us = hrt_absolute_time ();
149
- _freefall_hysteresis.set_state_and_update (_get_freefall_state (), now_us);
150
- _ground_contact_hysteresis.set_state_and_update (_get_ground_contact_state (), now_us);
151
- _maybe_landed_hysteresis.set_state_and_update (_get_maybe_landed_state (), now_us);
152
- _landed_hysteresis.set_state_and_update (_get_landed_state (), now_us);
153
- _ground_effect_hysteresis.set_state_and_update (_get_ground_effect_state (), now_us);
154
- }
155
-
156
- void LandDetector::_update_topics ()
157
- {
158
- _actuator_armed_sub.update (&_actuator_armed);
159
- _vehicle_acceleration_sub.update (&_vehicle_acceleration);
160
- _vehicle_local_position_sub.update (&_vehicle_local_position);
161
- }
162
-
163
- void LandDetector::_update_total_flight_time ()
164
- {
165
- _total_flight_time = static_cast <uint64_t >(_param_total_flight_time_high.get ()) << 32 ;
166
- _total_flight_time |= static_cast <uint32_t >(_param_total_flight_time_low.get ());
167
- }
168
-
169
165
} // namespace land_detector
0 commit comments