Using Scripts
By now you know how to define an SBA sequence. In the last example, we created a sequence to pick up an item from the floor.
Here's what that code looked like:
However, this SBA framework has its own scripting language. Read on to learn how to use it.
What is a Scripting Language?
A scripting language is essentially a simplified programming language. This also means the syntax and logic are similar. Scripting languages are good for focused languages, like with SBA's.
The SBA Scripting Language
The SBA Scripting language (SBASL) syntax is structured like below:
Each line consists of a command, then parameters, each separated by spaces.
Here are the supported commands:
Here's the sequence from the last example in SBASL:
This is a lot simpler than the Java version!
The SBALexer
Now, to run the script we just created, we'll need a SBALexer
.
Let's modify the sample TeleOp from How it Works:
Motors and Servos
To change which motors and servos are included in the SBALexer
, you'll need to modify the initializer in SBALexer.java
.
The highlighted lines indicate where motors and servos are setup with the SBALexer
. Say you have a linear slide on your robot, and you want to use it with SBALexer
.
Assuming it's declared as bot.slideMotor
, you can add this line to the initializer:
SBALexer.java | |
---|---|
You can also specifiy a default power to run the motor with.
SBALexer.java | |
---|---|
Similarly, you can add a new servo bot.wristServo
with:
SBALexer.java | |
---|---|
Constants
You can also make use of constants in SBALexer
. Also setup in the initializer, you can use constants in place of numbers anywhere in a SBA script.
SBALexer.java | |
---|---|
You can use these constants in your SBA scripts like in this example that will open the claw:
Using constants, we can refactor our existing SBA script routine to use constants:
Using the Dashboard
Here's where the real advantage of using SBASL comes in. If you use FTC Dashboard, you can edit static variables in the browser, and view their results live without changing code and restarting the robot. This is a huge timesaver during testing.
To make use of the FTC Dashboard with SBASL, let's modify our TeleOp:
Now you can edit your script from the browser!
Newlines in the Dashboard
Unfortunately, you can't enter multiline strings on the FTC Dashboard. To work around this, you can use a substitute newline, like "/"
. To implement, simply modify one line in your OpMode: