Log in

View Full Version : Packages/Importing problem with FRC Java


TeamMarcies
06-01-2015, 20:14
So currently I am trying to use the following VALID java statement:

public Executor executor = Executors.newSingleThreadExecutor();

It requires these two imports:

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

which require this package:

package java.util.concurrent;

Which isn't found according to Netbeans but is found when I am working with a default java project.

Is this simply not allowed to be used?
I am wanting to use threads in autonomous mode and through that, use Executors that will allow threads to be placed in queue so I can have them executed in the order I have threads be executed in.

Also, are threads allowed to be used?

Ben Wolsieffer
06-01-2015, 20:17
Is this code for the roboRIO, and if so, how did you set up your project for Netbeans. If you manually set up the project for Netbeans that might have something to do with it.

TeamMarcies
06-01-2015, 20:24
We are using the new 2015 roboRIO.

I've been programming for the team for 2 years and I've just decided to go ahead and clean up our messy code and start using threads to make things easier.

I have set up the project properly.

File -> New Project - > Java FRC -> IterativeRobot

I know it's working because it's recognizing the FRC java API which I am knees deep in with the code. It's just this problem where I can't seem to import a java utility I need for threads.

Team3266Spencer
06-01-2015, 20:43
Just use the command-based template. No reason to rewrite the wheel.

Ben Wolsieffer
06-01-2015, 20:45
Are you using Netbeans? Netbeans is not officially supported anymore, so I suspect you are either trying to use the 2014 plugins or you have something wrong with how you tried to integrate the 2015 API into Netbeans.

I would try using Eclipse to test and make sure your code works in the official environment.

Ben Wolsieffer
06-01-2015, 20:51
Just use the command-based template. No reason to rewrite the wheel.

I also recommend using the command-based system. It gives you a lot of the advantages of multithreaded code without worrying about race conditions, deadlocks and other threading weirdness. You will also be able to get more help from other teams.

TeamMarcies
06-01-2015, 20:52
Alright, thanks!

I'll start transitioning to Eclipse and let you know the results.

Ben Wolsieffer
06-01-2015, 20:54
You've probably already seen this, but in case you haven't, this will help with the installation and setup: http://wpilib.screenstepslive.com/s/4485/m/13809/l/145002-installing-eclipse-c-java

TeamMarcies
06-01-2015, 23:09
Yep! Eclipse did the trick. Thanks guys. I'll be sure to check out the command-based programming - it seems neat.

Just one quick question: Am I able to do a while(true) loop in a thread and have it persistently be doing something or will it crash the network/roboRIO?

Basically what I'm doing here:

public void run() {
R.autonomousCounter = 0;
while (true) {
if ((Math.floor(R.autonomousCounter / loopsPerSecond)) < seconds) {
if (forward) {
theRobot.drive(-robotDriveSpeed, gyro.getAngle() * gyroModifierSpeed);
}else if (!forward) {
theRobot.drive(robotDriveSpeed, gyro.getAngle() * gyroModifierSpeed);
}
}else{
break;
}
}
theRobot.drive(0.0, 0.0);
}

Team3266Spencer
06-01-2015, 23:16
Yep! Eclipse did the trick. Thanks guys. I'll be sure to check out the command-based programming - it seems neat.

Just one quick question: Am I able to do a while(true) loop in a thread and have it persistently be doing something or will it crash the network/roboRIO?

Basically what I'm doing here:

public void run() {
R.autonomousCounter = 0;
while (true) {
if ((Math.floor(R.autonomousCounter / loopsPerSecond)) < seconds) {
if (forward) {
theRobot.drive(-robotDriveSpeed, gyro.getAngle() * gyroModifierSpeed);
}else if (!forward) {
theRobot.drive(robotDriveSpeed, gyro.getAngle() * gyroModifierSpeed);
}
}else{
break;
}
}
theRobot.drive(0.0, 0.0);
}

Just do it this way:

public void run() {
R.autonomousCounter = 0;
while ((Math.floor(R.autonomousCounter / loopsPerSecond)) < seconds) {
if (forward) {
theRobot.drive(-robotDriveSpeed, gyro.getAngle() * gyroModifierSpeed);
}else if (!forward) {
theRobot.drive(robotDriveSpeed, gyro.getAngle() * gyroModifierSpeed);
}
}
theRobot.drive(0.0, 0.0);
}

I still wouldn't be worrying with threads though.

cgmv123
06-01-2015, 23:17
Am I able to do a while(true) loop in a thread and have it persistently be doing something or will it crash the network/roboRIO?

Add
Timer.delay(0.005);
at the end of your loop and you should be fine.

TeamMarcies
06-01-2015, 23:46
Greatly appreciate the help guys, thanks!