Code help please

Hey guys, I am trying out some code, but I am coming across an issue and VSCode won’t stop giving me this issue ;-; I can’t figure out why it keeps saying “The method run(() → {}) is undefined for the type Drivetrain”

Here is my code for Drivetrain.java:

package frc.robot.subsystems;

import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel;
import com.revrobotics.RelativeEncoder;

import frc.robot.Constants;

public class Drivetrain extends SubsystemBase {
  CANSparkMax leftFrontMotor = new CANSparkMax(Constants.DrivetrainConstants.leftFrontCANID,
      CANSparkMaxLowLevel.MotorType.kBrushless); // (port number--accessed using rev robotics CAN identification tool,
                                                 // type of motor)
  CANSparkMax rightFrontMotor = new CANSparkMax(Constants.DrivetrainConstants.rightFrontCANID,
      CANSparkMaxLowLevel.MotorType.kBrushless);
  CANSparkMax leftBackMotor = new CANSparkMax(Constants.DrivetrainConstants.leftBackCANID,
      CANSparkMaxLowLevel.MotorType.kBrushless);
  CANSparkMax rightBackMotor = new CANSparkMax(Constants.DrivetrainConstants.rightBackCANID,
      CANSparkMaxLowLevel.MotorType.kBrushless);

  RelativeEncoder leftEncoder = leftFrontMotor.getEncoder();
  RelativeEncoder rightEncoder = rightFrontMotor.getEncoder();

  MotorControllerGroup leftControllerGroup = new MotorControllerGroup(leftFrontMotor, leftBackMotor);
  MotorControllerGroup rightControllerGroup = new MotorControllerGroup(rightFrontMotor, rightBackMotor);

  DifferentialDrive differentialDrive = new DifferentialDrive(leftControllerGroup, rightControllerGroup);

  public Drivetrain() {
    leftEncoder.setPosition(0);
    rightEncoder.setPosition(0);

    leftBackMotor.follow(leftFrontMotor);
    rightBackMotor.follow(rightFrontMotor);

    leftControllerGroup.setInverted(true);
    leftControllerGroup.setInverted(false);
  }

  public Command drive(double xSpeed, double zRotation) {
    return run(() -> {
      differentialDrive.arcadeDrive(xSpeed, zRotation);
    });
  }

  @Override
  public void periodic() {
    // This method will be called once per scheduler run
  }
}

I’m guessing you want to find the class with the run method you’re looking for in it and then do import static ... to use run() in your code without specifying the class.

run(Runnable) is present in SubsystemBase, so that shouldn’t be giving you the problem.

Could you post the entire context of your error message? i.e., where is it appearing, what is the surrounding text, etc?

1 Like

It’s just the run() keyword itself. It’s happening in subsystems/Drivetrain.java. It says: “The method run(() → {}) is undefined for the type DrivetrainJava”

Yes, but where does the message appear?

Does it appear under PROBLEMS at the bottom? Is it giving you a red underline?

Does it appear under TERMINAL at the bottom, and if so, could you post the entire contents of that tab?

Ohh yes PROBLEMS tab with red underline

Wait is it possible that it is because the project year is 2022?

Yes, run(Runnable) was not added to SubsystemBase until after the 2022.4.1 release. You should probably upgrade: WPILib Installation Guide — FIRST Robotics Competition documentation

1 Like

Ohhh ok thanks!!! If that’s the case, should I restart my project?

Opening it in the latest version of WPILib VS code should automatically prompt you to update the project

image

3 Likes

YUP IT WORKED! I’m so happy lol

1 Like

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