-
Notifications
You must be signed in to change notification settings - Fork 1
Using Timers in Blocks
This tutorial explains the basic use of Timers, for programming in Blocks. If you're in hurry (no time), skip to the .
Many new Blocks programmers use the Sleep command (MyOpMode.sleep) as their only control of time.
This is a very basic and common use of time, in a linear Op Mode used for Autonomous. It tells the robot to accept no new instructions for the specified time.
But what if other activities need to happen, while that motor runs? In Autonomous, for example, you might need to collect sensor data while the robot moves forward for 3 seconds. In TeleOp, you might need to continue driving with gamepads while a CR servo rotates for 2 seconds. The Sleep command will not allow this; "no new instructions".
In FTC Blocks, timer commands are found in menu at the left side, under Utilities, Time, ElapsedTime.
In general you will create your own timer, then use pre-made commands or methods to access the contents of that timer. This is not as simple as 'look at the clock' or 'look at the stopwatch' in real life. There are more steps, based on the structure and protections of Object-Oriented Programming (OOP). Java is an OOP language, and Blocks is built on Java.
The first step is to create a new timer object. In Java, an Object is an instance of a Class. FTC Blocks provides a class called ElapsedTime. A class contains variables and methods (commands). Your new timer is an instance or example of that class, inheriting its characteristics. Here we call the new timer object "myTimer". You could think of it as "My New Stopwatch", although you won't be stopping it.
Set myTimer to new Time.ElapsedTime
This is not the usual Blocks "set" command. Instead of assigning a number to a variable, this Block creates, or instantiates, a new object called myTimer. myTimer is an instance of the class ElapsedTime, which we see here is actually a subclass of a more general class called Time.
As soon as a new timer is created, it begins running from a starting time of zero. However you will probably not rely on that zero start value, because you will use the timer later in your Op Mode, when the time value has changed.
Now suppose your Op Mode is ready to use myTimer, probably sometime after 'waitForStart'. You don't know how much time has passed, so you might want to set the contents of myTimer to a definite starting value. Your choices are: zero, or the current system time (time since the Op Mode was initialized). The simplest choice is zero.
Description: Reset the specified elapsed time object.
This Block uses a method called "reset", from the class called ElapsedTime. It resets your new clock myTimer to zero. Your new clock will start running immediately and keep running. All you can do is read its changing time value.
It is very important to understand that "myTimer" is simply the name of the 'stopwatch', not the time value itself. It has the color purple and appears in Variables, but reading "myTimer" as a number will not give you the time.
Instead you must use a built-in method or command, to get or retrieve the contents of myTimer. Here is the simplest way.
Description: Return the elapsed time since the last reset of the specified elapsed time object in the resolution of the elapsed time object.
We can read this from right to left. This Block uses a method or command named "Time"; you can consider its real name to be "Get A Timer's Current Time Value". Here the method gets the value from myTimer, and passes, or returns, it to the left. On the left could be a variable to hold that number, or it could be another Block that accepts number input.
Here the variable "currentTime" holds the number of seconds or milliseconds that myTimer has been running, since it was last reset to zero. Finally you have the number you want, but remember that this number is fixed, not changing. It was the time value at the exact moment you checked myTimer.
Here the Telemetry Block builds a message showing the
.
← new ElapsedTime
Description: Create a new elapsed time object.
← new ElapsedTime startTime call System.nanoTime
Description: Create a new elapsed time object with seconds resolution and initialize it to the current system time or a value that you plug in in its place.
← new ElapsedTime resolution
Resolution [SECONDS or MILLISECONDS]
Description: Create a new elapsed time object with the current system time with a resolution of SECONDS or MILLISECONDS.
← ElapsedTime.StartTime timer
Description: Return the start time of the specified elapsed time object using the current resolution of the elapsed time object.
← ElapsedTime.Time timer
Description: Return the elapsed time since the last reset of the specified elapsed time object in the resolution of the elapsed time object.
← ElapsedTime.seconds timer
Description: Return the elapsed time in seconds since the last reset of the elapsed time object.
← ElapsedTime.Milliseconds timer
Description: Return the elapsed time in milliseconds since the last rest of the elapsed time object.
← ElapsedTime.Resolution timer
Description: Return the current resolution of the elapsed time object – either SECONDS or MILLISECONDS.
call ElapsedTime.reset timer
Description: Reset the specified elapsed time object.
call ElapsedTime.log timer label
Description: Log a message indicating how long the elapsed time object has been running using the indicated ,
← call ElapsedTime.toText timer
Description: Return a text string giving the elapsed time of the elapsed time object.