Hello everyone,
I have been working on my autonomous for about a week now and I am noticing one significant problem that randomly appears. The issue is that sometimes one of the two claws will keep spinning without stopping in autonomous. This only happens sometimes and only happens when the robot is ready to pick up the cube. I think the issue has something to do with the amount that the robot turns because sometimes the robot is not exactly centered when it turns toward the cube. As a result, one claw hits the cube before the other and so the motor is still running but the claw is not actually moving. When this happens, the other claw does not stop moving ever, no matter whether it has crossed the desired value or not. However, usually, if the robot turns perfectly and the cube is in the center, both claws stop at the same time and the autonomous works as it should(kind of).
Here is the code:
void closeClawNoReset(int ticks) {
int LeftClawOriginalPos = nMotorEncoder[LeftClaw];
int RightClawOriginalPos = nMotorEncoder[RightClaw];
int clawHoldTarget = getCurrentArmPos();
while(abs(RightClawOriginalPos - nMotorEncoder[RightClaw]) < ticks || abs(LeftClawOriginalPos - nMotorEncoder[LeftClaw]) < ticks) {
if(abs(RightClawOriginalPos - nMotorEncoder[RightClaw]) < ticks) {
motor[RightClaw] = -100;
}
else {
motor[RightClaw] = 0;
}
if(abs(LeftClawOriginalPos - nMotorEncoder[LeftClaw]) < ticks) {
motor[LeftClaw] = -100;
}
else {
motor[LeftClaw] = 0;
}
holdArmPosInAuton(clawHoldTarget);
}
motor[LeftClaw] = motor[RightClaw] = 0;
}
The issue is here:
int LeftClawOriginalPos = nMotorEncoder[LeftClaw];
int RightClawOriginalPos = nMotorEncoder[RightClaw];
By setting these two equal, when you go here:
if(abs(RightClawOriginalPos - nMotorEncoder[RightClaw]) < ticks)
and here:
if(abs(LeftClawOriginalPos - nMotorEncoder[LeftClaw]) < ticks)
for example RightClawOriginalPos - nMotorEncoderRightClaw will always = 0 and
therefore will always be < ticks.
Better:
int LeftClawOriginalPos = 0;
int RightClawOriginalPos = 0;
Try that.
Thanks for your input, but I do not think that is the problem. This is because the variables int LeftClawOriginalPos and int RightClawOriginalPos are both being set to the current value of the encoder as soon as the function is called. Notice that the encoder values are not being reset in the beginning of the function, so this is important. Then, in the while loop, the absolute value of the difference is being checked, and if it is less than the number of desired ticks, it will enter the program. In reality, you are calculating the amount that the encoder has moved. The first time it is checked, the value will be 0, which is less than ticks, so it will start the motors. Once the encoder values are changing, the difference will change as well. For example, if LeftClawOriginalPos = 500, ticks is 250, and the motor has moved a little so the value of the encoder is 600, the following will happen:
500 - 600 which is -100. absolute value makes it +100.
100 < 250, so it will enter the loop.
Once the encoder reaches a value higher than 750, the loop should stop because:
500 - 751 = -251. absolute value makes it +251.
251 > 250, so it should exit the loop
I may be wrong though so I appreciate any advice. Thanks