View unanswered posts | View active topics It is currently Tue Sep 23, 2014 2:24 pm






Reply to topic  [ 3 posts ] 
TETRIX servos controlled by joystick 
Author Message
Rookie

Joined: Mon Dec 09, 2013 2:08 pm
Posts: 8
Post 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);
  }
}


Fri May 09, 2014 12:54 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post 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
  }
}

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Fri May 09, 2014 2:00 pm
Profile
Rookie

Joined: Mon Dec 09, 2013 2:08 pm
Posts: 8
Post Re: TETRIX servos controlled by joystick
Thanks, it worked fine, I tried it when I got into the robotics lab this morning.


Mon May 12, 2014 9:52 am
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.