How can i set a limit on how far the talon srx can go up and down?

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot;

import com.ctre.phoenix.motorcontrol.ControlMode;
import com.ctre.phoenix.motorcontrol.NeutralMode;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
import edu.wpi.first.wpilibj.motorcontrol.MotorControllerGroup;
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);

WPI_TalonSRX window = new WPI_TalonSRX(10);
Servo Servo = new Servo(7);

Joystick gamePad = new Joystick(0);

// Store the original servo position in a variable
double originalPosition;

public void robotInit()
window.set(ControlMode.PercentOutput, 0);

originalPosition = Servo.getAngle();

//Inverted setting




public void robotPeriodic() {}

public void autonomousInit() {}

public void autonomousPeriodic() {}

public void teleopInit()


public void teleopPeriodic() {

window.set(ControlMode.PercentOutput, 0.9);
else if (gamePad.getRawButton(5)){
window.set(ControlMode.PercentOutput, -0.9);
else {
window.set(ControlMode.PercentOutput, 0);

if (gamePad.getRawButton(1)) {
// Rotate servo to angle
} else {
// Set servo angle to the original position

// Drive control - Arcade Drive
double leftStickY = -gamePad.getRawAxis(1);
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.3;
leftSpeed = Math.copySign(Math.min(Math.abs(leftSpeed), maxSpeed), leftSpeed);
rightSpeed = Math.copySign(Math.min(Math.abs(rightSpeed), maxSpeed), rightSpeed);

// set motor speeds

public void disabledInit()

// Set servo angle to the original position


public void disabledPeriodic() {}

public void simulationInit() {}

public void simulationPeriodic() {}

Howdy! is there a question here? Somthing not working right or just sharing?

I the future, chief has a nice code formatter

So your code is nice and formatted
like this without having to span multiple messages

Let us know if theres is something you were looking for!

1 Like

We need a lot more info to be able to help, talk us through the problem :slight_smile:


How can i set a limit on how far the talon srx can go up and down?

How can i set a limit on how far the talon srx can go up and down?

You are going to need some sort of sensor to know if are at a limit. Limit switches, an encoder, or a potentiometer might work.

When you say “up and down” is this an elevator?

Which motor is attached to this talon SRX?

Sorry i should’ve said forward and reverse also the motor its connected to is a car window motor. I dont have a physical limit switch so i was thinking about using a encoder.

Sure! So there are a lot of great encoders which you can use here, but the SRX Mag Encoder is pretty plug and play. You can read the encoder and stop the window motor once its moved a certain number of rotations!

Let me know if you are looking for greater detail.

1 Like