Skip to content
Snippets Groups Projects
ros_interpreter.cpp 1.65 KiB
Newer Older
#include "dynamic_graph_bridge/ros_interpreter.hh"

Olivier Stasse's avatar
Olivier Stasse committed
namespace dynamicgraph {
static const int queueSize = 5;

Interpreter::Interpreter(ros::NodeHandle& nodeHandle)
Guilhem Saurel's avatar
Guilhem Saurel committed
    : interpreter_(), nodeHandle_(nodeHandle), runCommandSrv_(), runPythonFileSrv_() {}
Olivier Stasse's avatar
Olivier Stasse committed

void Interpreter::startRosService() {
Guilhem Saurel's avatar
Guilhem Saurel committed
  runCommandCallback_t runCommandCb = boost::bind(&Interpreter::runCommandCallback, this, _1, _2);
Olivier Stasse's avatar
Olivier Stasse committed
  runCommandSrv_ = nodeHandle_.advertiseService("run_command", runCommandCb);

Guilhem Saurel's avatar
Guilhem Saurel committed
  runPythonFileCallback_t runPythonFileCb = boost::bind(&Interpreter::runPythonFileCallback, this, _1, _2);
  runPythonFileSrv_ = nodeHandle_.advertiseService("run_script", runPythonFileCb);
Olivier Stasse's avatar
Olivier Stasse committed
}

Guilhem Saurel's avatar
Guilhem Saurel committed
bool Interpreter::runCommandCallback(dynamic_graph_bridge_msgs::RunCommand::Request& req,
                                     dynamic_graph_bridge_msgs::RunCommand::Response& res) {
  interpreter_.python(req.input, res.result, res.standardoutput, res.standarderror);
Olivier Stasse's avatar
Olivier Stasse committed
  return true;
}

Guilhem Saurel's avatar
Guilhem Saurel committed
bool Interpreter::runPythonFileCallback(dynamic_graph_bridge_msgs::RunPythonFile::Request& req,
                                        dynamic_graph_bridge_msgs::RunPythonFile::Response& res) {
Olivier Stasse's avatar
Olivier Stasse committed
  interpreter_.runPythonFile(req.input);
  res.result = "File parsed";  // FIX: It is just an echo, is there a way to
                               // have a feedback?
  return true;
}

Guilhem Saurel's avatar
Guilhem Saurel committed
void Interpreter::runCommand(const std::string& command, std::string& result, std::string& out, std::string& err) {
Olivier Stasse's avatar
Olivier Stasse committed
  interpreter_.python(command, result, out, err);
  if (err.size() > 0) {
    ROS_ERROR(err.c_str());
  }
Olivier Stasse's avatar
Olivier Stasse committed
}

Guilhem Saurel's avatar
Guilhem Saurel committed
void Interpreter::runPythonFile(std::string ifilename) { interpreter_.runPythonFile(ifilename); }
Olivier Stasse's avatar
Olivier Stasse committed

}  // end of namespace dynamicgraph.