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 : VICON (visual positioning) simulation 17 : 18 : ./Tools/autotest/sim_vehicle.py --gdb --debug -v ArduCopter -A --serial5=sim:vicon: 19 : param set SERIAL5_PROTOCOL 1 20 : graph SIMSTATE.lat-GPS_GLOBAL_ORIGIN.latitude SIMSTATE.lng-GPS_GLOBAL_ORIGIN.longitude 21 : reboot 22 : 23 : mavproxy.py --master tcp:localhost:5763 --source-system=72 24 : vehicle 17 25 : graph VISION_POSITION_ESTIMATE.x VISION_POSITION_ESTIMATE.y 26 : 27 : */ 28 : 29 : #pragma once 30 : 31 : #include "SIM_config.h" 32 : 33 : #if AP_SIM_VICON_ENABLED 34 : 35 : #include "SIM_Aircraft.h" 36 : 37 : #include <SITL/SITL.h> 38 : 39 : #include <AP_HAL/utility/RingBuffer.h> 40 : 41 : #include "SIM_SerialDevice.h" 42 : 43 : namespace SITL { 44 : 45 : class Vicon : public SerialDevice { 46 : public: 47 : 48 : Vicon(); 49 : 50 : // update state 51 : void update(const Location &loc, const Vector3d &position, const Vector3f &velocity, const Quaternion &attitude); 52 : 53 : private: 54 : 55 : // TODO: make these parameters: 56 : const uint8_t system_id = 17; 57 : const uint8_t component_id = 18; 58 : 59 : uint64_t last_observation_usec; // time last observation was sent 60 : uint64_t time_offset_us; // simulated timeoffset between external system and autopilot 61 : 62 : // buffer of messages to send 63 : struct { 64 : uint64_t time_send_us; // system time this message should be sent or 0 if no message to send 65 : mavlink_message_t obs_msg; // message to be sent 66 : } msg_buf[3]; 67 : 68 : // SIM_VICON_TYPE parameter bit values 69 : enum class ViconTypeMask : uint8_t { 70 : VISION_POSITION_ESTIMATE = (1 << 0), 71 : VISION_SPEED_ESTIMATE = (1 << 1), 72 : VICON_POSITION_ESTIMATE = (1 << 2), 73 : VISION_POSITION_DELTA = (1 << 3), 74 : ODOMETRY = (1 << 4), 75 : }; 76 : 77 : // return true if the given message type should be sent 78 77335 : bool should_send(ViconTypeMask type_mask) const { return (((uint8_t)type_mask & _sitl->vicon.type_mask.get()) > 0); } 79 : 80 : // get unused index in msg_buf 81 : bool get_free_msg_buf_index(uint8_t &index); 82 : 83 : void update_vicon_position_estimate(const Location &loc, 84 : const Vector3d &position, 85 : const Vector3f &velocity, 86 : const Quaternion &attitude); 87 : 88 : void maybe_send_heartbeat(); 89 : uint32_t last_heartbeat_ms; 90 : 91 : // position delta message 92 : Quaternion _attitude_prev; // Rotation to previous MAV_FRAME_BODY_FRD from MAV_FRAME_LOCAL_NED 93 : Vector3d _position_prev; // previous position from origin (m) MAV_FRAME_LOCAL_NED 94 : 95 : mavlink_status_t mav_status; 96 : }; 97 : 98 : } 99 : 100 : #endif // AP_SIM_VICON_ENABLED