View unanswered posts | View active topics It is currently Thu Dec 18, 2014 5:40 am






Reply to topic  [ 6 posts ] 
Servo Programming 
Author Message
Rookie

Joined: Tue Oct 19, 2010 1:16 am
Posts: 9
Post Servo Programming
OK, so I am now in charge of our teams programming. I have the basics of driving, one movement servo, and some autonumous. But I have hit a road block with a part of the programming.

We have a few servos that need to work different angles at different times. And I have it so that is goes to one set place, and moves to Angle 1 easily, but When I try to get to Angle 2 and Angle 3, it starts twitching really badly. How do I stop this from happening.

This is what I have in the Code right now.. which sort of works, but the twitching problem.
Code:
#pragma config(Hubs,  S1, HTMotor,  HTMotor,  HTServo,  none)
#pragma config(Motor,  motorA,          motorA,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          motorB,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorC,        tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_S1_C1_1,     motorD,        tmotorNormal, openLoop, reversed)
#pragma config(Motor,  mtr_S1_C1_2,     motorE,        tmotorNormal, openLoop)
#pragma config(Motor,  mtr_S1_C2_1,     motorF,        tmotorNormal, PIDControl, reversed, encoder)
#pragma config(Motor,  mtr_S1_C2_2,     motorG,        tmotorNormal, openLoop)
#pragma config(Servo,  srvo_S1_C3_1,    ,                     tServoStandard)
#pragma config(Servo,  srvo_S1_C3_2,    ,                     tServoStandard)
#pragma config(Servo,  srvo_S1_C3_3,    ,                     tServoStandard)
#pragma config(Servo,  srvo_S1_C3_4,    ,                     tServoStandard)
#pragma config(Servo,  srvo_S1_C3_5,    ,                     tServoStandard)
#pragma config(Servo,  srvo_S1_C3_6,    ,                     tServoStandard)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "JoystickDriver.c"  //Include file to "handle" the Bluetooth messages.


void initializeRobot()
{

  return;
}

task main()
{
  initializeRobot();

  waitForStart();   // wait for start of tele-op phase

  while (true)
  {

    motor[motorD] = joystick.joy1_y1;//Driving Base Right
    motor[motorE] = joystick.joy1_y2;//Driving Base Left
    motor[motorG] = joystick.joy2_y1;// Arm

   if(joy2Btn(6))//Bucket Twist
    {
    servoTarget[servo6] = 12;
   }
   else
   {
     servoTarget[servo6] = 117;
   }
   if(joy2Btn(1))//Bucket Level, Angle 1
    {
      servoTarget[servo4] = 28;
      servoTarget[servo5] = 209;
    }
   else
    {
      servoTarget[servo4] = 105;
      servoTarget[servo5] = 137;
    }
    if(joy2Btn(2))//Bucket Level, Angle 2
    {
      servoTarget[servo4] = 61;
      servoTarget[servo5] = 176;
    }
    if(joy2Btn(3))//Bucket Level, Angle 3
    {
      servoTarget[servo4] = 143;
      servoTarget[servo5] = 176;
    }
   }
}


Tue Oct 19, 2010 1:28 am
Profile
Rookie
User avatar

Joined: Mon Feb 23, 2009 12:40 pm
Posts: 27
Post Re: Servo Programming
You're servos are twitching because you are setting them to two different values at the same time when either buttons 2 or 3 are pressed. Remember, if button 2 is pressed and button 1 is not pressed, then you are setting them to both the else position from the button 1 if and to the true position from the button 2 if. Obviously if both button 1 and button 2 are pressed at the same time, you have a different conflict. You should probably use a single if block.

if(...)
...
else if (...)
...
else if (...)
...
else
...

Hope this helps.

David


Tue Oct 19, 2010 9:08 am
Profile
Rookie

Joined: Tue Oct 19, 2010 1:16 am
Posts: 9
Post Re: Servo Programming
Ah, yep, I see now... No more twitching issues.

One other question, is there any way to program so the 12VDC Motor can go to certain angles?


Tue Oct 19, 2010 9:57 am
Profile
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Servo Programming
Yes there is. It requires more coding than a servo, but you can use encoders to set the motors to a certain angle. Why you would want to do this, I don't know, but here is the psuedo-code.

Write a function which accepts a parameter of an angle:
void motorServo(int ang){
}

Then find the ratio of the encoder count to the angle you want it at. This will be different for everybody if, say for instance, you're using a few gears. So just set the encoder to zer, and then turn the motor to a certain angle. Record what number the encoder was at. Do the same for a different angle. Then see how many clicks of the encoder result in 1 degree. set this ratio as a variable, so that if it changes, you can use the same code. I'll call this variable angDegRatio.

In the function, use this logic:
if the encoder value is more than ang*angDegRatio, then turn left until the value is equal to ang * angDegRatio.

else, turn right until the encoder value is eual to the angDegRatio.

Now here is a little trick you may want to do. Most likely, your motors will overshoot the mark. So make two functions, one fast and one slow. In the fast function, run the motors at a high power, so that they get close the the angle that you want to go to, and then, in the slow function, run the motors at a low power to zero in on your angle.

so your implementation would look like this:

slowMotorServo(45);
fastMotorServo(45);

Or you can put everything into one funtion if you're more comfortable with that. I personally like to divide things up into their basic components, but you can do whatever you want.

_________________
sudo rm -rf /


Tue Oct 19, 2010 9:03 pm
Profile
Online
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1359
Post Re: Servo Programming
Or you can use PID control logic where the motor power is computed by multiplying a Proportional constant Kp to the difference between the target encoder value and the current encoder value. Therefore, if you are far away from the target encoder value, the computed motor power will be large. So you get there faster. But of course, you still have to tune the Kp constant to minimize overshoot.


Mon Nov 08, 2010 2:54 am
Profile
Rookie

Joined: Fri Oct 21, 2011 11:14 pm
Posts: 9
Post Re: Servo Programming
Just wondering, what is the difference between using

servo[]

and ServoTarget[]

Thanks


Sun Oct 30, 2011 6:56 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 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.