English
Powered By GitBook
Adding restrictions to the 2D Model
It is possible to add restrictions by editing an XML file to prepare exercises for students.
There are three types of restrictions:
    1.
    Time restrictions. For example, a time limit to complete a task or a specific action in a specific time frame.
    2.
    Spatial. For example, adding regions ("Start", "Finish") or prohibiting/forcing a robot, its sensor, or some moving object to be at a certain time in a certain place.
    3.
    Device restrictions. For example, a limitation on the set of sensors or on the behavior of devices.

Constraint description structure

The main tag<constraints>...</constraints>which contains all the restrictions is used to describe them. Used as a container. Restrictions are described inside the tag, each inner tag must be one of four:
Tag
Description
timelimit
Time limit.
A constraint with an arbitrary condition in violation of which a specified error will be generated.
event
The main tool for setting dynamic constraints. Used as a container.
init
An unconditional event that runs before the program starts executing.

<constraints>...</constraints>

The main tag containing all restrictions. Used as a container.
1
<constraints>
2
<!-- Time limit -->
3
<timelimit value="30000"/>
4
5
<!-- Conditional constraint. An error message will be shown at violation. -->
6
<constraint checkOnce="true" failMessage="The robot must be at start before run!">
7
<inside objectId="robot1" regionId="start_zone"/>
8
</constraint>
9
10
<!-- Initializing variable x with value 2 -->
11
<init>
12
<setter name="x">
13
<int value="2"/>
14
</setter>
15
</init>
16
17
</constraints>
Copied!

<timelimit/>

Time limit. Mandatory.

Attributes

Attribute
Description
value="value"
Timeout in milliseconds after which the execution will be terminated and the "Time limit exceeded" error will be displayed.

Syntax:

1
<timelimit value="35000"/>
Copied!

<constraint>...</constraint>

A constraint with an arbitrary condition. If the condition is violated a specified error message will be displayed. Can be used as a container. Has one child tag: <conditions>...</conditions>.

Attributes

Attribute
Description
checkOnce="true"
Boolean attribute. If the value is true, then the restriction will be checked once at program startup. Is useful for one-time checks such as sensor layout etc.
failMessage="Error!"
An error message will be displayed when a restriction is violated.

Syntax:

1
<constraint checkOnce="true" failMessage="The robot must be at start before run!">
Copied!
1
<!-- Checks if the IR distance sensor is connected to the A1 port -->
2
<constraint checkOnce="true" failMessage="The IR distance sensor must be connected to the robots A1 port!">
3
<equals>
4
<typeOf objectId="robot1.A1"/>
5
<string value="twoDModel::robotModel::parts::RangeSensor"/>
6
</equals>
7
</constraint>
Copied!
1
<!-- Checks if the robot is inside the specified region for the entire program execution time-->
2
<constraint failMessage="The robot has left the allowed zone!">
3
<inside objectId="robot1" regionId="warzone"/>
4
</constraint>
Copied!

<event>...</event>

The main tool for setting dynamic constraints. Used as a container.
An event is just a pair (condition, trigger).

Attributes

Attribute
Description
settedUpInitially="true"
An attribute that allows you to indicate whether the event is set up at the start of the program. The event can be set up or dropped. In the setup state, the event pulls its trigger if its condition is met, otherwise it's just ignored by the system.
The default value is false.
id="finish checker"
Events unique ID. The event can be referred by id from others. Optional.
dropsOnFire = "true"
A boolean attribute that indicates whether the event should continue to be set up after it is triggered or not. Optional. The default value is true.

Syntax:

1
<event id="finish checker" settedUpInitially="false">
2
<condition>
3
<inside objectId="robot1" regionId="finish"/>
4
</condition>
5
<trigger>
6
<success/>
7
</trigger>
8
</event>
Copied!

<init>...</init>

Unconditional event executing before the program starts.
Example:
1
<!-- Setting the "my_value" variable to the value of two-->
2
<init>
3
<setter name="my_value">
4
<int value="2"/>
5
</setter>
6
</init>
Copied!

Conditions

Now lets talk about the conditions in the <constraint> and <event> elements. Conditions are set using the <condition> tag if only one of the atomic conditions is tested, or the <conditions> tag if a compound condition is tested.

<condition>...</condition>

