ROBOTC.net forums
http://robotc.net/forums/

TETRIX servos controlled by joystick
http://robotc.net/forums/viewtopic.php?f=1&t=8747
Page 1 of 1

Author:  Team6608 [ Fri May 09, 2014 12:54 pm ]
Post subject:  TETRIX servos controlled by joystick

I am trying to control a TETRIX servo using a controller button. The idea is that when you press a certain button, the servo's position goes up by 10, when you press another, it goes down by the same. The external servo debugger can control the servo, but my code isn't working. Below is a snippet of my code, the task that isn't working, and then the task main in case I have some error there. The task drive works just fine. Thanks!

Code:
task camera(){
   while(true)
      getJoystickSettings(joystick);

      int a = ServoValue[servo1];

      if(joy1Btn(08) == true){
            
            a = a + 10;
            servo[servo1] = a;
      }
      if(joy1Btn(07) == true){
            a = a - 10;
            servo[servo1] = a;
      }

      servo[servo1] = a;
}

task main()
{
  while (true)
  {


      StartTask(Drive);
     StartTask(camera);
  }
}

Author:  rcahoon [ Fri May 09, 2014 2:00 pm ]
Post subject:  Re: TETRIX servos controlled by joystick

Two issues and two suggestions:
  • You don't want to put StartTask in a loop like that.
  • You need to put braces around the loop in camera(). The way you have it now, it will only repeat getJoystickSettings(joystick) forever, and the code that follows will never be executed
  • I would recommend adding a bit of delay in the camera() loop, otherwise the servo position will increment very quickly after you push the button, and will likely appear to jump from one extreme to the other.
  • Add some bounds checking when you increment and decrement

Code:
task camera(){
   while(true)
   {
      getJoystickSettings(joystick);

      int a = ServoValue[servo1];

      if(joy1Btn(08) && a <= 245){
            
            a = a + 10;
            servo[servo1] = a;
      }
      if(joy1Btn(07) && a >= 10){
            a = a - 10;
            servo[servo1] = a;
      }

      servo[servo1] = a;
      
      wait1Msec(50);
   }
}

task main()
{
  StartTask(Drive);
  StartTask(camera);
 
  while (true)
  {
    wait1Msec(50); // add a delay here so that the main task doesn't hog the CPU
  }
}

Author:  Team6608 [ Mon May 12, 2014 9:52 am ]
Post subject:  Re: TETRIX servos controlled by joystick

Thanks, it worked fine, I tried it when I got into the robotics lab this morning.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/