Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorials:beginner:process_modules [2016/01/22 16:51] – [Writing a process module for the turtlesim] gkazhoyatutorials:beginner:process_modules [2016/01/25 11:30] gkazhoya
Line 30: Line 30:
  
 ===== 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'', ''actionlib_msgs'', ''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>
Line 47: Line 49:
 </code> </code>
  
-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'' 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'' file, which should now look like this:
  
 <code lisp> <code lisp>
Line 69: Line 71:
                                                    "turtle-action-client"))))))                                                    "turtle-action-client"))))))
 </code> </code>
-==== 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.+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 ====
  
-We first need to connect to the turtlesim action server as an ActionLib client. To do this, let's create a ''turtle-action-client.lisp'' file in the ''src'' subdirectory and append the following to it:+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 125: 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. Create a new file ''process-modules.lisp'' with the following content:+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 turtle-actuators (action-designator)
   (roslisp:ros-info (turtle-process-modules)   (roslisp:ros-info (turtle-process-modules)
                     "Turtle navigation invoked with action designator `~a'."                     "Turtle 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) 
 +  `(with-process-modules-running 
 +       (turtle-actuators) 
 +     ,@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 ''turtle-actuators'' 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''turtle-actuators''When we will have several, we 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 156: 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>