Spybots Smart Parts  
  The Unofficial Resource Centre for Lego Spybotic  
   
img  
   
   

TUTORIALS

TUTORIALS

Intermediate

try, try & retry

Tutorial Details:
Difficulty Level: Intermediate
Topics Covered: Use of the try / retry commands in Mindscript.
Assumed Knowledge: The Basics, My First Program, Tasks, Sub-Routines etc.
Written By: BILL LANE

BACK

As discussed in Tasks, Sub-Routines etc Spybots are capable of running more than one sub-routine/ task at a time. This can lead to problems when more than one task wants to use the same resource. For example, two tasks try to run the same motor in opposite directions at the same time. Fortunately, Mindscript provides a structure to help us deal with this situation. This is the try command. Let's have a look at an example:

sub winGame(reps){
try{
Action(62,1, moveDance, reps ,200)
wait 400
stop tasks
}retry on fail
}

In this example we want the Spybot to do a little dance when we call this sub. To ensure there are no conflicts the command block has been wrapped around with the try command. As you can see try starts and finishes with it's own curly braces and is followed in this case with retry on fail. If another sub already has access to the motors then sub winGame will fail to run. It will then try to run this code block again. There are three other options: restart ( returns to the start of the sub or task and runs from there) ; abort (continue with the next command after the try); stop (go to the end of the sub or task).

It is possible to tell the program which subs have a greater priority with regards accessing resources. You do this by using the priority keyword at the top of the sub-routine or task. The priority can be from 1 (highest priority) to 8 (lowest priority). Here's winGame with a priority setting:

sub winGame(reps){
priority 1
try{
Action(62,1, moveDance, reps ,200)
wait 400
stop tasks
}retry on fail
}

 

 

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