Skip to content
alvaro555 edited this page Oct 23, 2024 · 29 revisions

Expression Block

The Expression block in Open Roberta Lab allows users to write complex expressions directly in text format using the TextlyJava language, enhancing the versatility of programming in Open Roberta Lab.

To access the Expression block in Open Roberta Lab, press Ctrl + 4 (or Strg + 4 for German keyboards).Once the block appears, users can write any expression and either generate the corresponding code or run a simulation using the available options in Open Roberta Lab.

The Expression block includes a combo box that allows users to select the appropriate data type for their expression. The available data types depend on the robot being used. For example, for the Microbitv2, users can choose from specific options tailored to that device. Selecting the correct data type ensures compatibility with other blocks and operations in Open Roberta Lab.

Example:

Consider the mathematical expression:

$$ c = \sqrt{a^2 + b^2 - 2ab \cdot \cos(\gamma)} $$

While this expression could be represented using multiple NEPO blocks, using the Expression block allows for a faster textual representation:

sqrt(a^2 + b^2 - 2*a*b * cos(gamma))

For example, given:

  • a = 5
  • b = 7
  • gamma = $$\frac{\pi}{3} $$

To compute the value of ( c ) using the Law of Cosines, the corresponding NEPO program representation is as follows: block_2 When utilizing the EvalExpr block for a textual expression, the representation of the equation is:

block_3

TextlyJava Expression Language Guide

TextlyJava is a flexible expression language used for programming robots and handling a variety of tasks like arithmetic operations, logic, image manipulations, and robot-specific commands. This guide will introduce how to use various types of expressions supported by TextlyJava.

Expressions Overview

Expressions in TextlyJava represent programmable blocks of various types, such as:

  • Numbers
  • Strings
  • Booleans
  • Connections
  • Colors
  • Images
  • Arrays of these types

Each expression can perform arithmetic, logical operations, and string manipulations, with specialized expressions for specific robots.

General Expressions:

Logic Expressions

Block (Image) Textly Representation Example
AND Block expr && expr a && b
OR Block expr || expr a || b
Equal Block expr == expr a == b
Not Equal Block expr != expr a != b
Greater Than Block expr > expr a > b
Less Than Block expr < expr a < b
Greater Equal Block expr >= expr a >= b
Less Equal Block expr <= expr a <= b
Ternary Block expr ? expr : expr a == b ? x : y
NOT Block !expr !a
True Block true true
False Block false false
Null Block null null

Mathematical and Function Expressions

Block (Image) Textly Representation Example
Add Block expr + expr a + b
Subtract Block expr - expr a - b
Multiply Block expr * expr a * b
Divide Block expr / expr a / b
Power Block expr ^ expr a ^ b
Modulo Block expr % expr a % b
Unary Minus Block - expr -a
Sin Block sin(expr) sin(90)
Cos Block cos(expr) cos(90)
Tan Block tan(expr) tan(45)
Asin Block asin(expr) asin(1)
Acos Block acos(expr) acos(1)
Atan Block atan(expr) atan(1)
Exp Block exp(expr) exp(1)
Square Block square(expr) square(2)
Sqrt Block sqrt(expr) sqrt(4)
Abs Block abs(expr) abs(-10)
Log10 Block log10(expr) log10(100)
Log Block log(expr) log(10)
Random Int Block randomInt(expr, expr) randomInt(1, 100)
Random Float Block randomFloat() randomFloat()
Random Item Block randomItem(ListExpression) randomItem(ListExpression)
Floor Block floor(expr) floor(3.14)
Ceil Block ceil(expr) ceil(3.14)
Round Block round(expr) round(3.5)
Is Even Block isEven(expr) isEven(2)
Is Odd Block isOdd(expr) isOdd(3)
Is Prime Block isPrime(expr) isPrime(7)
Is Whole Block isWhole(expr) isWhole(3)
Is Positive Block isPositive(expr) isPositive(10)
Is Negative Block isNegative(expr) isNegative(-5)
Is Divisible By Block isDivisibleBy(expr, expr) isDivisibleBy(10, 5)
Sum Block sum(ListExpression) sum(ListExpression)
Max Block max(ListExpression) max(ListExpression)
Min Block min(ListExpression) min(ListExpression)
Average Block average(ListExpression) average(ListExpression)
Median Block median(ListExpression) median(ListExpression)
Standard Deviation Block stddev(ListExpression) stddev(ListExpression)
Constrain Block constrain(expr, expr, expr) constrain(50, 0, 100)
Cast to String Block castToString(expr) castToString(123)
Cast to Char Block castToChar(expr) castToChar(65)
Phi Constant Block phi phi
Pi Constant Block pi pi
E Constant Block e e
Sqrt2 Constant Block sqrt2 sqrt2
Sqrt(1/2) Constant Block sqrt_1_2 sqrt_1_2
Infinity Block inf inf

