I need help with servos and victor SP

I don’t know why the servo and the victor sp dot do anything when the buttons for each is pressed here is the code:

package frc.robot;

import com.ctre.phoenix.motorcontrol.NeutralMode;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
import edu.wpi.first.wpilibj.motorcontrol.VictorSP;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Servo;
import edu.wpi.first.wpilibj.TimedRobot;

/**

  • The VM is configured to automatically run this class, and to call the functions corresponding to
  • each mode, as described in the TimedRobot documentation. If you change the name of this class or
  • the package after creating this project, you must also update the build.gradle file in the
  • project.
    /
    public class Robot extends TimedRobot {
    /
    *
    • This function is run when the robot is first started up and should be used for any
    • initialization code.
      */
      WPI_TalonFX motorRight1 = new WPI_TalonFX(0);
      WPI_TalonFX motorRight2 = new WPI_TalonFX(1);
      WPI_TalonFX motorRight3 = new WPI_TalonFX(2);
      MotorControllerGroup groupRight = new MotorControllerGroup(motorRight1, motorRight2, motorRight3);

WPI_TalonFX motorLeft1 = new WPI_TalonFX(4);
WPI_TalonFX motorLeft2 = new WPI_TalonFX(3);
WPI_TalonFX motorLeft3 = new WPI_TalonFX(5);
MotorControllerGroup groupLeft = new MotorControllerGroup(motorLeft1, motorLeft2, motorLeft3);

VictorSP window = new VictorSP(3);
Servo exampleServo = new Servo(1);

//Controller
Joystick gamePad = new Joystick(0);

// Store the original servo position in a variable
double originalPosition = exampleServo.getAngle();

@Override
public void robotInit()
{
//Inverted setting
groupRight.setInverted(true);
groupLeft.setInverted(false);

motorLeft1.setNeutralMode(NeutralMode.Brake);
motorLeft2.setNeutralMode(NeutralMode.Brake);
motorLeft3.setNeutralMode(NeutralMode.Brake);
motorRight1.setNeutralMode(NeutralMode.Brake);
motorRight2.setNeutralMode(NeutralMode.Brake);
motorRight3.setNeutralMode(NeutralMode.Brake);

}

@Override
public void robotPeriodic() {}

@Override
public void autonomousInit() {}

@Override
public void autonomousPeriodic() {}

@Override
public void teleopInit() {}

@Override
public void teleopPeriodic() {

// Spin control - using two joysticks
double leftStickY = -gamePad.getRawAxis(4);
double rightStickY = -gamePad.getRawAxis(4);

// check if left joystick is held down and right joystick is up
if (leftStickY < -0.5 && rightStickY > 0.5) {
groupLeft.set(0.5);
groupRight.set(-0.5);
}
// check if left joystick is up and right joystick is down
else if (leftStickY > 0.5 && rightStickY < -0.5) {
groupLeft.set(-0.5);
groupRight.set(0.5);
} else {
// Drive control - Arcade Drive
double moveSpeed = -leftStickY; // inverted for proper control direction
double turnSpeed = gamePad.getRawAxis(4);

// adjust move speed for better control at lower speeds
moveSpeed = Math.copySign(Math.pow(moveSpeed, 2.0), moveSpeed);

// combine move and turn speeds
double leftSpeed = moveSpeed + turnSpeed;
double rightSpeed = moveSpeed - turnSpeed;

// limit motor speed to maximum allowed value
double maxSpeed = 0.8;
leftSpeed = Math.copySign(Math.min(Math.abs(leftSpeed), maxSpeed), leftSpeed);
rightSpeed = Math.copySign(Math.min(Math.abs(rightSpeed), maxSpeed), rightSpeed);

// set motor speeds
groupLeft.set(leftSpeed);
groupRight.set(rightSpeed);

 if(gamePad.getRawButton(5)){
window.set(1.0);

}
else if (gamePad.getRawButton(6)){
window.set(-1.0);
}
else {
window.stopMotor();
}

if (gamePad.getRawButton(6)) {
// Rotate servo to angle
exampleServo.setAngle(90.0);
} else {
// Set servo angle to the original position
exampleServo.setAngle(originalPosition);
}
}
}

@Override
public void disabledInit()
{
groupLeft.stopMotor();
groupRight.stopMotor();
}

@Override
public void disabledPeriodic() {}

@Override
public void simulationInit() {}

@Override
public void simulationPeriodic() {}
}

As for the obvious solutions, make sure the wiring is hooked up to the motor controller correctly, and that the buttons are mapped accordingly. If those have already been checked, then something needs to be done about the code. As for the code, it is possible that in the else block for the Victor SP motor speed controlled by buttons 5 and 6, stopMotor() is being called, which means that if neither button is being pressed, the motor will be stopped. It is also important to consider that something is wrong with the original position of the servo, and that therefore the code attempts to adjust the angle to a position not within its range of motion. Me personally, I don’t think either of the code sections would actually cause the behavior you described, but I have no idea what else could be amiss.

The infamous copy/paste/paste error instead of typing out each statement as desired. Forgetting to change the second paste makes pasting again almost not worth it.

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