command_manager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson and Daniel Koch, BYU MAGICC Lab
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <stdbool.h>
33 #include <stdlib.h>
34 
35 #include "command_manager.h"
36 #include "rosflight.h"
37 
38 namespace rosflight_firmware
39 {
40 
41 typedef enum
42 {
45 } att_mode_t;
46 
47 typedef struct
48 {
52 
54 {
55  { RC::STICK_X, 0 },
56  { RC::STICK_Y, 0 },
57  { RC::STICK_Z, 0 }
58 };
59 
60 typedef struct
61 {
65 } mux_t;
66 
68  RF_(_rf),
69  failsafe_command_(multirotor_failsafe_command_)
70 {}
71 
73 {
74  RF_.params_.add_callback([this](uint16_t param_id)
75  {
76  this->param_change_callback(param_id);
77  }, PARAM_FIXED_WING);
78  RF_.params_.add_callback([this](uint16_t param_id)
79  {
80  this->param_change_callback(param_id);
82 
83  init_failsafe();
84 }
85 
87 {
88  (void) param_id; // suppress unused parameter warning
89  init_failsafe();
90 }
91 
93 {
95 
98  else
100 }
101 
103 {
104  // get initial, unscaled RC values
109 
110  // determine control mode for each channel and scale command values accordingly
112  {
117  }
118  else
119  {
120  // roll and pitch
121  control_type_t roll_pitch_type;
123  {
124  roll_pitch_type = RF_.rc_.switch_on(RC::SWITCH_ATT_TYPE) ? ANGLE : RATE;
125  }
126  else
127  {
129  }
130 
131  rc_command_.x.type = roll_pitch_type;
132  rc_command_.y.type = roll_pitch_type;
133 
134  // Scale command to appropriate units
135  switch (roll_pitch_type)
136  {
137  case RATE:
140  break;
141  case ANGLE:
144  default:
145  break;
146  }
147 
148  // yaw
151 
152  // throttle
154  }
155 }
156 
158 {
159  uint32_t now = RF_.board_.clock_millis();
160 
161  // if we are still in the lag time, return true
162  if (now < rc_stick_override_[channel].last_override_time + RF_.params_.get_param_int(PARAM_OVERRIDE_LAG_TIME))
163  {
164  return true;
165  }
166  else
167  {
168  if (fabsf(RF_.rc_.stick(rc_stick_override_[channel].rc_channel))
170  {
171  rc_stick_override_[channel].last_override_time = now;
172  return true;
173  }
174  return false;
175  }
176 }
177 
179 {
180  bool override_this_channel = false;
181  //Check if the override switch exists and is triggered, or if the sticks have deviated enough to trigger an override
183  || stick_deviated(channel))
184  {
185  override_this_channel = true;
186  }
187  else // Otherwise only have RC override if the offboard channel is inactive
188  {
189  if (muxes[channel].onboard->active)
190  {
191  override_this_channel = false;
192  }
193  else
194  {
195  override_this_channel = true;
196  }
197  }
198  // set the combined channel output depending on whether RC is overriding for this channel or not
199  *muxes[channel].combined = override_this_channel ? *muxes[channel].rc : *muxes[channel].onboard;
200  return override_this_channel;
201 }
202 
204 {
205  bool override_this_channel = false;
206  // Check if the override switch exists and is triggered
208  {
209  override_this_channel = true;
210  }
211  else // Otherwise check if the offboard throttle channel is active, if it isn't, have RC override
212  {
213  if (muxes[MUX_F].onboard->active)
214  {
215  // Check if the parameter flag is set to have us always take the smaller throttle
217  {
218  override_this_channel = (muxes[MUX_F].rc->value < muxes[MUX_F].onboard->value);
219  }
220  else
221  {
222  override_this_channel = false;
223  }
224  }
225  else
226  {
227  override_this_channel = true;
228  }
229  }
230 
231  // Set the combined channel output depending on whether RC is overriding for this channel or not
232  *muxes[MUX_F].combined = override_this_channel ? *muxes[MUX_F].rc : *muxes[MUX_F].onboard;
233  return override_this_channel;
234 }
235 
237 {
238  return rc_override_;
239 }
240 
242 {
243  for (int i = 0; i < 4; i++)
244  {
245  if (muxes[i].onboard->active)
246  return true;
247  }
248  return false;
249 }
250 
252 {
253  new_command_ = true;
254  offboard_command_ = new_offboard_command;
255 }
256 
258 {
259  new_command_ = true;
260  rc_command_ = new_rc_command;
261 }
262 
264 {
265  new_command_ = true;
267 }
268 
269 
271 {
272  bool last_rc_override = rc_override_;
273 
274  // Check for and apply failsafe command
276  {
278  }
279  else if (RF_.rc_.new_command())
280  {
281  // Read RC
282  interpret_rc();
283 
284  // Check for offboard control timeout (100 ms)
286  {
287  // If it has been longer than 100 ms, then disable the offboard control
288  offboard_command_.F.active = false;
289  offboard_command_.x.active = false;
290  offboard_command_.y.active = false;
291  offboard_command_.z.active = false;
292  }
293 
294  // Perform muxing
299 
300  // Light to indicate override
301  if (rc_override_)
302  {
303  RF_.board_.led0_on();
304  }
305  else
306  {
307  RF_.board_.led0_off();
308  }
309  }
310 
311  // There was a change in rc_override state
312  if (last_rc_override != rc_override_)
313  {
315  }
316  return true;
317 }
318 
319 
320 }
rc_stick_override_t rc_stick_override_[3]
bool switch_mapped(Switch channel)
Definition: rc.cpp:126
virtual void led0_off()=0
void param_change_callback(uint16_t param_id)
bool switch_on(Switch channel)
Definition: rc.cpp:121
virtual void led0_on()=0
control_channel_t * combined
float stick(Stick channel)
Definition: rc.cpp:116
control_channel_t * rc
float get_param_float(uint16_t id) const
Get the value of a floating point parameter by id.
Definition: param.h:326
rc_stick_override_t rc_stick_override[]
void add_callback(std::function< void(int)> callback, uint16_t param_id)
Definition: param.cpp:308
const State & state() const
Definition: state_manager.h:82
void set_new_offboard_command(control_t new_offboard_command)
virtual uint32_t clock_millis()=0
int get_param_int(uint16_t id) const
Get the value of an integer parameter by id.
Definition: param.h:316
void set_new_rc_command(control_t new_rc_command)
bool do_roll_pitch_yaw_muxing(MuxChannel channel)
bool stick_deviated(MuxChannel channel)
control_channel_t * onboard
bool new_command()
Definition: rc.cpp:356


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24