KnowRob basics

This page describes the 'catkinized' version of KnowRob that has been converted to the catkin buildsystem and the new rosjava. The documentation for older versions can be found here.

Installing and launching the system

If you would just like to try KnowRob, you can use the binary packages, but if you would like to experiment more and adapt the packages, you should install the source distribution. Please refer to the installation guide for instructions. Make sure to set up your workspace before starting this tutorial.

You can now launch the system using the rosprolog script which takes the name of the package to be launched as parameter. When calling a package with rosprolog, the file is loaded, and all of referenced packages as well. That procedure ensures that all packages are initialized when being loaded. For more information on how to load your own packages, have a look at the rosprolog documentation or into one of the files.

 rosrun rosprolog rosprolog knowrob_common
You should now see the Prolog console.

Querying the KnowRob ontology

The KnowRob taxonomy that is contained in the file knowrob.owl in the knowrob_common package is loaded by default, so you can start exploring the available classes, e.g. with

 ?- owl_subclass_of(A, knowrob:'FoodOrDrink'). <ENTER>
 A = '' ;
 A = '' ;
 A = '' ;
 A = '' ;
 A = '' ;
 A = '' 

Some notes on the query syntax: Predicates in a query can be linked with a comma, denoting the logical AND, or a semicolon for the logical OR. Each query is finished with a '.'. You can step through the results with the semicolon, or just hit <ENTER> again to stop generating more results. For more information on query predicates, have a look at the Semweb library documentation.

Before continuing with the tutorial, try to get familiar with the taxonomy and its main classes and properties, either by exploring the hierarchy from Prolog or, more convenient, by loading knowrob.owl into the Protege editor.

Loading and querying environment information

So far, we only performed reasoning on the class level. For robotic applications, it is also important to reason about instances of these classes, for example observed objects or actions. An example set of instances are the semantic maps, contained as OWL file in the knowrob_map_data package. We can parse the OWL file containing the map by using a package:// URL and the path inside the package:

 ?- owl_parse('package://knowrob_map_data/owl/ccrl2_semantic_map.owl').

You can query for properties of instances using the rdf_has(S,P,O) or owl_has(S,P,O) predicates, which retrieve all triples with matching Subject, Predicate, or Object from the knowledge base. The following query, for example, asks for all objects of type knowrob:'Drawer'.

 ?- owl_has(A, rdf:type, knowrob:'Drawer').
 A = knowrob:'Drawer1' ;
 A = knowrob:'Drawer103' ;
 A = knowrob:'Drawer109' ;

For getting an overview of the information that is available about one object instance, we can query for all triples where the respective instance fills the Subject slot. In this example we pass two unbound variables P and O as arguments and receive all possible combinations of values as result.

 ?- owl_has(knowrob:'Drawer1', P, O).
 P = knowrob:prismaticallyConnectedTo,
 O = knowrob:'Door4' ;
 P = knowrob:depthOfObject,
 O = literal(type(xsd:double,'0.574538')) ;
 P = knowrob:heightOfObject,
 O = literal(type(xsd:double,'0.338121')) ;
 P = knowrob:widthOfObject,
 O = literal(type(xsd:double,'0.58045006')) ;
 P = knowrob:properPhysicalParts,
 O = knowrob:'Door4' ;
 P = knowrob:properPhysicalParts,
 O = knowrob:'Handle127' ;
 P = knowrob:properPhysicalParts,
 O = knowrob:'Slider4' ;
 P = knowrob:describedInMap,
 O = '' ;
 P = '',
 O = literal(drawer_fridge_bottom_fixed_link) ;
 P = rdf:type,
 O = owl:'NamedIndividual' ;
 P = rdf:type,
 O = knowrob:'Drawer'

Visualizing objects

KnowRob includes a browser-based visualization canvas that can display 3D data such as the environment map or the robot itself. The canvas is based on rosbridge and the robotwebtools libraries that provide tools for displaying information from ROS in a browser. To forward information from the ROS world to the browser, you need to launch the rosbridge websocket server in another terminal using the following command:

roslaunch rosbridge_server rosbridge_websocket.launch

You can just leave the rosbridge node running in the background while you are working on KnowRob. The following queries load a semantic environment map with a set of objects inside and push them to the 3D canvas. The result should look like the image below.

% Load the visualization Prolog module
?- register_ros_package(knowrob_vis).
% Load an environment map to get some example objects
?- owl_parse('package://knowrob_map_data/owl/ccrl2_semantic_map.owl').
% Start the KnowRob side of the visualization canvas. This will start
% a web server at http://localhost:1111 -- open that page in a browser
?- visualisation_server.
% Select the map instance of type 'SemanticEnvironmentMap' 
% and add it to the 3D canvas
?- owl_individual_of(A, knowrob:'SemanticEnvironmentMap'), 

Using the SWI Prolog tracer for debugging

In Prolog, the debugger is called “tracer”. Like a debugger in other programming languages, it allows to step through the program execution and to inspect the variable bindings. In addition, it shows the call stack of predicates, from the query to the current context, and the “choice points” that are still open. When asked a query, Prolog performs a depth-first search for solutions, starting from the query, trying to prove it from the facts in the Prolog database. Whenever there are multiple possible outcomes a predicate can have, Prolog continues with the first one, but creates a “choice point” to remember that there are still other options that have not yet been explored. If the first option did not lead to a result or if more results are to be computed, it will return to these choice points and further explore the solution space from there.

The graphical tracer is an important tool for debugging Prolog programs as it allows to follow the inference and to figure out where the execution fails. The following pages explain the process in general and list the predicates for controlling the graphical tracer.