Inverting the motors through software

When we ran the java code the motors were inverted so we tried to change its value to negative in software but it would not let us. We know we can just do it physically on the robot, but it is good to learn how to do it through software. We looked online and we came accross this:

public void setInvertedMotor(RobotDrive.MotorType motor, boolean isInverted)

How do we add that in our code to make the motors negative? Here is our code so far that does the basic drive and starts compressor.

CODE:

package edu.wpi.first.wpilibj.templates;

import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Gyro;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.DigitalInput;

/**

  • The VM is configured to automatically run this class, and to call the

  • functions corresponding to each mode, as described in the IterativeRobot

  • documentation. If you change the name of this class or the package after

  • creating this project, you must also update the manifest file in the resource

  • directory.
    */
    public class RobotTemplate extends SimpleRobot {

    Joystick leftstick = new Joystick(1);
    Joystick rightstick = new Joystick(2);
    Jaguar rearleft = new Jaguar(10);
    Jaguar rearright = new Jaguar(9);
    Jaguar frontleft = new Jaguar(8);
    Jaguar frontright = new Jaguar(7);
    Victor victor = new Victor(4,5);
    Compressor compressor = new Compressor(11,5);

    RobotDrive drivetrain = new RobotDrive(frontleft,rearleft,frontright,rearright);

    public void robotInit()
    {
    System.out.println(“Initializing robot”);
    compressor.start();//Start the compressor
    }

    public void autonomous(){
    System.out.println(“Entered the autonomous mode”);
    for(int i = 0; i < 4; i++){
    drivetrain.drive(-0.5, 0);
    Timer.delay(1.0);
    drivetrain.drive(0.0,0.75);

    }
    drivetrain.drive(0.0,0.0);
    

    }

    public void operatorControl(){
    System.out.println(“Entered the teleop mode”);
    while(isOperatorControl() && isEnabled()){
    drivetrain.arcadeDrive(leftstick, false);

    }
    

    }

}

use drivetrain.setInvertedMotor()
There are some default parameter is auto enters, so u can just edit that to the motor you need inverted

Right after this line:


RobotDrive drivetrain = new RobotDrive(frontleft,rearleft,frontright,rearright );

You would add code like this:


drivetrain.setInvertedMotor(RobotDrive.kFrontLeft, true);
drivetrain.setInvertedMotor(RobotDrive.kRearLeft, true);

(or use kFrontRight and kRearRight…just remember to invert both motors on the side!)