How to get this dynamic array function to work which includes arrays with servos

So I've been working on gaits and I came up with a way to move servos together at the same time eg. share a loop and write the new positions. To write the code in a clean way I decided to send the combined motion command like this:

void moveServos(int servoGroupArr[][3], int servoGroupArrLen, int motionDuration) {
  // access servoGroupArr[0][1] -> should be startPos for servo1
}


moveServos(
  {
    {servo1, startPos, endPos},
    {servo2, startPos, endPos},
    ...
  },
  arrayAboveLen,
  servoDelay
);

Unfortunately I keep having problems with either of these:

  • cannot convert '' to 'int' or 'int**'
  • subscript int[int]
  • cannot convert 'Servo' to 'int' after initialization

The last error, I tried to initially declare the brace-enclosed initializer as a variable

I've been reading around, can try structs next or I don't know, any ideas are welcome.

The primary array length can change but the inner array length is fixed for now(unless later on I need more info).

Yeah, you can't embed initializer lists in code. C doesn't handle them. Maybe in the year 3000... :slight_smile: Allowing it would have such drastic ramifications.

The correct design pattern for this, is to pass a pointer to a struct that you have initialized with the same initializer list.

I came up with this as a solution, it sucks(not clean) but it does work... the servo to get around that issue of "can't turn Servo into int" I am just going to enumerate them.

Ahh... I'm sad it's still verbose oh well(more lines) granted I'm a noob at this level of language

int servoGroupArr[][3] = {
  {1, startPos, endPos},
  {2, startPos, endPos},
};

moveServos(
  servoGroupArr,
  2,
  10 // motion delay
);

void moveServos(int servoGroupArr[][3], int servoGroupArrLen, int motionDuration) {
  Servo servoToMove = servoGroupArr[loop-index][0] == 1 ? servo1 : servero2;
}

ynoloopy:
Ahh... I'm sad it's still verbose oh well(more lines) granted I'm a noob at this level of language

In my experience verbose is good. It makes it easy (possible?) to understand code when you come back to it 6 months later.

...R

That is a fair point, I'm not sure how unrealistic it is where you can't upload anymore(run out of space). Currently I'm seeing something like 20% storage usage(Nano) but I am also importing two libraries right off the bat.

It will be interesting to see just what kind of processing you can do... example processing IMU data and sending it as well as driving the 9 servos for motion, etc... I intend to do some basic navigation as well... I imagine anything more complex you might have to do on a secondary computer(like an R Pi) or upload it to a nearby computer(like something more advanced than an ultrasonic sensor to stop/change direction but actual planning).

I also have to deal with the Software serial thing(bad data) but also trying not to use it so can send data too(telemetry). All in all pretty exciting... I had to get over some dumb things like sharing ground for servos but yeah it's pretty straight forward.

ynoloopy:
That is a fair point, I'm not sure how unrealistic it is where you can't upload anymore(run out of space).

The verbosity of the c++ code has little relevance for the size of the compiled program.

One way that I discovered that IMHO makes my code less verbose and easier to read is like this snippet. It is a style that is very common in Python and some kind Forum member told me how to do it in C++

for (auto &aS : activeServos) { // iterates over the activeServos array
                            // which happens to be an array of structs
    if (aS.turnoutNdx > 0) { // meaning in use, so skip
        continue;
    }

    for (byte n = 1; n < numMegaServos; n++) {
        auto &tD = turnoutData[n];  // I can use the shorter tD in place of turnoutData[n]
        if (tD.moveInProgress == true or tD.curState == tD.command ) { 
            continue;
        }

...R

The verbosity of the c++ code has little relevance for the size of the compiled program.

Oh that's interesting, I'm glad to hear that, guess it's due to optimization from the compiler? Also the words we use "high level" gets condensed down to machine language maybe idk.

I will read up on that, I've seen some of these things (overloading? the &as).

I do have to factor that in at some point, to at least keep position of current motion so then transitioning/doing a new motion, I don't just jump to the new/default positions/can use current position.

Array of structs

Yeah I'll have to check that out. I think that will be clean, I can keep properties like initial position, current position, etc...

aS.turnoutNdx

This too. I think... the struct is like an OOP thing with internal "private variables" idk... could be completely off but yeah eg. the tD.moveInProgress

Anyway thanks for the new ideas