Hello, does anyone have any tips for building a tank drive code?
Are you using Command based or a timed robot? and what language?
Do the zero to robot! Introduction — FIRST Robotics Competition documentation
We are using Java with a Command Based Robot.
Okay, thanks for the tip! I’ll check it out.
There are some examples for how to use the WPILib DifferentialDrive class to program a tank drive.
Here is arcade drive. With arcade drive, one joystick axis is used for driving forward and backward while another axis is used for steering: allwpilib/Robot.java at main · wpilibsuite/allwpilib · GitHub
The above code is okay for getting started, but more changes are necessary to make it effective. First thing is that you will likely need to add two motors - one for each side - because most tank drivetrains have two motors per side. You will need to make those extra motors “follow” the existing ones.
The tip that I always give teams is to use a controller like an Xbox controller or Logitech gamepad that has 2 joysticks on it. This is because many students are familiar with this kind of controller.
Next, my tip is to use something known as Split Arcade, which is talked about a fair amount in this forum. With Split Arcade, the left stick of the controller is used for forward-back movement and the right stick of the controller is used for turning. This is very similar to controls for many first person shooter games, minus the strafing that is impossible with tank drive. Also with split arcade, it is possible to easily drive forward and backward in a straight line, and also possible to turn on a dime, because forward-back movement and turning are not on the same stick.
My final tip is to make sure your drivetrain motors are in brake mode. This will mean that the motors will try to resist movement if they are given a non-moving input. This is crucial for not rolling off the charge station this year - I’ve watched several attempted balances by tank drive robots where they immediately fell off the charge station while balancing because their drivetrain motors were not in brake mode. The magic of brake mode is such that even while the robot is disabled, the motors resist movement very effectively.
But let’s go back to the basics. What motor controllers are you using? Brushless motor controllers like Spark Maxs and Falcons have their own libraries that you may have already downloaded. These libraries have documentation with examples as well, and they can be valuable for your efforts.
Something a lot of people overlook is that controller input usually has to be inverted. For example, if you’re using an XBoxController with the left stick for forward/backward and the right stick to rotate:
var forwardSpeed = -xboxController.getLeftY();
var rotationSpeed = -xboxController.getRightX();
The forward speed is inverted because an XBoxController returns a negative value when you push it up. The rotation is inverted because WPILib uses CCW rotation as positive.
In this case, I would recommend checking out 319s codebase, they do a lot of things well and the code is well commented
On the topic of controller inputs, one general tip (not tank drive specific) to gain finer control of your robot at slow speeds is to square your controller axes inputs. For instance
var yInput = Math.abs(controller.getLeftY()) * controller.getLeftY();
Watch this epic youtube channel FRC 0 to Autonomous - YouTube
SlewRateLimiters are also something to look into, not as a replacement for exponential control but in addition to it. Especially if your robot can buck with acceleration.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.