Problem with servo inside a class

I have been trying to write a library for an ultrasound sensor mounted on a servo and I am going a little bit crazy.
I have written a very basic sketch to nail down the problem, so if someone can help, he or she is more than welcome.

First, a basic sketch that works (obviously).

#include <Servo.h>

Servo servo;

void setup() {
  servo.attach(10);
}

void loop() {
  servo.writeMicroseconds(1540);
  delay(1000);
  servo.writeMicroseconds(940);
  delay(1000);
  servo.writeMicroseconds(2130);
  delay(1000);
}

Then, with the servo defined inside a class :

#include <Servo.h>

class Test {
private:
  Servo servo;
  unsigned char _servo_pin;

public:
  Test(unsigned char servo_pin) {
    _servo_pin = servo_pin;
    servo.attach(10);
  }

  void front() {
    servo.writeMicroseconds(1540);

  }  
  void left() {
    servo.writeMicroseconds(940);

  }  
  void right() {
    servo.writeMicroseconds(2130);

  }
};

Test my_servo(10);

void setup() {
}

void loop() {
  my_servo.front();
  delay(1000);
  my_servo.left();
  delay(1000);
  my_servo.right();
  delay(1000);
}

The servo does not move at all…

Please help …

In your class code you have to qualify the scope. eg. Test::thefunction right…left…ect. The class definition is in a header bearing the classname.h

I realize this is only prototype code, but why allow the class to accept a pin number, and then use pin ten anyway? Why wrap an existing class like this at all?

I think he is exploring class libraries. read this thread and review the code in the cpp and h files. It was quite the learing curve. http://arduino.cc/forum/index.php/topic,84812.0.html

I have simplfied the code to a maximum to point to the problem.

The class is directly defined in the Arduino sketch, so there is no .h or .cpp involved .

In this case, adding the scope generates an error :

servo_test_2:13: error: extra qualification 'Test::' on member 'front'.

Anyway, the behaviour is the same.

Wait a minute, in your first post you said the servo does not move, now you're saying the sketch doesn't even compile?

declaring a variable type does not cause the creation of the class instance, merely establishes a pointer variable for the instance once created. Try this:

void setup()
    {
      Test my_class = new Test;
    }

@AWOL

I have tried to remove all of the possible doubts. So I attached the servo with a constant value instead of a parameter.

The library is supposed to wrap the servo and the ultrasound sensor functions. The code given is just there to point to my problem.

When the servo is defined inside the class, it does not react as expected.

I am going to rewrite the example in a more conventional way (.h/.cpp) and post it.

Try this:

#include <Servo.h>

class Test {
private:
  Servo servo;
  unsigned char _servo_pin;

public:
  Test(unsigned char servo_pin) {
    _servo_pin = servo_pin;
    servo.attach(10);
  }

  void front() {
    servo.writeMicroseconds(1540);

  }  
  void left() {
    servo.writeMicroseconds(940);

  }  
  void right() {
    servo.writeMicroseconds(2130);

  }
};

Test my_servo(10);

void setup() {
  my_servo= new test;
}

void loop() {
  my_servo.front();
  delay(1000);
  my_servo.left();
  delay(1000);
  my_servo.right();
  delay(1000);
}

ajofscott: ...

That's not correct and isn't even close to compiling. new returns a pointer -- does my_servo look like a pointer?

OP, you may want to check out initialization lists. I'm not completely sure if they're necessary for constructors that take no arguments (like Servo) but I know they are necessary for constructors that do take arguments.

This is the code rewritten with .h/.cpp library .

Test.h

#ifndef Test_h
#define Test_h
class Test
{
public:
	Test(unsigned char _servo_pin);
	void front();
	void right();
	void left();
	
	
private:
	int _servo_pin;
	
	};

#endif

Test.cpp

#include "Arduino.h"
#include <Servo.h>
#include "Test.h"


unsigned char _servo_pin;
Servo _servo;
	
