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

Smooth dynammic servo motion
http://robotc.net/forums/viewtopic.php?f=11&t=2726
Page 1 of 1

Author:  magicode [ Sun Nov 07, 2010 7:20 pm ]
Post subject:  Smooth dynammic servo motion

Our robot requires servos to move smoothly based on user input whithout interrupting any other functions. I wrote this code, and felt that maybe beginner programmers would benifit from it. Also, if anybody has a way to make this more efficient (without using multitasking, because this is for beginner programmers) please let me know.

We wanted to move the servo up if button 6U is pressed, and down if button 6D is pressed. This is more complicated than it sounds, because if you do this:
Code:
int servoPos = 0;
while(true){
  if(vexRT[btn6U]) servopos += 1; //vexRT[btn6U] returns true or false based
                                              //on whether it is pressed or not
  if(vexRT[btn6D]) servopos -= 1;
  servo[arm] = servoPos;
}


then the servo will go fully up as soon as you press the button, because the microprocessor cycles through the loop hundreds of times while you are holding the button.

now if we do this:
Code:
int servoPos = 0;
while(true){
  if(vexRT[btn6U]){
    servopos += 1;
    wait1MSec(100);//stops the program for a tenth of a second to stop
                           //servoPos from incrementing too quickly
  }
  if(vexRT[btn6D]){
    servopos -= 1;
    wait1MSec(100);
  }
  servo[arm] = servoPos;
}


then the other controls get interrupted while the program is waiting, and time sensetive functions get messed up.

My solution was to use timers like this:
Code:

int servoPos = 110;
int upperLimit = 110;
int lowerLimit = 200;

while(true){

if(vexRT[btn6U]){
  if(time1[T1] > 10){
    ClearTimer(T1);
  }
  else if(time1[T1] < 5 && incServo){
    //move the servo only 5 milliseconds out of 10
    if(servoPos < upperLimit) servoPos += 1;
    //stop servoPos from exeeding upperLimit
  }
}
else if(vexRT[btn6D]){
  if(time1[T1] > 10){
    ClearTimer(T1);
  }
  else if(time1[T1] < 5 && incServo){
    if(servoPos > lowerLimit) servoPos -= 1;
    //stop servoPos from going below lowerLimit
  }
}

servo[arm] = servoPos;


That's it. Comment if you have questions or see a problem. Thanks.

Author:  jbflot [ Mon Nov 08, 2010 10:04 am ]
Post subject:  Re: Smooth dynammic servo motion

Clever! Thanks for sharing your solution.

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