Clarification:

  • ListExpression: In all examples where ListExpression is used, it refers to a predefined list of numbers. In TextlyJava, you can only work with predefined lists, so you need to define ListExpression beforehand. An example of a predefined list could be:

textly ListExpression = [1, 2, 3, 4, 5]

Text Expressions

Block (Image) Textly Representation Example
ConstStr Block "String" "Hello World"
Create Text With Block createTextWith(expr, expr, ...) createTextWith("Hello", " ", "World")
Cast To Number Block castToNumber(expr) castToNumber("123")
Cast String To Number Block castStringToNumber(expr) castStringToNumber("HelloWorld", 1)

List Expressions

Block (Image) Textly Representation Example
List Block [expr, expr, ...] [1, 2, 3, 4, 5]
Create List With Block createListWith(expr, expr, ...) createListWith(1, 5)
Create Empty List Block createEmptyList(Type of list) createEmptyList(Number)
Size Block size(ListExpression) size(ListExpression)
Is Empty Block isEmpty(ListExpression) isEmpty(ListExpression)
Index of First Block indexOfFirst(ListExpression, expr) indexOfFirst(ListExpression, 2)
Index of Last Block indexOfLast(ListExpression, expr) indexOfLast(ListExpression, 3)
Get Block get(ListExpression, index) get(ListExpression, 0)
Get From End Block getFromEnd(ListExpression, index) getFromEnd(ListExpression, 1)
Get First Block getFirst(ListExpression) getFirst(ListExpression)
Get Last Block getLast(ListExpression) getLast(ListExpression)
Get and Remove Block getAndRemove(ListExpression, index) getAndRemove(ListExpression, 1)
Get and Remove From End Block getAndRemoveFromEnd(ListExpression, index) getAndRemoveFromEnd(ListExpression, 1)
Get and Remove First Block getAndRemoveFirst(ListExpression) getAndRemoveFirst(ListExpression)
Get and Remove Last Block getAndRemoveLast(ListExpression) getAndRemoveLast(ListExpression)
Sub List Block subList(ListExpression, from, to) subList(ListExpression, 1, 3)
Sub List From Index to Last Block subListFromIndexToLast(ListExpression, index) subListFromIndexToLast(ListExpression, 1)
Sub List From Index to End Block subListFromIndexToEnd(ListExpression, index) subListFromIndexToEnd(ListExpression, 1)
Sub List From First to Index Block subListFromFirstToIndex(ListExpression, index) subListFromFirstToIndex(ListExpression, 1)
Sub List From First to Last Block subListFromFirstToLast(ListExpression) subListFromFirstToLast(ListExpression)
Sub List From First to End Block subListFromFirstToEnd(ListExpression) subListFromFirstToEnd(ListExpression)
Sub List From End to Index Block subListFromEndToIndex(ListExpression, index) subListFromEndToIndex(ListExpression, 1)
Sub List From End to End Block subListFromEndToEnd(ListExpression, index) subListFromEndToEnd(ListExpression, 1)
Sub List From End to Last Block subListFromEndToLast(ListExpression, index) subListFromEndToLast(ListExpression, 1)

