Society of Robots - Robot Forum

Software => Software => Topic started by: Mastermime on November 26, 2012, 11:11:30 PM

Title: Servo Code
Post by: Mastermime on November 26, 2012, 11:11:30 PM
Hello,  I used Webbotlib Project Designer to generate some code to upload to my Axon II, however I wasn't able to add a servo into Project Designer because there was no suitable power source for it.  My hardware solution was to put a put a linear regulator inbetween the 12v pin and the servo to drop it down to 5v.  I stiil couldnt generate the files though with that servo so I thought I could just add it later. 

So my question is, how do I add it into my AVR file so everything will remain properly configure?  Also, what code should I use to control a modified servo for continuous rotation?
Title: Re: Servo Code
Post by: newInRobotics on November 27, 2012, 02:43:59 AM
My hardware solution was to put a put a linear regulator inbetween the 12v pin and the servo to drop it down to 5v.
Let's say that under load servo draws 1A, You want to drop 7V with linear regulator, that equates to 7W of wasted power and probably something that Your linear regulator is not capable of. Much better to use either separate power supply (say 2 Li-Ion batteries giving You around 7.4V, or a switching regulator which will nicely step down voltage from 12V to whatever You want without overheating).

So my question is, how do I add it into my AVR file so everything will remain properly configure?
In project designer just add separate 5V power supply to keep program happy.

Also, what code should I use to control a modified servo for continuous rotation?
Modified servo is controlled the same way unmodified is. The only difference is that PWM no controls speed and not position.
Title: Re: Servo Code
Post by: Mastermime on November 27, 2012, 11:44:12 AM
Quote
Let's way that under load servo draws 1A, You want to drop 7V with linear regulator, that equates to 7W of wasted power and probably something that Your linear regulator is not capable of. Much better to use either separate power supply (say 2 Li-Ion batteries giving You around 7.4V, or a switching regulator which will nicely step down voltage from 12V to whatever You want without overheating).

I'd rather not add a whole other battery just for a servo.  The Hitec HS 625MG servo draws only 250ma.  Thats 2.1W wasted.  I could build a current boosting circuit using a power transistor.

Quote
In project designer just add separate 5V power supply to keep program happy.
How would I add it to the AVR file or would this just cause more issues?
Title: Re: Servo Code
Post by: Mastermime on November 29, 2012, 07:38:43 PM
Ok I configured the servo in there just fine.  I dont care about the speed since I'm just using the servo to pan a camera.  Could someone just post a small code snippet of what I should use?  Could I do something like this? I dont want the servo to be at max speed. I want to set to slower speed but dont know how?

Code: [Select]
if (tempbyte == 'x')
    act_setSpeed(&center,DRIVE_SPEED_MAX); 
    return 0;
Title: Re: Servo Code
Post by: newInRobotics on November 30, 2012, 03:23:09 AM
If You are using unmodified servo there is no nice way to set speed as servo set's its own speed based on distance it has to travel, the further it goes the faster it does so. You can, however, make it run in segments, which will decrease speed.
Title: Re: Servo Code
Post by: Mastermime on December 01, 2012, 05:18:48 PM
Using PWM?
Title: Re: Servo Code
Post by: newInRobotics on December 03, 2012, 03:27:02 PM
Using PWM?
To run servo You use PWM anyway. By run servo in segments I meant that if, say, servo turns 60° in 2s, then if You make it turn twice by 30° it will run slower as internal circuitry in servo makes servo axle rotate slower due to the fact that it has to travel shorter distance. Does it make sense?
Title: Re: Servo Code
Post by: jwatte on December 04, 2012, 12:47:25 AM
The basic control loop you want to set up is a loop that runs over and over, and each time calculates:
Where am I now? (You have to remember this!)
Where do I next want to be?
When do I next want to be there?
Given that, what is the next small step I can take?

If you run this, say, 30 times a second (for a regular 30 Hz servo update cycle,) then it might look something like:

Code: [Select]
int curTimeDelay = 1500; // center
int targetTimeDelay = 1500; // center
long millisToTarget = 0; // not changing
unsigned int lastTime = 0;

void setup() {
    lastTime = millis();
}

void loop() {

  readCommand(); // figures out possible new value of "targetTimeDelay" and "millisToTarget"

  while (millis() - lastTime < 30) {
    // do nothing
  }
  lastTime = millis();

  if (millisToTarget == 0) {
    curTimeDelay = targetTimeDelay;
  }
  else {
    long millisToGo = (targetTimeDelay - curTimeDelay);
    int deltaMillis = millisToGo * 30 / millisToTarget;
    curTimeDelay += deltaMillis;
    if (millisToTarget < 30) {
      millisToTarget -= 30;
    }
    else {
      millisToTarget -= 30;
    }

    setServoPWMTime(THE_SERVO, curTimeDelay);
}

This code will move the target time given to the servo over a period of time, rather than just telling it "I want you there NOW!"