The condition being checked is described inside this tag.
Example:
1
<condition>
2
<!-- The condition of equality of two values is described inside the tag -->
3
<equals>
4
<objectState object="robot1.display.smiles"/>
5
<bool value="true"/>
6
</equals>
7
</condition>
Copied!

<conditions>...</conditions>

Used to create compound conditions. The logical bond is the mandatory attribute. The bond may be and or or. Negation is specified by the <not> tag without attributes. Other <conditions> elements may also appear among subexpressions.

Attributes:

Attribute
Description
glue="and"
Logical bond.

Syntax:

1
<conditions glue="and">
2
<!-- Condition1 -->
3
<!-- Condition2 -->
4
<!-- ... -->
5
<!-- ConditionN -->
6
</conditions>
7
8
<conditions glue="and">
9
<not>
10
<!-- Condition1 -->
11
</not>
12
</conditions>
13
14
<conditions glue="and">
15
<timer timeout="1000" forceDropOnTimeout="true"/>
16
<conditions glue="or">
17
<greater>
18
<objectState object="robot1.display.labels.size"/>
19
<int value="20"/>
20
</greater>
21
<less>
22
<objectState object="robot1.display.labels.size"/>
23
<int value="19"/>
24
</less>
25
</conditions>
26
</conditions>
Copied!

Atomic conditions

Atomic condition is one of the following elements:
Tag
Description
Value comparison operations.
inside
Sets spatial constraints.
Checks whether an event is set up or not.
timer
Sets the time in ms after which the specified condition is considered true.

<equals>...</equals>

Equals. The functional symbols value comparison operation. Can be used as a container.

Syntax:

1
<equals>
2
<objectState object="robot1.display.labels.first.text"/>
3
<string value="finish"/>
4
</equals>
Copied!

<notEqual>...</notEqual>

Not equal. The functional symbols value comparison operation. Can be used as a container.

Syntax:

1
<notEqual>
2
<objectState object="robot1.display.labels.first.text"/>
3
<string value="finish"/>
4
</notEqual>
Copied!

<greater>...</greater>

Greater. The functional symbols value comparison operation. Can be used as a container.

Syntax:

1
<greater>
2
<objectState object="robot1.display.labels.size"/>
3
<int value="0"/>
4
</greater>
Copied!

<less>...</less>

Less. The functional symbols value comparison operation. Can be used as a container.

Syntax:

1
<less>
2
<objectState object="robot1.display.labels.size"/>
3
<int value="10"/>
4
</less>
Copied!

<inside/>

Sets spatial constraints.

Attributes

Attribute
Description
objectId="id"
Object ID.
regionId="id"
Region ID.

Syntax:

1
<!-- Limits the location of the "robot1" robot to the "start" zone -->
2
<inside objectId="robot1" regionId="start"/>
Copied!

<settedUp/> and <dropped/>

Checks whether the event set up or not.

Attributes

Attribute
Description
id="event1"
The ID of the event is checked

Syntax:

1
<!-- The "event1" event is set up condition -->
2
<condition>
3
<settedUp id="event1"/>
4
</condition>
5
6
<!-- The "event2" event is dropped condition -->
7
<condition>
8
<dropped id="event2"/>
9
</condition>
Copied!

Example:

The "check event" event conditions check that the other "Try move" event is set up and the "Go back" event is dropped. If both of them return "true" after the check the program successfully ends.
1
<event id="check event" settedUpInitially="true">
2
<conditions glue="and">
3
<settedUp id="Try move"/>
4
<dropped id="Go back"/>
5
</conditions>
6
<trigger>
7
<success/>
8
</trigger>
9
</event>
Copied!

<timer/>

A predicate that starts to return "true" when the specified time has passed since the moment when this event was set, and up to that moment it returns "false".

Attributes

Attribute
Description
timeout="1000"
The time interval after which this predicate will become true. Mandatory. The value must be a non-negative integer.
forceDropOnTimeout="true"
A Boolean attribute that allows you to drop the event that has this timer in a condition.
If set to true, the event will be dropped even if there are other active timers and unmet conditions. Optional. The default value is true.

Syntax:

1
<timer timeout="1000" forceDropOnTimeout="false"/>
Copied!

Example

