Spybots Smart Parts  
  The Unofficial Resource Centre for Lego Spybotic  
   
img  
   
   

TUTORIALS

TUTORIALS

intermediate

Tasks, Sub-routines etc - mindscript

Tutorial Details:
Difficulty Level: Intermediate
Topics Covered: Creating and using Tasks, Sub-routines and macros in Mindscript.
Assumed Knowledge: The Basics, My First Program, Constants, Variables & Counters,Conditional(if), Conditional (Select), Forever Yours
Written By: BILL LANE

Print Version

Up until now we've written all our code into the main task. But if we wanted to do anything a little bit involved this would very quickly become very messy. It would also be very ineffficient. Because there would be certain command combinations that we would need more than once. Rather than rewriting these blocks of commands over and over we can place them into their own task and call that task whenever we need to use that sequence of commands. Let's look at a simple example:

main{
forever{start mySound wait 200}
}

task mySound{
sound 1
}

The main task runs a forever loop that runs the mySound task by using the start keyword. It then waits 2 seconds before repeating the process. We use the task keyword to declare the task called mySound. It's structure is identical to the main task. Task mySound plays a short sound and then ends. The result is a beep sound that repeats once every 2 seconds.

One thing to note here is that both tasks are running in parallel. In this example the forever loop could have been placed in mySound. So that the main task starts mySound and then mySound keeps going until it's told to stop. In this way you could have multiple tasks all running continuously doing their own things. As such it is good that Mindscript provides a stop keyword to complement the start. To stop mySound we need only write stop mySound. This could be done from mySound, from the main task or from any other task. Another option is to use stop tasks which ends all running tasks (including the main task).

Spybotics allows us to have up to 8 tasks (including watchers which are counted as a task). The problem with tasks is you don't have many to play with and they aren't very flexible. They are good for managing distinct jobs within a mission. But a more flexible structure is a sub-routine. You can declare up to 32 sub-routines. Below is our first example using a sub instead of a task:

sub mySound{
sound 1
}

main{
forever{mySound wait 200}
}

N.B. Global.h declares 140 sub-routines that are pre-programmed into the sypbots ROM. We have already made use of some of these to control lights, sound and movement. But if you want to make full use of their potential you'll need to look through the Spybotics ROM documentation that is included in the Mindstorms SDK download.

The structure is almost identical to the task, with the word sub used instead of the task keyword. Notice you can start the sub-routine by simply writing it's name. No need to use start. Another thing to note is that sub-routines are declared before the main task and tasks are declared after the main task. I don't know why this is. But I do know you'll get an error if you try to do it the other way around.

One advantage of sub-routines (in Mindscript) is that you can send them values (called parameters) that can be used to change what they do. For example:

sub mySound(thisSound, thisTime){
sound thisSound
wait thisTime
}

main{
select myVariable{
when 1{mySound(1,50)}
when 2{mySound(2,75)}
when 3{mySound(3,100)}
}

Here we've declared two parameters for mySound; thisSound and thisTime, by using brackets after the name but before the curly braces. In the main task we have a conditional select and dependant on the value of myVariable we send mySound different values(inside brackets). The first value represents the first parameter; thisSound. The second value represents the second parameter; thisTime. Which means we use one sub-routine but it can play many different sounds.

A third option is to use a macro. On the surface a macro is nearly identical to a sub-routine. To create them you just write macro instead of sub. You can choose to use parameters or to not use parameters. Macros are also declared before the main task. You just write their name to use them; you don't need to write start or any other keyword to call them.

But there is one very important difference between a macro and a sub-routine. If you use a sub-routine only one copy of that sub-routine is kept in memory and different tasks and sub-routines may call that sub-routine when required. But a macro acts as a kind of shorthand for a block of code. When the program is compiled wherever the macro's name appears the code is substituted for the name. So that multiple copies of that code block will appear in the downloaded program.

Hopefully, that's enough to get you started with tasks and sub-routines. Have a look at "Home Brew Energy Crisis" in the Advanced tutorials to see an example of how this can be applied.

 

This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution.

 

 
 
DISCLAIMER: All content is provided as is, with no warranty stated or implied regarding the quality or accuracy of any content on or off this site. All trademarks, service marks, and copyrights are property of their respective owners. This site is not sponsored, authorized or sanctioned by the LEGO Group nor representative of their opinions in any way.PRIVACY POLICY