Search
K
Comment on page

Elapsed Time - Blocks

Introduction to Elapsed Time

One way to create an autonomous code is to use a timer to define which actions should occur when. Within the SDK actions can be set to a timer by using ElapsedTime.
Timers consist of two main categories: count up and count down. In most applications a timer is considered to be a device that counts down from a specified time interval. For instance, the timer on a phone or a microwave. However, some timers, like stopwatches, count upwards from zero. These types of timers measure the amount of time that has elapsed.
ElapsedTime is a count up timer. Registering the amount of time elapsed from the start of a set event, like the starting of a stopwatch. In this case, it is the amount of time elapsed from when the timer is created or reset within the code.
The ElapsedTime timer starts counting the amount of time elapsed from the point of its creation within a code. For instance, in this section ElapsedTime will be created in the section of code that occurs when the op mode is initialized. There is no option to stop the ElapsedTime timer. Instead, the
block can be used within your code to reset the timer at various intervals.
Once the timer has been reset, the amount of time that has elapsed is queried by calling blocks like
. The time given by the queried blocks can be used in loops to dictate how long a specific action should take place.
Sections
Goals of Section
Learning the logic needed to use elapsed time for autonomous control.

Basics of Programming with Elapsed Time

Since this section focuses on creating an autonomous program using ElapsedTime it is important to understand where the elapsed time related blocks are located. At the top of the Categorize Blocks section there is a drop down menu for Utilities. The utilities drop down is a list of various utilities in alphabetical order. Towards the bottom of the the list select Time drop down menu. From there you can select Elapsed Time.

Programming with Elapsed Time

Start by creating a new op mode call HelloWorld_ElapsedTime using the BasicOpMode sample.
When creating an op mode a decision needs to be made on whether or not to set it to autonomous mode. For applications under 30 seconds, typically required for competitive game play changing the op mode type to autonomous is recommended. For applications over 30 seconds, setting the code to the autonomous op mode type will limit your autonomous code to 30 seconds of run time. If you plan on exceeding the 30 seconds built into the SDK, keeping the code as a teleoperated op mode type is recommended.
For information on how op modes work please visit the Introduction to Programming section.
For more information on how to change the op mode type check out the Test Bed - Blocks section.
Create a variable named runtime.
For information on creating variables in blocks please revisit the Test Bed - Blocks section.
Add the
block to the op mode below the
comment block.
In order to utilize elements of the ElapsedTime, runtime will act as the ElapsedTime variable. Add the
block to the
block.
Before moving on to the rest of the ElapsedTime structure lets go ahead and add the motor related blocks. Add
to the op mode to the while loop.
When there are multiple of the same type of variable (such as multiple Dc Motor variables) the variable specific blocks will choose a default variable based on alphabetical order. For this example Op Mode Dc Motor blocks will default to the arm variable. Click the arrow next to the motor name to change the arm motor variable to the rightmotor variable. Use the variable drop down menu on the block to change from arm to rightmotor.
If you recall from Programming Drivetrain Motors article; the motors on the drivetrain mirror each other. The mirrored nature of the motor mounting causes the motors to rotate in opposing directions. In order to remedy this discrepancy the direction of the right motor needs to be reversed. Add the
block to the op mode under the the
block set.
The goal is to have the motor move forward for 3 seconds. To accomplish this the While loops needs to be edited so that it triggers when the op mode is active and the ElapsedTime timer is less than or equal to 3 seconds. Lets start by creating the less than or equal to condition. Grab the
from the Logic menu.
Select the
block from the Elapsed Time menu. Drop the block into the left side of the
block. Use the drop down menu to change the generic
to the
variable.
Grab the
block from the Math menu.
Add the number block to the right side of the
block. Change the number block to 3.
Right now the
is equal to three. Use the arrow next to the equal sign to choose the less than or equal to sign from the drop down menu.
Set this block set to the side for now. Grab an
block from the Logic menu
Add the call
block to the left side of the
block. Add the
block set to the right side of the
block.
This block set will replace the
block that is currently attached to the while loop. With this block set in place the while loop will now activate when both conditions of the and block are true.
It is important to know that, within a linear op mode, a while loop must always have the
Boolean as a condition. This condition ensures that the while loop will terminate when the stop button is pressed.
Use tape to mark the distance from where the robot starts to where you would like it to end up. Try running the code using the following conditions:
  • Press the init button and immediately press play
  • Press the init button, wait 30 seconds and then press play
What difference in behavior did you notice?
Recall that theElapsedTime timer starts when the timer is created, which occurs where the
block is placed. Since the timer is created prior to
, the timer will start when the program is initialized.
If you tested the program you may have noticed that the robot didn't move the during the second run. Depending on how long you wait to start after initialization the timer may be close to or past 3 seconds by the time the program is played. To keep this from happening the timer should be reset once the op mode is active. Grab the call
block. Use the drop down menu on the variable block to change the
to
.
Add the
to the op mode beneath the
comment and above the while loop.
As mentioned in previous sections, it can be beneficial to have a telemetry output when testing code. In the following example telemetry is used to output the amount of time that has passed with the timer.
The above code will allow your motor to drive straight for 3 seconds. Additional movements can be added by duplicating the while loop. Right click the while loop block and select duplicate
Once you have duplicated the while loop you can change some of the basic information like motor power or the time interval of the loop. You will also need to add a
block between the two loops.

Full Code Example