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 #ifndef FLYER_CONTROLLER_CONTROL_MODE_H 00035 #define FLYER_CONTROLLER_CONTROL_MODE_H 00036 #include <ros/ros.h> 00037 #include <nodelet/nodelet.h> 00038 #include <pluginlib/class_list_macros.h> 00039 #include <diagnostic_updater/diagnostic_updater.h> 00040 #include "starmac_msgs/OperatorCommands.h" 00041 #include "flyer_controller/control_mode_status.h" 00042 #include "flyer_controller/control_mode_output.h" 00043 #include "flyer_controller/control_mode_cmd.h" 00044 #include "flyer_controller/control_utils.h" 00045 #include <nav_msgs/Odometry.h> 00046 #include <angles/angles.h> 00047 #include <tf/tf.h> 00048 #include <algorithm> 00049 00050 using std::min; 00051 using std::max; 00052 00053 #define CHECK_PARAMETER(x,msg) {if(!(x)) {ROS_FATAL_STREAM(msg); ros::requestShutdown(); return;}} 00054 00055 namespace flyer_controller 00056 { 00057 namespace ControlModeTypes 00058 { 00059 enum ControlModeStates 00060 { 00061 ERROR = 0, OFF = 1, IDLE = 2, STANDBY = 3, ACTIVE = 4 00062 }; 00063 typedef ControlModeStates ControlModeState; 00064 } 00065 00066 00067 class ControlMode : public nodelet::Nodelet 00068 { 00069 protected: 00070 ros::NodeHandle nh; 00071 ros::NodeHandle nh_priv; 00072 // Parameters 00073 double status_report_rate; 00074 double control_output_rate; 00075 bool event_driven; // Set true to output control whenever new state estimate received, otherwise output at given rate 00076 00077 // Publishers 00078 ros::Publisher control_mode_status_pub; 00079 ros::Publisher output_pub; 00080 // Subscribers 00081 ros::Subscriber control_mode_cmd_sub; 00082 ros::Subscriber opercmd_sub; 00083 ros::Subscriber state_sub; 00084 // Timers 00085 ros::Timer report_status_timer; 00086 ros::Timer output_controls_timer; 00087 // Diagnostic Updater 00088 diagnostic_updater::Updater diag_updater; 00089 // Member variables 00090 ControlModeTypes::ControlModeState state; // mode state (OFF, IDLE, etc) for reporting status 00091 std::string info; // info string for reporting status 00092 starmac_msgs::OperatorCommands latest_opercmd; 00093 bool ready; 00094 nav_msgs::Odometry latest_state; 00095 bool got_first_state; 00096 bool seen_max_alt; 00097 bool seen_min_alt; 00098 bool got_first_joy; 00099 long int state_count; // how many state messages have been received 00100 long int control_count; // how many times has outputControl been called 00101 // Methods 00102 void diagnostics(diagnostic_updater::DiagnosticStatusWrapper& stat); 00103 void requestRegistration(const std::string& mode_name, double wait_time=0); 00104 void requestRegistration2(const ros::TimerEvent& event, const std::string& mode_name, double wait_time=0); 00105 void startDataFlow(); 00106 void outputControlTimerCallback(const ros::TimerEvent& e); 00107 virtual void controlModeCmdCallback(const control_mode_cmdConstPtr& msg); 00108 void operCmdCallback(const starmac_msgs::OperatorCommandsConstPtr& msg); 00109 virtual void stateCallback(const nav_msgs::OdometryConstPtr& msg); 00110 virtual void reportStatusTimerCallback(const ros::TimerEvent& e); 00111 virtual void outputControl(); // should be implemented by derived class to actually publish control output 00112 // Members 00113 double latest_alt_cmd; // 0..1 (scaled and clipped) 00114 ros::Timer reg_timer; 00115 bool state_updated; 00116 00117 public: 00118 ControlMode(); 00119 ~ControlMode(); 00120 std::string stateToString(ControlModeTypes::ControlModeState t); 00121 protected: 00122 virtual void onInit(); 00123 00124 }; 00125 00126 00127 } 00128 #endif