Test::Test(unsigned char servo_pin ) {
	_servo_pin = servo_pin;
	_servo.attach(_servo_pin);
}

void Test::front() {
	_servo.writeMicroseconds(1540);
	}

void Test::right() {
	_servo.writeMicroseconds(940);
	}

void Test::left() {
	_servo.writeMicroseconds(2130);
	}

and the Arduino sketch.

#include <Servo.h>
#include <Test.h>

Test my_servo(10);

void setup(){
  
}
void loop() {

  my_servo.front();
  delay(1000);
  my_servo.left();
  delay(1000);
  my_servo.right();
  delay(1000);
}

It does not work either …

Why the separate instances of _servo_pin?

@AWOL

You are right. I wrote the code a bit too fast.
This is a corrected version.

Test.h

#include <Servo.h>
#ifndef Test_h
#define Test_h
class Test
{
public:
	Test(unsigned char _servo_pin);
	void front();
	void right();
	void left();
	
	
private:
	int _servo_pin;
	Servo _servo;
	
	};

#endif

and Test.cpp

#include "Arduino.h"
//#include <Servo.h>
#include "Test.h"


//unsigned char _servo_pin;
//Servo _servo;
	
Test::Test(unsigned char servo_pin ) {
	_servo_pin = servo_pin;
	_servo.attach(_servo_pin);
}

void Test::front() {
	_servo.writeMicroseconds(1540);
	}

void Test::right() {
	_servo.writeMicroseconds(940);
	}

void Test::left() {
	_servo.writeMicroseconds(2130);
	}

Not working either.

Ok I have the class lib version working, however the pulse rates are way too high and the servo is complaining(singing) and getting hot. I have viewed the waveforms on the oscillosope and the app is running correctly.
frame dead times for a servo should be around 10-15 ms

Still wrong...

I changed _servo_pin to an int in all files. I also did the test with unsigned char.

Still not working.

If that can help, when I start the Arduino, the servo goes to one of the side fully, as if there was no pulse on the PWM output. I don't have a logic analyzer, so I can't be sure.

I have done the testing with two servos, a Futaba S3003 and a cheap microservo.

The microservo goes to one side fully, and the S3003 wobbles.

I have gone another way and have derived my new class from Servo.

Test_2.h

#include <Servo.h>
#ifndef Test_2_h
#define Test_2_h
class Test_2 : public Servo
{
public:
	Test_2();
	void front();
	void right();
	void left();
	
	
private:
//	unsigned char  _servo_pin;
//	Servo _servo;
	
	};

#endif

Test_2.cpp

#include "Arduino.h"
//#include <Servo.h>
#include "Test_2.h"


//unsigned char _servo_pin;
//Servo _servo;
	
Test_2::Test_2() {
//	_servo_pin = servo_pin;
//	_servo.attach(_servo_pin);
}

void Test_2::front() {
	writeMicroseconds(1540);
	}

void Test_2::right() {
	writeMicroseconds(940);
	}

void Test_2::left() {
	writeMicroseconds(2130);
	}

And the Arduino sketch

#include <Servo.h>
#include <Test_2.h>

Test_2 my_servo;

void setup(){
  my_servo.attach(10);
}
void loop() {

  my_servo.front();
  delay(1000);
  my_servo.left();
  delay(1000);
  my_servo.right();
  delay(1000);
}

It works (not surprising, I guess).

Now I have to add the ultrasonic functions…

Has anyone worked with multiple inheritance.
I would like to make an Ultrasound_Sensor class and derive my ultrasonic scanner from both classes Servo and Ultrasound_Sensor.

Thanks anyway to all of you.

Grounds all commoned correctly?

Yes. I am using a DFRduino Romeo with connector for servos. Anyway, my new class works allright now and the sweep example works too.

I have to look for multiple inheritance tutorials now.... I think it is cleaner that way than with a private servo variable inside a class.

Have a nice day.

Any idea why his class wrapper drove the frame rate out of sight high?