1
$\begingroup$

We're students trying to make a clawbot for a Science Seminar class. However, for some reason, whenever we try to move the arm or the claw in a certain way, it will lock up and only move that direction. Code attached. Please help.

#pragma config(Motor, port1, frWheel, tmotornormal, openLoop, reversed) //Setting up the motors
#pragma config(Motor, port5, brWheel, tmotornormal, openLoop, reversed)
#pragma config(Motor, port3, flWheel, tmotornormal, openLoop)
#pragma config(Motor, port4, blWheel, tmotornormal, openLoop)
#pragma config(Motor, port10, Arm, tmotornormal, openLoop)
#pragma config(Motor, port6, Claw, tmotornormal, openLoop)

task main() 
{
    int a = 0; //Arm integer
    int c = 0; //Claw integer
    while(true) 
    {
        motor[frWheel] = vexRT(Ch2); //Wheels
        motor[brWheel] = vexRT(Ch2);
        motor[flWheel] = vexRT(Ch3);
        motor[blWheel] = vexRT(Ch3);
        if(a >= -30 && a <= 30)  {
            if(vexRT[Btn8D] == 1) //If arm down button pressed...
            {
                motor[Arm] = --a; //then arm will go down.
            }
            else if(vexRT[Btn8U] == 1)
            {
                motor[Arm] = ++a;
            }
            else(vexRT[Btn8U] == 0 && vexRT[Btn8D] == 0);
            {
                motor[Arm] = a;
            }
        }
        else
        {

        }

        if(c <= 30 && c >= -30)
        {
            if(vexRT[Btn7U] == 1) //If claw up button pressed...
            {
                motor[Claw] = ++c; //Claw will open.
            }
            else if(vexRT[Btn7D] == 1)
            {
                motor[Claw] = --c;
            }
            else(vexRT[Btn7D] == 0 && vexRT[Btn7U] == 0);
            {
                motor[Claw] = c;
            }
        }
        else
        {

        }
    }
}
$\endgroup$
2
  • $\begingroup$ If you make short presses on the up, and then down, buttons, will the arm go up then down? In other words, does it lock up only when it has reached a count of +/- 30? $\endgroup$
    – SteveO
    Commented Jan 24, 2017 at 22:18
  • $\begingroup$ Thanks for the help, and no, even if we press the button for a fraction of a second it'll immediately go in the direction we pressed, and nowhere else, regardless of its position. $\endgroup$ Commented Jan 24, 2017 at 22:27

2 Answers 2

0
$\begingroup$

Your conditional logic needs to be improved. Consider the following scenario:

  • a is initialized to zero.
  • The operator presses Btn8d (arm down)
  • a is decremented to -1, then -2, etc.
  • If the button continues to be pressed, a eventually reaches -30. Depending on the loop rate, this may happen very quickly.
  • When a == 30 the statement if(a >= -30 && a <= 30) is still true.
  • a is decremented to -31.
  • Recovery is not possible because the conditional will never be true again.

Your code needs to handle this corner condition.

Also, when is the vexRT[] array updated? If it is not continuously updated with the values of the button presses, then you will need to update it at the top of your while loop.

$\endgroup$
2
  • $\begingroup$ We'll test the code on the bot, now that we've revised it. If it doesn't work, we'll ask for more help, but for the meantime, I'm marking this as an answer. Thank you! $\endgroup$ Commented Jan 25, 2017 at 16:53
  • $\begingroup$ Did the code revisions work? If not, post another question with your code changes and we can take a look. $\endgroup$
    – SteveO
    Commented Jan 26, 2017 at 2:04
0
$\begingroup$

There are two reasons reason for your problem:

  1. The main loop is executed very fast - you only check conditionals and assign some values to variables. Total execution time will be probably in order of milliseconds. That means that when you press the button for even as short as 0.1s the loop will execute thousands of times. I would advice on checking the button states only every 50-200 milliseconds.
  2. As already mentioned by SteveO, there is also an error in your logic. When variables a and c reach their maximum values, there is no way to reset them to their original state. I would do that like:

    if(buttonIsPressed()) {
      changeVariable();
    }
    if(variable > maxValue) {
      variable = maxValue;
    } 
    
$\endgroup$
3
  • $\begingroup$ Thank you for your insight! Looking into how to implement a timer as is. Are you sure that making the claw motor equal c will reset it to it's original position and not to the value of c? $\endgroup$ Commented Jan 25, 2017 at 16:59
  • $\begingroup$ Oops, no of course not. Somehow, when I looked at it I thought it was 0 not C. Silly mistake $\endgroup$
    – mactro
    Commented Jan 25, 2017 at 18:18
  • $\begingroup$ We've all been there, don't worry about it. $\endgroup$ Commented Jan 25, 2017 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.