Announcement

Collapse
No announcement yet.

Limiting motor movements with encoders

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Limiting motor movements with encoders

    Is there a way to use the encoders to say that if it reaches a certain point, it cannot go any further? For example, it could have a range of 0-12000 or some other value. Our purpose for this is to ensure a linear slide stays on track. Thank you!

  • #2
    Originally posted by mathew View Post
    Is there a way to use the encoders to say that if it reaches a certain point, it cannot go any further? For example, it could have a range of 0-12000 or some other value. Our purpose for this is to ensure a linear slide stays on track. Thank you!
    Sure, let's say you have a method called setSlidePower, like the following:
    Code:
    public void setSlidePower(double power)
    {
        if (power < 0.0 && slideMotor.getPosition() > 0 || power > 0.0 && slideMotor.getPosition() < 12000)
        {
            // We only allow the motor to move if we are retracting and encoder is still positive or if we are extending and encoder has not
            // exceeded 12000.
            slideMotor.setPower(power);
        }
        else
        {
            slideMotor.setPower(0.0);
        }
    }
    
    public void loop()
    {
        ...
        setSlidePower(gamepad2.left_stick_y);
        ...
    }

    Comment


    • #3
      Originally posted by mikets View Post
      Sure, let's say you have a method called setSlidePower, like the following:
      Code:
      public void setSlidePower(double power)
      {
          if (power < 0.0 && slideMotor.getPosition() > 0 || power > 0.0 && slideMotor.getPosition() < 12000)
          {
              // We only allow the motor to move if we are retracting and encoder is still positive or if we are extending and encoder has not
              // exceeded 12000.
              slideMotor.setPower(power);
          }
          else
          {
              slideMotor.setPower(0.0);
          }
      }
      
      public void loop()
      {
          ...
          setSlidePower(gamepad2.left_stick_y);
          ...
      }
      Ah, that makes sense. The thing that i was mainly confused about was the if statement, and how to allow the power to become greater than 0 again after it has reached its distance threshold. I will be sure to try this tomorrow. Thank you!

      Comment


      • #4
        Originally posted by mathew View Post
        Ah, that makes sense. The thing that i was mainly confused about was the if statement, and how to allow the power to become greater than 0 again after it has reached its distance threshold. I will be sure to try this tomorrow. Thank you!
        This is basically "software limit switches".

        Comment

        Working...
        X