PLEASE HELP! (Autonomous)

I’m not sure if it’s too much to ask but could someone help me find an example of a simple drive forward autonomous using sensors in java? I’m using vscode. Any and all help is appreciated. Thanks!

What kind of motor controller are you using? And where are you drive encoders (assuming that is the sensor you are referring to) plugged in - the RoboRio, or to a motor controller directly?

If you have encoders on the drive train plugged into the RoboRio, you can look at wpilib examples to get started.

https://docs.wpilib.org/en/latest/docs/software/sensors/encoders-software.html

This is the most basic type of “drive forward using a sensor”.

If you’re using a Talon SRX, with drive encoders plugged directly into the data port of the Talon, you can use sample code and instructions from CTRE as a starting point. You can do the same kind of logic as the wpilib example, but look into position closed-loop control modes once you’ve got the hang of the basics.

If it’s a Spark Max, they have similar examples for closed-loop control:

1 Like

Assuming you’re using Java, a pseudo-autonomous “bang-bang” could be as follows

@Override
pubic void autonomousInit() {
    sensor.reset(); // Make sure that the sensor is zeroed (if it is something like an encoder)
    timer.reset(); // A timer as a fail safe to not run too long
    timer.start();
}

@Override
public void autonomousPeriodic() {
    if(timer.get() <= 15.0) { // 15 seconds, the length of autonomous
        if(sensor.getDistance() <= targetDistance) { // Some arbitrary distance
            drive.tankDrive(0.5, 0.5); // Move forward at half speed
        } else {
            drive.tankDrive(0.0, 0.0); // Stop when you've gone at least that distance
        } 
}

This code assumes you have a WPILibj Timer and are using the timedRobot class.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.