Go Down

Topic: Servo.h not behaving as i expect (Read 1 time) previous topic - next topic

Teacup

Hey guys,

So i am using attempting to program a leg for a walker, but I am still a fairly new programmer and am not understanding why something is happening.

When i attach the servo within the setup and write to it within the loop everythign behaves exactly as I would expect.  However, I have been trying to make everything a little more robust and portable between projects so I have been creating a class called Leg, from this class I create and write the servo object.  This class is called within the main loop function.  The class functions are bellow, they are held within a .h n .cpp file called Leg.

The problem that is occuring is that the two servo's that are attached will vibrate and not follow any predictable manner when i use the functions setS1 or setS2 in the main loop.

Is there something in the way servo.h works that stops it from working successfully when called from within another class?  Any ideas?  I am guessing this is the problem but frankly I am stumped.

Cheers,
David

Quote
#include <Servo.h>
#include <Arduino.h>
#include <Leg.h>

Leg::Leg()
{
  //d2 = 58;
  //d3 = 138;
  s1 = 0;
  s2 = 0;
 
  int lowerLimit = 540;   // 0 degrees at 540 microsecs with 0 degs tollerance
  int upperLimit = 2360;  //180 degrees at 2360 microsecs with 0 degs tollerance
  static int legNumber = 0;
  servo1.attach(legNumber + 1,lowerLimit, upperLimit);
  servo2.attach(legNumber + 2,lowerLimit, upperLimit);
  legNumber = legNumber + 3;
}

void Leg::setS1(int inputS1)
{
  int upperLimit = 180;
  if (inputS1 > upperLimit) inputS1 = upperLimit;
  if (inputS1 < 0) inputS1 = 0;
  s1 = inputS1;
  servo1.write(s1);
}

void Leg::setS2(int inputS2)
{
  int upperLimit = 130;
  if (inputS2 > upperLimit) inputS2 = upperLimit;
  if (inputS2 < 0) inputS2 = 0;
  s2 = inputS2;
  servo2.write(s2);
}

johnwasser

You forgot to show where servo1 and servo2 are created.

You seem to be using pins 1 and 2 for your leg servos.  Are you not using Serial?  Serial uses pins 0 and 1.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

PaulS

Do you know when your constructor is called, relative to when the pins that you attach them to are ready to have servos attached to them? Probably nor. Most of what you are doing in the constructor should be done later, in a begin() method that you call from setup().

That static variable, legNumber, is declared in the wrong place, if you want the variable shared by all instances of the class. It should be in the header file, not in the constructor.

Finally, you are using it to define which 2 pins to use. Why is it then incremented by 3?

Teacup

Hey guys!  Thanks for the reply, you solves it.  The problem was that I was attaching the servos in the constructore.  Sorry, still learning.  I have changed it so there is now an attachServo function and it now behaves perfectly.

John:  I was not using a serial connection to talk to the servos at this point though i might talk to a Servo controller later.  I was just using the Servo library which I think utilises a software driven interupt (though i am not sure).

Paul:  Thanks, it was was the contstructor.  Thanks for the comment on the variable lebNumber, i am no longer using it with the new function, however, it is good advice for future programming.  I was incrementing it by three because the leg actually ahs three servo motors, however I thought it best to get the functions working in 2D first and then work my way up.  Was just leaving a little reminder to myself.

Thanks a heap!  Very happy right now.

DuaneB

Hi,
   Here is a little on how the servo library works -

http://rcarduino.blogspot.com/2012/01/can-i-control-more-than-x-servos-with.html

And here is a little on the power problem if you haven't faced it already -

http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html

It sounds like you want to build something that will require a lot of power before its finished.

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Teacup

Hello DuaneB,

Yes, I am indeed building something very power draining.  The whole unit has not been built yet and as such the leg that I have in front of me is running straight from the Arduino which is still being powered from it's USB connection.  It seems that this provides sufficient power for the moment.  When I build the whole unit I will run a seperate power supply for it.

The current plan is to build a walking quadruped with four legs, each comprised of three seperate servos allowing a relatively large degree of freedom in three dimensions.  Currently I am focusing on just getting all of the basic functions working for a single leg and with just two servo's such that there is only freedom to move on a 2D plane. 

I read through all of the first link and it is very informative, thank you for writing it.  I am glad to know how Servo.h works.

Cheers :).  David

DuaneB

Hi,
   I would suggest that you start working from a separate power supply now before you join the 'My Arduino wont connect to USB Anymore' club.

   Its crazy to slowly destroy a USB Port in an expensive computer when all you need is some AA batteries especially as you know you will need external power eventually.

   Just a suggestion,

EDIT : I speak from experience having writting off a USB port in the past two weeks due to a short circuit in a PCB.

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Teacup

Ah,

Thanks for the advice, I will deffinately follow it.  I have an old computer which I may harvest the power supply from (I think i read it is a decent idea to do this from either your page or one stemming from your page).

What is a PCB?

Yeah i run everything through a breadboard and am currently changing everything fairly often so I guess shor circuts are a real risk.

DuaneB

Hi,

PCB is printed circuit board, I was not quite using a pcb but strip board which is a prototyping board that is between bread board and a proper PCB, it let's you take a bread board design and solder it as a long term project.

With regards to servo power, here is a quick demonstration of what happens to you arduino when your servos get going -

http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Duane B
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Teacup

Yeah,

I have seen that before when I was operating my arduino from a battery rather than the USB.  The whole thing shut down for very short periods whenever a load to the servo was applied.

Setting up a power supply is one of my tasks on this project but i am not sure what the best way is.  Is there a configuration of resistors and capacitors which is regarded as especially good?

Cheers,
David


Grumpy_Mike

You don't use resistors in a power supply you use a voltage regulator.

Go Up