Let's look at using <timer/> with different forceDropOnTimeout attribute values.
The "check region" event checks time and spatial limits. The first condition (timer) becomes true after 1000ms. Its value doesn't change after that. The second one (inside) checks that the robot is inside the "start_zone" region. When both conditions will be met simultaneously the program will end successfully.
1. Since the forceDropOnTimeout attribute is "false", the event will be still set up after a specified timeframe and wait for the second condition to be met.
1
<event id="check region" settedUpInitially="true">
2
<conditions glue="and">
3
<timer timeout="1000" forceDropOnTimeout="false"/>
4
<inside objectId="robot1" regionId="start_zone"/>
5
</conditions>
6
<trigger>
7
<success/>
8
</trigger>
9
</event>
Copied!
2. Since the forceDropOnTimeout attribute is "true", the event will be dropped after the specified timeframe despite the presence of a second condition. Therefore, if the robot is not in the required region right after 1000 ms, then the success message will not be displayed even if the robot will be there after some time.
1
<event id="check region" settedUpInitially="true">
2
<conditions glue="and">
3
<timer timeout="1000" forceDropOnTimeout="true"/>
4
<inside objectId="robot1" regionId="start_zone"/>
5
</conditions>
6
<trigger>
7
<success/>
8
</trigger>
9
</event>
Copied!

Variable types and arithmetic operations

Variable or operation
Description
Integer, fractional, string, and boolean constants.
Variable value.
Get the object state.
typeOf
Get the meta-type of the object with the specified identifier.
Unary arithmetic functions that have exactly one child, which must be an integer value.
Binary arithmetic functions that have exactly two child elements, each of them must be an integer.

<int/>, <double/>, <string>, <bool/>

Setting a constant.

Attributes

Attribute
Description
value="0″
Constant value.

Syntax:

1
<int value="0"/>
2
3
<string value="finish"/>
Copied!

<variableValue/>

Variable value.
It is possible to take the property of any variable using a dot. For example, "rect.width" will return the width of the rectangle stored in "rect".

Attributes

Attribute
Description
name="my_value"
Variable name

Syntax:

1
<variableValue name="rotation"/>
Copied!

<objectState/>

Get the object state.

Attributes

Attribute
Description
object="robot1.display.labels.size"
Object ID

Syntax:

1
<objectState object="robot1.display.labels.first.text"/>
Copied!

Example

1
<!-- Assign to the "rotation" variable the robots rotation angle -->
2
<setter name="rotation">
3
<objectState object="robot1.rotation"/>
4
</setter>
5
<!-- Checking if the "rotation" value equals to the robots rotation angle -->
6
<equals>
7
<variableValue name="rotation"/>
8
<objectState object="robot1.rotation"/>
9
</equals>
Copied!

<typeOf/>

Get the objects with specified ID meta-type. For example, the type of the wall object with id=777 will be wall.
Most often, this element will be needed to check the type of connected sensors and motors.

Attributes

Attribute
Description
objectId="id"
Objects unique ID.

Syntax:

1
<typeOf objectId="robot1.A3"/>
Copied!

<minus>..</minus>, <abs>...</abs>

Unary arithmetic operations for changing the sign and taking the modulus of a number.

Syntax:

1
<minus>
2
<objectState object="robot1.rotation"/>
3
</minus>
4
5
<abs>
6
<objectState object="robot1.rotation"/>
7
</abs>
Copied!

Example:

1
<!-- Modulus of the difference of the "rotation" variable and the robots rotation angle -->
2
<abs>
3
<difference>
4
<variableValue name="rotation"/>
5
<objectState object="robot1.rotation"/>
6
</difference>
7
</abs>
Copied!

<sum>, <difference>, <min>, <max>

Sum and difference of values. Minimum and maximum value.

Example:

1
<!-- The difference of the "rotation" variable and the robots rotation angle -->
2
<difference>
3
<variableValue name="rotation"/>
4
<objectState object="robot1.rotation"/>
5
</difference>
6
7
<!-- The sum of the "counter" variable and one -->
8
<sum>
9
<variableValue name="counter"/>
10
<int value="1"/>
11
</sum>
Copied!

Triggers

Tag
Description
trigger
An action or a group of actions that will be performed one or many times after the event condition is met.
fail
Display an error message. Finish checking the task.
success
Display success message, finish the check.
setter
Set the variable value.
Setting or dropping the event.

<trigger>...</trigger>

An action or a group of actions that will be performed one or many times after the event condition is met.

Syntax:

1
<trigger>
2
<success/>
3
</trigger>
Copied!

<fail/>

Display an error message. Finish checking the task.

Attributes

Attribute
Description
message="Wrong answer!"
Error message text

