Timers (Java)

I’m very unfamiliar with timers on Java programming, as I have only ever used the once or twice. I want to learn how to setup a timer (Command based). I have no clue on what to have in the import for the timer, nor do i know how to make the timer run a specific action (ex: driveMotor.setSpeed(0.8)) for a specific amount of time in seconds. Would someone be willing to help me with this, or send me bits of code that i can base my code off of?

Thanks.
~Robert Byrd, Java Programmer/Machinist/SOLIDWORKS Modeler/Electrician for Roboticus Team 6553

This is basically a duplicate of your previous thread. If the advice there doesn’t help you, the advice here likely won’t, either.

My previous thread was about subsystems and commands.

I’d recommend taking a look at the TimedCommand class

public class TimedDrive extends TimedCommand {
    
    public TimedDrive(String name, double timeout) {
        super(name, timeout);
    }
    
    public void execute(){
        //drive robot
    }
    
    public void end(){
        //set motor speed to 0
    }
    
    public boolean isFinished(){
        return super.isFinished();
    }
    
}

The double timeout that the constructor takes in is the amount of time that the command will run for, before ending.

2 Likes

To be fair, Oblarg’s previous post, as well as vkoutha’s answer, provided code in both the old and new command based frameworks, to accomplish your example task (running a motor for a fixed period of time). Neither solution required using Java timers.

If you’re looking for advice on using Java’s built in Timer class (which schedules blocks of code to run in the background), GeeksForGeeks has a decent tutorial with example code. This solution is much more heavy-handed than Oblarg’s, and is not a path I’d recommend for your example use case.

Additionally, WPIlib implemented a class named Timer in 2019. It provides raw time metrics (High resolution time-since-power-on from the FPGA, and low-resolution match time from the FMS), and some utility methods to act like a stopwatch. If you have a technical reason Oblarg’s solution isn’t sufficient, this may be another path to go down.

While programming in a TimedRobot class, the WPILIB timer is usually the way our team keeps track of time events.

Can you provide additional details as to which path you’re looking to pursue, and why (or why not)?

1 Like

Say for example, i wanted the robot to move for 5 seconds, stop and move forward 5 seconds again.
if (joystick.getRawButton(7)){
if(timer.get() < 5){
leftMotors.setSpeed(-0.5);
rightMotors.setSpeed(0.5);
}else{
leftMotors.stopMotor();
rightMotors.stopMotor();
}
}else{
timer.stop();
}

would i just put this code twice, like this?
if (joystick.getRawButton(7)){
if(timer.get() < 5){
leftMotors.setSpeed(-0.5);
rightMotors.setSpeed(0.5);
}else{
leftMotors.stopMotor();
rightMotors.stopMotor();
}if(timer.get() < 5){
leftMotors.setSpeed(-0.5);
rightMotors.setSpeed(0.5);
}else{
leftMotors.stopMotor();
rightMotors.stopMotor();
}
}else{
timer.stop();
}

Please do correct me if i am incorrect. (Using Java with Command Based Robot)

(Edit: sorry the code doesn’t show like it should. looks better in the Raw form.)

You can make Discourse display java code with correct formatting/syntax highlighting as follows:

```java
your code goes here
```

I’m going to reiterate my advice that you spend some time doing basic java tutorials - the code you’ve posted will not do what you think it will, for reasons that have very little to do with the library API and a lot to do with fundamental control-flow errors (it also does not use the command-based API at all).

Fundamentally, how do you imagine the code you’ve written executing?

I think of it as if i put this, enable code, and test it, this will happen. i sometimes go to MS Paint and draw out the mechanical and electrical and that will help me imagine it.

That’s not what I mean.

I mean, what do you expect the computer to do, line-by-line, when it executes the code you posted above?

You can’t write code with only high-level understanding and no low-level understanding. If you don’t have a specific mental model of the computer’s logic as it executes the code you’ve written, then you are not programming - you are merely copy-pasting and hoping.

To be honest, i have no clue. I’m just trying to translate the code from Iterative to Command. I’ve been trying to grasp the concept of timers, since my team still hasn’t used any in Command Robot form.

The WPILib Timer() class, as has been mentioned, has very little to do with the Command-based framework. There have been multiple examples of the functionality you’re trying to accomplish implemented using the available command-based tools both in this thread and in your previous one; you haven’t given us any indication that you have attempted to read/understand them, or that you’ve attempted to read/understand the command-based documentation that has also been linked.

If you have attempted to use those resources and do not understand them, I will suggest again that you spend some time becoming more comfortable with programming in general before attempting to work with robot code.

If you haven’t attempted to read/understand the resources people have given you, then they’ve wasted a fair amount of time/effort trying to help you and it is inconsiderate to continue asking for help until you do so.

1 Like

All I’ve asked for is simple help on how to make a button activate a timer that moves the robot. And, as stated earlier, I am still on a very pressing time crunch.

You were given approximately 90% of the code you need to do that in your previous thread. If you can’t plumb up that code to do what you want, then you do not have a very good chance of implementing the features you’ve described.

Your pressing time crunch notwithstanding, it is not pedagogically useful to have other people code your robot for you over the internet - it teaches you nothing, and there’s no guarantee the code will work given that none of us have access to your robot. From everything you’ve posted, it does not seem that you have the baseline level of programming knowledge required to write working robot code at this point. There’s no shame in that - everyone starts somewhere - but you need to focus on learning the basics first. No one can meaningfully learn calculus if they haven’t mastered arithmetic.

If you really need working code - even at the cost of not understanding any of it - your best bet is to find someone local who can spend some time with your robot to get it working for you.

1 Like

OP - You have to be very careful with your wording.

Emphasis mine. The title of this thread is “Timers (Java)”. In this and the previous thread, you’ve been asking about Timers, Iterative/Command programming, and websites. These questions have been confusing where your true gap in knowledge is.

What Oblarg has been hinting at is that you’re falling into the X/Y problem - you’ve pre-supposed a certain solution to your task, which may or may not be appropriate to the task itself.

To be fair - it’s very very hard to self-identify when you’re asking about Y, but really need to be asking about X. However, through the posts here, I’m pretty confident in saying: You’re barking up the wrong tree.

I’m asking you, please: Detatch yourself from the chosen solution. Talk about the task at hand.

You have stated:

Though there is no way I can code your whole robot for you, I am willing to provide you well-commented code to do this stated scope, if you can provide the following information:

  1. What port is the button plugged into on the roboRIO?
  2. Is the button “active high”, or “active low” (ie, what boolean values are seen in the code when the button is pressed, and not pressed?)
  3. What type of motor controllers are used for the motors? How many? What ports are they plugged into, or what CAN ID’s are they assigned?
  4. On the left side: does positive motor command make the wheels push the robot forward or backward?
  5. On the right side: does positive motor command make the wheels push the robot forward or backward?
  6. I assume that the motors should usually be all turned off, unless the button is pushed. Immediately after the button is pushed, they should run with a constant motor command for a fixed period of time. Is this correct? If so, what is that duration, and what is that motor command value?
  7. What should the robot do if the button is pushed while the motors are running? Does it need to change behavior?

Note: I provide this as a concise example of how to accomplish the stated task, and the comments and variable names chosen will be for educational purposes, so you can have a concrete example of how to accomplish your stated task. Though it will be functional, I will never sign up to say it is sufficient for any particular purpose or task or robot.

4 Likes

I’m not sure if you’re aware of this, but the documentation for WPILibJ 2019 is available here, and further documentation on how to use these classes and their methods are described here. I hope these resources can help you in a more timely manner

1 Like

Ha :smiley: