22 #include <boost/asio.hpp> 23 #include <boost/bind.hpp> 24 #include <boost/shared_array.hpp> 25 #include <boost/system/system_error.hpp> 39 #include <netinet/in.h> 43 #include <sys/socket.h> 45 #include <Eigen/Eigen> 47 #include <gazebo/common/Plugin.hh> 48 #include <gazebo/common/common.hh> 49 #include <gazebo/gazebo.hh> 50 #include <gazebo/msgs/msgs.hh> 51 #include <gazebo/physics/physics.hh> 52 #include <gazebo/transport/transport.hh> 54 #include <CommandMotorSpeed.pb.h> 57 #include <OpticalFlow.pb.h> 58 #include <ignition/math.hh> 62 #include <mavlink/v2.0/common/mavlink.h> 70 using lock_guard = std::lock_guard<std::recursive_mutex>;
95 "/gazebo/command/motor_speed";
113 received_first_reference_(false),
115 protocol_version_(2.0),
116 motor_velocity_reference_pub_topic_(
117 kDefaultMotorVelocityReferencePubTopic),
118 use_propeller_pid_(false),
119 use_elevator_pid_(false),
120 use_left_elevon_pid_(false),
121 use_right_elevon_pid_(false),
122 imu_sub_topic_(kDefaultImuTopic),
123 opticalFlow_sub_topic_(kDefaultOpticalFlowTopic),
124 lidar_sub_topic_(kDefaultLidarTopic),
127 left_elevon_joint_(
nullptr),
128 right_elevon_joint_(
nullptr),
129 elevator_joint_(
nullptr),
130 propeller_joint_(
nullptr),
131 gimbal_yaw_joint_(
nullptr),
132 gimbal_pitch_joint_(
nullptr),
133 gimbal_roll_joint_(
nullptr),
136 zero_position_disarmed_{},
137 zero_position_armed_{},
143 serial_enabled_(
false),
149 serial_dev_(io_service_),
153 hil_state_level_(
false) {}
160 void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf);
161 void OnUpdate(
const common::UpdateInfo& );
204 void ImuCallback(ImuPtr& imu_msg);
205 void LidarCallback(LidarPtr& lidar_msg);
207 void send_mavlink_message(
210 void pollForMAVLinkMessages(
double _dt, uint32_t _timeoutMs);
216 void parse_buffer(
const boost::system::error_code& err, std::size_t bytes_t);
219 return serial_dev_.is_open();
224 static const size_t kNOutMax = 18u;
225 static const size_t kNumMotors = 12u;
226 static const size_t kNumServos = 6u;
228 static const u_int32_t kMotorSpeedFlag = 1;
229 static const u_int32_t kServoPositionFlag = 2;
231 double alt_home = 488.0;
235 double input_offset_[kNOutMax];
236 double input_scaling_[kNOutMax];
237 std::string joint_control_type_[kNOutMax];
238 std::string gztopic_[kNOutMax];
239 double zero_position_disarmed_[kNOutMax];
240 double zero_position_armed_[kNOutMax];
241 int input_index_[kNOutMax];
242 transport::PublisherPtr joint_control_pub_[kNOutMax];
260 void handle_control(
double _dt);
270 struct sockaddr_in myaddr_;
271 struct sockaddr_in srcaddr_;
273 unsigned char buf_[65535];
274 struct pollfd fds[1];
277 ignition::math::Vector3d optflow_gyro_{};
transport::SubscriberPtr lidar_sub_
common::PID propeller_pid_
common::PID right_elevon_pid_
transport::SubscriberPtr opticalFlow_sub_
void parse_buffer(const char *pfx, uint8_t *buf, const size_t bufsize, size_t bytes_received)
std::lock_guard< std::recursive_mutex > lock_guard
const boost::shared_ptr< const gz_mav_msgs::CommandMotorSpeed > CommandMotorSpeedPtr
std::string motor_velocity_reference_pub_topic_
boost::thread callback_queue_thread_
static constexpr size_t MAX_TXQ_SIZE
transport::SubscriberPtr sonar_sub_
ignition::math::Vector3d mag_d_
#define MAVLINK_MAX_PACKET_LEN
bool use_left_elevon_pid_
mavlink_message_t m_buffer_
boost::asio::io_service io_service
transport::NodePtr node_handle_
static const uint32_t kDefaultMavlinkUdpPort
static constexpr ssize_t MAX_SIZE
Maximum buffer size with padding for CRC bytes (280 + padding)
std::atomic< bool > tx_in_progress_
void handle_message(const mavlink::mavlink_message_t *mmsg, msgT &rst)
std::string lidar_sub_topic_
transport::PublisherPtr motor_velocity_reference_pub_
ignition::math::Vector3d gravity_W_
static const std::string kDefaultMotorVelocityReferencePubTopic
const boost::shared_ptr< const lidar_msgs::msgs::lidar > LidarPtr
const boost::shared_ptr< const gz_sensor_msgs::Imu > ImuPtr
boost::asio::serial_port serial_dev_
mavlink_status_t m_status_
static const std::string kDefaultNamespace
static constexpr auto kDefaultBaudRate
static constexpr auto kDefaultDevice
ignition::math::Vector3d velocity_prev_W_
static const std::string kDefaultLidarTopic
std::recursive_mutex mutex_
common::Time last_imu_time_
static const std::string kDefaultImuTopic
Framing
Rx packer framing status. (same as mavlink::mavlink_framing_t)
static const uint32_t kDefaultQGCUdpPort
Eigen::VectorXd input_reference_
std::string imu_sub_topic_
physics::JointPtr right_elevon_joint_
bool received_first_reference_
const boost::shared_ptr< const opticalFlow_msgs::msgs::opticalFlow > OpticalFlowPtr
std::vector< common::PID > pids_
physics::JointPtr gimbal_pitch_joint_
boost::asio::io_service io_service_
MAVLINK_FRAMING_BAD_SIGNATURE
transport::SubscriberPtr mav_control_sub_
common::PID left_elevon_pid_
common::PID elevator_pid_
physics::JointPtr gimbal_yaw_joint_
physics::JointPtr propeller_joint_
transport::SubscriberPtr imu_sub_
static constexpr double kDefaultImuInterval
physics::JointPtr left_elevon_joint_
bool use_right_elevon_pid_
physics::JointPtr gimbal_roll_joint_
event::ConnectionPtr updateConnection_
Pointer to the update event connection.
std::array< uint8_t, MAX_SIZE > rx_buf_
static const std::string kDefaultOpticalFlowTopic
std::deque< MsgBuffer > tx_q_
MAVLINK_FRAMING_INCOMPLETE
std::normal_distribution< float > randn_
common::Time last_actuator_time_
std::default_random_engine rand_
physics::JointPtr elevator_joint_
std::vector< physics::JointPtr > joints_
void do_write(bool check_tx_state)
std::string opticalFlow_sub_topic_
std::string mavlink_control_sub_topic_