To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 1169
1168  |  1170
Subject: 
What I'm up to - RCX Piglet 0.6
Newsgroups: 
lugnet.robotics
Date: 
Sun, 6 Dec 1998 18:21:26 GMT
Original-From: 
stephen p spackman <stephen@acm.NOMORESPAMorg>
Viewed: 
1310 times
  
I thought I'd post an example programme in RCX Piglet. I'm not yet
completely comfortable with the language design, so there's still no
live implementation on the RCX (just various simulations in Java, which
are easier to debug).

I took the programme that Dave Baum posted a while back (included at the
bottom), and translated. I think the reason the Piglet version came out
so long is that I've used a number of auxilliary functions that don't
pay off spacewise in a programme so short.

Of course, the consequence of this is that you don't see Piglet doing
anything nqc can't - in particular there's no substantive recursion or
data structure work - though perhaps you can get the sense that we're
operating at a rather higher level than C.

(There's actually a bug in my translation, which is that the whenever
clause in run is quite likely to trigger again during a hunt, causing
all kinds of confusion. I've left it there beacuse I'm still thinking
about whether that's best addressed by user code, by priorities, by
behaviour suspension, or by pushing sensor hysteresis down into the
interrupt side. For now you can code around it by using the rec [] hack
in hunt, but that's not pretty; but whatever the final solution is it
will look more like that presented here.)

stephen
=================================================================================
Tracker.pig ....
Programme and language copyright 1998 stephen p spackman
---------------------------------------------------------------------------------
(Wiring)
eye. light 1;
left. motor 0;
right. motor 2;

(Timing)
normalSpeed. 6;
turnSpeed. 2;
initialTime. 200;
guardTime. 2500; (This seems too big...)

(Perception primitives)
inside: [[< 55] [eye]];
outside: [[> 55] [eye]];
marker: [[< 48] [eye]];

(Motor primitives)
move: [! left; ! right];
stop: [move 0 0];
go: [s.; move s s];
rotate: [s.; move s ~ s];
saveMotion: [make Vec 2 ? left ? right];
restoreMotion: [v.; move do v];

(Basic behaviours)

( We store the turn speed while hunting in a state variable as an
optimisation:
  this way we can start by continuing the previous rotation, and try the
other
  direction only if that fails. )
hs. ?! turnSpeed;

hunt: behaviour (Find the line again after we drift off it)
  [ h.; (Behaviour control object.)
    s. saveMotion;
    when inside [restoreMotion s; ! h true (i.e. stop the behaviour)];
    rec (Allow subfunction 'seek' to refer to itself)
      [ seek:; dt.;
rotate hs; ! hs ~ hs;
in h after dt [seek × 2 dt];
      ] initialTime;
  ];

run: behaviour (Drive along the line)
  [ r.;
    when marker [stop; ! r true];
    whenever outside [in r hunt];
    go normalSpeed;
  ];

turnBack: behaviour (Spin in place, finding the line going back the way
we came)
  [ t.;
    rotate turnSpeed;
    when outside [in t after guardTime [in t when inside [stop]]];
  ];

(Main programme)

(Here '/' discards unwanted results; the '[]' are null guards:)
/ when [] [run] [tone 5; / when [] [turnBack] [/ run]]

=================================================================================
Dave's nqc original...
---------------------------------------------------------------------------------
/* tracker.nqc
* written by Dave Baum for nqcc 0.5 b1
*
* This is a more complex program for a line-following robot.
* As usual, outputs A and C drive right and left tank treads,
* while input 1 is a light sensor at the front of the robot.
*
* The follow() subroutine follows a thin line until in
* encounters a very dark spot, at which point the robot
* stops.  When following a line, the robot runs forward
* until the light sensor reading exceeds a given value,
* at which point it starts turning right (or left) a bit
* looking for the rest of the line.  It continues to
* oscillate left and right until it finds the line, then
* continues forward.  The program remembers the direction
* previously used to find the line, so it can start turning
* the same way next time it loses the line.
*
* The main program follows a line until it reaches the end (a
* dark spot), then beeps, turns around, and follows the line
* back to the start.  The "test" surface should be white with
* a thin black line (1/8 inch thick) which can turn and curve
* as you desire.  Solid dark "endpoint" should be at both ends
* of the line - 1" wide black squares work well.
*
*/

#define EYE IN_1
#define LEFT   OUT_C
#define RIGHT  OUT_A

#define NORMAL_SPEED 6
#define TURN_SPEED 2
#define DELAY  20

#define CUTOFF 55
#define STOP   48


int direction, time, eye;

#define INITIAL_TIME 2

task main
{
   Sensor(EYE, IN_LIGHT);


   direction = 1;
   time = INITIAL_TIME;

   follow();
   PlaySound(5);

   turn_around();
   follow();
}


sub turn_around
{
   Fwd(LEFT, TURN_SPEED);
   Rev(RIGHT, TURN_SPEED);

   wait(EYE > CUTOFF);
   Sleep(25);
   wait(EYE < CUTOFF);

   Off(LEFT+RIGHT);
}


sub follow
{
   Fwd(LEFT+RIGHT, NORMAL_SPEED);
   while(true)
   {
      eye = EYE;
      if (eye < STOP) break;
      if (eye <= CUTOFF) continue;

      {
         ClearTimer(0);
         if (direction == 1)
         {
            Fwd(RIGHT, TURN_SPEED);
            Rev(LEFT, TURN_SPEED);
         }
         else
         {
            Fwd(LEFT, TURN_SPEED);
            Rev(RIGHT, TURN_SPEED);
         }

         while(true)
         {
            if (EYE < CUTOFF)
            {
               time = INITIAL_TIME;
               break;
            }

            if (Timer(0) > time)
            {
               direction *= -1;
               time *= 2;
               break;
            }
         }

         Fwd(RIGHT+LEFT, NORMAL_SPEED);
      }
   }

   Off(RIGHT+LEFT);
}



1 Message in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR