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
tutorials:beginner:assigning_actions [2016/01/25 21:45] – [Automatically choosing a process module for an action] gkazhoyatutorials:beginner:assigning_actions [2016/03/04 14:25] (current) – old revision restored (2016/01/25 23:21) gkazhoya
Line 7: Line 7:
 ===== Setting up fact groups to select a process module ===== ===== Setting up fact groups to select a process module =====
  
-We'll need to update few dependencies first. To your *.asd file, add cram-plan-library to your :depends-on list. You also need to add a :file (which we'll call "selecting-process-modules" in the :components list that depends on all previous tutorial filesYour *.asd file should look like this:+First let's create new file ''selecting-process-modules.lisp'' and add it to our ''*.asd'' file, which 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 cl-tf cram-plan-library)+                             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 "location-designators" :depends-on  ("package")) +             (:file "location-designators" :depends-on ("package")) 
-             (:file "process-modules" :depends-on  ("package" "control-turtlesim" "simple-plans" "action-designators" "turtle-action-client" "location-designators")) +             (:file "process-modules" :depends-on ("package" 
-             (:file "selecting-process-modules" :depends-on  ("package" "control-turtlesim" "simple-plans" "action-designators" "turtle-action-client" "location-designators" "process-modules"))))))+                                                   "control-turtlesim" 
 +                                                   "simple-plans" 
 +                                                   "action-designators" 
 +                                                   "turtle-action-client")) 
 +             (:file "selecting-process-modules" :depends-on ("package" 
 +                                                             "action-designators" 
 +                                                             "location-designators" 
 +                                                             "process-modules"))))))
 </code> </code>
  
-We'll need to define a couple new designator properties (navigation and goal) in package.lisp so it should now look like this+We only need to add a couple of Prolog rules to implement automatic process module choosing, append this to ''selecting-process-modules.lisp'':
- +
-<code lisp> +
-(in-package :cl-user) +
- +
-(desig-props:def-desig-package cram-beginner-tutorial +
-  (:nicknames :tut) +
-  (:use #:cpl +
-        #:roslisp +
-        #:cl-transforms +
-        #:cram-designators) +
- +
-  (:desig-properties +
-   ;; action properties +
-   #:shape +
-   #:navigation +
-   #:goal +
-   #:radius +
-   #:triangle +
-   #:square +
-   #:pentagon +
-   #:hexagon +
-   ;; location Properties +
-   #:vpos +
-   #:hpos +
-   #:left +
-   #:right +
-   #:top +
-   #:bottom +
-   #:center)) +
-</code> +
- +
-Prerequisites done, let's now create a selecting-process-modules.lisp file in the src folder of your tutorial, and let's first add this bit of code to it:+
  
 <code lisp> <code lisp>
 (in-package :tut) (in-package :tut)
  
-(cram-reasoning:def-fact-group navigation-action-designator (action-desig+(def-fact-group navigation-process-modules (available-process-module 
-  (cram-reasoning:<- (action-desig ?designator (navigation ?goal)) +                                            matching-process-module
-  (desig-prop ?designator (type navigation)) +  (<- (available-process-module actionlib-navigation)) 
-  (desig-prop ?designator (goal ?goal)))) +  (<- (available-process-module simple-navigation))
-</code>+
  
-What this snippet of code does is create a fact group that will be used to resolve action designators. In particular, if the designator contains a (type navigation) pair, and a (goal <an object, assumed to be a location designator by other code in this tutorial>), then the resolution procedure extracts the designator type (navigation in this case) and the object associated to the goal, and puts them both in a list which it returns. +  (<- (matching-process-module ?designator actionlib-navigation
- +    (desig-prop ?designator (:type :shape))) 
-Let's create a process module that can consume designators of type navigation, and append it to selecting-process-modules.lisp: +  (<- (matching-process-module ?designator simple-navigation
- +    (desig-prop ?designator (:type :goal))))
-<code lisp> +
-(cram-process-modules:def-process-module turtle-navigation-handler (action-designator+
-  (roslisp:ros-info (turtle-process-modules) +
-                    "Turtle navigation invoked with action designator `~a'." +
-                    action-designator+
-  (destructuring-bind (cmd action-goal) (reference action-designator) +
-    (ecase cmd +
-        (navigation +
-          (print (cl-transforms:origin (reference action-goal))) +
-          (move-to (cl-transforms:origin (reference action-goal)))))))+
 </code> </code>
  
-What this process module does is resolve the action designator it receives as a parameter, and if the designator is of type navigation, resolves the goal as a location designator, then moves the turtle to the resolved location.+The ''navigation-process-modules'' fact group extends the predicates ''cram-process-modules:matching-process-module'' and ''cram-process-modules:available-process-module''.
  
-Now comes the part where we instruct the system how to reason about process modulesand we need to append the following to selecting-process-modules.lisp:+The first two facts say that we have two process modules available for automatically matching them to designators (this is used for switching process modules for simulating and executing on the real robot in parallel), in our case no conditions need to be true for this fact to hold, our process modules are always available. ''matching-process-module'' predicates match designators with specific properties (in our case ''(:type :shape)'' or ''(:type :goal))'') to corresponding process modules names. See previous tutorials to find how these action designators are resolved and how do their corresponding process modules use them.
  
-<code lisp> +For convenience, let's add one more function to ''selecting-process-modules.lisp'':
-(cram-reasoning:def-fact-group turtle-navigation (cram-process-modules:matching-process-module +
-                                   cram-process-modules:available-process-module) +
-  (cram-reasoning:<- (cram-process-modules:matching-process-module ?designator turtle-navigation-handler) +
-    (desig-prop ?designator (type navigation))) +
-  (cram-reasoning:<- (cram-process-modules:available-process-module turtle-navigation-handler))) +
- +
-(cram-reasoning:def-fact-group turtle-actuators (cram-process-modules:matching-process-module +
-                                  cram-process-modules:available-process-module) +
-  (cram-reasoning:<- (cram-process-modules:matching-process-module ?designator turtle-actuators) +
-    (desig-prop ?designator (type shape))) +
-  (cram-reasoning:<- (cram-process-modules:available-process-module turtle-actuators))) +
-</code>  +
- +
-These fact groups extend the predicates cpm:matching-process-module and cpm:available-process-module. Both must be extended, which simply means defining what their values are if applied to the designators defined in this tutorial. For example, the first fact group says that, if a designator has a (type navigation) pair, then the matching-process-module is the turtle-navigation-handler we defined previously. As for turtle-actuators, it is a process module we defined in a [[tutorials:beginner:process_modules|previous tutorial]]; the kinds of designators it consumes also had their resolution mechanism [[tutorials:beginner:designators|defined previously]]. +
- +
-Notice that in this tutorial, available-process-module always gets our process modules. No conditions need to be true, our process modules are simply available. +
- +
-For convenience, let's add one more function to selecting-process-modules.lisp:+
  
 <code lisp> <code lisp>
-(defun do-action-designator (action-desig)+(defun perform-some-action (action-desig)
   (let ((turtle-name "turtle1"))   (let ((turtle-name "turtle1"))
     (start-ros-node turtle-name)     (start-ros-node turtle-name)
     (init-ros-turtle turtle-name)     (init-ros-turtle turtle-name)
     (top-level     (top-level
-      (cpm:with-process-modules-running (turtle-navigation-handler turtle-actuators) +      (with-turtle-process-modules 
-        (cram-plan-library:perform action-desig)))))+        (pm-execute-matching action-desig)))))
 </code> </code>
  
-This function is mostly similar to previous ones we used to call process modules; the difference is that rather than calling cpm:pm-execute with a process module and designator, we call cram-plan-library:perform with an (action) designatorand allow the system to decide what process module to use. In our case the decision is very simple, just a matter of matching type to appropriate consumer; more complex inferences are possible for real systems.+This function is mostly similar to previous ones we used to call process modules; the difference is that rather than calling ''pm-execute'' with a process module and an action designator, we call ''pm-execute-matching'' with designator and allow the system to decide what process module to use. In our case the decision is very simple, just a matter of matching type to appropriate consumer; more complex inferences are possible for real systems.
  
 ===== Running action designators ===== ===== Running action designators =====
  
-Let's load our code in roslisp repl. Make sure you have roscore, turtlesim, and the turtle actionlib shape_client running in terminals in the background.+Let's load our code in ''roslisp_repl''. Make sure you have ''roscore''''turtlesim'', and the TurtleSim ActionLib ''shape_client'' running in terminals in the background (see [[http://cram-system.org/tutorials/beginner/process_modules|a previous tutorial]] for the exact commands).
  
-With that, let's first make a couple of designators for test, so type this in roslisp repl's command line:+With that, let's first make a couple of designators for test, so type this in REPL's command line:
  
 <code lisp> <code lisp>
-CL-USER> (in-package :tut+TUT> (defparameter center-location 
-TUT> (defparameter shape-action (make-designator 'action `((type shape) (shape triangle) (radius 1.6))))+       (make-designator :location '((:horizontal-position :center) (:vertical-position :center)))) 
 +CENTER-LOCATION 
 +TUT> (defparameter goal-action 
 +       (make-designator :action `((:type :goal(:goal ,center-location)))) 
 +GOAL-ACTION 
 +TUT> (defparameter shape-action 
 +       (make-designator :action '((:type :shape) (:shape :triangle) (:radius 1.6))))
 SHAPE-ACTION SHAPE-ACTION
-TUT> (defparameter navigation-action (make-designator 'action `((type navigation) (goal ,(make-designator 'location `((vpos center) (hpos center))))))) 
-NAVIGATION-ACTION 
 </code> </code>
  
-With these designators defined, see what happens if you try to call them. For example,+With these designators defined, see what happens if you try to execute them. For example,
  
 <code lisp> <code lisp>
-TUT> (do-action-designator shape-action)+TUT> (perform-some-action goal-action)
 WARNING: WARNING:
    Before starting node, node-status equalled RUNNING instead of :shutdown.  Shutting the previous node invocation down now.    Before starting node, node-status equalled RUNNING instead of :shutdown.  Shutting the previous node invocation down now.
-[(ROSLISP EVENT-LOOP) INFO] 1415203378.618: Terminating ROS Node event loop +[(ROSLISP EVENT-LOOP) INFO] 1453760373.719: Terminating ROS Node event loop 
-[(ROSLISP TOP) INFO] 1415203379.040: Shutdown complete +[(ROSLISP TOP) INFO] 1453760374.142: Shutdown complete 
-[(ROSLISP TOP) INFO] 1415203379.042: Node name is /turtle1 +[(ROSLISP TOP) INFO] 1453760374.149: Node name is /turtle1 
-[(ROSLISP TOP) INFO] 1415203379.042: Namespace is / +[(ROSLISP TOP) INFO] 1453760374.149: Namespace is / 
-[(ROSLISP TOP) INFO] 1415203379.042: Params are NIL +[(ROSLISP TOP) INFO] 1453760374.150: Params are NIL 
-[(ROSLISP TOP) INFO] 1415203379.042: Remappings are: +[(ROSLISP TOP) INFO] 1453760374.150: Remappings are: 
-[(ROSLISP TOP) INFO] 1415203379.042: master URI is 127.0.0.1:11311 +[(ROSLISP TOP) INFO] 1453760374.150: master URI is 127.0.0.1:11311 
-[(ROSLISP TOP) INFO] 1415203380.047: Node startup complete +[(ROSLISP TOP) INFO] 1453760375.155: Node startup complete 
-[(TURTLE-PROCESS-MODULES) INFO] 1415203380.190: Turtle navigation invoked with action designator `#<ACTION-DESIGNATOR ((TYPE +[(TURTLE-PROCESS-MODULES) INFO] 1453760375.181: Turtle simple navigation invoked with action designator `#<ACTION-DESIGNATOR ((TYPE 
-                                                                        SHAPE+                                                                               GOAL
-                                                                       (SHAPE +                                                                              (GOAL 
-                                                                        TRIANGLE+                                                                               #<LOCATION-DESIGNATOR 
-                                                                       (RADIUS +                                                                                 ((HORIZONTAL-POSITION 
-                                                                        1.6)) {1005C58BE3}>'+                                                                                   CENTER
-[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1415203380.231Waiting for turtle shape action server... +                                                                                  (VERTICAL-POSITION 
-[(TURTLE-SHAPE-ACTION-CLIENTINFO] 1415203382.278: Turtle shape action client created+                                                                                   CENTER)) 
-[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1415203394.276: Nav action finished. +                                                                                 {100A70EDE3}>)) {100B05B333}>'
-[TURTLE_ACTIONLIB-MSG:SHAPERESULT +[(TURTLE-PROCESS-MODULES) INFO] 1453760375.184Going to point #<3D-VECTOR (4.381962776184082d0 4.012950897216797d0 0.0d0)>
-   INTERIOR_ANGLE: +T
-     1.0471975803375244d0 +
-   APOTHEM: +
-     0.800000011920929d0]+
 </code> </code>
  
-The turtle should also have moved and traced the requested triangle. We can also try and bring it somewhere near the center of its screen:+The turtle should have moved somewhere to the center of its screen
 + 
 +Let's see if it will draw the triangle shape:
  
 <code lisp> <code lisp>
-TUT> (do-action-designator navigation-action)+TUT> (perform-some-action shape-action)
 WARNING: WARNING:
    Before starting node, node-status equalled RUNNING instead of :shutdown.  Shutting the previous node invocation down now.    Before starting node, node-status equalled RUNNING instead of :shutdown.  Shutting the previous node invocation down now.
-[(ROSLISP EVENT-LOOP) INFO] 1415203565.890: Terminating ROS Node event loop +[(ROSLISP EVENT-LOOP) INFO] 1453760384.426: Terminating ROS Node event loop 
-[(ROSLISP TOP) INFO] 1415203566.382: Shutdown complete +[(ROSLISP TOP) INFO] 1453760384.903: Shutdown complete 
-[(ROSLISP TOP) INFO] 1415203566.385: Node name is /turtle1 +[(ROSLISP TOP) INFO] 1453760384.906: Node name is /turtle1 
-[(ROSLISP TOP) INFO] 1415203566.385: Namespace is / +[(ROSLISP TOP) INFO] 1453760384.907: Namespace is / 
-[(ROSLISP TOP) INFO] 1415203566.385: Params are NIL +[(ROSLISP TOP) INFO] 1453760384.907: Params are NIL 
-[(ROSLISP TOP) INFO] 1415203566.385: Remappings are: +[(ROSLISP TOP) INFO] 1453760384.907: Remappings are: 
-[(ROSLISP TOP) INFO] 1415203566.386: master URI is 127.0.0.1:11311 +[(ROSLISP TOP) INFO] 1453760384.907: master URI is 127.0.0.1:11311 
-[(ROSLISP TOP) INFO] 1415203567.392: Node startup complete +[(ROSLISP TOP) INFO] 1453760385.913: Node startup complete 
-[(TURTLE-PROCESS-MODULES) INFO] 1415203567.525: Turtle navigation invoked with action designator `#<ACTION-DESIGNATOR ((TYPE +[(TURTLE-PROCESS-MODULES) INFO] 1453760385.930: Turtle shape navigation invoked with action designator `#<ACTION-DESIGNATOR ((TYPE 
-                                                                        NAVIGATION+                                                                              SHAPE
-                                                                       (GOAL +                                                                             (SHAPE 
-                                                                        #<LOCATION-DESIGNATOR +                                                                              TRIANGLE
-                                                                          ((VPOS +                                                                             (RADIUS 
-                                                                            CENTER+                                                                              1.6)) {100BA6F7A3}>'
-                                                                           (HPOS +[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1453760385.968Waiting for turtle shape action server... 
-                                                                            CENTER)) +[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1453760387.968: Turtle shape action client created. 
-                                                                          {10074B8163}>)) {10074B8533}>'+[(TURTLE-SHAPE-ACTION-CLIENT) INFO] 1453760399.919: Nav action finished. 
- +[TURTLE_ACTIONLIB-MSG:SHAPERESULT 
-#<CL-TRANSFORMS:3D-VECTOR (5.806711673736572d0 5.260904312133789d0 0.0d0)>  +   INTERIOR_ANGLE: 
-T+     1.0471975803375244d0 
 +   APOTHEM: 
 +     0.800000011920929d0]
 </code> </code>
- 
-You should also see the turtle move to a central location. 
-