View Full Version : Java Robot Template
2185Bilal
02-06-2012, 20:12
Hey,
We just switched from Labview Programming to Java.
And i have a question:
Which template is the best to program or wat are the benefits and disadvantages
by templates i mean the java robot templates (ie Simple Robot Template, iterative robot template, command based template)
Thanks very much
AlexD744
02-06-2012, 21:48
I've had experience with both the iterative template and the command-based template. I've only glossed over the simple template. However, I would say that the command-based template was extremely easy to use once you got the hang of it. It was a weird switch from iterative, but it made autonomous programming go so much faster with the addParallel and addSequential commands. WaitCommand was also really handy to have, instead of having to creat your own. However, I would practice with it before using it in season, just because it's different and takes a little getting used to. At least it did for me.
As far as iterative goes, it's not too different from command based, except there's a lot more grunt work involved in creating autonomous, including a state machine. However, it's still doable and a great template. And I guess to be fair, the command based template has a lot more grunt work in creating classes, but I found that easier than creating a state machine.
I don't know much about simple robot.
If you haven't you should peruse some of the documentation that FIRST has, including the cookbook and the getting started guide.
2185Bilal
03-06-2012, 09:54
Thank u, I want to learn the iterative robot template, but i cant find any manuals or reference guides to learn from. Would some one plz help me and suggesting Any documentation :cool:
I would start at this link:
http://firstforge.wpi.edu/sf/docman/do/listDocuments/projects.wpilib/docman.root.c_and_java_documentation
Starting with this document:
Java Getting Started guide
Then this one:
WPI Robotics Library Users Guide
Lastly, this one:
2012 WPILib Cookbook
Good luck!
If you have any questions, just post here!
2185Bilal
03-06-2012, 15:37
Thank very much...
So I have decided to use the Iterative Robot Template.
But I have a question:
I created a new method with this code:
public void shooter ()
{
}
Now how do i call this method in the telopInit() method
Thanks in advance
You would be better off creating a new class for the "Shooter" object and have that contain such methods as "shoot()" and "aim()". For example:
public class Shooter {
public Shooter() {
//initialization code goes here
}
public void shoot() {
//code goes here
}
public void aim() {
//code goes here
}
}
Then in your main class:
public class Main extends RobotMain {
Shooter shooter;
public void robotInit() {
shooter = new Shooter();
}
public void teleopPeriodic() {
shooter.aim();
shooter.shoot();
}
}
Calling a method in teleopInit() is not recommended because it is only called once, when the robot enters teleop period. You would be better off calling it in teleopPeriodic, which updates about every 50 ms, or in teleopContinuous, which updates as fast as possible.
You should also take a look at the Java tutorials (http://docs.oracle.com/javase/tutorial/java/index.html) Oracle has on their website to learn the basics of Java.
2185Bilal
03-06-2012, 21:11
Thanks you very much
Now i have one more question how do i set the speed of a motor with a joystick
This is what i have right now:
package bilal.robotics.code;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.ButtonType;
public class Shooter
{
Jaguar shooter = new Jaguar(5);
Joystick controller = new Joystick(2);
final int buttonA = 1;
public Shooter()
{
}
public void shoot(){
if(controller.getRawButton(buttonA)) {
shooter.set();
}
}
}
I want the shooter motor to run according to the value of axis Y on the joystick when button A is pressed
Well, you would need to define a "setSpeed()" method, something like this:
public void setSpeed(double speed) {
shooterJaguar.set(speed);
}
I would recommend that you change the name of your jaguar to something like "shooterJag" or at least not "shooter" which is the name of your class - it could get confusing.
Now, on to your question. The Joystick class has getX(), getY(), and getZ() methods that return values between -1 and 1 based on the Joystick's movement on those axes. A simple way to do it would be
setSpeed(controller.getY());
but it would spin the shooter backwards if you moved the joystick backwards on the y-axis. A good way to do this would be
setSpeed(Math.abs(controller.getY()));
or
setSpeed(controller.getY() < 0 ? 0 : controller.getY());
The first snippet spins the shooter no matter what value the joystick is sending out -- if it goes into the negative range, it just uses the absolute value as if you were moving it in the positive range.
The second snippet will only spin the shooter if the joystick is in the positive range. If it is returning values that are less than zero, it will set the shooter speed to zero, else it will use the value that it's giving.
Also, you should have an else statement in your "shoot()" method that resets the speed to zero because Jaguars will keep running at the last speed they have been set to. If you don't have this, it will keep spinning at the speed it had been set to when you take your finger off of buttonA. It's really easy to do this -- just add this to the end of the if statement in the "shoot()" method.
} else {
setSpeed(0);
}
So your class would look something like this:
package bilal.robotics.code;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Joystick.ButtonType;
public class Shooter {
Jaguar shooterJaguar = new Jaguar(5);
Joystick controller = new Joystick(2);
final int buttonA = 1;
public Shooter() {
}
public void setSpeed(double speed) {
shooterJaguar.set(speed);
}
public void shoot() {
if(controller.getRawButton(buttonA)) {
setSpeed(controller.getY() < 0 ? 0 : controller.getY());
} else {
setSpeed(0);
}
}
}
2185Bilal
06-06-2012, 22:23
Thank you very much
Would you please help with my other thread, named "Code and Deployment Help". No one has seemed to have answered it. ::rtm:: :eek: :confused: :)
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.