How to use multiple servos and other functions simultaneousl

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

When you tell a servo to move to a position, it will move there as quickly as possible. If you want it to move slowly, you need to move it in increments as in this code. You can adjust the speed by adjusting the delay.

Code: Select all

  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
For multiple servos, this can be done with the sweeper class from the tutorial I posted earlier:
https://learn.adafruit.com/multi-taskin ... lean-sweep

User avatar
cxc21
 
Posts: 68
Joined: Mon Feb 28, 2011 5:56 pm

Re: How to use multiple servos and other functions simultane

Post by cxc21 »

Yes, this is what I am doing. But moving in 1 degree results in a jittery not in smooth continuous nor slow movement. Basically same speed but nasty jittery forward and backward moving increments. Tried different power source for the servo as recommended solution by some but it is all the same. One of the 0-180 servos is actually behaving like a continuous servo probably mislabeled, the other has this jittery movement. I ordered two more to see if the servo is shoot too. I think I need something like writeMicroseconds() for finer increments?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

You might try varying the increment and/or timings to find a combination that works more smoothly. Some load on the servos may help to damp the jitteriness also.

The PID controllers in the servo are optimized for 'average' use and may not be stable under all conditions. Some high-end servos allow you to tune the PID parameters, but for most common servos you have to work with them as-is.

User avatar
cxc21
 
Posts: 68
Joined: Mon Feb 28, 2011 5:56 pm

Re: How to use multiple servos and other functions simultane

Post by cxc21 »

Well, tried all kind of increments, none will smooth things. I guess will ad a delay between increment steps and ad a 100uF to buffer things. I think signal is coming before servo is done moving. Tons of suggestions about the problem on the arduino forums, including writing sinus functions for the movements, all involve microsecond. This will take some time to understand and use. Looks like adafruit didn't chime in on this yet with a learning chapter?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

I think signal is coming before servo is done moving.
That in itself should not be a problem - if the servo is moving linearly. Most likely, the servo is overshooting the incremental position and oscillating a bit before it stops moving. I see this a lot in budget servos. They use a high proportional constant to improve the 'speed' spec. But it makes for some rough operation on short moves.

User avatar
cxc21
 
Posts: 68
Joined: Mon Feb 28, 2011 5:56 pm

Re: How to use multiple servos and other functions simultane

Post by cxc21 »

Thanks for the explanation. All of these micro servos ( I need that size) seem to be stupid cheap. Would have no problems spending more but looks like there aren't any better ones?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

The HiTec and Futaba servos are a more expensive, but tend to be smoother running in general. However, their micro servos are a slightly different form-factor than the budget micros. So they are not a 'drop-in' replacement.

Some of the digital servos have configurable control parameters that let you fine-tune them for specific applications. But they require a bit more of an investment. Both for the servos and for a programmer to tune them.

User avatar
cxc21
 
Posts: 68
Joined: Mon Feb 28, 2011 5:56 pm

Re: How to use multiple servos and other functions simultane

Post by cxc21 »

This works much better but smooth it ain't. If I reduce power to the servo from 5 to 3V it is even better. However, when switched on the servo moves violently to the wrong side before starting the sweep. Is there a way to prevent that or at least have it start at the opposite end?

Code: Select all

class Sweeper
{
  Servo servo;              // the servo
  int pos;              // current servo position 
  int increment;        // increment to move for each interval
  int  updateInterval;      // interval between updates
  unsigned long lastUpdate; // last update of position
 
public: 
  Sweeper(int interval)
  {
    updateInterval = interval;
    increment = 1;
  }
  
  void Attach(int pin)
  {
    servo.attach(pin);
  }
  
  void Detach()
  {
    servo.detach();
  }
  
  void Update()
  {
    if((millis() - lastUpdate) > updateInterval)  // time to update
    {
      lastUpdate = millis();
      pos += increment;
      servo.write(pos);
      delay(15);
      Serial.println(pos);
      if ((pos >= 70) || (pos <= 0)) // end of sweep
      {
        // reverse direction
        increment = -increment;
      }
    }
  }
};
Last edited by adafruit_support_bill on Tue Aug 07, 2018 5:54 am, edited 1 time in total.
Reason: Please use [code] tags when submitting code to the forums

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

For an analog servo, reducing the voltage has the same effect as reducing the proportional constant in the PID.

Not sure what is causing the jerk at startup. Could be that the pin is transmitting some arbitrary pulses between the time you call 'attach()" and the first call to 'write()'. Make sure that you 'write' the initial position to the servo as soon as possible after the Attach.

User avatar
cxc21
 
Posts: 68
Joined: Mon Feb 28, 2011 5:56 pm

Re: How to use multiple servos and other functions simultane

Post by cxc21 »

I don't think it is arbitrary. It jumps to 70 or 0 degree, not sure what sets the directions in the sketch?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

The "attach" function simply connects the timer to the servo. So it will start sending pulses for some default pulse width - whatever that happens to be. So the servo will seek to that position.

User avatar
cxc21
 
Posts: 68
Joined: Mon Feb 28, 2011 5:56 pm

Re: How to use multiple servos and other functions simultane

Post by cxc21 »

Ok. looks like I need to understand how to implement servo easing into this. Is that part of the adafruit servo library?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

It is not part of any servo library I know of. The fundamental issue with most servos is that at power-up, you have no idea what the initial servo position is. So any position you start with has a 1 in 180 chance of being within a degree of correct.

We do have analog feedback servos that allow you to read the initial servo position.
https://learn.adafruit.com/analog-feedb ... -loops-1-3

User avatar
cxc21
 
Posts: 68
Joined: Mon Feb 28, 2011 5:56 pm

Re: How to use multiple servos and other functions simultane

Post by cxc21 »

Those are too big. I give up on the servos. Basically I can get them to move at the right speed and angle but 1 degree movements are just too jerky. Servo easing I couldn't get to work. When you define the servo class you can easily ad more servos, but they all do the same thing and move between the same angles. Would need two different servo classes one for each servo. but can't figure out how to accomplish this. But the jerkiness of the movement is the deal breaker. Mini stepper motors should be better but can one drive them the Circuit Playground?

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use multiple servos and other functions simultane

Post by adafruit_support_bill »

We do have stepper support via Crickit: https://learn.adafruit.com/adafruit-cri ... n-steppers

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”