Algorithm for counting number of lines travelled over

General Robotics Forum - All aspects of robots and their applications. 

Page 1 of 2       1 2 > last >> Bookmark this page:  YahooMyWeb Yahoo!  Google Google  Windows Live Favorites Windows Live  del.icio.us del.icio.us  digg digg  Add to Netscape Netscape
Subject Author Date
Algorithm for counting number of lines travelled over tastytoad 12-22-2005
If you were  Registered and logged in, you could reply and use other advanced thread options
Posted by on December 22, 2005, 8:44 am
I'm having a problem that I hope someone can see the logic I have
possibly overlooked.

The robot is to pass through, say, 5 white lines on a black surface
and then come to a rest. I am trying to work out the general logic to
the algorithm rather than the proper syntax of the programming
language.

I have written the program like so:

initialize variable lines_to_count = 5
initialize variable on_white_line = false

turn motors on and stay on until otherwise turned off

while (lines_to_count > 0)

if (sensor detected white surface) then
on_white_line = true

if (on_white_line = true) and (sensor detected black surface) then
lines_to_count = lines_to_count -1
on_white_line = false

loop while

turn motors off


All that does, however, if make the motors turn on momentarily and then
immediately stop. It's as if the while loop is never entered or is
executed so quickly followed by the turn motor off that it seems not to
have run at all.

Can anyone see a problem with the logic? I'm stumped....can't explain
why the robot just stops immediately upon being activated.

I would think the above program would make the robot move forward, stay
in the WHILE loop as it keeps moving forward keepiing track of the
number of lines it passeed by and, when the fifth line is passed, the
robot stops.

Notice the two IF statements in the WHILE loop are mutually exclusive
because one is looking for a white surface, the other a black surface.
Line counting is determined by finding a white surface (the first IF)
followed some time later by detecting a transition to a black surface
(the second IF). The second IF toggles on_white_line variable back to
false so that it can be resued for the next white line the robot
encounters. You can assume the sensors are working perfectly.

Thanks.


Posted by on December 22, 2005, 11:02 am
Hmm...I have rechecked the code...seems the problem was a bit of syntax
afterall. Sorry for the bother.


Posted by Padu on December 22, 2005, 1:07 pm

> I'm having a problem that I hope someone can see the logic I have
> possibly overlooked.
> The robot is to pass through, say, 5 white lines on a black surface
> and then come to a rest. I am trying to work out the general logic to
> the algorithm rather than the proper syntax of the programming
> language.
> I have written the program like so:
> initialize variable lines_to_count = 5
> initialize variable on_white_line = false
> turn motors on and stay on until otherwise turned off
> while (lines_to_count > 0)
> if (sensor detected white surface) then
> on_white_line = true
> if (on_white_line = true) and (sensor detected black surface) then
> lines_to_count = lines_to_count -1
> on_white_line = false
> loop while
> turn motors off
> All that does, however, if make the motors turn on momentarily and then
> immediately stop. It's as if the while loop is never entered or is
> executed so quickly followed by the turn motor off that it seems not to
> have run at all.
> Can anyone see a problem with the logic? I'm stumped....can't explain
> why the robot just stops immediately upon being activated.
> I would think the above program would make the robot move forward, stay
> in the WHILE loop as it keeps moving forward keepiing track of the
> number of lines it passeed by and, when the fifth line is passed, the
> robot stops.
> Notice the two IF statements in the WHILE loop are mutually exclusive
> because one is looking for a white surface, the other a black surface.
> Line counting is determined by finding a white surface (the first IF)
> followed some time later by detecting a transition to a black surface
> (the second IF). The second IF toggles on_white_line variable back to
> false so that it can be resued for the next white line the robot
> encounters. You can assume the sensors are working perfectly.
> Thanks.


You have a classic and simple FSM here, and the logic seems ok. So the
problem may likely be in the sintax of your program or getting the signal
out of the sensor. Remember that real world sensors are noisy, and if you
have not calibrated it correctly or taking the proper cautions to interpret
it, you may have problems.
From the symptoms you describe, seems like the program is actually working,
but the sensors are giving spikes that are crossing your "sensor detected
white surface" threshold.

