Need Help :) Issue with comparing encoder readings to double

Hi everyone, rookie team with little coding experience. Trying to set limits of our arm using the encoder on my neos. my high limit is -.7167 and resting position is 0. The encoder works, just not able to take its value and compare it to my high limit. For example: armEncoder > targetPosition, issue is that it can’t compare a relative encoder value with a double (arm encoder being the relativeEncoder and target position being a double value i made). Is there any way to convert the encoder reading to a double or make my target position double into the same datatype as the other one. I thought the encoder was supposed to return a double, but it doesn’t seem to. Ill include all my code down below. Sorry if its messy :slight_smile: Thanks in advance! Team 9474

package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.XboxController;
import com.revrobotics.RelativeEncoder;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkLowLevel.MotorType;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import java.math.BigDecimal;
public class Robot extends TimedRobot {
/**

  • This function is run when the robot is first started up and should be used for any
  • initialization code.
    */
    //Motor initilization!!
    //public CANSparkBase(int, device6 CANSparkLowLevel, )
    private CANSparkMax leftMotor2 = new CANSparkMax(4, MotorType.kBrushed);
    private CANSparkMax rightMotor1 = new CANSparkMax(3, MotorType.kBrushed);
    private CANSparkMax leftMotor1 = new CANSparkMax(2, MotorType.kBrushed);
    private CANSparkMax rightMotor2 = new CANSparkMax(1, MotorType.kBrushed);
    private CANSparkMax intakeMotor = new CANSparkMax(5, MotorType.kBrushless);
    private CANSparkMax armMotor = new CANSparkMax(6, MotorType.kBrushless);
    private RelativeEncoder armEncoder;
    private DifferentialDrive drive;
    private DifferentialDrive drive2;

//These will be used later for my whack acceleration thing!
public final double gainspeed = 0.5;
public final double gainturn = 0.5;
public double multiplier = 0;
public double multiplier2 = 0;
public double timer = 0;
public double targetPosition = 5.00;
public double restingPosition = 0.00;

XboxController controller = new XboxController(0);

@Override
public void robotInit() {
drive = new DifferentialDrive(leftMotor1, rightMotor1);
drive2 = new DifferentialDrive(leftMotor2, rightMotor2);

}

@Override
public void robotPeriodic() {}

@Override
public void autonomousInit() {
}

@Override
public void autonomousPeriodic() {

}
@Override
public void teleopInit() {

}

@Override
public void teleopPeriodic() {
double f_b = controller.getRawAxis(1);
double l_r = controller.getRawAxis(4);

// Drive the robot

drive.arcadeDrive(-l_r, f_b);
drive2.arcadeDrive(-l_r, -f_b);
if(controller.getLeftTriggerAxis() > .5){
intakeMotor.set(.28);}
else {intakeMotor.set(0);
}
armEncoder = armMotor.getEncoder();
SmartDashboard.putNumber(“Encoder Position”, armEncoder.getPosition());
if(controller.getRightTriggerAxis() >.8){
armMotor.set(.2);
}else if(armEncoder > targetPosition){

}
else{
armMotor.set(0);

}
if(controller.getAButton() == true){
armMotor.set(-0.2);

}else{
armMotor.set(0);
}

}

@Override
public void disabledInit() {}

@Override
public void disabledPeriodic() {}

@Override
public void testInit() {}

@Override
public void testPeriodic() {}

@Override
public void simulationInit() {}

@Override
public void simulationPeriodic() {}
}

armEncoder is an object so it can’t be compared to double. If you look at the relativeEncoder API, I think you’ll find a method that returns a double that corresponds to the position.

Your code will be much easier to read if you put triple backticks (```) at the beginning and the end to tell the forum not to format it.

Thanks, made this quickly, will try it out on Monday, hopefully it works

  armMotor.set(.2);
  double currentArmPosition = armEncoder.getPosition();
  if(currentArmPosition < -5.00){
    armMotor.set(0);
  }

}
  else{ 
     armMotor.set(0);

}
if(controller.getAButton() == true){
  armMotor.set(-0.2);
  double currentArmPosition = armEncoder.getPosition();
  if(currentArmPosition > 0){
    armMotor.set(0);
  }

}else{
  armMotor.set(0);
}```