00001 #pragma once 00002 00022 #ifndef __ROSTHREAD_H 00023 #define __ROSTHREAD_H 00024 00025 00026 00027 #include "cvd/thread.h" 00028 #include "uga_tum_ardrone/filter_state.h" 00029 #include "std_msgs/String.h" 00030 #include "geometry_msgs/Twist.h" 00031 #include "ardrone_autonomy/Navdata.h" 00032 #include "ros/ros.h" 00033 #include "sensor_msgs/Joy.h" 00034 #include "std_srvs/Empty.h" 00035 #include "std_msgs/Empty.h" 00036 00037 class uga_tum_ardrone_gui; 00038 00039 struct ControlCommand 00040 { 00041 inline ControlCommand() {roll = pitch = yaw = gaz = 0;} 00042 inline ControlCommand(double roll, double pitch, double yaw, double gaz) 00043 { 00044 this->roll = roll; 00045 this->pitch = pitch; 00046 this->yaw = yaw; 00047 this->gaz = gaz; 00048 } 00049 double yaw, roll, pitch, gaz; 00050 }; 00051 00052 class RosThread : private CVD::Thread 00053 { 00054 private: 00055 // the associated thread's run function. 00056 void run(); 00057 00058 // keep Running 00059 bool keepRunning; 00060 00061 // ros stuff 00062 ros::Subscriber dronepose_sub; 00063 ros::Publisher vel_pub; 00064 ros::Subscriber vel_sub; 00065 ros::Subscriber uga_tum_ardrone_sub; 00066 ros::Publisher uga_tum_ardrone_pub; 00067 ros::Subscriber navdata_sub; 00068 ros::Subscriber joy_sub; 00069 ros::Publisher takeoff_pub; 00070 ros::Publisher land_pub; 00071 ros::Publisher toggleState_pub; 00072 ros::Publisher pub_reset; 00073 ros::ServiceClient toggleCam_srv; 00074 std_srvs::Empty toggleCam_srv_srvs; 00075 ros::ServiceClient flattrim_srv; 00076 std_srvs::Empty flattrim_srv_srvs; 00077 ros::Subscriber takeoff_sub; 00078 ros::Subscriber land_sub; 00079 ros::Subscriber toggleState_sub; 00080 00081 00082 ros::NodeHandle nh_; 00083 00084 // counters for Hz 00085 unsigned int dronePoseCount; 00086 unsigned int velCount; 00087 unsigned int navdataCount; 00088 unsigned int joyCount; 00089 unsigned int velCount100ms; 00090 00091 // Update the drone state to find out whether we need to reset or not 00092 int drone_state; 00093 std_msgs::Empty emp_msg; 00094 00095 static pthread_mutex_t send_CS; 00096 public: 00097 RosThread(void); 00098 ~RosThread(void); 00099 00100 // start and stop system and respective thread. 00101 // to be called externally 00102 void startSystem(); 00103 void stopSystem(); 00104 00105 uga_tum_ardrone_gui* gui; 00106 00107 00108 // callbacks 00109 void droneposeCb(const uga_tum_ardrone::filter_stateConstPtr statePtr); 00110 void comCb(const std_msgs::StringConstPtr str); 00111 void velCb(const geometry_msgs::TwistConstPtr vel); 00112 void navdataCb(const ardrone_autonomy::NavdataConstPtr navdataPtr); 00113 void joyCb(const sensor_msgs::JoyConstPtr joy_msg); 00114 void landCb(std_msgs::EmptyConstPtr); 00115 void toggleStateCb(std_msgs::EmptyConstPtr); 00116 void takeoffCb(std_msgs::EmptyConstPtr); 00117 ControlCommand lastJoyControlSent; 00118 bool lastL1Pressed; 00119 bool lastR1Pressed; 00120 00121 00122 // send command functions. can be called from any thread & are thread-safe. 00123 // writes a string message to "/uga_tum_ardrone/com". 00124 // is thread-safe (can be called by any thread, but may block till other calling thread finishes) 00125 void publishCommand(std::string c); 00126 void sendControlToDrone(ControlCommand cmd); 00127 void sendLand(); 00128 void sendTakeoff(); 00129 void sendToggleState(); 00130 void sendToggleCam(); 00131 void sendFlattrim(); 00132 void sendResetMsg(); 00133 }; 00134 00135 #endif /* __ROSTHREAD_H */