PX4 Firmware
PX4 Autopilot Software http://px4.io
output_limit.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-2015 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "output_limit.h"
35 
36 #include <px4_platform_common/defines.h>
37 #include <math.h>
38 #include <stdbool.h>
39 #include <drivers/drv_hrt.h>
40 #include <stdio.h>
41 
42 #define PROGRESS_INT_SCALING 10000
43 
45 {
47  limit->time_armed = 0;
48  limit->ramp_up = true;
49 }
50 
51 void output_limit_calc(const bool armed, const bool pre_armed, const unsigned num_channels, const uint16_t reverse_mask,
52  const uint16_t *disarmed_output, const uint16_t *min_output, const uint16_t *max_output,
53  const float *output, uint16_t *effective_output, output_limit_t *limit)
54 {
55 
56  /* first evaluate state changes */
57  switch (limit->state) {
59 
60  if (armed) {
61 
62  /* set arming time for the first call */
63  if (limit->time_armed == 0) {
64  limit->time_armed = hrt_absolute_time();
65  }
66 
67  if (hrt_elapsed_time(&limit->time_armed) >= INIT_TIME_US) {
69  }
70  }
71 
72  break;
73 
75  if (armed) {
76  if (limit->ramp_up) {
78 
79  } else {
81  }
82 
83  /* reset arming time, used for ramp timing */
84  limit->time_armed = hrt_absolute_time();
85  }
86 
87  break;
88 
90  if (!armed) {
92 
93  } else if (hrt_elapsed_time(&limit->time_armed) >= RAMP_TIME_US) {
95  }
96 
97  break;
98 
100  if (!armed) {
101  limit->state = OUTPUT_LIMIT_STATE_OFF;
102  }
103 
104  break;
105 
106  default:
107  break;
108  }
109 
110  /* if the system is pre-armed, the limit state is temporarily on,
111  * as some outputs are valid and the non-valid outputs have been
112  * set to NaN. This is not stored in the state machine though,
113  * as the throttle channels need to go through the ramp at
114  * regular arming time.
115  */
116 
117  unsigned local_limit_state = limit->state;
118 
119  if (pre_armed) {
120  local_limit_state = OUTPUT_LIMIT_STATE_ON;
121  }
122 
123  unsigned progress;
124 
125  /* then set effective_output based on state */
126  switch (local_limit_state) {
129  for (unsigned i = 0; i < num_channels; i++) {
130  effective_output[i] = disarmed_output[i];
131  }
132 
133  break;
134 
136  hrt_abstime diff = hrt_elapsed_time(&limit->time_armed);
137 
138  progress = diff * PROGRESS_INT_SCALING / RAMP_TIME_US;
139 
140  if (progress > PROGRESS_INT_SCALING) {
141  progress = PROGRESS_INT_SCALING;
142  }
143 
144  for (unsigned i = 0; i < num_channels; i++) {
145 
146  float control_value = output[i];
147 
148  /* check for invalid / disabled channels */
149  if (!PX4_ISFINITE(control_value)) {
150  effective_output[i] = disarmed_output[i];
151  continue;
152  }
153 
154  uint16_t ramp_min_output;
155 
156  /* if a disarmed output value was set, blend between disarmed and min */
157  if (disarmed_output[i] > 0) {
158 
159  /* safeguard against overflows */
160  unsigned disarmed = disarmed_output[i];
161 
162  if (disarmed > min_output[i]) {
163  disarmed = min_output[i];
164  }
165 
166  unsigned disarmed_min_diff = min_output[i] - disarmed;
167  ramp_min_output = disarmed + (disarmed_min_diff * progress) / PROGRESS_INT_SCALING;
168 
169  } else {
170 
171  /* no disarmed output value set, choose min output */
172  ramp_min_output = min_output[i];
173  }
174 
175  if (reverse_mask & (1 << i)) {
176  control_value = -1.0f * control_value;
177  }
178 
179  effective_output[i] = control_value * (max_output[i] - ramp_min_output) / 2 + (max_output[i] + ramp_min_output) / 2;
180 
181  /* last line of defense against invalid inputs */
182  if (effective_output[i] < ramp_min_output) {
183  effective_output[i] = ramp_min_output;
184 
185  } else if (effective_output[i] > max_output[i]) {
186  effective_output[i] = max_output[i];
187  }
188  }
189  }
190  break;
191 
193 
194  for (unsigned i = 0; i < num_channels; i++) {
195 
196  float control_value = output[i];
197 
198  /* check for invalid / disabled channels */
199  if (!PX4_ISFINITE(control_value)) {
200  effective_output[i] = disarmed_output[i];
201  continue;
202  }
203 
204  if (reverse_mask & (1 << i)) {
205  control_value = -1.0f * control_value;
206  }
207 
208  effective_output[i] = control_value * (max_output[i] - min_output[i]) / 2 + (max_output[i] + min_output[i]) / 2;
209 
210  /* last line of defense against invalid inputs */
211  if (effective_output[i] < min_output[i]) {
212  effective_output[i] = min_output[i];
213 
214  } else if (effective_output[i] > max_output[i]) {
215  effective_output[i] = max_output[i];
216  }
217 
218  }
219 
220  break;
221 
222  default:
223  break;
224  }
225 
226 }
void output_limit_calc(const bool armed, const bool pre_armed, const unsigned num_channels, const uint16_t reverse_mask, const uint16_t *disarmed_output, const uint16_t *min_output, const uint16_t *max_output, const float *output, uint16_t *effective_output, output_limit_t *limit)
bool ramp_up
if true, motors will ramp up from disarmed to min_output after arming
Definition: output_limit.h:69
#define PROGRESS_INT_SCALING
High-resolution timer with callouts and timekeeping.
#define RAMP_TIME_US
Definition: output_limit.h:57
enum output_limit_state state
Definition: output_limit.h:67
static struct actuator_armed_s armed
Definition: Commander.cpp:139
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
Definition: drv_hrt.h:102
uint64_t time_armed
Definition: output_limit.h:68
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
void output_limit_init(output_limit_t *limit)
Library for output limiting (PWM for example)
#define INIT_TIME_US
Definition: output_limit.h:53
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).