MIRA
UnitWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Redistribution and modification of this code is strictly prohibited.
9  *
10  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
11  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
13  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
20  */
21 
30 #ifndef _MIRA_TOOLBOXES_PYTHON_UNITWRAPPERY_H_
31 #define _MIRA_TOOLBOXES_PYTHON_UNITWRAPPERY_H_
32 
33 #include <Python.h>
34 
36 
37 #include <transform/Pose.h>
38 #include <fw/Unit.h>
39 
40 #include <python/PythonSet.h>
41 #include <python/PythonException.h>
44 #include <python/ScopedGIL.h>
45 
46 namespace mira { namespace python {
47 
49 
50 class PyUnitWrapper;
51 
52 class PyUnit : public mira::Unit
53 {
55 
56  friend class PyUnitWrapper;
57  friend class PythonUnitLoader;
58 
59 protected:
60  PyUnit(Duration cycleTime = Duration::milliseconds(500),
61  Authority::Flags flags = Authority::NORMAL) :
62  Unit(cycleTime, flags)
63  {}
64 
65 public:
66  virtual ~PyUnit()
67  {
68  // this allows the unit to be safely deleted in Python without segfaulting
69  ScopedGILLock lock;
70  mUnitObject = boost::python::object();
71  checkout();
72  setPropertyNode(boost::shared_ptr<PropertyNode>());
73  }
74 
75  void setUnitObject(boost::python::object unit)
76  {
77  mUnitObject = unit;
78  }
79 
81  {
82  return boost::python::extract<PyUnitWrapper&>(mUnitObject);
83  }
84 
85  template<class Reflector>
86  void reflect(Reflector& r);
87 
88  void initialize();
89  void process(const mira::Timer& timer);
90  void finalize();
91 
92 protected:
93  boost::python::object mUnitObject;
94 };
95 
97 {
98 public:
99  PyUnitWrapperBase() : mUnit(NULL) {}
100 
101  virtual ~PyUnitWrapperBase() {}
102 
103  virtual void reflect(BaseReflectorWrapper& r) = 0;
104 
105  virtual void initialize() = 0;
106 
107  virtual void process() = 0;
108 
109  virtual void finalize() = 0;
110 
111  void setUnit(PyUnit* unit) { mUnit = unit; }
112 
114  {
115  if(!isValid()) {
116  MIRA_THROW(XRuntime, "Unit is not properly initialized")
117  }
118  return *mUnit;
119  }
120 
122  {
123  return mUnit;
124  }
125 
126  bool isValid() const { return mUnit != NULL; }
127 
128 private:
129 
130  PyUnit* mUnit;
131 };
132 
134 
135 class PyUnitWrapper: public PyUnitWrapperBase, public boost::python::wrapper<PyUnitWrapperBase>
136 {
137 public:
138  PyUnitWrapper(PyObject* self) :
139  mSelf(self),
140  mCycleTime(Duration::milliseconds(500)),
141  mFlags(Authority::NORMAL)
142  {
143  boost::python::detail::initialize_wrapper(self, this);
144  }
145  PyUnitWrapper(PyObject* self, uint32 flags) :
146  mSelf(self),
147  mCycleTime(Duration::milliseconds(500)),
148  mFlags((Authority::Flags)flags)
149  {
150  boost::python::detail::initialize_wrapper(self, this);
151  }
152 
153  PyUnitWrapper(PyObject* self, const Duration& cycleTime) :
154  mSelf(self),
155  mCycleTime(cycleTime),
156  mFlags(Authority::NORMAL)
157  {
158  boost::python::detail::initialize_wrapper(self, this);
159  }
160 
161  PyUnitWrapper(PyObject* self, const Duration& cycleTime, uint32 flags) :
162  mSelf(self),
163  mCycleTime(cycleTime),
164  mFlags((Authority::Flags)flags)
165  {
166  boost::python::detail::initialize_wrapper(self, this);
167  }
168 
169  virtual ~PyUnitWrapper();
170 
171  void checkinCycleTime(const std::string& ns, const std::string& name,
172  const Duration& cycleTime);
173 
174  void checkin(const std::string& ns, const std::string& name)
175  {
176  checkinCycleTime(ns, name, mCycleTime);
177  }
178 
179  void checkout();
180 
181  boost::python::handle<> self()
182  {
183  return boost::python::handle<>(boost::python::borrowed(mSelf));
184  }
185 
186  void reflect(BaseReflectorWrapper& r);
187  void initialize();
188  void process();
189  void finalize();
190 
191  void bootup(const std::string& message);
192  void warning(const std::string& category, const std::string& message);
193  void error(const std::string& category, const std::string& message);
194  void ok(const std::string& category);
195 
196  void spin();
197 
198  std::string getNamespace();
199  std::string getName();
200  std::string getGlobalName();
201  std::string getID();
202  std::string getGlobalID();
203  const Duration& getCycleTime();
206 
207  std::string resolveName(const std::string& name);
208 
209  bool waitForService(const std::string& service);
210 
211  bool waitForServiceDuration(const std::string& service, const Duration& d);
212 
213  std::string waitForServiceInterface(const std::string& service);
214 
215  std::string waitForServiceInterfaceDuration(const std::string& service,
216  const Duration& d);
217 
218  bool existsService(const std::string& service);
219 
220  boost::python::list queryServicesForInterface(const std::string& interface);
221 
222  void publishService();
223 
224  bool doesChannelExist(const std::string& channelID);
225 
226  bool waitForChannel(const std::string& channelID);
227 
228  bool waitForChannelDuration(const std::string& channelID, const Duration& d);
229 
230  mira::Channel<void> getChannel(const std::string& channelID);
231 
232  Pose2 getTransform2Latest(const std::string& target, const std::string& source);
233  Pose2 getTransform2(const std::string& target, const std::string& source, const Time& time);
234  Pose3 getTransform3Latest(const std::string& target, const std::string& source);
235  Pose3 getTransform3(const std::string& target, const std::string& source, const Time& time);
236  void publishTransform2Now(const std::string& node, const Pose2& t);
237  void publishTransform2(const std::string& node, const Pose2& t, const Time& timestamp);
238  void publishTransform3Now(const std::string& node, const Pose3& t);
239  void publishTransform3(const std::string& node, const Pose3& t, const Time& timestamp);
240  void publishTransformIndirect2Now(const std::string& frameID,
241  const std::string& targetID,
242  const std::string& sourceID,
243  const Pose2& transform);
244  void publishTransformIndirect2(const std::string& frameID,
245  const std::string& targetID,
246  const std::string& sourceID,
247  const Pose2& transform,
248  const Time& timestamp);
249  void publishTransformIndirect3Now(const std::string& frameID,
250  const std::string& targetID,
251  const std::string& sourceID,
252  const Pose3& transform);
253  void publishTransformIndirect3(const std::string& frameID,
254  const std::string& targetID,
255  const std::string& sourceID,
256  const Pose3& transform,
257  const Time& timestamp);
258  void addTransformLink(const std::string& childID, const std::string& parentID);
259 
265  boost::python::object channelPublish(const std::string& channelID,
266  boost::python::object type);
267 
269  {
270  public:
271  virtual ~BaseCallback() {}
272  };
273 
274  template<typename T>
275  class Callback : public BaseCallback
276  {
277  public:
278  Callback(const boost::python::object& callable)
279  : mCallable(callable)
280  {
281  }
282 
284  {
286  try
287  {
288  mCallable(read);
289  }
290  catch(boost::python::error_already_set&)
291  {
292  MIRA_THROW(mira::XRuntime, "Failed to call callback: " <<
294  }
295  }
296 
297  protected:
298  boost::python::object mCallable;
299  };
300 
301  template<typename T>
303  {
304  public:
305  CallbackInterval(const boost::python::object& callable)
306  : mCallable(callable)
307  {
308  }
309 
311  {
313  try
314  {
315  mCallable(read);
316  }
317  catch(boost::python::error_already_set&)
318  {
319  MIRA_THROW(mira::XRuntime, "Failed to call callback: " <<
321  }
322  }
323 
324  protected:
325  boost::python::object mCallable;
326  };
327 
335  boost::python::object subscribe(const std::string& channelID,
336  boost::python::object type);
337 
341  boost::python::object subscribeCb(const std::string& channelID,
342  boost::python::object type,
343  boost::python::object callable);
344 
348  boost::python::object subscribeIntervalCb(const std::string& channelID,
349  boost::python::object type,
350  boost::python::object callable,
351  const Duration& storageDuration);
352 
356  void unsubscribe(const std::string& channelID, boost::python::object type);
357 
361  bool isSubscribedOn(const std::string& channelID);
362 
363 protected:
364 
369  template<typename T>
370  Channel<T> subscribe_internal(const std::string& channelID,
371  boost::python::object callable,
372  const Duration& storageDuration)
373  {
374  using namespace boost::python;
375  boost::shared_ptr<CallbackInterval<T>> callback(new CallbackInterval<T>(callable));
376  mCallbacks.push_back(callback);
377  return getUnit().subscribeInterval(channelID, &CallbackInterval<T>::operator(), callback.get(), storageDuration);
378  }
379 
384  template<typename T>
385  Channel<T> subscribe_internal(const std::string& channelID,
386  boost::python::object callable)
387  {
388  using namespace boost::python;
389  boost::shared_ptr<Callback<T>> callback(new Callback<T>(callable));
390  mCallbacks.push_back(callback);
391  return getUnit().subscribe(channelID, &Callback<T>::operator(), callback.get());
392  }
393 
399  template<typename T>
400  Channel<T> subscribe_internal(const std::string& channelID)
401  {
402  return getUnit().subscribe<T>(channelID);
403  }
404 
409  template<typename T>
410  void unsubscribe_internal(const std::string& channelID)
411  {
412  getUnit().unsubscribe<T>(channelID);
413  }
414 
419  template<typename T>
420  Channel<T> publish_internal(const std::string& channelID)
421  {
422  return getUnit().publish<T>(channelID);
423  }
424 
425  PyObject* mSelf;
428  std::vector<boost::shared_ptr<BaseCallback>> mCallbacks;
429 
430  template<typename T>
431  friend Channel<T> subscribe(PyUnitWrapper& unit, const std::string& channelID);
432 
433  template<typename T>
434  friend Channel<T> subscribeCb(PyUnitWrapper& unit, const std::string& channelID,
435  boost::python::object callable);
436 
437  template<typename T>
438  friend Channel<T> subscribeIntervalCb(PyUnitWrapper& unit, const std::string& channelID,
439  boost::python::object callable,
440  const Duration& storageDuration);
441 
442  template<typename T>
443  friend Channel<T> publish(PyUnitWrapper& unit, const std::string& channelID);
444 
445  template<typename T>
446  friend void unsubscribe(PyUnitWrapper& unit, const std::string& channelID);
447 
448 };
449 
451 
452 template<class Reflector>
453 void PyUnit::reflect(Reflector& r)
454 {
455  using namespace boost::python;
457 
458  ScopedGILLock lock;
459  ReflectorWrapper<Reflector> reflectorWrapper(r);
460  try
461  {
462  PyUnitWrapper& u = unit();
463  MIRA_REFLECT_CALL(Reflector, r, "PyUnit::reflect", u.reflect(reflectorWrapper));
464  }
465  catch (const error_already_set&)
466  {
467  MIRA_THROW(XRuntime, "Failed to call reflect on unit: " << getLastExceptionString());
468  }
469 }
470 
472 
480 template<typename T>
481 Channel<T> subscribe(PyUnitWrapper& unit, const std::string& channelID) {
482  return unit.subscribe_internal<T>(channelID);
483 }
484 
491 template<typename T>
492 Channel<T> subscribeCb(PyUnitWrapper& unit, const std::string& channelID,
493  boost::python::object callable) {
494  return unit.subscribe_internal<T>(channelID, callable);
495 }
496 
503 template<typename T>
504 Channel<T> subscribeIntervalCb(PyUnitWrapper& unit, const std::string& channelID,
505  boost::python::object callable,
506  const Duration& storageDuration) {
507  return unit.subscribe_internal<T>(channelID, callable, storageDuration);
508 }
509 
517 template<typename T>
518 void unsubscribe(PyUnitWrapper& unit, const std::string& channelID) {
519  unit.unsubscribe_internal<T>(channelID);
520 }
521 
528 template<typename T>
529 Channel<T> publish(PyUnitWrapper& unit, const std::string& channelID) {
530  return unit.publish_internal<T>(channelID);
531 }
532 
533 
534 template <typename T>
536 {
543  static T get(Channel<T>& channel)
544  {
545  return channel.get();
546  }
547 
554  static T getTime(Channel<T>& channel, const Time& time)
555  {
556  return channel.get(time);
557  }
558 
565  static T getTimeDuration(Channel<T>& channel, const Time& time,
566  const Duration& tolerance)
567  {
568  return channel.get(time, tolerance);
569  }
570 
577  static ChannelRead<T> read(Channel<T>& channel)
578  {
579  return channel.read();
580  }
581 
588  static ChannelRead<T> readTime(Channel<T>& channel, const Time& time)
589  {
590  return channel.read(time);
591  }
592 
599  static ChannelRead<T> readTimeDuration(Channel<T>& channel, const Time& time,
600  const Duration& tolerance)
601  {
602  return channel.read(time, tolerance);
603  }
604 
613  static void postTimeFrame(Channel<T>& channel, const T& value, const Time& time,
614  const std::string& frameID)
615  {
616  auto w = channel.write();
617  w->timestamp = time;
618  w->value() = value;
619  w->frameID = frameID;
620  }
621 
622  static void postTime(Channel<T>& channel, const T& value, const Time& time)
623  {
624  channel.post(value, time);
625  }
626 
627  static void post(Channel<T>& channel, const T& value)
628  {
629  channel.post(value);
630  }
631 };
632 
633 template <typename T>
635 {
636  static bool waitForPublisherTime(Channel<T>& channel, const Duration& d)
637  {
638  ScopedGILThreadLock lock;
639  return channel.waitForPublisher(d);
640  }
641 
642  static bool waitForPublisher(Channel<T>& channel)
643  {
645  }
646 
647  static bool waitForDataTime(Channel<T>& channel, const Duration& d)
648  {
649  ScopedGILThreadLock lock;
650  ChannelRead<T> r = channel.waitForData(d);
651  return r.isValid();
652  }
653 
654  static bool waitForData(Channel<T>& channel)
655  {
657  }
658 };
659 
660 template<typename T>
663  return read->timestamp;
664  }
665 
666  static std::string getFrameID(ChannelRead<T>& read) {
667  return read->frameID;
668  }
669 
671  return read->sequenceID;
672  }
673 };
674 
675 template<typename T>
677  static const T& getValue(ChannelRead<T>& read) {
678  return read->value();
679  }
680 };
681 
688 template<typename T>
691 
693  {
694  return static_cast<IntervalListIterator>(rv.begin());
695  }
696 
698  {
699  return static_cast<IntervalListIterator>(rv.end());
700  }
701 };
702 
704 
711 template<typename T>
712 boost::python::object convertJSONtoObject(const json::Value& value)
713 {
714  JSONDeserializer deserializer(value);
715  T obj;
716  deserializer.deserialize(obj);
717  return boost::python::object(obj);
718 }
719 
727 template<typename T>
729 {
730  JSONSerializer json;
731  return json.serialize(obj);
732 }
733 
735 
736 }}
737 
738 MIRA_NO_PUBLIC_DEFAULT_CONSTRUCTOR(python::PyUnit) // this must be outside of namespaces
739 
740 
742 #define MIRA_PYTHON_COMMA_HELPER ,
743 
747 #define MIRA_PYTHONCONNECTOR_ALL_TYPES(MACRO) \
748 MACRO(bool, "Bool"); \
749 MACRO(int, "Int"); \
750 MACRO(int64, "Int64"); \
751 MACRO(float, "Float"); \
752 MACRO(std::string, "String");
753 
754 #define MIRA_PYTHONCONNECTOR_ALL_TYPES_2_HELPER(MACRO, TYPE, PREFIX) \
755 MACRO(TYPE, bool, PREFIX, "Bool"); \
756 MACRO(TYPE, int, PREFIX, "Int"); \
757 MACRO(TYPE, int64, PREFIX, "Int64"); \
758 MACRO(TYPE, float, PREFIX, "Float"); \
759 MACRO##_STRING(TYPE, std::string, PREFIX, "String");
760 
761 #define MIRA_PYTHONCONNECTOR_ALL_TYPES_2(MACRO) \
762 MIRA_PYTHONCONNECTOR_ALL_TYPES_2_HELPER(MACRO, bool, "Bool"); \
763 MIRA_PYTHONCONNECTOR_ALL_TYPES_2_HELPER(MACRO, int, "Int"); \
764 MIRA_PYTHONCONNECTOR_ALL_TYPES_2_HELPER(MACRO, int64, "Int64"); \
765 MIRA_PYTHONCONNECTOR_ALL_TYPES_2_HELPER(MACRO, float, "Float"); \
766 MIRA_PYTHONCONNECTOR_ALL_TYPES_2_HELPER(MACRO, std::string, "String");
767 
773 #define MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(type, name) \
774  .def("subscribeInterval", &mira::python::subscribeIntervalCb<type>) \
775  .staticmethod("subscribeInterval") \
776  .def("subscribe", &mira::python::subscribeCb<type>) \
777  .def("subscribe", &mira::python::subscribe<type>) \
778  .staticmethod("subscribe") \
779  .def("unsubscribe", &mira::python::unsubscribe<type>) \
780  .staticmethod("unsubscribe") \
781  .def("publish", &mira::python::publish<type>) \
782  .staticmethod("publish") \
783  .def("convertJSONtoObject", &mira::python::convertJSONtoObject<type>) \
784  .staticmethod("convertJSONtoObject") \
785  .def("convertObjectToJSON", &mira::python::convertObjectToJSON<type>) \
786  ; \
787  class_<mira::Channel<type> >((std::string("Channel")+name).c_str()) \
788  .def("post", &mira::python::ChannelAccessWrapper<type>::postTimeFrame) \
789  .def("post", &mira::python::ChannelAccessWrapper<type>::postTime) \
790  .def("post", &mira::python::ChannelAccessWrapper<type>::post) \
791  .def("get", &mira::python::ChannelAccessWrapper<type>::get) \
792  .def("get", &mira::python::ChannelAccessWrapper<type>::getTime) \
793  .def("read", &mira::python::ChannelAccessWrapper<type>::read) \
794  .def("read", &mira::python::ChannelAccessWrapper<type>::readTime) \
795  .def("read", &mira::python::ChannelAccessWrapper<type>::readTimeDuration) \
796  .def("hasPublisher", &Channel<type>::hasPublisher) \
797  .def("hasSubscriber", &Channel<type>::hasSubscriber) \
798  .def("isValid", &Channel<type>::isValid) \
799  .def("waitForPublisher", &mira::python::ChannelWaitWrapper<type>::waitForPublisherTime) \
800  .def("waitForPublisher", &mira::python::ChannelWaitWrapper<type>::waitForPublisher) \
801  .def("waitForData", &mira::python::ChannelWaitWrapper<type>::waitForDataTime) \
802  .def("waitForData", &mira::python::ChannelWaitWrapper<type>::waitForData) \
803  ; \
804  class_<mira::ChannelRead<type> >((std::string("ChannelRead")+name).c_str())\
805  .add_property("Value", make_function(&mira::python::ChannelReadWrapper<type>::getValue, \
806  return_value_policy<reference_existing_object>())) \
807  .add_property("Timestamp", &mira::python::ChannelReadWrapper<type>::getTimestamp) \
808  .add_property("FrameID", &mira::python::ChannelReadWrapper<type>::getFrameID) \
809  .add_property("SequenceID", &mira::python::ChannelReadWrapper<type>::getSequenceID) \
810  ; \
811  class_<mira::ChannelReadInterval<type> >((std::string("ChannelReadInterval")+name).c_str())\
812  .def("__len__", &mira::ChannelReadInterval<type>::size) \
813  .def("__iter__", boost::python::range<return_value_policy<return_by_value> >(&mira::python::ChannelReadIntervalWrapper<type>::begin, &mira::python::ChannelReadIntervalWrapper<type>::end)) \
814  ;
815 
822 #define MIRA_PYTHONCONNECTOR_TYPE_FOOTER(type) \
823  MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(type,#type)
824 
829 #define MIRA_PYTHONCONNECTOR_PRIMITIVE_CHANNEL_WRAPPER(type, name) \
830  class_<mira::Channel<type> >("Channel" name) \
831  .def("post", &mira::python::ChannelAccessWrapper<type>::postTimeFrame) \
832  .def("post", &mira::python::ChannelAccessWrapper<type>::postTime) \
833  .def("post", &mira::python::ChannelAccessWrapper<type>::post) \
834  .def("get", &mira::python::ChannelAccessWrapper<type>::get) \
835  .def("get", &mira::python::ChannelAccessWrapper<type>::getTime) \
836  .def("get", &mira::python::ChannelAccessWrapper<type>::getTimeDuration) \
837  .def("read", &mira::python::ChannelAccessWrapper<type>::read) \
838  .def("read", &mira::python::ChannelAccessWrapper<type>::readTime) \
839  .def("read", &mira::python::ChannelAccessWrapper<type>::readTimeDuration) \
840  .def("hasPublisher", &Channel<type>::hasPublisher) \
841  .def("hasSubscriber", &Channel<type>::hasSubscriber) \
842  .def("isValid", &Channel<type>::isValid) \
843  .def("waitForPublisher", &mira::python::ChannelWaitWrapper<type>::waitForPublisherTime) \
844  .def("waitForPublisher", &mira::python::ChannelWaitWrapper<type>::waitForPublisher) \
845  .def("waitForData", &mira::python::ChannelWaitWrapper<type>::waitForDataTime) \
846  .def("waitForData", &mira::python::ChannelWaitWrapper<type>::waitForData) \
847  ; \
848  class_<mira::ChannelRead<type> >((std::string("ChannelRead")+name).c_str())\
849  .add_property("Value", make_function(&mira::python::ChannelReadWrapper<type>::getValue, \
850  return_value_policy<return_by_value>())) \
851  .add_property("Timestamp", &mira::python::ChannelReadWrapper<type>::getTimestamp) \
852  .add_property("FrameID", &mira::python::ChannelReadWrapper<type>::getFrameID) \
853  .add_property("SequenceID", &mira::python::ChannelReadWrapper<type>::getSequenceID) \
854  ; \
855  class_<mira::ChannelReadInterval<type> >((std::string("ChannelReadInterval")+name).c_str())\
856  .def("__len__", &mira::ChannelReadInterval<type>::size) \
857  .def("__iter__", range<return_value_policy<return_by_value> >(&mira::python::ChannelReadIntervalWrapper<type>::begin, &mira::python::ChannelReadIntervalWrapper<type>::end)) \
858  ;
859 
860 #define MIRA_PYTHONCONNECTOR_PRIMITIVE_VECTOR_CHANNEL_WRAPPER(type, name) \
861  class_<std::vector<type> >("Vector" name) \
862  .def(vector_indexing_suite<std::vector<type> >()) \
863  MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(std::vector<type>, "Vector" name)
864 
865 #define MIRA_PYTHONCONNECTOR_PRIMITIVE_SET_CHANNEL_WRAPPER(type, name) \
866  MIRA_PYTHONCONNECTOR_PYTHONSET_FUNCTIONS(type, "Set" name, \
867  MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(std::set<type>, "Set" name))
868 
869 #define MIRA_PYTHONCONNECTOR_PRIMITIVE_MAP_CHANNEL_WRAPPER(type1, type2, name1, name2) \
870  class_<std::map<type1, type2> >("Map" name1 name2) \
871  .def(map_indexing_suite<std::map<type1, type2> >()) \
872  MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(std::map<type1 MIRA_PYTHON_COMMA_HELPER type2>, "Map" name1 name2)
873 
874 #define MIRA_PYTHONCONNECTOR_PRIMITIVE_MAP_CHANNEL_WRAPPER_STRING(type1, type2, name1, name2) \
875  class_<std::map<type1, type2> >("Map" name1 name2) \
876  .def(map_indexing_suite<std::map<type1, type2>, true>()) \
877  MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(std::map<type1 MIRA_PYTHON_COMMA_HELPER type2>, "Map" name1 name2)
878 
879 #define MIRA_PYTHONCONNECTOR_PRIMITIVE_MAP_OF_VECTORS_CHANNEL_WRAPPER(type1, type2, name1, name2) \
880  class_<std::map<type1, std::vector<type2> > >("Map" name1 "Vector" name2) \
881  .def(map_indexing_suite<std::map<type1, std::vector<type2> > >()) \
882  MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(std::map<type1 MIRA_PYTHON_COMMA_HELPER std::vector<type2> >, "Map" name1 "Vector" name2)
883 
884 #define MIRA_PYTHONCONNECTOR_PRIMITIVE_MAP_OF_VECTORS_CHANNEL_WRAPPER_STRING(type1, type2, name1, name2) \
885  class_<std::map<type1, std::vector<type2> > >("Map" name1 "Vector" name2) \
886  .def(map_indexing_suite<std::map<type1, std::vector<type2> >, true>()) \
887  MIRA_PYTHONCONNECTOR_NAMED_TYPE_FOOTER(std::map<type1 MIRA_PYTHON_COMMA_HELPER std::vector<type2> >, "Map" name1 "Vector" name2)
888 
889 #endif
PyUnitWrapper(PyObject *self, const Duration &cycleTime)
Definition: UnitWrapper.h:153
void deserialize(T &value)
boost::python::object subscribeIntervalCb(const std::string &channelID, boost::python::object type, boost::python::object callable, const Duration &storageDuration)
See above.
boost::python::object channelPublish(const std::string &channelID, boost::python::object type)
Untyped publish method that is called from the unit or from the entity that wants to publish directly...
static ChannelRead< T > readTimeDuration(Channel< T > &channel, const Time &time, const Duration &tolerance)
Typed get method for typed channel which must be declared for every wrapped type. ...
Definition: UnitWrapper.h:599
static bool waitForDataTime(Channel< T > &channel, const Duration &d)
Definition: UnitWrapper.h:647
void post(const Stamped< T > &value)
PyUnit & getUnit()
Definition: UnitWrapper.h:113
CallbackInterval(const boost::python::object &callable)
Definition: UnitWrapper.h:305
void addTransformLink(const std::string &childID, const std::string &parentID)
static IntervalListIterator begin(ChannelReadInterval< T > &rv)
Definition: UnitWrapper.h:692
Channel< T > subscribe_internal(const std::string &channelID, boost::python::object callable)
Typed subscribe method that is called from within the subscribe methods contained in the type wrapper...
Definition: UnitWrapper.h:385
PyUnitWrapper(PyObject *self, uint32 flags)
Definition: UnitWrapper.h:145
Definition: UnitWrapper.h:275
void publishTransformIndirect3(const std::string &frameID, const std::string &targetID, const std::string &sourceID, const Pose3 &transform, const Time &timestamp)
static bool waitForPublisherTime(Channel< T > &channel, const Duration &d)
Definition: UnitWrapper.h:636
bool existsService(const std::string &service)
static T getTimeDuration(Channel< T > &channel, const Time &time, const Duration &tolerance)
Typed get method for typed channel which must be declared for every wrapped type. ...
Definition: UnitWrapper.h:565
json::Value convertObjectToJSON(const T &obj)
Generate a JSON representation using MIRA&#39;s JSON serialization capabilities.
Definition: UnitWrapper.h:728
void unsubscribe_internal(const std::string &channelID)
Typed unsubscribe method that is called from within the unsubscribe methods contained in the type wra...
Definition: UnitWrapper.h:410
bool doesChannelExist(const std::string &channelID)
void ok(const std::string &category)
void setUnitObject(boost::python::object unit)
Definition: UnitWrapper.h:75
void publishTransform3(const std::string &node, const Pose3 &t, const Time &timestamp)
Definition: UnitWrapper.h:535
void read(const std::string &s, Value &oValue)
#define MIRA_REFLECT_BASE(reflector, BaseClass)
Channel< T > publish_internal(const std::string &channelID)
Typed publish method that is called from within the subscribe methods contained in the type wrappers...
Definition: UnitWrapper.h:420
void checkinCycleTime(const std::string &ns, const std::string &name, const Duration &cycleTime)
boost::python::list queryServicesForInterface(const std::string &interface)
uint32_t uint32
PyUnitWrapper(PyObject *self)
Definition: UnitWrapper.h:138
PyUnitWrapper(PyObject *self, const Duration &cycleTime, uint32 flags)
Definition: UnitWrapper.h:161
void publishTransform2(const std::string &node, const Pose2 &t, const Time &timestamp)
static IntervalListIterator end(ChannelReadInterval< T > &rv)
Definition: UnitWrapper.h:697
static Time getTimestamp(ChannelRead< T > &read)
Definition: UnitWrapper.h:662
std::string waitForServiceInterfaceDuration(const std::string &service, const Duration &d)
void serialize(const std::string &name, const T &value, const std::string &comment="")
const Authority::Flags & getAuthorityFlags()
void unsubscribe(const std::string &channelID)
Pose2 getTransform2(const std::string &target, const std::string &source, const Time &time)
Duration mCycleTime
Definition: UnitWrapper.h:426
void publishTransformIndirect3Now(const std::string &frameID, const std::string &targetID, const std::string &sourceID, const Pose3 &transform)
#define MIRA_THROW(ex, msg)
Channel< T > publish(const std::string &channelID)
Pose3 getTransform3(const std::string &target, const std::string &source, const Time &time)
static const T & getValue(ChannelRead< T > &read)
Definition: UnitWrapper.h:677
boost::python::handle self()
Definition: UnitWrapper.h:181
const Duration & getCycleTime()
const_iterator end() const
std::vector< boost::shared_ptr< BaseCallback > > mCallbacks
Definition: UnitWrapper.h:428
Callback(const boost::python::object &callable)
Definition: UnitWrapper.h:278
static void postTimeFrame(Channel< T > &channel, const T &value, const Time &time, const std::string &frameID)
Typed post method for typed channel which must be declared for every wrapped type.
Definition: UnitWrapper.h:613
boost::python::object subscribe(const std::string &channelID, boost::python::object type)
Untyped subscribe method that is called from the unit or from the entity that wants to subscribe dire...
bool waitForService(const std::string &service)
const_iterator begin() const
bool isValid() const
bool waitForChannelDuration(const std::string &channelID, const Duration &d)
Definition: UnitWrapper.h:268
void bootup(const std::string &message)
friend Channel< T > publish(PyUnitWrapper &unit, const std::string &channelID)
Typed publish method every wrapped type must contain.
Definition: UnitWrapper.h:529
bool waitForServiceDuration(const std::string &service, const Duration &d)
#define MIRA_REFLECT_CALL(ReflectorType, reflector, context, COMMAND)
friend class PythonUnitLoader
Definition: UnitWrapper.h:57
bool waitForChannel(const std::string &channelID)
void publishTransformIndirect2Now(const std::string &frameID, const std::string &targetID, const std::string &sourceID, const Pose2 &transform)
PropertyHint type(const std::string &t)
Exception handling for python exceptions.
static ChannelRead< T > readTime(Channel< T > &channel, const Time &time)
Typed get method for typed channel which must be declared for every wrapped type. ...
Definition: UnitWrapper.h:588
PyUnitWrapperBase()
Definition: UnitWrapper.h:99
boost::python::object mUnitObject
Definition: UnitWrapper.h:93
void setPropertyNode(boost::shared_ptr< PropertyNode > node)
Definition: ReflectorWrapper.h:133
ChannelRead< T > read()
Channel< T > subscribe_internal(const std::string &channelID, boost::python::object callable, const Duration &storageDuration)
Typed subscribe method that is called from within the subscribe methods contained in the type wrapper...
Definition: UnitWrapper.h:370
Definition: UnitWrapper.h:676
PyObject * mSelf
Definition: UnitWrapper.h:425
Definition: UnitWrapper.h:661
void process(const mira::Timer &timer)
static std::string getFrameID(ChannelRead< T > &read)
Definition: UnitWrapper.h:666
void error(const std::string &category, const std::string &message)
static uint32 getSequenceID(ChannelRead< T > &read)
Definition: UnitWrapper.h:670
PyUnit * getUnitPtr()
Definition: UnitWrapper.h:121
boost::python::object mCallable
Definition: UnitWrapper.h:298
void checkin(const std::string &ns, const std::string &name)
Definition: UnitWrapper.h:174
static T get(Channel< T > &channel)
Typed get method for typed channel which must be declared for every wrapped type. ...
Definition: UnitWrapper.h:543
#define MIRA_OBJECT(classIdentifier)
void publishTransform2Now(const std::string &node, const Pose2 &t)
bool isValid() const
Definition: UnitWrapper.h:126
Interface for exporting std::set to python Most of the code taken from Andreas Beyer <mail <at> a-bey...
A scoped global interpreter (GIL) lock.
Definition: ScopedGIL.h:54
void checkout()
Definition: UnitWrapper.h:634
static Duration infinity()
json_spirit::mValue Value
virtual ~PyUnitWrapperBase()
Definition: UnitWrapper.h:101
bool isSubscribedOn(const std::string &channelID)
Check if subscribed on the channel.
static T getTime(Channel< T > &channel, const Time &time)
Typed get method for typed channel which must be declared for every wrapped type. ...
Definition: UnitWrapper.h:554
#define MIRA_NO_PUBLIC_DEFAULT_CONSTRUCTOR(CLASS)
ChannelRead< T > waitForData(const Duration &timeout=Duration::infinity()) const
void reflect(BaseReflectorWrapper &r)
ChannelWrite< T > write()
Stamped< T > get()
std::string resolveName(const std::string &name)
Include this instead of boost/python.hpp to reduce compile time warning spam from Boost internal inco...
void publishTransformIndirect2(const std::string &frameID, const std::string &targetID, const std::string &sourceID, const Pose2 &transform, const Time &timestamp)
Definition: UnitWrapper.h:52
static bool waitForData(Channel< T > &channel)
Definition: UnitWrapper.h:654
void operator()(ChannelReadInterval< T > read)
Definition: UnitWrapper.h:310
boost::python::object subscribeCb(const std::string &channelID, boost::python::object type, boost::python::object callable)
See above.
Also wrap ChannelReadInterval<T>::begin and ChannelReadInterval<T>::end to return the ChannelReadInte...
Definition: UnitWrapper.h:689
Channel< T > subscribeInterval(const std::string &channelID, boost::function< void(ChannelReadInterval< T >)> fn, const Duration &storageDuration, bool independentThread=false, const Time &startAfter=Time::unixEpoch())
std::string getLastExceptionString()
Extract the message from the last python exception.
Pose2 getTransform2Latest(const std::string &target, const std::string &source)
boost::python::object convertJSONtoObject(const json::Value &value)
Create an object instance from JSON data MIRA&#39;s JSON deserialization capabilities.
Definition: UnitWrapper.h:712
void operator()(ChannelRead< T > read)
Definition: UnitWrapper.h:283
Definition: UnitWrapper.h:135
static void post(Channel< T > &channel, const T &value)
Definition: UnitWrapper.h:627
Authority::Flags mFlags
Definition: UnitWrapper.h:427
Definition: ReflectorWrapper.h:168
void publishTransform3Now(const std::string &node, const Pose3 &t)
mira::Channel< void > getChannel(const std::string &channelID)
Channel< T > subscribe_internal(const std::string &channelID)
Typed subscribe method that is called from within the subscribe methods contained in the type wrapper...
Definition: UnitWrapper.h:400
Definition: UnitWrapper.h:96
void warning(const std::string &category, const std::string &message)
virtual ~PyUnit()
Definition: UnitWrapper.h:66
static bool waitForPublisher(Channel< T > &channel)
Definition: UnitWrapper.h:642
PyUnitWrapper & unit()
Definition: UnitWrapper.h:80
virtual ~BaseCallback()
Definition: UnitWrapper.h:271
A scoped global interpreter (GIL) lock.
Definition: ScopedGIL.h:99
boost::python::object mCallable
Definition: UnitWrapper.h:325
static ChannelRead< T > read(Channel< T > &channel)
Typed get method for typed channel which must be declared for every wrapped type. ...
Definition: UnitWrapper.h:577
Channel< T > subscribe(const std::string &channelID, const Duration &storageDuration=Duration::seconds(0))
void unsubscribe(const std::string &channelID, boost::python::object type)
Untyped unsubscribe method that is called from the unit.
virtual void reflect(BaseReflectorWrapper &r)=0
A python interpreter singleton.
void setUnit(PyUnit *unit)
Definition: UnitWrapper.h:111
Pose3 getTransform3Latest(const std::string &target, const std::string &source)
bool waitForPublisher(const Duration &timeout=Duration::infinity()) const
static void postTime(Channel< T > &channel, const T &value, const Time &time)
Definition: UnitWrapper.h:622
std::string waitForServiceInterface(const std::string &service)
ChannelReadInterval< T >::IntervalList::const_iterator IntervalListIterator
Definition: UnitWrapper.h:690
void reflect(Reflector &r)
Definition: UnitWrapper.h:453