/* * Copyright (C) 2015 Open Source Robotics Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #ifndef _GAZEBO_CESSNA_PLUGIN_HH_ #define _GAZEBO_CESSNA_PLUGIN_HH_ #include #include #include #include #include #include #include #include #include #include namespace gazebo { /// \brief Allow moving the control surfaces of a Cessna C-172 plane. This /// plugin might be used with other models that have similar control surfaces. /// /// The plugin requires the following parameters: /// Name of the joint controlling the propeller spin. /// Maximum angular speed in rpm. /// Name of the joint controlling the left aileron. /// Name of the joint controlling the left flap. /// Name of the joint controlling the right aileron. /// Name of the joint controlling the right flap. /// Name of the joint controlling the rear elevators. /// Name of the joint controlling the rudder. /// /// The following parameters are optional: /// P gain for the PID that controls the propeller's speed. /// I gain for the PID that controls the propeller's speed. /// D gain for the PID that controls the propeller's speed. /// P gain for the PID that controls the position of the /// control surfaces. /// I gain for the PID that controls the position of the /// control surfaces. /// D gain for the PID that controls the position of the /// control surfaces. /// /// The plugin will be subscribed to the following topic: /// "~//control" The expected value is a Cessna message. /// /// The plugin will advertise the following topic with the current state: /// "~//state" class GAZEBO_VISIBLE CessnaPlugin : public ModelPlugin { /// \brief Constructor. public: CessnaPlugin(); /// \brief Destructor. public: ~CessnaPlugin(); // Documentation Inherited. public: virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf); /// \brief Read an SDF parameter with a joint name and initialize a pointer /// to this joint. /// \param[in] _sdfParam SDF parameter containing a joint name. /// \param[in] _sdf Pointer to the SDF element containing the parameters. /// \param[out] _joint Pointer to the joint to be initialized. /// \return True if the SDF parameter is found and the joint name is found, /// false otherwise. private: bool FindJoint(const std::string &_sdfParam, sdf::ElementPtr _sdf, physics::JointPtr &_joint); /// \brief Update the control surfaces controllers. /// \param[in] _info Update information provided by the server. private: void Update(const common::UpdateInfo &_info); /// \brief Callback executed when a new message containing control commands /// is received. /// \param[in] _msg New message containing control commands. private: void OnControl(ConstCessnaPtr &_msg); /// \brief Update PID Joint controllers. /// \param[in] _dt time step size since last update. private: void UpdatePIDs(double _dt); /// \brief Publish Cessna state. private: void PublishState(); /// \brief Joint indexes. private: static const unsigned int kLeftAileron = 0; private: static const unsigned int kLeftFlap = 1; private: static const unsigned int kRightAileron = 2; private: static const unsigned int kRightFlap = 3; private: static const unsigned int kElevators = 4; private: static const unsigned int kRudder = 5; private: static const unsigned int kPropeller = 6; /// \brief Pointer to the update event connection. private: event::ConnectionPtr updateConnection; /// \brief Node used for using Gazebo communications. private: transport::NodePtr node; /// \brief Subscriber pointer. private: transport::SubscriberPtr controlSub; /// \brief Publisher pointer. private: transport::PublisherPtr statePub; /// \brief Pointer to the model; private: physics::ModelPtr model; /// \brief Control surfaces joints. private: std::array joints; /// \brief Max propeller RPM. private: int32_t propellerMaxRpm = 2500; /// \brief Next command to be applied to the propeller and control surfaces. private: std::arraycmds; /// \brief Velocity PID for the propeller. private: common::PID propellerPID; /// \brief Position PID for the control surfaces. private: std::array controlSurfacesPID; /// \brief keep track of controller update sim-time. private: gazebo::common::Time lastControllerUpdateTime; /// \brief Controller update mutex. private: std::mutex mutex; }; } #endif