How to code Tray at different speeds

So I have seen many teams that have a tray that goes forward slowly to place the cubes and returns to its original position much faster. I am confused as to how these teams are achieving two different speeds when moving the tray. Help will be much appreciated!!

I think it’s some squared function or something, dont quote me.
So squared function forward, no squared function backward



Then just change the percent


ok I will try that
(20 char)

Like @ButterNubs said, set the velocity of the motor to your desired slow speed for when you move it up, and then when you go down change it back so its fast.

I would also advise using some type of setpoints to make your placement more consistent. I believe to use the built in function is motor.rotateTo() in VexCode. That will make it so instead of your driver needing to let go every time, they can just hold the button down. It also can be used to compress two buttons down to one (on button press the tray goes to its placement position, else the tray goes back to its starting position).


How would the code look like for that?

1 Like

I would write a code so that if the motor reaches a certain degree then it changes the velocity

It might just go down faster because of gravity. Though you could code it to go one speed up and another speed down, that’s what my team does.

I don’t use VexCode very often so this example might be off, but I think its mostly correct. For reference I am using this VCS documentation site. I think VCS and VC-Text have the same functions, but I could be wrong.

This example assumes your tray motor needs to turn 45 degrees to reach its tray up position, that you want it to go up at 50 percent speed, your tray motor is called tray_m, and you want A to be the tray tilt button.

//If A is pressed, move the tray to its upright position. If A is not pressed, put the tray back down.
  tray_m.rotateTo(45, rotationUnits::deg, 50, velocityUnits::pct, false);
  tray_m.rotateTo(0, rotationUnits::deg, 100, velocityUnits::pct, false);

Also try not to confuse rotateTo with rotateFor. While similar, they do slightly different things (rotateFor is relative whereas rotateTo is absolute).

For extra fun, you could have the button check to see if the tray is in position. And if the tray is up, it will call a function so the robot automatically reverses the rollers and backs up.


That helps a lot!! Thank you! I will be able to implement this tomorrow.


So I was working on my aton, and I was wondering how to achieve my tray moving at two different speeds when I wanted it to in autonomous.

Instead of defining the speed at the beginning of autonomous just use this in the brackets to control its speed for that command

100, velocityUnits::pct

So I would put that code before my tray line to set the percentage to 100?

(I’m totally ripping this off from the post above but…)
Use it like how it’s used here for it to work

  tray_m.rotateTo(0, rotationUnits::deg, 100, velocityUnits::pct, false);
1 Like

Oh, ok I get it now! Thank you!

If you know what a p loop is, that’s what I use for my traytilt function.

What is a “P loop”? How do I use it?

A proportional loop is a control method that uses the idea of ‘error’. You take your setpoint (your target position), and subtract it by the motor’s current position. As you move away from your target, your error will grow larger. And as you get closer, your error gets smaller. If you set the motor’s speed to the value of your error, you would see your motor speed up and slow down depending on its positon.

The problem is that this error doesn’t work very well with the voltages/speeds the motor takes. So we multiply the error by a constant called kP (proportional). The value kP contains depends on how you want the motor to behave. If kP is too large, your motor will overshoot its target. When correcting for the overshoot, it will overshoot again. This repeated overshooting creates an oscillating pattern. If kP is too small, the motor might move slowly or it might not ever reach its target (as it won’t have enough power to push the motor). So we want kP to be tuned to a nice goldilox zone.

An easy way to tune kP is to set kP to 1. If it doesnt overshoot, increase kP. If it overshoots, you lower the value of kP. Rinse and repeat till you find a good value.

Better than just a P loop is a PID loop. It is a proportional loop, but it also has integral and derivative values. The integral helps move the motor to its position as it gets closer to the target. And the derivative helps lower the effects when you change the target. PID is what the motor.rotateTo() function uses to reach its target. The hold brake mode also uses PID to keep its position.