Syntax:

1
<fail message="Wrong answer!"/>
Copied!

<success/>

The task is successfully done.

Attributes

Attribute
Description
deffered="false"
Optional. The default value is "false". If set to "true" the trigger won't stop the program, I. e. the checker will wait until the program finishes and either reports the success if there were no errors, or otherwise, it will finish with an error. In other words, you won't get the error "The program has finished, but the task is not completed": the program will either terminate successfully or with a meaningful error like "Time limit exceeded".

Syntax:

1
<success/>
Copied!

<setter>...</setter>

Set the variable value.

Attributes

Attribute
Description
name="my_value"
Variable name

Example:

1
<!-- Creating the "total_score" variable with the value of zero -->
2
<setter name="total_score">
3
<int value="0"/>
4
</setter>
5
6
<!-- Addind 2 to the "total_score" -->
7
<setter name="total_score">
8
<sum>
9
<variableValue name="total_score"/>
10
<int value="2"/>
11
</sum>
12
</setter>
Copied!

<setUp/>, <drop/>

Setting up or dropping an event.

Attributes

Attribute
Description
id="finish checker"
The ID of the event.

Example:

1
<!-- Set up the "finish checker" event -->
2
<triggers>
3
<setUp id="finish checker"/>
4
</triggers>
Copied!

Sensor names

TRIK Robot Sensors

Name
Description
twoDModel::robotModel::parts::RangeSensor
Distance sensor
trik::robotModel::twoD::parts::TwoDLightSensor
Light sensor
twoDModel::robotModel::parts::TouchSensor
Touch sensor
trik::robotModel::twoD::parts::LineSensor
Line sensor

Lego EV3 Robot sensors

Name
Description
twoDModel::robotModel::parts::RangeSensor
Distance sensor
twoDModel::robotModel::parts::LightSensor
Light sensor
twoDModel::robotModel::parts::TouchSensor
Touch sensor
twoDModel::robotModel::parts::ColorSensorRed
Color sensor (red)
twoDModel::robotModel::parts::ColorSensorGreen
Color sensor (green)
twoDModel::robotModel::parts::ColorSensorBlue
Color sensor (blue)
twoDModel::robotModel::parts::ColorSensorPassive
Color sensor (passive)
twoDModel::robotModel::parts::ColorSensorFull
Color sensor EVX/NXT (color)
twoDModel::robotModel::parts::ColorSensorAmbient
Color sensor EV3 (ambient)
ev3::robotModel::twoD::parts::GyroscopeSensor
Gyroscope
ev3::robotModel::twoD::parts::GyroscopeSensor
Compass

Additional robot properties

Property
Description
robot1.rotation
A current robot's rotation angle
robot1.x and robot1.y
Robots coordinate
robot1.led.color
LED color
robot1.marker.isDown
Checks if the robot has a marker and it draws a line on the field. Returns "true" or "false".
robot1.shell.lastPhrase
Get the last robots phrase.

Working with the controller display

Property
Description
robot1.display.sadSmiles
Checks if there is a sad smile on the controller display. Returns "true" or "false".
robot1.display.smiles
Checks if there is a happy smile on the controller display. Returns "true" or "false".
robot1.display.labels.first.text
Message (label) text that was displayed first.
robot1.display.labels.last.text
Message (label) text that was displayed last.
robot1.display.labels.size
Get the displayed label number.

Example

Checking that the word “message” was displayed on the controller screen. The case is important.
1
<equals>
2
<objectState object="robot1.display.labels.first.text"/>
3
<string value="message"/>
4
</equals>
Copied!

Restrictions example

