MAV'RIC
|
00001 /******************************************************************************* 00002 * Copyright (c) 2009-2016, MAV'RIC Development Team 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * 2. Redistributions in binary form must reproduce the above copyright notice, 00012 * this list of conditions and the following disclaimer in the documentation 00013 * and/or other materials provided with the distribution. 00014 * 00015 * 3. Neither the name of the copyright holder nor the names of its contributors 00016 * may be used to endorse or promote products derived from this software without 00017 * specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00029 * POSSIBILITY OF SUCH DAMAGE. 00030 ******************************************************************************/ 00031 00032 /******************************************************************************* 00033 * \file joystick.hpp 00034 * 00035 * \author MAV'RIC Team 00036 * \author Basil Huber 00037 * 00038 * \brief This file is to decode the set manual command message from MAVLink 00039 * 00040 ******************************************************************************/ 00041 00042 00043 #ifndef JOYSTICK_HPP_ 00044 #define JOYSTICK_HPP_ 00045 00046 #include "status/state.hpp" 00047 #include "control/control_command.hpp" 00048 #include "control/controller.hpp" 00049 00050 00051 00052 #define MAX_JOYSTICK_RANGE 0.8 ///< Scale down the joystick channel amplitude, as done in remote 00053 00054 00055 class Joystick 00056 { 00057 public: 00058 00062 typedef enum 00063 { 00064 BUTTON_UNPRESSED = 0, 00065 BUTTON_PRESSED = 1, 00066 } button_pressed_t; 00067 00068 enum class throttle_mode_t 00069 { 00070 ZERO_CENTER = 0, //< throttle stick in center is zero throttle 00071 ZERO_DOWN //< throttle stick all the way down is zero throttle 00072 }; 00073 00074 00075 00079 typedef union 00080 { 00081 uint16_t button_mask; 00082 // unamed bitfield structure, use to access directly the flags 00083 struct 00084 { 00085 button_pressed_t button_16 : 1; 00086 button_pressed_t button_15 : 1; 00087 button_pressed_t button_14 : 1; 00088 button_pressed_t button_13 : 1; 00089 button_pressed_t button_12 : 1; 00090 button_pressed_t button_11 : 1; 00091 button_pressed_t button_10 : 1; 00092 button_pressed_t button_9 : 1; 00093 button_pressed_t button_8 : 1; 00094 button_pressed_t button_7 : 1; 00095 button_pressed_t button_6 : 1; 00096 button_pressed_t button_5 : 1; 00097 button_pressed_t button_4 : 1; 00098 button_pressed_t button_3 : 1; 00099 button_pressed_t button_2 : 1; 00100 button_pressed_t button_1 : 1; 00101 }; 00102 // identical bitfield, but named (useful for initialisation) 00103 struct 00104 { 00105 button_pressed_t button_16 : 1; 00106 button_pressed_t button_15 : 1; 00107 button_pressed_t button_14 : 1; 00108 button_pressed_t button_13 : 1; 00109 button_pressed_t button_12 : 1; 00110 button_pressed_t button_11 : 1; 00111 button_pressed_t button_10 : 1; 00112 button_pressed_t button_9 : 1; 00113 button_pressed_t button_8 : 1; 00114 button_pressed_t button_7 : 1; 00115 button_pressed_t button_6 : 1; 00116 button_pressed_t button_5 : 1; 00117 button_pressed_t button_4 : 1; 00118 button_pressed_t button_3 : 1; 00119 button_pressed_t button_2 : 1; 00120 button_pressed_t button_1 : 1; 00121 } button; 00122 } button_t; 00123 00127 struct channels_t 00128 { 00129 float x; // Longitudinal (pitch) 00130 float y; // Lateral (roll) 00131 float z; // Vertical (thrust) 00132 float r; // Rotation (yaw) 00133 }; 00134 00135 00139 struct conf_t 00140 { 00141 throttle_mode_t throttle_mode; 00142 channels_t scale_attitude; 00143 channels_t scale_velocity; 00144 }; 00145 00146 00150 Joystick(conf_t conf = default_config()); 00151 00157 float throttle() const; 00158 00159 00165 float roll() const; 00166 00167 00173 float pitch() const; 00174 00175 00181 float yaw() const; 00182 00183 00189 Mav_mode get_mode(const Mav_mode current_mode) ; 00190 00196 void button_update(uint16_t buttons); 00197 00198 00202 static inline conf_t default_config(); 00203 00204 00205 button_t buttons_; 00206 channels_t channels_; 00207 Mav_mode mav_mode_desired_; 00208 00209 private: 00210 arm_action_t arm_action_; 00211 throttle_mode_t throttle_mode_; 00212 }; 00213 00214 00215 Joystick::conf_t Joystick::default_config() 00216 { 00217 conf_t conf; 00218 conf.throttle_mode = throttle_mode_t::ZERO_DOWN; 00219 return conf; 00220 } 00221 00222 #endif // JOYSTICK_HPP_