Top Banner
Mindscript Presented by Dean Thomas Matthew Horoszowski
22

Mindscript Presented by Dean Thomas Matthew Horoszowski.

Jan 03, 2016

Download

Documents

Briana Carson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Mindscript

Presented by

Dean Thomas

Matthew Horoszowski

Page 2: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Overview

Created by Lego

Some syntactically similarities to C/C++

Allows the use of header files

Not to be confused with the open source Mindscript project

Page 3: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Program Layoutprogram programname { program body }

Inside the program body we have compiler directives and declarations followed by:

macro macroname (parameterlist) { command stack }main { command stack }watcher watchername monitor eventlist { command stack

} [restart on event]task taskname { command stack }fragment (x, y) { command stack }comment (x, y) “Any literal string all on one line”

Page 4: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Watchers and Taskswatcher watchername monitor eventlist { command stack } [restart on event]task taskname { command stack }

start watchernamestop watchernamestart tasknamestop tasknamestart mainstop mainstop tasks

Watcher and task are both thread definitionsWatchers are just tasks that wait for events to be triggeredMain is just another task

Start and stop are used to control the flow of tasks

Page 5: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Macrosmacro macroname (parameterlist) { commands }parameterlist Comma separated list of the parameters of the macro

Macros are called by name.Macros can be downloaded to RAM subroutines or the macro code can be

expanded inline. The compiler will optimize to reduce code size. Example:program flashBeep {

var note = 50macro flash(times, freq) {

repeat times {tone freq for 10}}macro beeps {sound 3 sound 5 sound 1}main {

forever {flash(2, note) beeps note += 5}}

}

Page 6: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Conditionalsif value relop value { commands } [ else { commands } ]

if value is [not] range { commands } [ else { commands } ]

select value {

when val1 { Commands }

when val2 { Commands }

...

when valN { Commands }

[ else { Commands } ]

}

If statements are the common conditional statement

Select statements use when to separate different cases

Page 7: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Loopswhile value relop value { commands }while value is [not] range { commands }repeat repeatnumber|random a [to b] { commands }repeat { commands } until eventlistforever { commands }

There are 3 kinds of loops: while, repeat, and forever

while is a loop that continues until the given condition is met

repeat is a loop that continues for a certain number of iterations

forever is a loop that continues indefinitely

Page 8: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Waitwait time|random time1 [to time2]

wait until eventlist

wait can be used to count time

time is in 100ths of a second

wait can also block until an event is triggered

Page 9: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Eventsevent eventname when value relop valuefire eventlisttrigger eventlistmonitor eventlist { commands } [retry, abort, restart, stop] on eventrepeat { commands } until eventlistwait until eventlistif eventlist { commands } [ else { commands } ] // Only inside watcher

stack

fire triggers an event immideatlytrigger tests the condition for each event in the eventlist and throw

events if the condition is trueMonitor watches for an event during a block of codeRepeate untill will is like monitor but loops until event is triggeredWait until will block untill any of the events in the list are triggeredIf event checks to see if a event is being triggered and responds

accordingly

Page 10: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Access Controlpriority prt

try { commands } [retry, abort, restart, stop] on fail

prt Number or constant (1-8, 1: High priority)

Priority sets the importance of the current task

Try will attempt a series of commands that make use of system resources, and respond with a fail if priority is too low to access them right now

Page 11: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Sensorssensor sensorname on port

port number (1, 2 or 3 for RCX input ports)

sensorname is typesensorname as modesensorname is type as mode

type Unknown, switch, temperature, light, rotationmode raw, boolean, transition, periodic, percent, celsius, fahrenheit,

angle

clear sensorname

Sensor is used to decalare access to a hardware sensorIs determins the type of sensorAs declares the mode of data used by the sensorClear simply releases a sensor binding

Page 12: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Motorsoutput name on port

on portson ports for time|random t1 [to t2]off portsfloat portsforward ports, fd ports // Short form of forwardbackward ports, bk ports // Short form of backwarddirection fdports, bkports, dir fdports, bkports // Combined fd and bk