You need to start in the blue square then drive along the wall to the red one using the IR distance sensor.
1
<?xml version="1.0" encoding="UTF-8"?>
2
<root>
3
<world>
4
<background />
5
<walls>
6
<!-- The wall-->
7
<wall id="{wall1}" end="300:150" begin="0:100" />
8
<wall id="{wall2}" end="550:100" begin="300:150" />
9
<wall id="{wall3}" end="650:200" begin="550:100" />
10
<wall id="{wall4}" end="850:300" begin="650:200" />
11
<wall id="{wall5}" end="1200:300" begin="850:300" />
12
<wall id="{wall6}" end="1600:50" begin="1200:300" />
13
</walls>
14
<skittles />
15
<balls />
16
<colorFields />
17
<images />
18
<regions>
19
<!-- Regions-->
20
<region visible="true" id="finish" x="1300" color="#ff0000" text="Finish" type="rectangle" width="300" textX="0" textY="0" y="-200" filled="true" height="300" />
21
<region visible="true" id="start_zone" x="-50" color="#0000ff" text="Start" type="rectangle" width="150" textX="0" textY="0" y="100" filled="true" height="-150" />
22
<region visible="false" id="warzone_1" x="-50" color="#ffff00" text="warzone1" type="rectangle" width="650" textX="0" textY="0" y="-50" filled="true" height="200" />
23
<region visible="false" id="warzone_2" x="600" color="#ffff00" text="warzone2" type="rectangle" width="250" textX="0" textY="0" y="0" filled="true" height="250" />
24
<region visible="false" id="warzone_3" x="850" color="#ffff00" text="warzone3" type="rectangle" width="750" textX="0" textY="0" y="50" filled="true" height="250" />
25
</regions>
26
</world>
27
<robots>
28
<!-- Robot description -->
29
<robot id="trikKitRobot" position="0:0" direction="0">
30
<sensors>
31
<sensor port="A1###input###А1###sensorA1" position="75:25" type="trik::robotModel::parts::TrikInfraredSensor" direction="45" />
32
<sensor port="M3###output###JM3$$C$$3###" position="75:25" type="kitBase::robotModel::robotParts::Motor" direction="0" />
33
<sensor port="M4###output###JM4$$D$$4###" position="75:25" type="kitBase::robotModel::robotParts::Motor" direction="0" />
34
</sensors>
35
<startPosition id="{ee2c46c1-23fc-4cda-98f4-77d9de775305}" x="25" y="25" direction="0" />
36
<wheels left="M3###output###М3###" right="M4###output###М4###" />
37
</robot>
38
</robots>
39
<constraints>
40
<!-- Time limit (30 seconds) -->
41
<timelimit value="30000" />
42
<!-- Spatial restriction of the start region. Checks once at the program start -->
43
<constraint checkOnce="true" failMessage="The robot must be inside the blue square at the start!">
44
<inside regionId="start_zone" objectId="robot1" />
45
</constraint>
46
<!-- Sensors type and presence restriction -->
47
<constraint checkOnce="true" failMessage="Only the IR distance sensor must be connected to the A1 port">
48
<conditions glue="and">
49
<equals>
50
<typeOf objectId="robot1.A1" />
51
<string value="trik::twoDModel::robotModel::parts::RangeSensor" />
52
</equals>
53
<equals>
54
<typeOf objectId="robot1.A2" />
55
<string value="undefined" />
56
</equals>
57
<equals>
58
<typeOf objectId="robot1.A3" />
59
<string value="undefined" />
60
</equals>
61
<equals>
62
<typeOf objectId="robot1.A4" />
63
<string value="undefined" />
64
</equals>
65
<equals>
66
<typeOf objectId="robot1.A5" />
67
<string value="undefined" />
68
</equals>
69
<equals>
70
<typeOf objectId="robot1.A6" />
71
<string value="undefined" />
72
</equals>
73
<equals>
74
<typeOf objectId="robot1.D1" />
75
<string value="undefined" />
76
</equals>
77
<equals>
78
<typeOf objectId="robot1.D2" />
79
<string value="undefined" />
80
</equals>
81
</conditions>
82
</constraint>
83
<!-- Robot ride control -->
84
<constraint failMessage="The robot tried to move out of the permitted area!">
85
<conditions glue="or">
86
<inside regionId="warzone_1" objectId="robot1" />
87
<inside regionId="warzone_2" objectId="robot1" />
88
<inside regionId="warzone_3" objectId="robot1" />
89
</conditions>
90
</constraint>
91
<!-- An event that checks if the robot has entered the finish area -->
92
<event settedUpInitially="true">
93
<condition>
94
<timer timeout="100" forceDropOnTimeout="true" />
95
</condition>
96
<trigger>
97
<setUp id="finish checker" />
98
</trigger>
99
</event>
100
<!-- Event reporting the successful finish -->
101
<event id="finish checker" settedUpInitially="false">
102
<condition>
103
<inside regionId="finish" objectId="robot1" />
104
</condition>
105
<trigger>
106
<success />
107
</trigger>
108
</event>
109
</constraints>
110
</root>
Copied!
Last modified 8mo ago