View unanswered posts | View active topics It is currently Fri Oct 31, 2014 11:08 am






Reply to topic  [ 2 posts ] 
Help with boolean variables 
Author Message
Rookie

Joined: Thu Mar 01, 2012 9:11 pm
Posts: 6
Post Help with boolean variables
Our team's robot has an arm that starts to sag. As a fix to this I set up a toggle switch that uses a boolean to check for whether or not it should be active. When it's active it is supposed to counteract gravity, but I'm having some problems. (As a side note anyone who plays league may find the names humorous)
Code:
#pragma config(Hubs,  S1, HTMotor,  HTServo,  HTMotor,  none)
#pragma config(Sensor, S1,     ,               sensorI2CMuxController)
#pragma config(Motor,  motorA,           ,             tmotorNXT, openLoop)
#pragma config(Motor,  motorB,           ,             tmotorNXT, openLoop)
#pragma config(Motor,  motorC,           ,             tmotorNXT, openLoop)
#pragma config(Motor,  mtr_S1_C1_1,     leftm,         tmotorTetrix, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C1_2,     rightm,        tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C3_1,     liftm,         tmotorTetrix, openLoop)
#pragma config(Motor,  mtr_S1_C3_2,     motorG,        tmotorTetrix, openLoop)
#pragma config(Servo,  srvo_S1_C2_1,    gateS,                tServoStandard)
#pragma config(Servo,  srvo_S1_C2_2,    servo2,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_3,    servo3,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_4,    servo4,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_5,    servo5,               tServoNone)
#pragma config(Servo,  srvo_S1_C2_6,    servo6,               tServoNone)
#include "JoystickDriver.c"

void ReverseSupportHecarim()   //counteracts gravity and prevents sagging
{
   motor[liftm]=30;
}
task main()
{
   bool DariusOP=false;
while(true)
{
getJoystickSettings(joystick);
//////////////////////////////////////////////////////////////////////////
//Arm
//////////////////////////////////////////////////////////////////////////
if(joy1Btn(4))
{
motor[liftm]=30;
}
if(joy1Btn(2))
{
motor[liftm]=-30;
}
if(!joy1Btn(4)&&!joy1Btn(2))
{
   motor[liftm]=0;
}
/////////////////////////////////////////////////////////////////////////
//Darius OP (monitors the toggle)
/////////////////////////////////////////////////////////////////////////
if(joy1Btn(6))
{
if(DariusOP==false)
{
   DariusOP=true;
}
if(DariusOP==true)
{
DariusOP=false;
}
}
while(DariusOP==true)
{
ReverseSupportHecarim();
}
}
}
}


Thu Feb 07, 2013 9:16 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 597
Post Re: Help with boolean variables
What exactly is your robot doing, and what do you want it to do?

I'm guessing the liftm motor doesn't do what you want it to when the joystick button 6 is pressed, correct? Looking at your code, there is an infinite loop in place that can cause issues:

Code:
bool DariusOP=false;
//Code

if(joy1Btn(6))
{
    if(DariusOP==false)
    {
      DariusOP=true;
    }
   
   if(DariusOP==true)
   {
    DariusOP=false;
   }
}

while(DariusOP==true)
  {
    ReverseSupportHecarim();
  }



And the function 'ReverseSupportHecarim():

Code:
void ReverseSupportHecarim()   //counteracts gravity and prevents sagging
{
   motor[liftm]=30;
}


So, line by line the code sets the boolean to false during initialization. When joystick button 6 is pressed, it checks to see if it is false (it is, since it was initialized as false) and sets it to true. The very next line checks to see if the boolean is true (which it now is, thanks to the previous if statement) and sets it to false; the two if statements cancel each other out, effectively. No matter what state the boolean is going into the first if statement, it will exit with value of false.

This then renders the next while loop (and essentially, the ReverseSupportHecarim() function) moot as they can never be utilized by the code.

But, if the while loop was entered with the boolean being true:

Code:
while(DariusOP==true)
  {
    ReverseSupportHecarim();
  }


It would never exit because there is nothing setting it to false either in the loop or in the ReverseSupportHecarim() function.

For this particular application, I would recommend putting a sensor (such as an encoder) on the motor/motor shaft so you can determine how much the motor has rotated, and if it is not at the target location act accordingly. Motors are a bit trickier to hold in one place than servos are motors are given a power level and will spin at that power level until they are told to stop; servos, however, are given positional values and will try to hold that position until a new position command is given. Depending on the design of your robot, you may want to consider using a servo, if possible. Otherwise, I would definitely recommend incorporating a position-sensing sensor (encoder, potentiometer, etc) onto the arm motor if possible.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Fri Feb 08, 2013 4:51 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.