Help With P Loop Please!

Hello, my team and I have been wondering how to accomplish making our stacking for the cubes faster. We’ve been brainstorming and we came across with making a P loop for our tray motor so that as the degrees changes and they get closer to dropping off the cubes, it’ll go slower to make it easier. The truth is I have no idea how to code that, any help would be greatly appreciated. Thank you!

Not sure if this is a P loop, but what you’re looking for might look something like this:
tilterMotor = abs(tilterPosition - targetPosition);
This basically will make it go slower as it gets closer because the difference between the position and target will get smaller.

error = target position - current tilter position
power = error * Kp
while(current tilter position < target position){
    error = target position - current tilter position
    power = error * Kp
    tilter.spin(fwd, power, pct)
}
tilter.stop(hold)
1 Like

My apologies, did not read “We do not know how to code that.” Anyways,

DO NOT absolute value the error. It will make it so after the target is reached, it will likely continue with momentum and the code will tell the motor to not just continue, but increase motor power to go away from the target and break.

1 Like

I would especially recommend adding some other form of exit condition. The last thing you want is a macro that freezes your bot because something jammed. an if(someButtonPressed) { break; } should do the trick for a manual exit.

1 Like

Oh yeah. I always forget not to yous abs(). We use it on our bot because of a certain issue that I won’t explain here because I don’t wanna get off topic. But yeah, for most bots you don’t wanna use absolute value. I’m pretty sure what @sarah_97963A said is the actual code for a P loop.

Where exactly on the code would this go? I will include a picture of my code.
38 PM

  1. the code I provided was only pseudo code
  2. under your tray motor control statements
    image
1 Like