View unanswered posts | View active topics It is currently Wed Apr 16, 2014 10:09 pm






Reply to topic  [ 2 posts ] 
Smooth dynammic servo motion 
Author Message
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post 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.

_________________
sudo rm -rf /


Sun Nov 07, 2010 7:20 pm
Profile
Site Admin
Site Admin

Joined: Tue May 15, 2007 9:02 am
Posts: 403
Post Re: Smooth dynammic servo motion
Clever! Thanks for sharing your solution.

_________________
Jesse Flot
CMU Robotics Academy
ROBOTC Support


Mon Nov 08, 2010 10:04 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 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.