Hi guys
I need help with programming the standalone, I’m using java and our robot is a tank drive. I would like to know the simplest way to do this
As a general comment: Autonomous is the time when you write software to automatically make the robot do something, without requiring driver input. In a very simplified sense, you write additional code with emulates the inputs a driver would have given to do the same task.
My favorite easy autonomous algorithm:
- Turn drivetrain motors on at 25% power for 2 seconds
- Turn drivetrain motors off.
5 points, easy! In 2018, this was a ranking point!
If you’ve never done auto before, I suggest starting with something like that. Then grow from there!
FRC 0 to Autonomous - YouTube might be of interest too.
Not to derail the thread but in 2018 you had to score a cube in the switch as well as have all 3 robots move in auto to get the autoquest.
Look into the command-based framework. It will make it much easier to build sensible (and maintainable) autonomous routines, and will allow you to reuse code between autonomous and teleoperated modes.
All good, and technically yes. All I was going for was “even simple is valuable”.
This is simple code we used to run 9330 at competitions, it includes an open loop autonomous mode we wrote in Java (shoots 3 balls in autonomous, plays defense & climbs). Tank drive using split arcade for control. Used java. There’s a lot here you can probably straight up copy.
Main.java:
package frc.robot;
import edu.wpi.first.wpilibj.RobotBase;
/**
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
* you are doing, do not modify this file except to change the parameter class to the startRobot
* call.
*/
public final class Main {
private Main() {}
/**
* Main initialization function. Do not perform any initialization here.
*
* <p>If you change your main robot class, change the parameter type.
*/
public static void main(String... args) {
RobotBase.startRobot(Robot::new);
}
Robot.java:
package frc.robot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.can.TalonFX;
public class Robot extends TimedRobot {
// Some easy constants for joystick button numbers for Logitech F320
public static final int A = 1;
public static final int B = 2;
public static final int X = 3;
public static final int Y = 4;
public static final int LB = 5;
public static final int RB = 6;
public static final int BACK = 7;
public static final int START = 8;
public static final int LT = 2;
public static final int RT = 3;
// Left and right drivetrain motors
// Requires CTRE Phoenix framework, need to set CAN IDs through Phoenix Tuner
TalonFX[] left1 = new TalonFX[] { new TalonFX(1)};
TalonFX[] right1 = new TalonFX[] { new TalonFX(3)};
TalonFX[] left2 = new TalonFX[] { new TalonFX(2)};
TalonFX[] right2 = new TalonFX[] { new TalonFX(4)};
//climber has been set to break mode through the Phoenix Tuner
TalonFX[] climber = new TalonFX[] { new TalonFX(5)};
TalonFX[] shooter = new TalonFX[] { new TalonFX(6)};
TalonFX[] shooterIntake = new TalonFX[] { new TalonFX(7)};
Joystick driverJS = new Joystick(0);
Joystick operatorJS = new Joystick(1);
Timer timer = new Timer();
double intakeSpeed = 0;
double shooterSpeed = 0;
@Override
public void robotInit() {
}
@Override
public void teleopPeriodic() {
// Get joystick values
double throttle = driverJS.getRawAxis(1);
double turn = driverJS.getRawAxis(4);
// Deadband
if ( Math.abs(throttle) < 0.1 )
throttle = 0;
if ( Math.abs(turn) < 0.1 )
turn = 0;
// If throttle is non-zero, scale turning by throttle
if ( Math.abs(throttle) > 0.05 )
turn *= Math.abs(throttle);
// Calculate left and right sides
double l = throttle + turn;
double r = throttle - turn;
// Set every motor controller
for (TalonFX s : left1 )
s.set(ControlMode.PercentOutput, l);
for (TalonFX s : left2 )
s.set(ControlMode.PercentOutput, l);
for (TalonFX s : right1 )
s.set(ControlMode.PercentOutput, -r);
for (TalonFX s : right2 )
s.set(ControlMode.PercentOutput, -r);
// Shooter intake speed
if ( operatorJS.getRawButton(A) )
intakeSpeed = -0.3;
// Off
else if ( operatorJS.getRawButton(B) )
intakeSpeed = 0;
// Shooter on
if ( operatorJS.getRawButton(X) )
shooterSpeed = -1;
// Off
else if ( operatorJS.getRawButton(Y) )
shooterSpeed = 0;
for ( TalonFX s : shooterIntake )
s.set(ControlMode.PercentOutput, intakeSpeed);
for ( TalonFX s : shooter )
s.set(ControlMode.PercentOutput, shooterSpeed);
// Left stick Y is climber
double climberSpeed = -operatorJS.getRawAxis(1);
// Climber JS deadband
if ( Math.abs(climberSpeed) < 0.15 )
climberSpeed = 0;
for ( TalonFX s : climber )
s.set(ControlMode.PercentOutput, climberSpeed);
}
@Override
public void autonomousInit() {
timer.reset();
timer.start();
}
@Override
public void autonomousPeriodic() {
//spin up shooter
if (timer.get()<2){
for ( TalonFX s : shooter )
s.set(ControlMode.PercentOutput, -1);
}
//turn on feed to shooter
else if(timer.get()<4){
for ( TalonFX s : shooterIntake )
s.set(ControlMode.PercentOutput, -0.3);
}
//rotate about 90 degrees
else if(timer.get()<4.5){
for (TalonFX s : left1 )
s.set(ControlMode.PercentOutput, -.5);
for (TalonFX s : left2 )
s.set(ControlMode.PercentOutput, -.5);
for (TalonFX s : right1 )
s.set(ControlMode.PercentOutput, -.5);
for (TalonFX s : right2 )
s.set(ControlMode.PercentOutput, -.5);
}
//drive off the line
else if(timer.get()<5.1){
for (TalonFX s : left1 )
s.set(ControlMode.PercentOutput, .5);
for (TalonFX s : left2 )
s.set(ControlMode.PercentOutput, .5);
for (TalonFX s : right1 )
s.set(ControlMode.PercentOutput, -.5);
for (TalonFX s : right2 )
s.set(ControlMode.PercentOutput, -.5);
}
//turn all the motors off
else{
for (TalonFX s : left1 )
s.set(ControlMode.PercentOutput, 0);
for (TalonFX s : left2 )
s.set(ControlMode.PercentOutput, 0);
for (TalonFX s : right1 )
s.set(ControlMode.PercentOutput, 0);
for (TalonFX s : right2 )
s.set(ControlMode.PercentOutput, 0);
for ( TalonFX s : shooterIntake )
s.set(ControlMode.PercentOutput, 0);
for ( TalonFX s : shooter )
s.set(ControlMode.PercentOutput, 0);
}
}
}
+1 for zero to autonomous. @SeanSun6814 is a wonderful human being and his content is great!
Best advice. Learned the new Command Based robot code and autonomous mode would literally be just issuing a set of commands during the autonomous period. You can then pretty much use the exact same command(s) [groups] in Teleop mode figure out what sequences you need for autonomous mode. Only differences are when your inputs are coming from that’s all.
thank you guys, we were able to start our autonomous routine using the time
if you have any more ideas, i’ll be grateful
One very specific tip: Add the ability to delay your start of autonomous by some number of seconds. It occasionally comes in handy when making sure an alliance of three robots doing simultaneous autonomous things won’t run into each other.
But more generally:
Start with analysis of the game manual. Figure out what gets lots of points.
Spend a bit of time thinking about what aspects of robot design would make autonomously getting points easy. The less software and mechanical complexity, the better.
Once you’ve got your basic mechanical characteristics, start breaking down the task of “score autonomous points” into sub-steps. Things like “drive forward”, “rotate left 30 degrees”, “follow S-shaped path”, “spool up shooter wheel”, “launch power cells”, etc.
For each sub-task, do some thinking on how you might accomplish each with software. Various sensors, Trajectory following, Vision Processing, and many other tools are available - ask around here if you’re not sure how to best accomplish some specific sub-step.
Mount hardware and write code (probably commands?) that uses those tools to move the robot.
Get it to work. Run it many many times. Get it super robust! Write many different autonomous routines, and add some mechanism to select which one you run!
Hey, i need some help with programming sequential commands in the autonomous. If you have any sample code. I will be grateful
If you are using command-based, this may help: Command Groups — FIRST Robotics Competition documentation.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.