ros_subscribe.cpp 8.39 KB
Newer Older
Joseph Mirabel's avatar
Joseph Mirabel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/format.hpp>
#include <boost/function.hpp>
#include <boost/make_shared.hpp>

#include <ros/ros.h>
#include <std_msgs/Float64.h>
#include <std_msgs/UInt32.h>

#include <dynamic-graph/factory.h>

#include "dynamic_graph_bridge/ros_init.hh"
#include "ros_subscribe.hh"

namespace dynamicgraph
{
  DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(RosQueuedSubscribe, "RosQueuedSubscribe");

  namespace command
  {
    namespace rosSubscribe
    {
      Clear::Clear
      (RosQueuedSubscribe& entity, const std::string& docstring)
	: Command
	  (entity,
	   std::vector<Value::Type> (),
	   docstring)
      {}

      Value Clear::doExecute ()
      {
	RosQueuedSubscribe& entity =
	  static_cast<RosQueuedSubscribe&> (owner ());

	entity.clear ();
	return Value ();
      }

      ClearQueue::ClearQueue
      (RosQueuedSubscribe& entity, const std::string& docstring)
	: Command
	  (entity,
	   boost::assign::list_of (Value::STRING),
	   docstring)
      {}

      Value ClearQueue::doExecute ()
      {
	RosQueuedSubscribe& entity =
	  static_cast<RosQueuedSubscribe&> (owner ());

	std::vector<Value> values = getParameterValues ();
	const std::string& signal = values[0].value ();
        entity.clearQueue (signal);

	return Value ();
      }

      List::List
      (RosQueuedSubscribe& entity, const std::string& docstring)
	: Command
	  (entity,
	   std::vector<Value::Type> (),
	   docstring)
      {}

      Value List::doExecute ()
      {
	RosQueuedSubscribe& entity =
	  static_cast<RosQueuedSubscribe&> (owner ());
	return Value (entity.list ());
      }

      Add::Add
      (RosQueuedSubscribe& entity, const std::string& docstring)
	: Command
	  (entity,
	   boost::assign::list_of
	   (Value::STRING) (Value::STRING) (Value::STRING),
	   docstring)
      {}

      Value Add::doExecute ()
      {
	RosQueuedSubscribe& entity =
	  static_cast<RosQueuedSubscribe&> (owner ());
	std::vector<Value> values = getParameterValues ();

	const std::string& type = values[0].value ();
	const std::string& signal = values[1].value ();
	const std::string& topic = values[2].value ();

	if (type == "double")
	  entity.add<double> (signal, topic);
	else if (type == "unsigned")
	  entity.add<unsigned int> (signal, topic);
	else if (type == "matrix")
	  entity.add<dg::Matrix> (signal, topic);
	else if (type == "vector")
	  entity.add<dg::Vector> (signal, topic);
	else if (type == "vector3")
	  entity.add<specific::Vector3> (signal, topic);
	else if (type == "matrixHomo")
	  entity.add<sot::MatrixHomogeneous> (signal, topic);
	else if (type == "twist")
	  entity.add<specific::Twist> (signal, topic);
	else
	  throw std::runtime_error("bad type");
	return Value ();
      }

      Rm::Rm
      (RosQueuedSubscribe& entity, const std::string& docstring)
	: Command
	  (entity,
	   boost::assign::list_of (Value::STRING),
	   docstring)
      {}

      Value Rm::doExecute ()
      {
	RosQueuedSubscribe& entity =
	  static_cast<RosQueuedSubscribe&> (owner ());
	std::vector<Value> values = getParameterValues ();
	const std::string& signal = values[0].value ();
	entity.rm (signal);
	return Value ();
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

      QueueSize::QueueSize
      (RosQueuedSubscribe& entity, const std::string& docstring)
	: Command
	  (entity,
	   boost::assign::list_of (Value::STRING),
	   docstring)
      {}

      Value QueueSize::doExecute ()
      {
	RosQueuedSubscribe& entity =
	  static_cast<RosQueuedSubscribe&> (owner ());

	std::vector<Value> values = getParameterValues ();
	const std::string& signal = values[0].value ();

	return Value ((unsigned)entity.queueSize (signal));
      }
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

      ReadQueue::ReadQueue
      (RosQueuedSubscribe& entity, const std::string& docstring)
	: Command
	  (entity,
	   boost::assign::list_of (Value::BOOL),
	   docstring)
      {}

      Value ReadQueue::doExecute ()
      {
	RosQueuedSubscribe& entity =
	  static_cast<RosQueuedSubscribe&> (owner ());

	std::vector<Value> values = getParameterValues ();
        bool read = values[0].value ();
        entity.readQueue (read);

        return Value ();
      }
Joseph Mirabel's avatar
Joseph Mirabel committed
170 171 172 173 174 175 176 177 178 179 180
    } // end of errorEstimator.
  } // end of namespace command.

  const std::string RosQueuedSubscribe::docstring_
  ("Subscribe to a ROS topics and convert it into a dynamic-graph signals.\n"
   "\n"
   "  Use command \"add\" to subscribe to a new signal.\n");

