Revolute joints are confusing!

I'm trying to build a system that has a bucket that travels up, rotates 180 degrees, travels back down, rotates 180 degrees again, then starts over. I'm using a prismatic joint to get it to move in a straight line, which seems to be working fine, but getting the revolute joint working has been giving me a headache for about 3 hours now. The first time I run it, it works correctly, but once it's upside-down, trying to create a new revolute joint just fails miserably and I'm not sure why. This is what I have so far:


Code:
      final Vec2 prismaticAxis = new Vec2(0,-1);
      //The bucket starts out at the bottom
      final Vec2 bottomAnchor = new Vec2(bodyBase.getPosition());
      //We want to rotate around a point 2 units to the right of the center of the bucket
      bottomAnchor.x += 2;
      //And the top anchor is 12 units away from the bottom anchor
      final Vec2 topAnchor = new Vec2(bottomAnchor);
      topAnchor.y -= 12;

      bucketMoverDef = new PrismaticJointDef();
      bucketMoverDef.initialize(groundBody, bodyBase, bottomAnchor, prismaticAxis);
      //As per the top anchor, the translation limits are set up to be 12 units away from the starting point
      bucketMoverDef.lowerTranslation = -12;
      bucketMoverDef.upperTranslation = 12;
      bucketMoverDef.enableLimit = true;
      bucketMoverDef.maxMotorForce = 100;
      bucketMoverDef.motorSpeed = 3;
      bucketMoverDef.enableMotor = true;
      
      bucketMover = (PrismaticJoint) world.createJoint(bucketMoverDef);

      bucketRotatorDef = new RevoluteJointDef();
      bucketRotatorDef.enableLimit = true;
      bucketRotatorDef.maxMotorTorque = 100;
      bucketRotatorDef.motorSpeed = 3;
      bucketRotatorDef.enableMotor = true;

      movingStep = new Steppable() {
         private void switchToRotating(boolean atTop) {
            world.destroyJoint(bucketMover);
            removeSteppable(this);
            addSteppable(rotatingStep);

            //At this point, it's done traveling vertically and needs to start rotating.  I have no idea what to do here to make this work
            bucketRotatorDef.initialize(groundBody, bodyBase, atTop ? topAnchor : bottomAnchor );
            //Note that the reference angle is 0 the first time, but -pi the second time.  Not sure what that means or how to handle it
            bucketRotatorDef.upperAngle = atTop ? MathUtils.PI : MathUtils.TWOPI;
            bucketRotatorDef.lowerAngle = atTop ? -100 : -100;
            bucketRotator = (RevoluteJoint) world.createJoint(bucketRotatorDef);
         }
         public void step(float dt) {
            switch(bucketMover.m_limitState) {
               case AT_LOWER:
                  //We've reached the bottom of the system, start rotating to move up (and set up our own speed for later)
                  bucketMoverDef.motorSpeed = 3;
                  switchToRotating(false);
                  break;
               case AT_UPPER:
                  //We've reached the top of the system, start rotating to move down (and set up our own speed for later)
                  bucketMoverDef.motorSpeed = -3;
                  switchToRotating(true);
                  break;
            }
         }
      };
      rotatingStep = new Steppable() {
         private void switchToMoving(boolean atTop) {
            //This section seems to work fine
            world.destroyJoint(bucketRotator);
            removeSteppable(this);
            addSteppable(movingStep);

            bucketMoverDef.initialize(groundBody, bodyBase, atTop ? topAnchor : bottomAnchor, prismaticAxis);
            bucketMover = (PrismaticJoint) world.createJoint(bucketMoverDef);
         }
         public void step(float dt) {
            //The limitState seems pretty much random approx. 100% of the time after the first successful rotation.
            switch (bucketRotator.m_limitState) {
               case AT_UPPER:
                  switchToMoving(bodyBase.getPosition().y < 7);
                  break;
               case AT_LOWER:
                  switchToMoving(bodyBase.getPosition().y < 7);
                  break;
            }
         }
      };
      addSteppable(movingStep);


For some reason, it's somehow rotating to a new angle instantly, regardless of the speed or maximum torque. Help?
> //Note that the reference angle is 0 the first time, but -pi the second time

True, but when you create a joint the current relative angle between the bodies is taken to be zero when it comes to specifying the limits.

If the bucket keeps rotating in the same direction all the time (I couldn't tell from your description) then the limits don't actually change, because the new starting point is now zero as far as the limits are concerned.
bucketRotatorDef.upperAngle = MathUtils.PI;
bucketRotatorDef.lowerAngle = 0;//the position when joint was created

But if the bucket is supposed to rotate back to the original position before coming down, it would be something like:
bucketRotatorDef.upperAngle = atTop ? 0 : MathUtils.PI;
bucketRotatorDef.lowerAngle = atTop ? -MathUtils.PI : 0;

It sounds like you are saying the starting point is always one of the limits, so one of the limits should be zero for any case.

Having said that though, you will probably find in practice that the body is not perfectly at the 0/180 degree position when it gets to the limit, especially if you have fast moving joints or powerful motors the body can go a bit past the limit. In most cases it's such a small offset that it's hard to see, but if you simply take the current rotation as the starting point when making a new joint, over time the limits would creep to a position which might be visibly different from where they should be. If this becomes a problem you'll need to set the bucket to the perfect 0/180 degree rotation before creating the new rotation joint.
Thanks, I finally got it!

Original thread: https://box2d.org/forum/viewtopic.php?t=7356