control_mode_attitude.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, UC Regents
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the University of California nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 #include "flyer_controller/control_mode_attitude.h"
00035 
00036 namespace flyer_controller
00037 {
00038   void ControlModeAttitude::onInit()
00039   {
00040     JoystickMode::onInit();
00041     // Parameters
00042     double max_pitch_cmd = 10; // [deg] commanded pitch corresponding to full deflection
00043     double max_roll_cmd = 10; // [deg] commanded roll corresponding to full deflection
00044     bool external_command_frame = false; // should commands be interpreted w.r.t. an external frame?
00045     double external_frame_heading = 0; // [deg] heading that will correspond to a -pitch command when using external frame
00046     bool direct_yaw_rate_control = false; // joystick yaw rate commands sent directly
00047 
00048     nh_priv.param("max_pitch_cmd", max_pitch_cmd, max_pitch_cmd);
00049     nh_priv.param("max_roll_cmd", max_roll_cmd, max_roll_cmd);
00050     nh_priv.param("external_command_frame", external_command_frame, external_command_frame);
00051     nh_priv.param("external_frame_heading", external_frame_heading, external_frame_heading);
00052     nh_priv.param("direct_yaw_rate_control", direct_yaw_rate_control, direct_yaw_rate_control);
00053     nh_priv.param("step_input_mode_enabled", step_input_mode_enabled, false);
00054     nh_priv.param("step_input_magnitude", step_input_magnitude, 0.5);
00055     nh_priv.param("step_input_joystick_roll_axis", step_input_joystick_roll_axis, 4);
00056     nh_priv.param("step_input_joystick_pitch_axis", step_input_joystick_pitch_axis, 5);
00057     CHECK_PARAMETER(max_pitch_cmd >= 0, "parameter value out of range");
00058     CHECK_PARAMETER(max_roll_cmd >= 0, "parameter value out of range");
00059 
00060     CHECK_PARAMETER((external_frame_heading >= -360) && (external_frame_heading <= 360), "parameter value out of range");
00061     NODELET_INFO_STREAM("max_pitch_cmd = " << max_pitch_cmd);
00062 
00063     att_control.configure(external_command_frame, external_frame_heading, direct_yaw_rate_control, max_pitch_cmd,
00064                           max_roll_cmd, max_yaw_rate_cmd, min_alt_cmd, max_alt_cmd);
00065 
00066     requestRegistration(mode_name_);
00067   }
00068 
00069   // Virtual methods from base class
00070   void ControlModeAttitude::outputControl()
00071   {
00072     control_mode_outputPtr output_msg(new control_mode_output);
00073     bool do_calcs = false;
00074     bool do_publish = false;
00075     output_msg->header.stamp = ros::Time::now();
00076 
00077     switch (state)
00078     {
00079       case ControlModeTypes::STANDBY:
00080         do_calcs = got_first_joy and got_first_state;
00081         do_publish = true;
00082         break;
00083       case ControlModeTypes::ACTIVE:
00084         do_calcs = true;
00085         do_publish = true;
00086         break;
00087       default:
00088         break;
00089     }
00090 
00091     if (do_calcs)
00092     {
00093       att_control.outputControl(boost::make_shared<nav_msgs::Odometry>(latest_state), boost::make_shared<
00094           starmac_msgs::OperatorCommands>(latest_opercmd), output_msg);
00095       if (step_input_mode_enabled)
00096       {
00097         //applyStepInputs(output_msg);
00098       }
00099     }
00100 
00101     if (do_publish)
00102     {
00103       output_msg->control_mode = "attitude";
00104       output_pub.publish(output_msg);
00105     }
00106   }
00107 
00108 
00109   // TODO: rework step inputs implementation for new way of handling joystick
00110 
00111 //  void ControlModeAttitude::applyStepInputs(control_mode_outputPtr& control_out)
00112 //  {
00113 //    // Check joystick for inputs, and modify control_out accordingly
00114 //    if (latest_opercmd.axes[step_input_joystick_roll_axis] != 0)
00115 //    {
00116 //      double step_input_value = step_input_magnitude * latest_opercmd.axes[step_input_joystick_roll_axis];
00117 //      control_out->roll_cmd = step_input_value;
00118 //      ROS_WARN("Applying roll axis step input of %f degrees", step_input_value);
00119 //    }
00120 //    else if (latest_opercmd.axes[step_input_joystick_pitch_axis] != 0)
00121 //    {
00122 //      double step_input_value = step_input_magnitude * latest_opercmd.axes[step_input_joystick_pitch_axis];
00123 //      control_out->pitch_cmd = step_input_magnitude * latest_opercmd.axes[step_input_joystick_pitch_axis];
00124 //      ROS_WARN("Applying pitch axis step input of %f degrees", step_input_value);
00125 //    }
00126 //  }
00127 
00128   void ControlModeAttitude::reportStatusTimerCallback(const ros::TimerEvent& e)
00129   {
00130     control_mode_statusPtr msg(new control_mode_status);
00131     //NODELET_INFO_STREAM(__PRETTY_FUNCTION__);
00132     msg->state = state;
00133     msg->info = info;
00134     msg->header.stamp = e.current_real;
00135     ready = (got_first_joy and got_first_state);
00136     msg->ready = ready; // TODO: check some more conditions..
00137     control_mode_status_pub.publish(msg);
00138     diag_updater.update();
00139   }
00140 
00141   void ControlModeAttitude::controlModeCmdCallback(const control_mode_cmdConstPtr& msg)
00142   {
00143     NODELET_INFO_STREAM("Heard command: " << msg->cmd);
00144     if (msg->cmd == "mode idle")
00145     {
00146       state = IDLE;
00147       info = "";
00148     }
00149     else if (msg->cmd == "mode standby")
00150     {
00151       state = STANDBY;
00152       info = "";
00153     }
00154     else if (msg->cmd == "mode active")
00155     {
00156       if (state == STANDBY)
00157       {
00158         state = ACTIVE;
00159         att_control.useCurrentYaw(boost::make_shared<nav_msgs::Odometry>(latest_state));
00160         info = "";
00161       }
00162       else
00163       {
00164         NODELET_ERROR("Invalid transition");
00165       }
00166     }
00167     else
00168     {
00169       NODELET_ERROR_STREAM("Command unknown: " << msg->cmd);
00170     }
00171   }
00172 
00173 PLUGINLIB_DECLARE_CLASS(flyer_controller, ControlModeAttitude, flyer_controller::ControlModeAttitude, nodelet::Nodelet)
00174 
00175 } // namespace


flyer_controller
Author(s): Patrick Bouffard
autogenerated on Sun Jan 5 2014 11:37:53