  RosQueuedSubscribe::RosQueuedSubscribe (const std::string& n)
    : dynamicgraph::Entity(n),
      nh_ (rosInit (true)),
181 182
      bindedSignal_ (),
      readQueue_ (false)
Joseph Mirabel's avatar
Joseph Mirabel committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  {
    std::string docstring =
      "\n"
      "  Add a signal reading data from a ROS topic\n"
      "\n"
      "  Input:\n"
      "    - type: string among ['double', 'matrix', 'vector', 'vector3',\n"
      "                          'matrixHomo', 'twist'],\n"
      "    - signal: the signal name in dynamic-graph,\n"
      "    - topic:  the topic name in ROS.\n"
      "\n";
    addCommand ("add",
		new command::rosSubscribe::Add
		(*this, docstring));
    docstring =
      "\n"
      "  Remove a signal reading data from a ROS topic\n"
      "\n"
      "  Input:\n"
      "    - name of the signal to remove (see method list for the list of signals).\n"
      "\n";
    addCommand ("rm",
		new command::rosSubscribe::Rm
		(*this, docstring));
    docstring =
      "\n"
      "  Remove all signals reading data from a ROS topic\n"
      "\n"
      "  No input:\n"
      "\n";
    addCommand ("clear",
		new command::rosSubscribe::Clear
		(*this, docstring));
    docstring =
      "\n"
      "  List signals reading data from a ROS topic\n"
      "\n"
      "  No input:\n"
      "\n";
    addCommand ("list",
		new command::rosSubscribe::List
		(*this, docstring));
    docstring =
      "\n"
      "  Empty the queue of a given signal\n"
      "\n"
Joseph Mirabel's avatar
Joseph Mirabel committed
229
      "  Input is:\n"
Joseph Mirabel's avatar
Joseph Mirabel committed
230 231 232 233 234
      "    - name of the signal (see method list for the list of signals).\n"
      "\n";
    addCommand ("clearQueue",
		new command::rosSubscribe::ClearQueue
		(*this, docstring));
Joseph Mirabel's avatar
Joseph Mirabel committed
235 236 237 238 239 240 241 242 243 244
    docstring =
      "\n"
      "  Return the queue size of a given signal\n"
      "\n"
      "  Input is:\n"
      "    - name of the signal (see method list for the list of signals).\n"
      "\n";
    addCommand ("queueSize",
		new command::rosSubscribe::QueueSize
		(*this, docstring));
245 246 247 248 249 250 251 252 253 254
    docstring =
      "\n"
      "  Whether signals should read values from the queues.\n"
      "\n"
      "  Input is:\n"
      "    - boolean.\n"
      "\n";
    addCommand ("readQueue",
		new command::rosSubscribe::ReadQueue
		(*this, docstring));
Joseph Mirabel's avatar
Joseph Mirabel committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  }

  RosQueuedSubscribe::~RosQueuedSubscribe ()
  {
    std::cout << getName() << ": Delete" << std::endl;
  }

  void RosQueuedSubscribe::display (std::ostream& os) const
  {
    os << CLASS_NAME << std::endl;
  }

  void RosQueuedSubscribe::rm (const std::string& signal)
  {
    std::string signalTs = signal+"Timestamp";

    signalDeregistration(signal);
    bindedSignal_.erase (signal);

    if(bindedSignal_.find(signalTs) != bindedSignal_.end())
    {
       signalDeregistration(signalTs);
       bindedSignal_.erase(signalTs);
    }
  }

  std::string RosQueuedSubscribe::list ()
  {
    std::string result("[");
    for (std::map<std::string, bindedSignal_t>::const_iterator it =
	   bindedSignal_.begin (); it != bindedSignal_.end (); it++) {
      result += "'" + it->first + "',";
    }
    result += "]";
    return result;
  }

  void RosQueuedSubscribe::clear ()
  {
    std::map<std::string, bindedSignal_t>::iterator it = bindedSignal_.begin();
    for(; it!= bindedSignal_.end(); )
    {
      rm(it->first);
      it = bindedSignal_.begin();
    }
  }

  void RosQueuedSubscribe::clearQueue (const std::string& signal)
  {
    if(bindedSignal_.find(signal) != bindedSignal_.end())
    {
       bindedSignal_[signal]->clear();
    }
  }

Joseph Mirabel's avatar
Joseph Mirabel committed
310 311 312 313 314 315 316 317 318 319 320
  std::size_t RosQueuedSubscribe::queueSize (const std::string& signal) const
  {
    std::map<std::string, bindedSignal_t>::const_iterator _bs =
      bindedSignal_.find(signal);
    if(_bs != bindedSignal_.end())
    {
       return _bs->second->size();
    }
    return -1;
  }

321 322 323 324 325 326 327 328 329 330 331
  void RosQueuedSubscribe::readQueue (bool read)
  {
    for (std::map<std::string, bindedSignal_t>::const_iterator it =
	   bindedSignal_.begin (); it != bindedSignal_.end (); it++) {
      std::cout << it->first << " : " << it->second->size() << '\n';
    }
    std::cout << std::endl;
    // TODO ensure that the signal are synchronised
    readQueue_ = read;
  }

Joseph Mirabel's avatar
Joseph Mirabel committed
332 333 334 335 336
  std::string RosQueuedSubscribe::getDocString () const
  {
    return docstring_;
  }
} // end of namespace dynamicgraph.