View unanswered posts | View active topics It is currently Tue Sep 02, 2014 5:26 pm






Reply to topic  [ 3 posts ] 
Why is this servo program not running 
Author Message
Rookie

Joined: Fri Oct 11, 2013 4:05 pm
Posts: 7
Post Why is this servo program not running
task main()
{
initializeRobot();
// wait for start of tele-op phase

while (true)
{ getJoystickSettings(joystick);
{
getJoystickSettings(joystick);//button 1 to close
if(joy2Btn(1)==1)
{

servo[servo1]=67;
servo[servo2]=67;
}

else

{
servo[servo1]=0;
servo[servo2]=0;

}}


if(joy2Btn(2)==1)
{
servo[servo1]=-67;//button 2 to open
servo[servo2]=-67;
}
else

{servo[servo1]=0;
{servo[servo2]=0;}


It's a simple open and close program, not sure what's wrong


Thu Oct 24, 2013 10:41 am
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: Why is this servo program not running
Most significant problem: Your braces aren't matched. You have to have the same number of open braces { as close braces }

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Thu Oct 24, 2013 1:06 pm
Profile
Rookie

Joined: Thu May 02, 2013 10:52 am
Posts: 22
Post Re: Why is this servo program not running
Like rcahoon said, the first problem is the lack, and in some cases abundance, of brackets. So here is it cleaned up:
Code:
task main()
{
  initializeRobot();
  // wait for start of tele-op phase

  while (true)
  {   
    getJoystickSettings(joystick);//button 1 to close
   
    if(joy2Btn(1)==1)
    {
      servo[servo1]=67;
      servo[servo2]=67;
    }
    else
    {
      servo[servo1]=0;
      servo[servo2]=0;
    }

    if(joy2Btn(2)==1)
    {
      servo[servo1]=-67;//button 2 to open
      servo[servo2]=-67;
    }
    else
    {
      servo[servo1]=0;
      servo[servo2]=0;
    }
  }
}


But there is a small problem in the logic of the code, so lets go through the code:
Code:
if(joy2Btn(1)==1)
{
  servo[servo1]=67;
  servo[servo2]=67;
}
else
{
  servo[servo1]=0;
  servo[servo2]=0;
}


So let's say that joystick button 1 is pressed, it will set the servos to 67, if it is released they will be set to 0. So far so good.

Code:
if(joy2Btn(2)==1)
{
  servo[servo1]=-67;//button 2 to open
  servo[servo2]=-67;
}
else
{
  servo[servo1]=0;
  servo[servo2]=0;
}


So let's say that joystick button 2 is pressed, it will set the servos to -67 (I am not 100% sure, but I think in RobotC the servos values are 0-255), if it is released they will be set to 0.

On their own they both look fine, but together.
Code:
if(joy2Btn(1)==1)
{
  servo[servo1]=67;
  servo[servo2]=67;
}
else
{
  servo[servo1]=0;
  servo[servo2]=0;
}

if(joy2Btn(2)==1)
{
  servo[servo1]=-67;//button 2 to open
  servo[servo2]=-67;
}
else
{
  servo[servo1]=0;
  servo[servo2]=0;
}

If button 1 is pressed, the first if/else will set the servos to 67, then the second one (unless you have both button 1 and 2 pressed) will set them to 0 (if both are pressed it will be -67). It will loop, as long as you have a button pressed, setting them to 67, 0, 67, 0, and so on, the servos will do nothing or twitch back and forth a little. The same thing will happen if button 2 is pressed. So the two statements need to be contained in one statement so that the servos can never be set to more than one value in one loop of the program, such as:
Code:
if(joy2Btn(1)==1)
{
  servo[servo1]=67;
  servo[servo2]=67;
}
else
{
  if(joy2Btn(2)==1)
  {
    servo[servo1]=-67;//button 2 to open
    servo[servo2]=-67;
  }
  else
  {
    servo[servo1]=0;
    servo[servo2]=0;
  }
}


Hope this helps.

_________________
What is easy you have done, what is hard you can do, what is impossible will take a little longer


Thu Oct 24, 2013 11:50 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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.