User-Defined Function Call

Block (Image) Textly Representation Example
User-Defined Function Call Block functionName(expr, expr, ...) myFunction(0, 0)

Specific Robot Expressions

MicrobitV2

Block (Image) Textly Representation Example
Predefined Image image(ImageName)
ImageName options:
heart, heartSmall, happy, smile,
sad, confused, angry, asleep,
surprised, silly, fabulous, meh,
yes, no, triangle, triangleLeft,
chessboard, diamond, diamondSmall, squareBig,
squareSmall, rabbit, cow, musicCrotchet,
musicQuaver, musicQuavers, pitchfork, xmas,
pacman, target, tshirt, rollerskate,
duck, house, tortoise, butterfly,
stickFigure, ghost, sword, giraffe,
skull, umbrella, snake
image(heart)
User-Defined Image image.define(Image defined by the user) image.define([ [0, 0, 0, 0, #], [0, 0, 0, 0, #], [0, 0, 0, 0, #], [0, 0, 0, 0, #], [0, 0, 0, 0, #] ])
Image Shift image.shift(Direction, expr, expr)
Direction options: up, down, left, right
image.shift(left, 2, 3)
Image Invert image.invert(expr) image.invert(image)
Accelerometer Sensor microbitv2.accelerometerSensor(Slot)
Slot options: x, y, z, strength
microbitv2.accelerometerSensor(x)
Logo Touch Sensor microbitv2.logoTouchSensor.isPressed() microbitv2.logoTouchSensor.isPressed()
Compass Sensor microbitv2.compassSensor.getAngle() microbitv2.compassSensor.getAngle()
Gesture Sensor microbitv2.gestureSensor.currentGesture(Gesture)
Gesture options: up, down, faceDown, faceUp, shake, freefall
microbitv2.gestureSensor.currentGesture(shake)
Keys Sensor microbitv2.keysSensor.isPressed(Button)
Button options: Defined in robot configuration
microbitv2.keysSensor.isPressed(A)
Light Sensor microbitv2.lightSensor.getLevel() microbitv2.lightSensor.getLevel()
Pin Get Value Sensor microbitv2.pinGetValueSensor(Pin, Mode)
Pin: Defined in robot configuration
Mode options: analog, digital, pulseHigh, pulseLow
microbitv2.pinGetValueSensor(P0, analog)
Pin Touch Sensor microbitv2.pinTouchSensor.isPressed(INT) microbitv2.pinTouchSensor.isPressed(1)
Sound Sensor microbitv2.soundSensor.microphone.soundLevel() microbitv2.soundSensor.microphone.soundLevel()
Temperature Sensor microbitv2.temperatureSensor() microbitv2.temperatureSensor()
Timer Sensor microbitv2.timerSensor() microbitv2.timerSensor()
Get LED Brightness microbitv2.getLedBrightness(expr, expr) microbitv2.getLedBrightness(1, 2)
Receive Message microbitv2.receiveMessage(DataType)
DataType options: Number, Boolean, String
microbitv2.receiveMessage(Number)

WeDo

Block (Image) Textly Representation Example
Pink Purple Blue Green LightGreen Orange Red White Yellow #color
Color options: #pink, #purple, #blue, #green, #lightgreen, #yellow, #orange, #red, #white, #rgb(XXXXXX)
#green, #rgb(008000)
Gyro Sensor - isTilted wedo.gyroSensor.isTilted(port, direction)
Port options: Defined in robot configuration
Direction options: up, down, back, front, no, any
wedo.gyroSensor.isTilted(port1, front)
Infrared Sensor wedo.infraredSensor(port)
Port options: Defined in robot configuration
wedo.infraredSensor(port2)
Keys Sensor wedo.keysSensor.isPressed(button)
Button options: Defined in robot configuration
wedo.keysSensor.isPressed(button1)
Timer Sensor timerSensor() timerSensor()

Ev3

Block (Image) Textly Representation Example
Color1
Color2
Color3
Color4
Color5
Color6
Color7
Color8
#rgb(hex) or #colorName
Colors: #rgb(0057a6) or #blue, #rgb(585858) or #gray, #rgb(000000) or #black, #rgb(f7d117) or #yellow, #rgb(00642e) or #green, #rgb(b30006) or #red, #rgb(FFFFFF) or #white, #rgb(532115) or #brown
#rgb(0057a6)
#rgb(f7d117)
Connect1 ev3.connectToRobot(expr) ev3.connectToRobot("weDo")
Connect2 ev3.receiveMessage(expr) ev3.receiveMessage(null)
Connect3 ev3.waitForConnection() ev3.waitForConnection()
MotorSpeed1 ev3.getSpeedMotor(port)
Port options: A, B, C, D
ev3.getSpeedMotor(B)
Volume1 ev3.getVolume() ev3.getVolume()
TouchSensor1 ev3.touchSensor.isPressed(port)
Port options: 1, 2, 3, 4
ev3.touchSensor.isPressed(1)
Ultrasonic1
Ultrasonic2
ev3.ultrasonicSensor.getDistance(port)
ev3.ultrasonicSensor.getPresence(port)
Port options: 1, 2, 3, 4
ev3.ultrasonicSensor.getDistance(4)
ev3.ultrasonicSensor.getPresence(4)
ColorSensor1 ev3.colorSensor(mode, port)
Modes: colour, light, ambientlight, rgb
Port options: 1, 2, 3, 4
ev3.colorSensor(colour, 3)
ev3.colorSensor(light, 3)
Infrared1
Infrared2
ev3.infraredSensor.getDistance(port)
ev3.infraredSensor.getPresence(port)
Port options: 1, 2, 3, 4
ev3.infraredSensor.getDistance(4)
ev3.infraredSensor.getPresence(4)
Encoder1
Encoder2
Encoder3
ev3.encoderSensor.getDegree(port)
ev3.encoderSensor.getRotation(port)
ev3.encoderSensor.getDistance(port)
Port options: A, B, C, D
ev3.encoderSensor.getDegree(B)
ev3.encoderSensor.getRotation(B)
ev3.encoderSensor.getDistance(B)
KeysSensor1 ev3.keysSensor.isPressed(button)
Buttons: up, down, left, right, escape, any
ev3.keysSensor.isPressed(up)
ev3.keysSensor.isPressed(any)
Gyro1
Gyro2
ev3.gyroSensor.getAngle(port)
ev3.gyroSensor.getRate(port)
Port options: 1, 2, 3, 4
ev3.gyroSensor.getAngle(2)
ev3.gyroSensor.getRate(2)
Timer1 ev3.timerSensor(port)
Port options: 1, 2, 3, 4, 5
ev3.timerSensor(1)
Sound1 ev3.soundSensor.getSoundLevel(port)
Port options: 1, 2, 3, 4
ev3.soundSensor.getSoundLevel(4)
HiTechCompass1
HiTechCompass2
ev3.hiTechCompassSensor.getAngle(port)
ev3.hiTechCompassSensor.getCompass(port)
Port options: 1, 2, 3, 4
ev3.hiTechCompassSensor.getAngle(4)
ev3.hiTechCompassSensor.getCompass(4)
HiTechInfrared1
HiTechInfrared2
ev3.hiTechInfraredSensor.getModulated(port)
ev3.hiTechInfraredSensor.getUnmodulated(port)
Port options: 1, 2, 3, 4
ev3.hiTechInfraredSensor.getModulated(4)
ev3.hiTechInfraredSensor.getUnmodulated(4)
HiTechColor1 ev3.hiTechColorSensor(mode, port)
Modes: colour, light, ambientlight, rgb
Port options: 1, 2, 3, 4
ev3.hiTechColorSensor(colour, 3)
ev3.hiTechColorSensor(rgb, 3)
Clone this wiki locally