PX4 Firmware
PX4 Autopilot Software http://px4.io
rcCalibrationCheck.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2019 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 "../PreFlightCheck.hpp"
35 
36 #include <parameters/param.h>
37 #include <systemlib/mavlink_log.h>
38 #include <uORB/topics/input_rc.h>
39 
40 /**
41  * Maximum deadzone value
42  */
43 #define RC_INPUT_MAX_DEADZONE_US 500
44 
45 /**
46  * Minimum value
47  */
48 #define RC_INPUT_LOWEST_MIN_US 500
49 
50 /**
51  * Maximum value
52  */
53 #define RC_INPUT_HIGHEST_MAX_US 2500
54 
56 {
57  char nbuf[20];
58  param_t _parameter_handles_min, _parameter_handles_trim, _parameter_handles_max,
59  _parameter_handles_rev, _parameter_handles_dz;
60 
61  unsigned map_fail_count = 0;
62 
63  const char *rc_map_mandatory[] = { /*"RC_MAP_MODE_SW",*/
64  /* needs discussion if this should be mandatory "RC_MAP_POSCTL_SW"*/
65  nullptr /* end marker */
66  };
67 
68  unsigned j = 0;
69 
70  /* if VTOL, check transition switch mapping */
71  if (isVTOL) {
72  param_t trans_parm = param_find("RC_MAP_TRANS_SW");
73 
74  if (trans_parm == PARAM_INVALID) {
75  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC_MAP_TRANS_SW PARAMETER MISSING."); }
76 
77  /* give system time to flush error message in case there are more */
78  px4_usleep(100000);
79  map_fail_count++;
80 
81  } else {
82  int32_t transition_switch;
83  param_get(trans_parm, &transition_switch);
84 
85  if (transition_switch < 1) {
86  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "Transition switch RC_MAP_TRANS_SW not set."); }
87 
88  map_fail_count++;
89  }
90  }
91  }
92 
93  /* first check channel mappings */
94  while (rc_map_mandatory[j] != nullptr) {
95 
96  param_t map_parm = param_find(rc_map_mandatory[j]);
97 
98  if (map_parm == PARAM_INVALID) {
99  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: PARAM %s MISSING.", rc_map_mandatory[j]); }
100 
101  /* give system time to flush error message in case there are more */
102  px4_usleep(100000);
103  map_fail_count++;
104  j++;
105  continue;
106  }
107 
108  int32_t mapping;
109  param_get(map_parm, &mapping);
110 
111  if (mapping > input_rc_s::RC_INPUT_MAX_CHANNELS) {
112  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: %s >= NUMBER OF CHANNELS.", rc_map_mandatory[j]); }
113 
114  /* give system time to flush error message in case there are more */
115  px4_usleep(100000);
116  map_fail_count++;
117  }
118 
119  if (mapping == 0) {
120  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: mandatory %s is unmapped.", rc_map_mandatory[j]); }
121 
122  /* give system time to flush error message in case there are more */
123  px4_usleep(100000);
124  map_fail_count++;
125  }
126 
127  j++;
128  }
129 
130  unsigned total_fail_count = 0;
131  unsigned channels_failed = 0;
132 
133  for (unsigned i = 0; i < input_rc_s::RC_INPUT_MAX_CHANNELS; i++) {
134  /* should the channel be enabled? */
135  uint8_t count = 0;
136 
137  /* initialize values to values failing the check */
138  float param_min = 0.0f;
139  float param_max = 0.0f;
140  float param_trim = 0.0f;
141  float param_rev = 0.0f;
142  float param_dz = RC_INPUT_MAX_DEADZONE_US * 2.0f;
143 
144  /* min values */
145  sprintf(nbuf, "RC%d_MIN", i + 1);
146  _parameter_handles_min = param_find(nbuf);
147  param_get(_parameter_handles_min, &param_min);
148 
149  /* trim values */
150  sprintf(nbuf, "RC%d_TRIM", i + 1);
151  _parameter_handles_trim = param_find(nbuf);
152  param_get(_parameter_handles_trim, &param_trim);
153 
154  /* max values */
155  sprintf(nbuf, "RC%d_MAX", i + 1);
156  _parameter_handles_max = param_find(nbuf);
157  param_get(_parameter_handles_max, &param_max);
158 
159  /* channel reverse */
160  sprintf(nbuf, "RC%d_REV", i + 1);
161  _parameter_handles_rev = param_find(nbuf);
162  param_get(_parameter_handles_rev, &param_rev);
163 
164  /* channel deadzone */
165  sprintf(nbuf, "RC%d_DZ", i + 1);
166  _parameter_handles_dz = param_find(nbuf);
167  param_get(_parameter_handles_dz, &param_dz);
168 
169  /* assert min..center..max ordering */
170  if (param_min < RC_INPUT_LOWEST_MIN_US) {
171  count++;
172 
173  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: RC%d_MIN < %u.", i + 1, RC_INPUT_LOWEST_MIN_US); }
174 
175  /* give system time to flush error message in case there are more */
176  px4_usleep(100000);
177  }
178 
179  if (param_max > RC_INPUT_HIGHEST_MAX_US) {
180  count++;
181 
182  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: RC%d_MAX > %u.", i + 1, RC_INPUT_HIGHEST_MAX_US); }
183 
184  /* give system time to flush error message in case there are more */
185  px4_usleep(100000);
186  }
187 
188  if (param_trim < param_min) {
189  count++;
190 
191  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: RC%d_TRIM < MIN (%d/%d).", i + 1, (int)param_trim, (int)param_min); }
192 
193  /* give system time to flush error message in case there are more */
194  px4_usleep(100000);
195  }
196 
197  if (param_trim > param_max) {
198  count++;
199 
200  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: RC%d_TRIM > MAX (%d/%d).", i + 1, (int)param_trim, (int)param_max); }
201 
202  /* give system time to flush error message in case there are more */
203  px4_usleep(100000);
204  }
205 
206  /* assert deadzone is sane */
207  if (param_dz > RC_INPUT_MAX_DEADZONE_US) {
208  if (report_fail) { mavlink_log_critical(mavlink_log_pub, "RC ERROR: RC%d_DZ > %u.", i + 1, RC_INPUT_MAX_DEADZONE_US); }
209 
210  /* give system time to flush error message in case there are more */
211  px4_usleep(100000);
212  count++;
213  }
214 
215  total_fail_count += count;
216 
217  if (count) {
218  channels_failed++;
219  }
220  }
221 
222  if (channels_failed) {
223  px4_sleep(2);
224 
225  if (report_fail) {
226  mavlink_log_critical(mavlink_log_pub, "%d config error%s for %d RC channel%s.",
227  total_fail_count,
228  (total_fail_count > 1) ? "s" : "", channels_failed, (channels_failed > 1) ? "s" : "");
229  }
230 
231  px4_usleep(100000);
232  }
233 
234  return total_fail_count + map_fail_count;
235 }
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
Definition: param.h:103
#define RC_INPUT_MAX_DEADZONE_US
Maximum deadzone value.
static orb_advert_t * mavlink_log_pub
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
Definition: parameters.cpp:589
Global flash based parameter store.
static int rcCalibrationCheck(orb_advert_t *mavlink_log_pub, bool report_fail, bool isVTOL)
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
Definition: parameters.cpp:370
#define RC_INPUT_HIGHEST_MAX_US
Maximum value.
#define RC_INPUT_LOWEST_MIN_US
Minimum value.
uint32_t param_t
Parameter handle.
Definition: param.h:98