No announcement yet.

Motor stop at specific position.

  • Filter
  • Time
  • Show
Clear All
new posts

  • Motor stop at specific position.

    How would you write the code to make a dc motor stop at a specific position using an encoder? We have ran this code multiple times and can not seem to figure out the problem with it. When the code is ran no motors will work what so ever. the motor labeled middleMotor is the specific encoded DC motor we are having problems with. The motor is a Neverest 40.
    Attached Files

  • #2
    The first time through the code the joystick for middle motor is zero. With no power the motor never turns, the encoders never advance, and you're stuck in the while loop checking the encoder forever.

    Just get rid of the while statement altogether. Keep the if max set Power to zero.


    • #3
      Now that we changed that code our encoder will stop the motor but we want the motor to be able to go back in the opposite direction. We only want the middle motor to go so far in one direction but have unlimited rotations in the opposite direction.


      • #4

        if (middleMotor.getCurrentPosition() <= -999 && gamepad2.right_stick_y < 0) {


        • #5
          That does not seem to be working for us, could you possibly supply or direct us to a very simple opmode that would limit the encoder motor?


          • #6
            Can you post your current version


            • #7
              I looked at your code again. Once I saw that you set mid motor to run using encoders my brain read the run to position as referring to a different motor.

              The motor can only be in one mode at any time. If you choose run to position anytime the motor has power regardless of + or - the motor will drive to the target position. This is not what you want.

              Leave the mode for the mid motor in run using encoder. Then use this code for the mid motor and nothing else. (adjust encoder number as needed.)

              if (Arm.getCurrentPosition()< -100 && gamepad1.left_stick_y < 0 ) Arm.setPower(0);
              else Arm.setPower(gamepad1.left_stick_y);