sumo bot: script block and run block

This is the place to ask question about Enchanting, talk about your Enchanting projects or simply find out more about Enchanting on the NXT.

Moderator: clintonb

sumo bot: script block and run block

Postby michael » Sat Jan 26, 2013 6:07 pm

Hi,
On the enchanting cards I find the sript of the sumo bots. (you also find a screenshot here:http://robotclub.ab.ca/articles/50/enchanting-02-is-out
I don't understand, why I have to use the script block and the run block in this example.
Why is it not possible to say "if foe ahead" - "attack". Why I have to use a variable named next action: if foe ahead - set next action to the script "attack"

And does it make a difference, which if clause comes first: "if foe ahead" or "if sees edge"?

Do I have the possibility to make an interrupt?
michael
New User
 
Posts: 4
Joined: Sat Jan 26, 2013 5:21 pm

Re: sumo bot: script block and run block

Postby clintonb » Sat Jan 26, 2013 10:50 pm

Hi Michael. What a great question.

One effective ways to make a robot do more than one thing is to use a behaviour-based approach, and the sumo robot does this. In a (simplified) behaviour-based approach, you have a series of triggers, or conditions which might cause the robot to do something, and a series of behaviours, of things the robot might do. The code (or, to be more technical, a routine called the 'arbiter' [which means 'decision maker']) goes through each of the triggers in turn, starting from the least important and going up to the most important, and sees if the trigger is active [meaning the condition is occurring]. If it is, it determines which behaviour corresponds to that trigger, and prepares to run it. However, as it goes through triggers of higher priority, if one of them is active, their behaviour will get to run instead. At the end of looking at all the triggers, it only runs one behaviour.

Here are the triggers and behaviours for the sumo robot:

Image

The 'the script' block allows us to store a (reference to a) routine in a variable, and then call it [run it/invoke it/do it] later. This is handy as it makes for clear, compact code.

Instead of remembering which behaviour to call [or routine to run], the sumo program could remember a name or string representing the routine to run, go through all the triggers and make its decision, and then go through every possible name that it may have remembered and run the corresponding function if it was the chosen one, like this:

Image.

This code may be clearer for a beginner, but it is longer and more likely to cause problems, for if you make a mistake typing the name of the behaviour (typing 'Wandr' instead of 'Wander', for example), it will not work right.

You asked if the order is important. It is. Start with the least important behaviour first. More important behaviours go later, and each gets a chance to overturn or change the decision. Only if no trigger of greater importance is active will a less important trigger dictate the next action.

[If you just coded it up to say, 'if foe ahead -> attack' and 'if sees edge -> retreat', then, if there was a moment that both conditions are true, they'd activate one after the other [or worse, at the same time! -- and no robot can not drive forwards and backwards at the same time!] and you'd not get the result that you want, which is to do only one task, whichever is most important.]


You also asked if you can have an 'interrupt'. No, not really. An interrupt stops whatever is going on to do something else, and Enchanting has no support for them. What you could do is make a high priority trigger, and run the corresponding behaviour whenever it is active. Note, however, that the sumo program uses what is called 'co-operative multi-tasking', which means that while a behaviour is running, no other behaviour has a chance to run. I'd typically recommend a behaviour do something really quick and simple, like set the motors to drive forward, because it can do that many times in a second and keep on checking on the conditions affecting the robot. If, however, you run, say, a retreat behaviour that tells the robot to drive backwards 10 cm and turn, while that is happening, no other behaviour has a chance to run (or interrupt it).
clintonb
Active User
 
Posts: 82
Joined: Fri May 28, 2010 1:44 am
Location: Cardston, Alberta, Canada


Return to Enchanting Questions and Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff