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 / 26695
26694  |  26696
Subject: 
Re: Drive types
Newsgroups: 
lugnet.robotics
Date: 
Tue, 9 Jan 2007 13:14:56 GMT
Viewed: 
3070 times
  
In lugnet.robotics, Philippe Hurbain wrote:
In lugnet.robotics, John Hansen wrote:
Precedes(task1, task2, ..., taskn);
Follows(task1, task2, ..., taskn);


Could you comment a little the use of Precedes/Follows for task switching? I
have trouble figuring when to use one or the other (or both?)


Here is a simple multi-threaded NQC program:

task music()
{
  while (true)
  {
    PlayTone(262,40);  Wait(50);
    PlayTone(294,40);  Wait(50);
    PlayTone(330,40);  Wait(50);
    PlayTone(294,40);  Wait(50);
  }
}

task main()
{
  start music;
  while(true)
  {
    OnFwd(OUT_A+OUT_B); Wait(300);
    OnRev(OUT_A+OUT_B); Wait(300);
  }
}

This program runs main and music concurrently.  NXT has a somewhat different
metaphor for multi-threaded execution which is modeled after dataflow
programming designs.  Any chunk of code (called a clump in NXT-ese) can be
followed by one or more other chunks of code as in a flow chart:

OperationA
    /\
   /  \
OpB  OpC

You can say that OperationA "precedes" a subsequent operation if you want to
chain together the execution of these separate operations.  You can also chain
together operations by saying that the subsequent operation (OpB) "follows" the
previous operation.  In the diagram above OpA precedes both OpB and OpC.  OpB
follows OpA.  OpC follows OpA.  OpC and OpB could also be said to follow other
operations if they depend on data inputs that other operations provide.  So by
using precedes and/or follows you can define a network of operation input (or
execution) dependencies and as operations have all their input dependencies met
they can commence operation themselves.

Here is one equivalent version of the above NQC program (in NXC):

#include "NXCDefs.h"

task music()
{
  while (true)
  {
    PlayTone(262,40);  Wait(500);
    PlayTone(294,40);  Wait(500);
    PlayTone(330,40);  Wait(500);
    PlayTone(294,40);  Wait(500);
  }
}

task movement()
{
  while(true)
  {
    OnFwd(OUT_AB, 75); Wait(3000);
    OnRev(OUT_AB, 75); Wait(3000);
  }
}

task main()
{
  Precedes(music, movement);
}

Here we see that main is like OperationA in the above diagram.  It is said to
precede two operations.  Those operations have no other execution dependencies
so as soon as task main completes the NXT virtual machine will schedule all of
the threads which follow it for simultaneous execution.  You can either list all
the tasks in one Precedes statement or you can have multiple Precedes statements
with each one listing a partial set of tasks which should "follow" the current
task.

Here's another way to accomplish the same thing:

#include "NXCDefs.h"

task main()
{
  // does nothing
}

task music()
{
  Follows(main);
  while (true)
  {
    PlayTone(262,40);  Wait(500);
    PlayTone(294,40);  Wait(500);
    PlayTone(330,40);  Wait(500);
    PlayTone(294,40);  Wait(500);
  }
}

task movement()
{
  Follows(main);
  while(true)
  {
    OnFwd(OUT_AB, 75); Wait(3000);
    OnRev(OUT_AB, 75); Wait(3000);
  }
}

Since the music operation and the movement operation are said to follow main
then once main exits the NXT virtual machine will execute both operations
concurrently.

Using Precedes and Follows you can define as complex (or simple) a net of thread
inter-dependencies as you may need.

John Hansen



Message has 3 Replies:
  Re: Drive types
 
(...) Whoops. The duration values in each of the PlayTone calls should be 400 rather than 40. John Hansen (18 years ago, 9-Jan-07, to lugnet.robotics)
  Re: Drive types
 
(...) Thanks John - quite clear now. Though I still don't understand why there are 2 different opcodes that can be used for more or less the same thing??? Philo (18 years ago, 9-Jan-07, to lugnet.robotics)
  Re: Drive types
 
(...) Besides Precedes and Follows, are there other ways to start additional threads? Specifically, I'd like to start additional threads and then continue the execution of the current thread. I might be misinterpreting this, but it seems that (...) (18 years ago, 13-Jan-07, to lugnet.robotics)

Message is in Reply To:
  Re: Drive types
 
(...) Hello John, Could you comment a little the use of Precedes/Follows for task switching? I have trouble figuring when to use one or the other (or both?) Thanks in advance, Philo (18 years ago, 9-Jan-07, to lugnet.robotics)

14 Messages in This Thread:





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

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

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