This is an old revision of the document!


Write an interface to your perception system

There are two main approaches how perception can be performed: Some perception algorithms continuously detect objects and output the results (in ROS terminology: publish the results on a topic), others perform recognition only on demand (in ROS: by calling a service). These two kinds of systems need to be interfaced in different ways: The former requires a topic listener that records the published object detections and adds them to the knowledge base, the latter can be interfaced by computables that trigger the perception procedure when a query involves the respective information.

In this tutorial, we explain on two minimal examples how to write interfaces to these two kinds of perception systems. Currently, there is no 'standard' perception system in ROS, so some manual work is still needed to interface your favourite object recognition with KnowRob. We therefore created two 'dummy' perception systems that output simulated random object detections. It should however be very easy to adapt the examples to any real perception system.

Before starting with the tutorial, it is important to first understand how object detections are represented in KnowRob. Further information on this topic can be found in Sections 3.2 and 6.1 in http://nbn-resolving.de/urn/resolver.pl?urn:nbn:de:bvb:91-diss-20111125-1079930-1-7.

Setting up the perception tutorial

The knowrob_perception tutorial is part of the knowrob_tutorials repository. You need to check it out into your ROS workspace (i.e. into a directory that is part of your ROS_PACKAGE_PATH). https://github.com/knowrob/knowrob_tutorials.git

git clone https://github.com:knowrob/knowrob_tutorials.git

After the checkout, you should be able to roscd into the knowrob_perception directory and to rosmake the package. If the 'roscd' command does not change your working directory to the knowrob_perception directory, please check your ROS configuration (e.g. make sure knowrob_perception is part of the ROS_PACKAGE_PATH), because otherwise the following steps will fail as well. Both commands should work without any error messages.

roscd knowrob_perception
rosmake

Interfacing topic-based perception systems

Publisher

The file src/edu/tum/cs/ias/knowrob/tutorial/DummyPublisher.java implements a simple dummy publisher that simulates a perception system that regularly detects objects and publishes these detections on the /dummy_object_detections topic once a second. Try to understand how these detections are generated by the generateDummyObjectDetection() method. You can start the publisher using

rosrun knowrob_perception_tutorial dummy_publisher

Once the publisher is running, you can have a look at the generated object poses by calling the following command from a different terminal.

rostopic echo /dummy_object_detections

It should output messages of the following form:

type: DinnerFork
pose: 
  header: 
    seq: 0
    stamp: 
      secs: 1357547989
      nsecs: 196672575
    frame_id: map
  pose: 
    position: 
      x: 0.300724629488
      y: 2.96134330258
      z: 1.56672560148
    orientation: 
      x: 0.0
      y: 0.0
      z: 0.0
      w: 1.0

Subscriber

The counterpart on the client side that consumes the object detections is implemented in the file src/edu/tum/cs/ias/knowrob/tutorial/DummySubscriber.java. The subscriber has been realized using two threads: The listenToObjDetections thread subscribes to the topic and puts the incoming messages into the callback queue. The updateKnowRobObjDetections thread processes all object detections in this queue and creates the corresponding representations in KnowRob. The rationale behind this setup is to keep the subscriber thread as light-weight as possible to avoid problems of missing messages when the program is occupied updating the knowledge base. While the creation of an object detection in KnowRob is quite fast, this structure is crucial if the processing becomes more complex.

The following

  while (n.isValid()) {

    obj = callback.pop();

    Matrix4d p = quaternionToMatrix(obj.pose.pose);         
    String q = "create_object_perception(" +
          "'http://ias.cs.tum.edu/kb/knowrob.owl#"+obj.type+"', [" 
          + p.m00 + ","+ p.m01 + ","+ p.m02 + ","+ p.m03 + ","
          + p.m10 + ","+ p.m11 + ","+ p.m12 + ","+ p.m13 + ","
          + p.m20 + ","+ p.m21 + ","+ p.m22 + ","+ p.m23 + ","
          + p.m30 + ","+ p.m31 + ","+ p.m32 + ","+ p.m33 +
          "], ['DummyObjectDetection'], ObjInst)";

    PrologInterface.executeQuery(q);
    n.spinOnce();
  }

startObjDetectionsListener()

KnowRob integration

prolog/perception_tutorial.pl

obj_detections_listener(Listener) :-
  jpl_new('edu.tum.cs.ias.knowrob.tutorial.DummySubscriber', ['knowrob_tutorial_listener'], Listener),
  jpl_call(Listener, 'startObjDetectionsListener', [], _).

Interfacing service-based perception systems

Perception service

src/edu/tum/cs/ias/knowrob/tutorial/DummyService.java

Service client

src/edu/tum/cs/ias/knowrob/tutorial/DummyClient.java

KnowRob integration

implemented in prolog/perception_tutorial.pl

integrated as computable prolog class

in contrast to topic-based example, which performed most processing on the Java side, we are doing more of the processing on the Prolog side

comp_object_detection(_ObjClass, ObjInst) :-

  % Call the DetectObject service for retrieving a new object detection.
  % The method returns a reference to the Java ObjectDetection message object
  jpl_call('edu.tum.cs.ias.knowrob.tutorial.DummyClient', 'callObjDetectionService', [], ObjectDetection),


  % Read information from the ObjectDetection object

  % Read type -> simple string; combine with KnowRob namespace
  jpl_get(ObjectDetection, 'type', T),
  atom_concat('http://ias.cs.tum.edu/kb/knowrob.owl#', T, Type),


  % Read pose -> convert from quaternion to pose list
  jpl_get(ObjectDetection, 'pose', PoseStamped),
  jpl_get(PoseStamped, 'pose', PoseQuat),

  jpl_call('edu.tum.cs.ias.knowrob.tutorial.DummyClient', 'quaternionToMatrix', [PoseQuat], PoseMatrix),
  knowrob_coordinates:matrix4d_to_list(PoseMatrix,PoseList),


  % Create the object representations in the knowledge base
  % The third argument is the type of object perception describing 
  % the method how the object has been detected
  create_object_perception(Type, PoseList, ['DummyObjectDetection'], ObjInst).

Adapting the examples to your system

Other kinds of perception systems

In this tutorial, we have concentrated on object recognition as a special case of a perception task. There are of course other perception tasks like the identification and pose estimation of humans, recognition and interpretation of spoken commands, etc. Most of these systems can however be interfaced in a very similar way: If they produce information continuously and asynchronously, a topic-based interface can be used. If they compute information on demand, the computable-based interface can be adapted.