Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : /* 16 : simulator connection for ardupilot version of Xplane 17 : */ 18 : 19 : #pragma once 20 : 21 : #include "SIM_config.h" 22 : 23 : #if AP_SIM_XPLANE_ENABLED 24 : 25 : #include <AP_HAL/utility/Socket_native.h> 26 : #include <AP_Filesystem/AP_Filesystem.h> 27 : 28 : #include "SIM_Aircraft.h" 29 : #include <AP_JSON/AP_JSON.h> 30 : 31 : namespace SITL { 32 : 33 : /* 34 : a Xplane simulator 35 : */ 36 : class XPlane : public Aircraft { 37 : public: 38 : XPlane(const char *frame_str); 39 : 40 : /* update model by one time step */ 41 : void update(const struct sitl_input &input) override; 42 : 43 : /* static object creator */ 44 0 : static Aircraft *create(const char *frame_str) { 45 0 : return NEW_NOTHROW XPlane(frame_str); 46 : } 47 : 48 : private: 49 : 50 : bool receive_data(void); 51 : void send_dref(const char *name, float value); 52 : void request_drefs(void); 53 : void request_dref(const char *name, uint8_t code, uint32_t rate_hz); 54 : void send_drefs(const struct sitl_input &input); 55 : void handle_rref(const uint8_t *p, uint32_t len); 56 : void select_data(void); 57 : void deselect_code(uint8_t code); 58 : int8_t find_data_index(uint8_t id); 59 : 60 : // return true if at least X 61 0 : bool is_xplane12(void) const { 62 0 : return xplane_version / 10000 >= 12; 63 : } 64 : 65 : const char *xplane_ip = "127.0.0.1"; 66 : uint16_t xplane_port = 49000; 67 : uint16_t bind_port = 49001; 68 : // udp socket, input and output 69 : SocketAPM_native socket_in{true}; 70 : SocketAPM_native socket_out{true}; 71 : 72 : uint64_t time_base_us; 73 : uint32_t last_data_time_ms; 74 : Vector3d position_zero; 75 : Vector3f accel_earth; 76 : bool connected = false; 77 : uint32_t xplane_frame_time; 78 : uint64_t seen_mask; 79 : 80 : struct { 81 : uint32_t last_report_ms; 82 : uint32_t data_count; 83 : uint32_t frame_count; 84 : } report; 85 : 86 : enum class DRefType { 87 : ANGLE = 0, 88 : RANGE = 1, 89 : FIXED = 2, 90 : }; 91 : 92 : struct DRef { 93 : struct DRef *next; 94 : char *name; 95 : DRefType type; 96 : uint8_t channel; 97 : float range; 98 : float fixed_value; 99 : }; 100 : 101 : // list of DRefs; 102 : struct DRef *drefs; 103 : uint32_t dref_debug; 104 : 105 : enum class JoyType { 106 : AXIS = 0, 107 : BUTTON = 1, 108 : }; 109 : 110 : // list of joystick inputs 111 : struct JoyInput { 112 : struct JoyInput *next; 113 : uint8_t axis; 114 : uint8_t channel; 115 : JoyType type; 116 : float input_min, input_max; 117 : uint32_t mask; 118 : }; 119 : struct JoyInput *joyinputs; 120 : 121 : char *map_filename; 122 : struct stat map_st; 123 : 124 : bool load_dref_map(const char *map_json); 125 : void add_dref(const char *name, DRefType type, const AP_JSON::value &dref); 126 : void add_joyinput(const char *name, JoyType type, const AP_JSON::value &d); 127 : void handle_setting(const AP_JSON::value &d); 128 : 129 : void check_reload_dref(void); 130 : 131 : uint32_t xplane_version; 132 : }; 133 : 134 : 135 : } // namespace SITL 136 : 137 : 138 : #endif // AP_SIM_XPLANE_ENABLED