Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorials:beginner:process_modules [2015/05/11 17:01] – created gkazhoyatutorials:beginner:process_modules [2016/03/04 14:25] (current) – old revision restored (2016/01/25 18:26) gkazhoya
Line 8: Line 8:
 ===== Process modules: an overview ===== ===== Process modules: an overview =====
  
-A process module is a program that controls a robot actuator. Different robots will have different kinds of actuators, requiring different kinds of controllers, and we would like to abstract away such specifics at the high level of task specification. When we ask a robot to clean a kitchen, we don't care that it has two arms or one; rather, as long as it has some capacity to manipulate objects, and some ability to move around, we can issue the cleaning task to it. Process modules allow us this flexibility by providing a well-defined, robot-independent interface to high level planning which works as an abstraction layer over the robot controllers.+A process module is a program that controls a robot actuator. Different robots will have different kinds of actuators, requiring different kinds of controllers, and we would like to abstract away such specifics at the high level of task specification. When we ask a robot to clean a kitchen, we don't care that it has two arms or one; rather, as long as it has some capacity to manipulate objects, and some ability to move around, we can issue the cleaning task to it. Process modules allow us this flexibility by providing a well-defined, robot-independent interface to high level planning which works as an abstraction layer over the robot controllers. You can read some more about process modules in the [[http://cram-system.org/doc/package/cram_process_modules|documentation of the package]].
  
-For better organization, process modules can also be referenced by aliases that describe what kind of function they can perform. Currently, in the cram_highlevel stack for manipulation platforms the following aliases are defined:+For better organization, process modules can also be referenced by aliases that describe what kind of function they can perform. For examplecurrently in the ''cram_pr2'' stack for the PR2 mobile manipulation platform the following aliases are defined:
  
   * :navigation   * :navigation
Line 22: Line 22:
  
 <code lisp> <code lisp>
-(with-designators ((my-designator (location '((close-to 'fridge)))))+(with-designators 
 +    ((my-designator :location '((:close-to :fridge))))
   (pm-execute :navigation my-designator))   (pm-execute :navigation my-designator))
 </code> </code>
  
-This will run a process module associated to :navigation and use my-designator to pass parameters to it. In this case, the designator is a semantic description of a target location and it's up to the process module to ask that this designator be resolved (by some appropriate other module in the system) so as to get an actual location and then control the robot to reach the target. The specifics of the controller may vary enormously; the robot might be differential drive, or legged. But these details are not important for this high level plan. All we want is for the robot to approach the fridge, in whatever way it can carry itself there.+This will run a process module associated with '':navigation'' and use ''my-designator'' as an input parameter. In this case, the designator is a semantic description of a target location and it's up to the process module to ask that this designator be resolved (by some appropriate other module in the system) so as to get an actual location and then control the robot to reach the target. The specifics of the controller may vary enormously; the robot might be differential drive, or legged. But these details are not important for this high level plan. All we want is for the robot to approach the fridge, in whatever way it can carry itself there.
  
 ===== Writing a process module for the turtlesim ===== ===== Writing a process module for the turtlesim =====
 +
 +For this tutorial we will use the [[http://wiki.ros.org/actionlib_lisp/Tutorials/actionlibBasicUsage|ActionLib]] turtlesim server for having a turtle draw a shape.
  
 Once again, some new dependencies must be declared in the tutorial files you've been working on.  Once again, some new dependencies must be declared in the tutorial files you've been working on. 
  
-In your package.xml file you need to add build and runtime dependencies on actionlib_lisp, turtle_actionlib, and cram_process_modules:+In your ''package.xml'' file you need to add build and runtime dependencies on ''actionlib_lisp''''actionlib_msgs''''turtle_actionlib'' and ''cram_process_modules'':
  
 <code> <code>
   <build_depend>actionlib_lisp</build_depend>   <build_depend>actionlib_lisp</build_depend>
 +  <build_depend>actionlib_msgs</build_depend>
   <build_depend>turtle_actionlib</build_depend>   <build_depend>turtle_actionlib</build_depend>
   <build_depend>cram_process_modules</build_depend>   <build_depend>cram_process_modules</build_depend>
  
-  <run_depend>cram_process_modules</run_depend> 
   <run_depend>actionlib_lisp</run_depend>   <run_depend>actionlib_lisp</run_depend>
 +  <run_depend>actionlib_msgs</run_depend>
   <run_depend>turtle_actionlib</run_depend>   <run_depend>turtle_actionlib</run_depend>
 +  <run_depend>cram_process_modules</run_depend>
 </code> </code>
  
-Similarly, in your .asd file you should add cram-language-designator-support, actionlib, actionlib_tutorials-msg, process-modules, turtle_actionlib-msg to the :depends-on list. Let's also create two new source files in the src folder of your tutorial, call them turtle-action-client.lisp and process-modules.lisp, and add them to your *.asd file which should now look like this:+Similarly, in your ''.asd'' file you should add ''actionlib''''actionlib_msgs-msg''''turtle_actionlib-msg'', ''cram-process-modules'' and ''cram-language-designator-support'' to the '':depends-on'' list. Let's also create two new source files in the ''src'' directory of your tutorial, call them ''turtle-action-client.lisp'' and ''process-modules.lisp'', and add them to your ''*.asd'' filewhich should now look like this:
  
 <code lisp> <code lisp>
 (defsystem cram-beginner-tutorial (defsystem cram-beginner-tutorial
-  :depends-on (roslisp cram-language turtlesim-msg cl-transforms geometry_msgs-msg designators cram-reasoning  +  :depends-on (cram-language roslisp turtlesim-msg geometry_msgs-msg cl-transforms 
-                 cram-language-designator-support actionlib actionlib_tutorials-msg process-modules turtle_actionlib-msg)+                             cram-designators cram-prolog 
 +                             actionlib actionlib_msgs-msg turtle_actionlib-msg 
 +                             cram-process-modules cram-language-designator-support)
   :components   :components
   ((:module "src"   ((:module "src"
             :components             :components
             ((:file "package")             ((:file "package")
-             (:file "control-turtlesim" :depends-on  ("package")) +             (:file "control-turtlesim" :depends-on ("package")) 
-             (:file "simple-plans" :depends-on  ("package" "control-turtlesim")) +             (:file "simple-plans" :depends-on ("package" "control-turtlesim")) 
-             (:file "action-designators" :depends-on  ("package")) +             (:file "action-designators" :depends-on ("package")) 
-             (:file "turtle-action-client" :depends-on  ("package")) +             (:file "turtle-action-client" :depends-on ("package")) 
-             (:file "process-modules" :depends-on  ("package" "control-turtlesim" "simple-plans" "action-designators" "turtle-action-client"))))))+             (:file "process-modules" :depends-on ("package" 
 +                                                   "control-turtlesim" 
 +                                                   "simple-plans" 
 +                                                   "action-designators" 
 +                                                   "turtle-action-client"))))))
 </code> </code>
 +
 +Finally, let's also add '':cram-process-modules'' and ''cram-language-designator-support'' to the use list of our Lisp package for convenience:
 +<code lisp>
 +  (:use :cpl :roslisp :cl-transforms :cram-designators :cram-process-modules
 +        :cram-language-designator-support)
 +</code>
 +
  
 ==== A process module for the turtlesim ==== ==== A process module for the turtlesim ====
  
-We first need to connect to the turtlesim as a client for an action that will have the turtle draw a shape. To do this, append the following to your turtle-action-client.lisp file+We first need to connect to the turtlesim action server with an ActionLib client. To do this, append the following to your ''turtle-action-client.lisp'':
  
 <code lisp> <code lisp>
Line 88: Line 106:
 </code> </code>
  
-The above code simply declares a variable *navp-client* which we can then initialize with a pointer to our ROS action client and retrieve this pointer later with the init-action-client and get-action-client functions, respectively.+The above code simply declares a variable ''*navp-client*'' which we can then initialize with a pointer to our ROS action client and retrieve this pointer later with the ''init-action-client'' and ''get-action-client functions'', respectively.
  
-We'll need a way to tell the action client to move along a shape (which is specified by a number of edges and a radius), so append this to process-modules.lisp:+We will need an action goal for our action client that should represent a shape (which is specified by a number of edges and a radius), so append this to the ''turtle-action-client.lisp'':
  
 <code lisp> <code lisp>
Line 115: Line 133:
 In the above code we define a simple function to convert an edge and radius pair of values into a goal for the action client, and a function that will call said action client, with some error handling built in (for example, if the function is called without an action client being defined, it will (re)initialize one and retry). In the above code we define a simple function to convert an edge and radius pair of values into a goal for the action client, and a function that will call said action client, with some error handling built in (for example, if the function is called without an action client being defined, it will (re)initialize one and retry).
  
-Now that the lower level of controlling the turtlesim is taken care of, it's finally time to look at process modules and their interface to the higher levels. Append the following to process-modules.lisp:+Now that the lower level of controlling the TurtleSim is taken care of, it's finally time to look at process modules and their interface to the higher levels. Append the following to your ''process-modules.lisp'' file:
  
 <code lisp> <code lisp>
 (in-package :tut) (in-package :tut)
  
-(cram-process-modules:def-process-module turtle-actuators (action-designator)+(def-process-module actionlib-navigation (action-designator)
   (roslisp:ros-info (turtle-process-modules)   (roslisp:ros-info (turtle-process-modules)
-                    "Turtle navigation invoked with action designator `~a'."+                    "Turtle shape navigation invoked with action designator `~a'."
                     action-designator)                     action-designator)
-  (destructuring-bind (cmd action-goal) (reference action-designator) +  (destructuring-bind (command action-goal) (reference action-designator) 
-    (ecase cmd +    (ecase command 
-      (shape +      (draw-shape 
-         (call-shape-action +       (call-shape-action 
-          :edges (turtle-shape-edges action-goal) +        :edges (turtle-shape-edges action-goal) 
-          :radius (turtle-shape-radius action-goal)))))) +        :radius (turtle-shape-radius action-goal))))))
  
 (defmacro with-turtle-process-modules (&body body) (defmacro with-turtle-process-modules (&body body)
-  `(cpm:with-process-modules-running +  `(with-process-modules-running 
-       (turtle-actuators)+       (actionlib-navigation)
      ,@body))      ,@body))
 </code> </code>
  
-First, we use the cram-process-modules:def-process-module macro to define turtle-actuators as a process module taking one parameter (action-designator). The other lines in the def-process-module are comprise the code for turtle-actuators.+First, we use the ''cram-process-modules:def-process-module'' macro to define ''actionlib-navigation'' as a process module taking one parameter (''action-designator''). The process module then chooses which action to perform depending on the command specified in the designator: ''destructuring-bind'' maps the results from ''(reference action-designator)'' to the variables ''command'' and ''action-goal'' respectively. Note that the inference rules we defined previously provide a name for the kind of action goal we have (currently, all are ''draw-shape''), and a ''turtle-shape'' object. We run an ''ecase'' on the kind of goal (currently, we only have the shape case) and use ''call-shape-action'' to tell the lower level to move the turtle around, given these parameters we infer from designator resolution.
  
-destructuring-bind will map the results from (reference action-designator) to the variables cmd and action-goal respectivelyNote that the inference rules we defined previously provide a name for the kind of action goal we have (currentlyall are "shape"), and a turtle-shape objectWe run an ecase on the kind of goal (currently, we only have the shape case) and use the call-shape-action to tell the lower level to move the turtle around, given these parameters we infer from designator resolution.+The ''with-turtle-process-modules'' macro is a macro we define for convenienceIt allows us to set up a context in which to run commands, knowing that the turtle process modules are all running concurrently. Right now we only have one defined, ''actionlib-navigation''. When we will have severalwe will add them to the list we pass to ''cram-process-modules:with-process-modules-running''Note that the ''with-process-modules-running'' macro (and therefore ''with-turtle-process-modules'' too) needs to be run inside a ''top-level'' form. We will see this below.
  
-The with-turtle-process-modules macro is a macro we define for convenience. It allows us to set up a context in which to run commands, knowing that the turtle process modules are all running concurrently. Right now we only have one defined, turtle-actuators. When we will have several, we will add them to the list we pass to cpm:with-process-modules-running. Note that the cpm:with-process-modules-running macro (and therefore with-turtle-process-modules too) needs to be run inside a top-level macro. We will see this below. +Let's try this out. Make sure you have ''roscore''''turtlesim'', and ''turtle_actionlib'' running. In a terminal tab for each,
- +
-Let's try this out. Make sure you have roscore, turtlesim, and turtle_actionlib running. In a terminal tab for each,+
  
 <code> <code>
Line 152: Line 167:
 </code> </code>
  
-(**Note** on Oct. 21st 2014: you should check out then catkin_make the ROS common_tutorials from github to make sure you have the newest turtle_actionlib version available, otherwise the turtle might not move. To clone the ros_common tutorials, you will need to  +For convenience, let's append one more function to ''process-modules.lisp'' that will do all our calls for us:
- +
-<code> +
-git clone https://github.com/ros/common_tutorials +
-</code> +
- +
-inside the src folder of a ROS workspace, then catkin_make it.) +
- +
-For convenience, let's append one more function to process-modules.lisp that will do all our calls for us:+
  
 <code lisp> <code lisp>
-(defun draw-hexagon (r)+(defun draw-hexagon (radius)
   (let ((turtle-name "turtle1"))   (let ((turtle-name "turtle1"))
     (start-ros-node turtle-name)     (start-ros-node turtle-name)
Line 169: Line 176:
     (top-level     (top-level
       (with-turtle-process-modules       (with-turtle-process-modules
-        (cpm:process-module-alias :manipulation 'turtle-actuators+        (process-module-alias :navigation 'actionlib-navigation
-          (cram-language-designator-support:with-designators +        (with-designators 
-            ((trajectory (action '((type shape) (shape hexagon) (radius r))))) +            ((trajectory :action `((:type :shape) (:shape :hexagon) (:radius ,radius)))) 
-            (cpm:pm-execute :manipulation trajectory))))))+          (pm-execute :navigation trajectory))))))
 </code> </code>
  
-What the function does is simply start a ROS node for our turtle and sets up the action client, then activates the turtle process modules (in our case, the sole existing one), creates a designator to describe how the turtle should move, and calls the cpm:pm-execute macro to have the process module follow the trajectory specified by the designator. +What the function does is simply start a ROS node for our turtle and sets up the action client, then activates the turtle process modules (in our case, the sole existing one), creates a designator to describe how the turtle should move, and calls the ''cram-process-modules:pm-execute'' macro to have the process module follow the trajectory specified by the designator. 
  
-Two other things to observe here are the use of the top-level macro, which sets up a CRAM running context and is needed for with-turtle-process-modules. Also, we declare that :manipulation is the alias for our turtle-actuators process module. That way, our plan can invoke a generic name like :manipulation, rather than a robot-specific one like turtle-actuators.+Two other things to observe here are the use of the ''top-level'' macro, which sets up a CRAM running context and is needed for ''with-turtle-process-modules''. Also, we declare that '':navigation'' is the alias for our ''actionlib-navigation'' process module. That way, our plan can invoke a generic name like '':navigation'', rather than a robot-specific one like ''actionlib-navigation''.
  
 Reload the tutorial in REPL, and let's try to start the tutorial Reload the tutorial in REPL, and let's try to start the tutorial
  
 <code lisp> <code lisp>
-TUT> (draw-hexagon 3+TUT> (draw-hexagon 2
-[(ROSLISP TOP) INFO] 1413894471.489: Node name is /turtle1 +[(ROSLISP TOP) INFO] 1453721727.445: Node name is /turtle1 
-[(ROSLISP TOP) INFO] 1413894471.489: Namespace is / +[(ROSLISP TOP) INFO] 1453721727.446: Namespace is / 
-[(ROSLISP TOP) INFO] 1413894471.489: Params are NIL +[(ROSLISP TOP) INFO] 1453721727.446: Params are NIL 
-[(ROSLISP TOP) INFO] 1413894471.489: Remappings are: +[(ROSLISP TOP) INFO] 1453721727.446: Remappings are: 
-[(ROSLISP TOP) INFO] 1413894471.489: master URI is 127.0.0.1:11311 +[(ROSLISP TOP) INFO] 1453721727.446: master URI is 127.0.0.1:11311 
-[(ROSLISP TOP) INFO] 1413894472.493: Node startup complete +[(ROSLISP TOP) INFO] 1453721728.452: Node startup complete 
-[(TURTLE-PROCESS-MODULES) INFO] 1413894472.501: Turtle navigation invoked with action designator `#<ACTION-DESIGNATOR ((TYPE+[(TURTLE-PROCESS-MODULES) INFO] 1453721728.467: Turtle shape navigation invoked with action designator `#<ACTION-DESIGNATOR ((TYPE
                                                                         SHAPE)                                                                         SHAPE)
                                                                        (SHAPE                                                                        (SHAPE
                                                                         HEXAGON)                                                                         HEXAGON)
                                                                        (RADIUS                                                                        (RADIUS
-                                                                        3)) {10084BC463}>'.+                                                                        2)) {10056DB513}>'.
 An error occured! An error occured!
 Client lost connection to server. Client lost connection to server.
 Reinitializing... Reinitializing...
  
-[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1413894480.479: Waiting for turtle shape action server... +[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1453721736.385: Waiting for turtle shape action server... 
-[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1413894482.480: Turtle shape action client created. +[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1453721738.386: Turtle shape action client created. 
-[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1413894505.711: Nav action finished.+[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1453721761.206: Nav action finished.
 [TURTLE_ACTIONLIB-MSG:SHAPERESULT [TURTLE_ACTIONLIB-MSG:SHAPERESULT
    INTERIOR_ANGLE:    INTERIOR_ANGLE:
      2.094395160675049d0      2.094395160675049d0
    APOTHEM:    APOTHEM:
-     2.598076105117798d0]+     1.7320507764816284d0]
 :SUCCEEDED :SUCCEEDED
 </code> </code>
  
-You should also see the turtle move in the turtlesim window and trace the required trajectory.+You should also see the turtle move in the TurtleSim window and trace the required trajectory.
  
 == Next == == Next ==
Line 217: Line 224:
  
 [[tutorials:beginner:location_designators|Using location designators with the turtlesim]] [[tutorials:beginner:location_designators|Using location designators with the turtlesim]]
-