Coding the lift to move to a certain height automatically

Im using a DR4B this year and was thinking in my code I could create certain heights for my lift to automatically move to by pressing a button to make it rotate to a certain position. Nothing is happening when I am pressing th buttons. Is this an error in my code?

int i = 0;
void height_up(void) {

 i++;

  if (i > 0 && i < 4) {
    if (i == 1) {
      RLift.rotateTo(double(-129), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(88), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
    } else if (i == 2) {
      RLift.rotateTo(double(-859), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(787), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
    } else {
      RLift.rotateTo(double(-1420), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(1370), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
    }
  }

  else {
    i--;
  }
}

void height_down(void) {
  

  i--;

  if (i > 0 && i < 4) {
    if (i == 1) {
      RLift.rotateTo(double(-129), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(88), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
    } else if (i == 2) {
      RLift.rotateTo(double(-859), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(787), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
    } else {
      RLift.rotateTo(double(-1420), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(1370), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
    }
  }

  else {
    i++;
  }
}

To test the logic, I would put some print statements in the code to make sure the value of I isnt stuck out of range. If I works fine, test the rotateTo method to make sure that it works as intended. I don’t see anything wrong with the immediately.

2 Likes

Are you using RLift or LLift anywhere else in your code? If so you may be assigning it to stop continuously so that the functions are overruled each time through your while(1) loop.

1 Like

Perhaps use BrakeType(). It knows where to go to, just not when to stop.

We need to see how you called the function

2 Likes

I have a lne of code that says If my RJoy = 0 then it puts my Lift motors on hold. would that still effect this code?

Controller1.ButtonUp.pressed(height_up);
    Controller1.ButtonDown.pressed(height_down);

Made a ouple of adjustments, still nothing. I have Psudocoded the part of my code that holds my lift when I’mnot moving the joystick. Wrote print codes at the ends of each result and nothing displays. Tunning out of ideas. Any help would be great!

void height_up(void) {

  i++;

  if (i >= 0 && i < 4) {
    if (i == 0) {
      RLift.rotateTo(double(-129), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(88), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      Brain.Screen.printAt(1, 20, "Ground Level/n"); 
    } else if (i == 1) {
      RLift.rotateTo(double(-859), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(787), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
                 Brain.Screen.printAt(1, 20, "LOW TOWER/n");
    } else if (i == 2) {
      RLift.rotateTo(double(-859), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(787), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
                 Brain.Screen.printAt(1, 20, "MID TOWER/n");
    } else {
      RLift.rotateTo(double(-1420), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(1370), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
                 Brain.Screen.printAt(1, 20, "TALL TOWER/n");
    }
  }

  else {
    i--;
  }
}

void height_down(void) {

  i--;

  if (i >= 0 && i < 4) {
    if (i == 0) {
      RLift.rotateTo(double(-129), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(88), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      Brain.Screen.printAt(1, 20, "Ground Level/n"); 
    } else if (i == 1) {
      RLift.rotateTo(double(-859), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(787), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
                 Brain.Screen.printAt(1, 20, "LOW TOWER/n");
    } else if (i == 2) {
      RLift.rotateTo(double(-859), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(787), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
                 Brain.Screen.printAt(1, 20, "MID TOWER/n");
    } else {
      RLift.rotateTo(double(-1420), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      LLift.rotateTo(double(1370), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct);
      Lift.stop(vex::brakeType::hold);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
                 Brain.Screen.printAt(1, 20, "TALL TOWER/n");
    }
  }

  else {
    i++;
  }
}
Controller1.ButtonUp.pressed(height_up);
Controller1.ButtonDown.pressed(height_down);

Thank you

You have set up a situation where the two motors are fighting each other. Your rotateTo is a blocking method – the code will attempt to move only the right motor until it gets to the target, before going on to the next line of code. As written, you are asking the right motor to turn to a position while the left motor is (likely) set to hold.

A couple of thoughts:
You seem to have a motor group named Lift which is both of the lift motors. Right click on the definition of motor_group and see all of the things you can do with it. Some important ones related to your problem:
Lift.setStopping(hold); will let you tell the lift motors to always brake with brakeType::hold.
Lift.resetRotation(); will set all the encoders in the Lift motor_group to 0 at that moment. Do this before the while loop of user control and then you can use Lift.rotateTo(...) to move your lift and the two motors will work together.
I am not certain what you are trying to accomplish with the joystick control within this void, but it looks like it will lead to problems.
Use a bigger number for the row in your printAt(...) so that the text is low enough to be seen.
GL

4 Likes

Thank you. I will make those adjustments now. I am using the Joystick control to allow me full control after the Lift moves to the desired height. Is it unnecessary?

The answer is, it depends. What I think you are aiming to do would often be done with joystick control of the lift motors in the user control while loop. Also in that loop you might press buttons to increment your lift up or down with a task that is running simultaneously with the user control. You might want to research tasks.

If your user control is constantly calling these height_up/height_down voids, then you will never have joystick control of your lift. Each time the void is called, it will first see the rotateTo and try and make that happen – if you try to drive the lift away from that position with joystick input, it will fight you.

3 Likes

Ok. but if in the functions themselves, I have code to activate joystick control like this:

RLift.rotateTo(double(-954), vex::rotationUnits::deg, double(75),
                     vex::velocityUnits::pct, false );
      LLift.rotateTo(double(870), vex::rotationUnits::deg, double(75),          
                 vex::velocityUnits::pct, true);
      LLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
      RLift.spin(vex::directionType::fwd, (Controller1.Axis2.value()),
                 vex::velocityUnits::pct);
                 Brain.Screen.printAt(1, 40, "MID TOWER\n");

and I put the joystick control in a while loop. would that work do you think?

You are not giving me enough context to answer. The code bit above in inside a function? When and how is that function called?

I think you are saying you want to structure your lift control like this:

void liftMid(){
// rotateTos for right and left
// joystick control right and left
// print 
}
void usercontrol(){
while(true){
liftMid();
}
}

If that is your plan, it will not work, because each time liftMid is called, it will drive the motors to the rotateTo values, and keep doing so even as you try to drive the motors to some other position with the joysticks.

I’m very much guessing your intent here, and stand by the first paragraph of my previous response… that is what I think you want to do.

1 Like