Have fun debugging ;-)

Cheers

Padu



Posted by Randy M. Dumse on December 23, 2005, 1:30 pm
>> Can anyone see a problem with the logic? I'm stumped....
>...
> You have a classic and simple FSM here, and the logic seems ok.

Padu, my first reaction was also that the algorythm posted was a FSM.
Since is was so "obviously state based" it caught my attention. The
psuedo code looked very much like what I've written about IsoStructure
in IsoMax(TM) FSM language documentation, as alternative ways with
regular languages to write state machines. So I decided to diagram it,
and code it in IsoMax(TM) as an exercise.

It was less of a maching than I'd thought, is a very subtle sort of
state machine, if that. I would have to say, it really doesn't qualify
as a whole machine, but only a single state with an exit transition.

While the state information is held in on_white_line and lines_to_count,
there is only a delay inside the loop. That delay does nothing but wait
(states are waits) until a count condition is met. There are no
transitions from state to state associated with the loop, despite the
first look of it.

The loop is really an input white-to-dark transition detector, with a
counter of these transitions. All outputs occur outside the loop itself.
The action of turning the motor on appears to be interactive and prior
to the loop being entered. The action of turning the motor off on
exiting the wait where the program ends.

Notice the code could be written the following way, and still function
nearly identically (except maybe run faster):

while (lines_to_count > 0)

while (sensor detected white surface) and (sensor detected black
surface)
lines_to_count = lines_to_count -1
loop while

loop while

turn motors off

This removes the use of the on_white_line variable, which obfuscates the
function of what is going on. The code is simply looking for a time when
the first sensor reading shows white, and a following sensor reading
shows black. This is the condition under which the count is decremented.
While the sensor reading continue to show black, there will be no count.
When the sensors start showing white, a second reading again showing
white, there will be no count. Only when at least one count has been
found to be white, and the following has been found to be black, does
the count increment.

Now as noted by others, if there is any noise as the sensor moves from
white to black (as one should well expect in the real world) the motors
will turn off almost immediate at the first edge (white to black _or_
black to white) because the noise will show multiple edges of both kind.

Generally, hysterisis is used to debounce an input sensor for exactly
this reason. Hysterisis is feedback, and feedback is another way of
sensing past state, and past state implies Finite State Machine. So this
algorythm could be made much more reliable and robust by adding a delay
after the detection of each edge. A FSM (no matter what language encoded
in) is what's needed to make this routine appropriate to its intended
task.

--
Randy M. Dumse
www.newmicros.com
Caution: Objects in mirror are more confused than they appear.



Posted by Randy M. Dumse on December 24, 2005, 3:20 pm
> while (lines_to_count > 0)
> while (sensor detected white surface) and (sensor detected black
> surface)
> lines_to_count = lines_to_count -1
> loop while
> loop while


Correction!

The inner while loop is the white to black event detector, so the loop
should continue until white ot black is detected.

while (lines_to_count > 0)

while (sensor detected white surface) and (sensor detected black
surface) not
loop while

lines_to_count = lines_to_count -1

loop while


Note the result is not'ed in logic. The outer loop is the count loop,
and should continue until the count is met.

Also there is a slight flaw in the logic I chose above, because if two
consecutive reads are white, and then two consecutive reads are black,
the loop will miss the edge.

The use of the variable on_white_line in the original code prevented
this edge being missed. Being part of the if conditional, and being
ballistic, in the same sense as a ballastic thermometer, once the sensor
was even once found to be white and not cleared until it was later found
black. The sensor would always be checked by the second if test to see
if it was ever black. although, not necessarily on the second subsequent
read. So there is a very subtle use of state information encapsulated in
the use of that variable.

I wish there were some formalism to check and extract the nature of
state information from different programming models. As far as I know,
no such generalized tool exists. I would very much encourage research on
the subject, and hope to discover such if no one else should precede me
in doing so.

--
Randy M. Dumse
www.newmicros.com
Caution: Objects in mirror are more confused than they appear.



Page 1 of 2       1 2 > last >>

The site map in XML format XML site map
other useful resources:
Official Robosapien Website
Lego Mindstorms Website

Contact Us | Privacy Policy