commandreverse ports power ports pwr|random pwr1 [to pwr2]

global (command)

Output declares motors

On, off, etc… used to control the motors

Global + command is used to access all the motors at once

Page 13: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Soundsound onsound offsound soundnumbertone freq for timeclear sound

soundnumber Constant giving System sound numberfreq Constant or variable giving tone frequency (1-20000)time Literal expression giving tone duration in 0.01 seconds up to2.55 sec.

Sound on and off used to control power to soundSound soundnumber used to declare what to issueTone for used to play a certain frewuency for a certain amount of

timeClear sound flushes the sound buffer

Page 14: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Variablesvar name {= value} // Allocates a global variablelocal name {= value}Name = valueclear Variable1

Both local and global variables exist

Arithmetic operators: + - * /Logical operators: & |Ordering done left to right, with () taking precedence

Assignment operators can also be used: += -= *= /= &= |=

Page 15: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Utility Commandsslot slotnumber // select program slot n (RCX: 1..5)boot rom // put RCX in boot mode ready to receive firmwareboot firmware // unlocks the firmwaresleep // turn off PBrick nowclear sleep // resets the PBricks sleep timersleep after timeout // turn off PBrick after t minutesrandomize // re-seeds the random number generatordisplay name[:decpoint] // selects a value to continuously

monitor on the LCDwatch hh:mm // set internal PBrick watch

Page 16: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Layout and CommentsWhite space, comments & case are ignored.Multi-line comments (or comments within code) start with /* and

end with */.Single line comments start with // - everything up to the end of

line is ignored.

Example:// This is a single line commentmain {

repeat /* this is a short comment */ 2 { // this is ignoredsound 3 wait 100/* This comment extendsover two lines */

}}

Page 17: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Example Program 1program Avoider {

#include <RCX2.h>#include <RCX2MLT.h>

const BACKTIME = 100 // 1 secondconst TURNTIME = 50 // 0.5 secondconst DANCETIME = 10 // 0.1 secondconst AVOIDLIMIT = 4 // 5 strikes and you’re outconst TICKTIME = 10 // 1 secondsensor LeftTouch on 1sensor RightTouch on 3timer Timer1counter AvoidCountevent LeftPressed when LeftTouch.pressedevent RightPressed when RightTouch.pressedevent DontBugMe when AvoidCount > AVOIDLIMITevent Tick when Timer1 = TICKTIME

Page 18: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Example Program 2main {

bbs_GlobalReset([A B C])priority 8clear timer1start Heartbeatstart AvoidTouchstart TooMuchdisplay AvoidCounttry {

forever { bb_Forward(A,C,1000) }} retry on fail

}watcher AvoidTouch monitor leftPressed, rightPressed {

priority 3try {

AvoidCount += 1sound 3if leftPressed {

bb_Backward (A, C, BACKTIME)bb_SpinLeft (A, C, TURNTIME)

} else {bb_Backward (A, C, BACKTIME)bb_SpinRight(A, C, TURNTIME)

}} stop on fail

} restart on event

Page 19: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Example Program 3watcher TooMuch monitor DontBugMe {

priority 2try {

sound 6sound 6bb_Dance(A, C, 1, DANCETIME)clear AvoidCount

} stop on fail} restart on eventwatcher Heartbeat monitor tick {

priority 7clear Timer1try {

tone 36 for 5 wait 10 tone 36 for 5} abort on fail

} restart on event}

Page 20: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Advantages

Made by Lego

Mindstorms SDK includes all necessary documentation

Full use of the bricks firmware

Closely coupled to the firmware’s design

Page 21: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Disadvantages

Very limited

Not many on-line resources

Limited number of subroutines and

tasks

Works in Windows only

Page 22: Mindscript Presented by Dean Thomas Matthew Horoszowski.

Links

Lego Mindscript Homepage - http://mindstorms.lego.com/sdk2/default.asp

Smart Parts Tutorials - http://users.ncable.net.au/~blane/smartParts/04000.htm