1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    2%
    3% FILE: ER1/er1test.pl
    4%
    5%       BAT axiomatization for controlling the Evolution ER1 robot
    6%
    7%  AUTHOR : Sebastian Sardina (2003)
    8%  EMAIL  : ssardina@cs.toronto.edu
    9%  WWW    : www.cs.toronto.edu/~ssardina www.cs.toronto.edu/cogrobo
   10%  TYPE   : system independent code
   11%  TESTED : SWI Prolog 5.0.10 http://www.swi-prolog.org
   12%           ECLIPSE 5.4 http://www.icparc.ic.ac.uk/eclipse/
   13
   14%           For more information on Golog and some of its variants, see:
   15%               http://www.cs.toronto.edu/~cogrobo/
   16%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   17%
   18%                             November, 2003
   19%
   20% This software was developed by the Cognitive Robotics Group under the
   21% direction of Hector Levesque and Ray Reiter.
   22% 
   23%        Do not distribute without permission.
   24%        Include this notice in any copy made.
   25% 
   26% 
   27%         Copyright (c) 2000 by The University of Toronto,
   28%                        Toronto, Ontario, Canada.
   29% 
   30%                          All Rights Reserved
   31% 
   32% Permission to use, copy, and modify, this software and its
   33% documentation for non-commercial research purpose is hereby granted
   34% without fee, provided that the above copyright notice appears in all
   35% copies and that both the copyright notice and this permission notice
   36% appear in supporting documentation, and that the name of The University
   37% of Toronto not be used in advertising or publicity pertaining to
   38% distribution of the software without specific, written prior
   39% permission.  The University of Toronto makes no representations about
   40% the suitability of this software for any purpose.  It is provided "as
   41% is" without express or implied warranty.
   42% 
   43% THE UNIVERSITY OF TORONTO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
   44% SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
   45% FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF TORONTO BE LIABLE FOR ANY
   46% SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
   47% RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
   48% CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
   49% CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   50%
   51%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   52% 
   53%  A basic action theory (BAT) is described with:
   54%
   55% -- fun_fluent(fluent)     : for each functional fluent (non-ground)
   56% -- rel_fluent(fluent)     : for each relational fluent (non-ground)
   57%
   58%           e.g., rel_fluent(painted(C)).
   59%           e.g., fun_fluent(color(C)).
   60%
   61% -- prim_action(action)    : for each primitive action (ground)
   62% -- exog_action(action)    : for each exogenous action (ground)
   63%
   64%           e.g., prim_action(clean(C)) :- domain(C,country).
   65%           e.g., exog_action(painte(C,B)):- domain(C,country), domain(B,color).
   66%
   67% -- senses(action,fluent)  : for each sensing action
   68%
   69%           e.g, poss(check_painted(C),  painted(C)).
   70%
   71% -- poss(action,cond)      : when cond, action is executable
   72%
   73%           e.g, poss(clean(C),   and(painted(C),holding(cleanear))).
   74%
   75% -- initially(fluent,value): fluent has value in S0 (ground)
   76%
   77%          e.g., initially(painted(C), false):- domain(C,country), C\=3.
   78%                initially(painted(3), true).
   79%                initially(color(3), blue).
   80%
   81% -- causes_val(action,fluent,value,cond)
   82%          when cond holds, doing act causes functional fluent to have value
   83%
   84%            e.g., causes_val(paint(C2,V), color(C), V, C = C2).
   85%               or causes_val(paint(C,V), color(C), V, true).
   86%
   87% -- causes_true(action,fluent,cond)
   88%          when cond holds, doing act causes relational fluent to hold
   89% -- causes_false(action,fluent,cond)
   90%          when cond holds, doing act causes relational fluent to not hold
   91%
   92%            e.g., causes_true(paint(C2,_), painted(C), C = C2).
   93%               or causes_true(paint(C,_), painted(C), true).
   94%            e.g., causes_false(clean(C2),  painted(C), C = C2).
   95%               or causes_false(clean(C),  painted(C), true).
   96%
   97% -- sort(name,domain_of_sort).      : all sorts used in the domain
   98%
   99%        e.g., varsort(c, colors).
  100%              varsort(temp, temperature).
  101%              color([blue, green, yellow, red]).       
  102%              temperature([-10,0,10,20,30,40]).
  103%
  104%
  105% A high-level program-controller is described with:
  106%
  107% -- proc(name,P): for each procedure P 
  108% -- simulator(N,P): P is the N exogenous action simulator
  109%
  110% The interface for Lego is described with:
  111%
  112% -- actionNum(action, num)  
  113%         action has RCX code num
  114% -- simulateSensing(action)
  115%         sensing result for action should be asked to the user
  116% -- translateSensing(action, sensorValue, sensorResult) 
  117%         translate the sensorValue of action to sensorResult
  118% -- translateExogAction(codeAction, action) 
  119%         translateSensing action name into codeAction and vice-versa
  120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  121
  122
  123
  124   
  125% There is nothing to do caching on (required becase cache/1 is static)
  126cache(_):-fail.
  127
  128
  129/* DOMAINS/SORTS */
  130%fl([1,2,3,4,5,6,7,8,9,10]).       % possible floors 
  131fl([1,2,3,4,5,6]).       % possible floors 
  132dir([up,down]).                    % possible directions 
  133temperature([15,20,25,30,35]).     % possible temperatures
  134
  135
  136  /*  FLUENTS and CAUSAL LAWS */
  137fun_fluent(state).                 % Moving, stopped, or suspended (lost)
  138causes_val(moveFwd(_),      state, moving, true).
  139causes_val(turnLeft,        state, moving, true).
  140causes_val(turnRight,       state, moving, true).
  141causes_val(arrive,          state, stopped, true).
  142causes_val(getStuck,        state, stopped, true).
  143causes_val(freeze,          state, stopped, true).
  144causes_val(stop_abnormally, state, suspended, true).
  145causes_val(suspend,         state, suspended, true).
  146causes_val(resume,          state, stopped, true).
  147
  148rel_fluent(sawObject(_)). 	   % An object has just been seen
  149causes_true(spotObject(O),    sawObject(O), true).
  150causes_false(lostObject(O),   sawObject(O), true).
  151causes_false(forgetObject(O), sawObject(O), true).
  152
  153rel_fluent(objectLost(_)). 	   % We lost the object
  154causes_true(lostObject(O),    objectLost(O), true).
  155causes_false(forgetObject(O), objectLost(O), true).
  156causes_false(spotObject(O),   objectLost(O), true).
  157
  158rel_fluent(silent).           % Should we talk or be silent?
  159causes_false(speakOn,  silent, true).
  160causes_true(speakOff,  slient, true).
  161
  162
  163rel_fluent(talking).          % Is the agent saying anything?
  164causes_true(say(_),        talking, true).
  165causes_true(say(_,_),      talking, true).
  166causes_false(finishSaying, talking, true).
  167
  168rel_fluent(sensingObjects).   % Is the agent watching for objects?
  169causes_true(senseOn(objects),   sensingObjects, true).
  170causes_false(senseOff(objects), sensingObjects, true).
  171
  172
  173  /*  ACTIONS and PRECONDITIONS*/
  174prim_action(moveFwd(_)). 
  175prim_action(turnLeft).
  176prim_action(turnRight).
  177prim_action(freeze).
  178prim_action(forgetObject(_)).
  179prim_action(say(_)).
  180prim_action(say(_,_)).
  181
  182prim_action(setObjectConfidence(_)).
  183prim_action(setLinearVelocity(_)).
  184prim_action(setPower(_,_)).
  185prim_action(setIR_oa(_,_)).
  186prim_action(senseOn(_)).
  187prim_action(senseOff(_)).
  188
  189exog_action(finishSaying).          
  190exog_action(getStuck).
  191exog_action(arrive). 
  192exog_action(spotObject(_)).
  193exog_action(lostObject(_)).
  194exog_action(stop_abnormally).    % Stop because confused
  195exog_action(speakOn).
  196exog_action(speakOff).
  197exog_action(suspend).
  198exog_action(resume).
  199
  200poss(moveFwd(_),       neg(state=moving)).
  201poss(turnLeft,         neg(state=moving)).
  202poss(turnRight,        neg(state=moving)).
  203poss(freeze,           true).
  204poss(forgetObject(O),  or(sawObject(O), objectLost(O))).
  205poss(say(_),           and(neg(talking),neg(silent))).
  206poss(say(_,_),         and(neg(talking),neg(silent))).
  207
  208poss(setObjectConfidence(_), true).
  209poss(setLinearVelocity(_), true).
  210poss(setPower(_,_), true).
  211poss(setIR_oa(_,_), true).
  212
  213poss(senseOn(_),  true).
  214poss(senseOff(_), true).
  215
  216
  217/* ABBREVIATIONS */
  218
  219
  220 /* INITIAL STATE: elevator is at floor 3, lights 2 and 5 are on */
  221initially(sawObject(_), false).
  222initially(objectLost(_), false).
  223initially(state, stopped).
  224initially(silent, false).
  225initially(sensingObjects, false).
  226initially(talking, false).
  227
  228
  229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  230% Definitions of complex actions
  231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  232proc(goSquare(Rot, M), 
  233     [moveFwd(M),
  234      if(Rot=right,turnRight,turnLeft),
  235      moveFwd(M),
  236      if(Rot=right,turnRight,turnLeft),
  237      moveFwd(M),
  238      if(Rot=right,turnRight,turnLeft),
  239      moveFwd(M),
  240      if(Rot=right,turnRight,turnLeft)
  241     ]).
  242
  243proc(recognizeObject(O), talk(O)).
  244
  245proc(talk(M), if(silent, ?(true), 
  246                 [?(neg(talking)), say(M,mike)]
  247                )
  248    ).
  249
  250% Get a random integer between L and U
  251proc(getNumber(L,U,N),
  252     ?(random(L,U,N))
  253    ).
  254
  255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  256%  Main Routine
  257%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  258
  259% THIS IS THE MAIN PROCEDURE FOR INDIGOLOG
  260proc(main,  mainControl(N)) :- controller(N), !.
  261proc(main,  mainControl(3)). % default one
  262
  263
  264% Robot travels a square of 2 meters
  265proc(mainControl(0), 
  266     [talk('ER1 square controller initiated!'),
  267      setLinearVelocity(20),
  268      prioritized_interrupts(
  269         [interrupt(or(state = moving, talking), wait),
  270          interrupt(true, [talk('Starting a new round'),
  271                           rndet(goSquare(right, 200), 
  272                                 [turnRight, goSquare(left,200), turnLeft]),
  273                           talk('Another round finished')
  274                          ])
  275         ]
  276                            )
  277     ]
  278    ).
  279
  280
  281% Robot travels a square of 2 meters while recognizing objects
  282proc(mainControl(2), 
  283     [talk('ER1 controller initiated successfully!'),
  284      setObjectConfidence(20),
  285      senseOn(objects),
  286      prioritized_interrupts(
  287         [interrupt(talking, wait),
  288          interrupt(o, sawObject(o), 
  289                    [talk(['Hey!, I have just seen the ',o]),
  290                     forgetObject(o)
  291                    ]),
  292          interrupt(state=moving, wait),
  293          interrupt(true, [talk('Starting a new round'),
  294                           rndet(goSquare(right, 200), 
  295                                 [turnRight, goSquare(left,200), turnLeft]),
  296                           talk('Another round finished')
  297                          ])
  298         ]
  299                            )
  300     ]
  301    ).
  302
  303% Has suspension, OA, while recognizing and forgetting objects
  304proc(mainControl(3), 
  305     [talk('ER1 IndiGolog controller initiated successfully!'),
  306      setObjectConfidence(20),
  307      senseOn(objects),
  308      setPower(moving, 40),
  309      setIR_oa(30, 150),
  310      prioritized_interrupts(
  311         [interrupt(state=suspended,
  312                    [freeze,
  313                     talk(['Execution suspended']),
  314                     ?(neg(state=suspended))
  315                    ]),
  316          interrupt(or(state=suspended,talking), wait),
  317          interrupt(o, sawObject(o), 
  318                    [talk(['Hey!, I have just seen the ', o]),
  319                     forgetObject(o)
  320                    ]),
  321          interrupt(o, objectLost(o), 
  322                    [talk(['I have just lost the object ', o]),
  323                     forgetObject(o)
  324                    ]),
  325          interrupt(state=moving, wait),
  326          interrupt(true, [talk('Starting a new round'),
  327                           pi(n,[getNumber(10,30,n), setLinearVelocity(n)]),
  328                           rndet(goSquare(right, 200), 
  329                                 [turnRight, goSquare(left,200), turnLeft]),
  330                           talk('Another round finished')
  331                          ]),
  332          interrupt(true, wait)
  333         ])  % END OF INTERRUPTS
  334     ]
  335    ).
  336
  337
  338% Robot travels a square of 2 meters (NOT FINISHED: moveTo)
  339proc(mainControl(5), 
  340     [talk('ER1 square controller initiated!'),
  341      prioritized_interrupts(
  342         [interrupt(or(state = moving, talking), wait),
  343          interrupt(true, [talk('Starting a new round'),
  344                           pi(place,
  345                           pi(x,
  346                           pi(y, [getNextPlace(x,y,place),
  347                                  moveTo(x,y),
  348                                  talk(['I am traveling to ',place,' now'])
  349                                 ]
  350                             )))
  351                          ])
  352         ]
  353                            )
  354     ]
  355    ).
  356
  357
  358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  359% EOF: ER1/er1test.pl
  360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%