Go to the documentation of this file.00001
00002 #include <rtt/RTT.hpp>
00003 #include <rtt/plugin/Plugin.hpp>
00004 #include <rtt/plugin/ServicePlugin.hpp>
00005 #include <rtt/internal/GlobalService.hpp>
00006 #include <rtt/scripting/Scripting.hpp>
00007
00008 #include <rtt_ros_msgs/Eval.h>
00009 #include <rtt_ros_msgs/RunScript.h>
00010 #include <rtt_ros_msgs/GetPeerList.h>
00011
00012 #include <ocl/DeploymentComponent.hpp>
00013
00014 #include <ros/ros.h>
00015
00016 using namespace RTT;
00017 using namespace std;
00018
00019 class ROSDeploymentService : public RTT::Service
00020 {
00021 public:
00022 ROSDeploymentService(OCL::DeploymentComponent* deployer);
00023
00024 private:
00025 OCL::DeploymentComponent *deployer_;
00026
00027 ros::NodeHandle nh_;
00028
00029 ros::ServiceServer eval_service_;
00030 ros::ServiceServer run_script_service_;
00031 ros::ServiceServer get_peer_list_service_;
00032
00033 bool eval_cb(
00034 rtt_ros_msgs::Eval::Request& request,
00035 rtt_ros_msgs::Eval::Response& response);
00036
00037 bool run_script_cb(
00038 rtt_ros_msgs::RunScript::Request& request,
00039 rtt_ros_msgs::RunScript::Response& response);
00040
00041 bool get_peer_list_cb(
00042 rtt_ros_msgs::GetPeerList::Request& request,
00043 rtt_ros_msgs::GetPeerList::Response& response);
00044
00045 RTT::OperationCaller<bool(std::string const&)> eval_;
00046 };
00047
00048
00049 ROSDeploymentService::ROSDeploymentService(OCL::DeploymentComponent* deployer) :
00050 Service("rosdeployment", static_cast<RTT::TaskContext*>(deployer)),
00051 deployer_(deployer),
00052 nh_("~"+deployer->getName())
00053 {
00054 if(deployer_) {
00055
00056 eval_service_ = nh_.advertiseService("eval",&ROSDeploymentService::eval_cb,this);
00057 run_script_service_ = nh_.advertiseService("run_script",&ROSDeploymentService::run_script_cb,this);
00058 get_peer_list_service_ = nh_.advertiseService("get_peer_list",&ROSDeploymentService::get_peer_list_cb,this);
00059
00060 eval_ = deployer_->getProvider<RTT::Scripting>("scripting")->eval;
00061 } else {
00062 RTT::log(RTT::Error) << "Attempted to load the rosdeployment service on a TaskContext which is not an OCL::DeploymentComponent. No ROS services will be advertised." << RTT::endlog();
00063 }
00064 }
00065
00066 bool ROSDeploymentService::eval_cb(
00067 rtt_ros_msgs::Eval::Request& request,
00068 rtt_ros_msgs::Eval::Response& response)
00069 {
00070 if (!eval_.ready()) return false;
00071 response.success = eval_(request.code);
00072 return true;
00073 }
00074
00075 bool ROSDeploymentService::run_script_cb(
00076 rtt_ros_msgs::RunScript::Request& request,
00077 rtt_ros_msgs::RunScript::Response& response)
00078 {
00079 response.success = deployer_->runScript(request.file_path);
00080 return true;
00081 }
00082
00083 bool ROSDeploymentService::get_peer_list_cb(
00084 rtt_ros_msgs::GetPeerList::Request& request,
00085 rtt_ros_msgs::GetPeerList::Response& response)
00086 {
00087 response.peers = deployer_->getPeerList();
00088 return true;
00089 }
00090
00091 bool loadROSDeploymentService(RTT::TaskContext *tc) {
00092 OCL::DeploymentComponent *deployer = dynamic_cast<OCL::DeploymentComponent*>(tc);
00093
00094 if(!deployer) {
00095 RTT::log(RTT::Error) << "The rosdeployment service must be loaded on a valid OCL::DeploymentComponent" <<RTT::endlog();
00096 return false;
00097 }
00098
00099 deployer->import("rtt_rosnode");
00100
00101 if(!ros::isInitialized()) {
00102 RTT::log(RTT::Error) << "The rtt_rosdeployment plugin cannot be used without the rtt_rosnode plugin. Please load rtt_rosnode." << RTT::endlog();
00103
00104 return false;
00105 }
00106
00107 RTT::Service::shared_ptr sp( new ROSDeploymentService( deployer ) );
00108 return tc->provides()->addService( sp );
00109 }
00110
00111 extern "C" {
00112 RTT_EXPORT bool loadRTTPlugin(RTT::TaskContext* tc);
00113 bool loadRTTPlugin(RTT::TaskContext* tc) {
00114 if(tc == 0) return true;
00115 return loadROSDeploymentService(tc);
00116 }
00117 RTT_EXPORT RTT::Service::shared_ptr createService();
00118 RTT::Service::shared_ptr createService() {
00119 RTT::Service::shared_ptr sp;
00120 return sp;
00121 }
00122 RTT_EXPORT std::string getRTTPluginName();
00123 std::string getRTTPluginName() {
00124 return "rosdeployment";
00125 }
00126 RTT_EXPORT std::string getRTTTargetName();
00127 std::string getRTTTargetName() {
00128 return OROCOS_TARGET_NAME;
00129 }
00130 }
00131