Pages: [1]   Go Down
Author Topic: Dynamic instantiation of objects?  (Read 868 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey folks, got a really newbie question here. I am working with the Servo library, and was wondering if there is a way for me to dynamically instantiate Servo objects.

In most examples, I see static instantiation of Servo objets, like this:

Servo myServo;

Which is cool when you know how many servos you're going to have. But is there a way for me to instantiate a number of Servo objects based on a variable, such as (I'm using C++ syntax here):

Servo arrayOfServos[MAX_SERVOS];
if (numServos < MAX_SERVOS)
{
  for (int i = 0; i < numServos; i++)
  {
    Servo newServo = new Servo();
    newServo.attach(somePin);
    newServo.write(0);
    arrayOfServos = newServo;
  }
}

Any help will be appreciated here!

Thanks,
- K.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50238
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Which is cool when you know how many servos you're going to have. But is there a way for me to instantiate a number of Servo objects based on a variable, such as (I'm using C++ syntax here):
You could, but, really, does that make sense?

The new operator returns a pointer of the appropriate type, so your example is incorrect.

The real question is, aside from just randomly waving servos around, what practical purpose is there in not knowing about the number of servos in advance?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can create objects dynamically, using pointers to refer to them subsequently, but that doesn't seem to be what you need here.

I think you just need to define an array of servos and then attach each servo to the associated pin.

Code:
// incomplete, untested
Servo servo[MAX_SERVOS];
const byte SERVO_PIN[MAX_SERVOS] = { 2, 3, 4, 5, 6}; // etc
for (int i = 0; i < MAX_SERVOS; i++)
{
    servo[i].attach(SERVO_PIN[i]);
}
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5236
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The syntax will be a bit different:

Code:
Servo *arrayOfServos[MAX_SERVOS];
if (numServos < MAX_SERVOS)
{
  for (int i = 0; i < numServos; i++)
  {
    Servo *newServo = new Servo();
    newServo->attach(somePin);
    newServo->write(0);
    arrayOfServos[i] = newServo;
  }
}

You can allocate new object this way. Unfortunately there is an error in the free() function of the C library in current IDEs resulting in a memory leak, so if you allocate and delete this objects often you probably run out of memory relatively soon. The Arduino has no memory management unit as todays PCs all have so even without that bug you have to take care to not fragment your memory too fast with such operations.

And as PeterH pointed out, the code as you presented it, doesn't need dynamical allocation.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26488
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Servo arrayOfServos[MAX_SERVOS];
if (numServos < MAX_SERVOS)
Besides, you're potentially wasting one object's worth.
« Last Edit: February 12, 2013, 04:35:13 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey folks, thanks for the replies. Apologies for the syntax errors - I switched over to C# for a few years and haven't written C/C++ in a while.

I was actually not aware that the compiler supports pointers as I didn't notice it being mentioned in the Arduino reference. What I was (more or less) trying to support was this:

void AssignServosToPins(int numServos, int *pinAssignments)
{
  int i;
  Servo **servoArray = (Servo **)malloc(numServos * sizeof(Servo *));
  for (i = 0 ; i < numServos; i++)
  {
    servoArray = new Servo();
    servoArray->attach(pinAssignments);
    servoArray->write(0);
  }
}

I'm not too worried about the free() bug as this will be a one-time initialization only. As I mentioned, I don't know the capabilities of the compiler too well, so I was not sure if malloc(), sizeof() and the "pointer-to-pointer" double inference syntax are properly supported in the compiler.

Appreciate everyone's help. I guess I'll just have to do some trial-and-error.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50238
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I guess I'll just have to do some trial-and-error.
Good idea. Just don't try to reference servoArray outside of that function.
Logged

Pages: [1]   Go Up
Jump to: