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

Joystick Program Lag
http://robotc.net/forums/viewtopic.php?f=52&t=10655
Page 1 of 1

Author:  theSTEMphenom [ Tue Mar 10, 2015 6:44 pm ]
Post subject:  Joystick Program Lag

Hello Community,
I am a programmer for FTC team 6965 WRA, and I recently ran into a problem. I programmed a short wait for a servo (400 Msecs) on joy2btn, but the wait distributes through the whole program.I.e. When a joystick on joy1 is moved there is also a long pause. Is there any way to fix this?
Thanks

Author:  cookthebook [ Tue Mar 10, 2015 11:10 pm ]
Post subject:  Re: Joystick Program Lag

Yes, there are many ways to get around this. I am taking a guess that your code for the button is something analogous to this:
Code:
if(joy2Btn(1)){
   wait1Msec(400);
   servo[test] = 0;
}

This is generally bad within a teleop program especially. The wait1Msec() method forces the brain to stop at that line for 400 milliseconds, you can see this if you run the debug. So any code you want to be running simultaneously will never be reached. Luckily, the NXT has four built in timers that run constantly, T1, T2, T3, and T4. You can check if a timer has reached a certain value (or greater) without having to affect the rest of the code. Here is a simple implementation that you could try:

Code:
bool pressed = false;//This will be used to see if the button has been pressed

task main()
{

while(true){
   if(joy2Btn(1)){
      pressed = true;//Tells the program that the servo is allowed to move
      ClearTimer(T1);//sets the timer to 0
   }
   //Note that you can press button 1 again before 400 msec's have passed and reset the counter
   
   if(pressed && time1(T1) >= 400){//time1() returns the value of T1 in milliseconds
      servo[test] = 0;
      pressed = false;//reset pressed so it doesn't happen again
   }
   
   //Other driving code should behave normally
}
}

So, the timer T1 resets once we press the button and we store a "true" in the pressed boolean variable that allows the servo to move. I believe this should work as described (though I haven't tested it). In general, if you want some sort of timed interval it's best to try to use the timers instead of a wait function.

If this isn't the source of your problem, please post some of your code. It is much easier to figure what's wrong that way.

Author:  theSTEMphenom [ Sat Mar 14, 2015 6:48 am ]
Post subject:  Re: Joystick Program Lag

Thanks very much. But I'll it work if I programmed it like this:
servo[Test]=100;
wait1Msec(400);

Author:  cookthebook [ Sat Mar 14, 2015 12:38 pm ]
Post subject:  Re: Joystick Program Lag

I'm not sure I see the point of putting the timer after the servo positioning. Are you waiting for the servo to reach its destination? If so it's not hard to just stop pushing buttons. Or are you waiting until it gets to 100 in order to send it a new value?

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