Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Trying to use a struct to simplify a function, please help

  1. #1
    Member
    Join Date
    Dec 2016
    Location
    Buena Park ca
    Posts
    98

    Trying to use a struct to simplify a function, please help

    Pardon my inexperience...

    I got this robot consisting of 8 motors and I am able to send commands to each of them by this function:

    HTML Code:
    void Ev_GoTo_Pos (float M1, float M2, float M3,  float M11, float M12, float M13, float M5, float M6)
    { //Show_Encs();
      if (SendOnce == 0)
      {
        GoTo_Pos (M1, 1);
        GoTo_Pos (M5 * 5, 5);//Neck Tilt
        GoTo_Pos (M11, 11);
    
        delay (4);  // MUST STAY AT 4 MS for long reply
        GoTo_Pos (M2 * 5, 2);
        GoTo_Pos (M6 * 6, 6);  //neck pan
        GoTo_Pos (M12 * 5, 12);
        delay (4);
        GoTo_Pos (M3 * 4, 3);
        //GoTo_Pos (M7 , 7);
        GoTo_Pos (-M13 * 4, 13);
        delay (4);
       
        SendOnce = 1;
      }
    
      ReadENC_Master = 1;
    }
    I would like to simplify the 8 arguments by defining waypoints such as :

    struct Way_Pt {

    float M1;
    float M2;
    float M3;
    float M11;
    float M12;
    float M13;
    float M5;
    float M6;
    };


    Way_Pt Squat = { -47.4, -103.7, 40.6, 75.2, 100.2, 17.2, 2.0, 0 };

    Way_Pt Stand = { -57.9, -84.1, 32.2, 78.8, 80.6, 8.8, -3.9, 2.1};

    then I would go

    Ev_GoTo_Pos (Stand);

    But it does not work, I get:

    cannot convert 'Way_Pt' to 'float' for argument '1' to 'void Ev_GoTo_Pos(float, float, float, float, float, float, float, float)'
    Ev_GoTo_Pos (Stand); // too fast

    Is there smarter way to do this?

    Thanks

  2. #2
    Senior Member
    Join Date
    Oct 2015
    Location
    Roma (IT, EU)
    Posts
    336
    You cannot initalize a struct that way, it's not an array.
    Also your declaration does not define a type.

    This should work (untested):
    Code:
    typedef struct Way_Pt {
    
    float M1;
    float M2;
    float M3;
    float M11;
    float M12;
    float M13;
    float M5;
    float M6;
    } Way_Pt;
    
    Way_Pt Squat;
    
    Squat.M1 = -47.4;
    Squat.M2 = -103.7;
    Squat.M3 = 40.6;
    ...etc...

  3. #3
    Member
    Join Date
    Dec 2016
    Location
    Buena Park ca
    Posts
    98
    Ok I tried this

    Code:
    typedef struct Way_Pt {
    
    float M1;
    float M2;
    float M3;
    float M11;
    float M12;
    float M13;
    float M5;
    float M6;
    } Way_Pt;
    
    Way_Pt Squat;
    
    float SquatVals [] = {-47.4, -103.7, 40.6, 75.2, 100.2, 17.2, 2.0, 0 };
    Squat.M1 = SquatVals [0];
    Squat.M2 = SquatVals [1];
    Squat.M3 = SquatVals [2];
    
    Squat.M11 = SquatVals [3];
    Squat.M12 = SquatVals [4];
    Squat.M13 = SquatVals [5];
    
    Squat.M5 = SquatVals [6];
    Squat.M6 = SquatVals [7];
    
    void Ev_GoTo_Pos_Struct (Squat)
    { //Show_Encs();
      if (SendOnce == 0)
      {
        GoTo_Pos (MotorValues.M1, 1);
        GoTo_Pos (MotorValues.M5 * 5, 5);//Neck Tilt
        GoTo_Pos (MotorValues.M11, 11);
    
        
       
        SendOnce = 1;
      }
    
      ReadENC_Master = 1;
    }
    It would not compile

    I get

    ^
    Sw_Cse:28: error: variable or field 'Ev_GoTo_Pos_Struct' declared void
    void Ev_GoTo_Pos_Struct (Squat)

  4. #4
    Senior Member
    Join Date
    Oct 2015
    Location
    Roma (IT, EU)
    Posts
    336
    You can't declare an argument of a function like that.
    Moreover, you declare Squat but in the function body use MotorValues instead.

    Try:

    Code:
    void Ev_GoTo_Pos_Struct (Way_Pt MotorValues)
    { //Show_Encs();
      if (SendOnce == 0)
      {
        GoTo_Pos (MotorValues.M1, 1);
      [....]

  5. #5
    Member
    Join Date
    Dec 2016
    Location
    Buena Park ca
    Posts
    98
    Actually I discovered that I can use float arrays as function arguments

    Problem solved, forget structs

    Thanks for your help.

    Mitch

  6. #6
    Senior Member
    Join Date
    Dec 2014
    Posts
    304
    You cannot initalize a struct that way, it's not an array.
    Yes, he can. C and C++ totally allows that.

    The actual error is this:

    Code:
    cannot convert 'Way_Pt' to 'float' for argument '1' to 'void Ev_GoTo_Pos(float, float, float, float, float, float, float, float)'
    He updated the code to pass a struct instead of a lot of floats, but he didn't update the receiving function to take a struct instead of a bunch of floats.

    Illustrating with three variables;

    Code:
    struct Waypoint {
      float A;
      float B;
      float C;
    };
    
    void GoToPos(Waypoint const &wp) {
      SendCommand(wp.A, 1);
      SendCommand(wp.B, 2);
      SendCommand(wp.C, 3);
    }
    
    Waypoint wp1 = { 3.0, 4.0, 5.0 };
    Waypoint wp2 = { 1.0, 6.0, -2.0 };
    
    void loop() {
      GoToPos(wp1);
      delay(1000);
      GoToPos(wp2);
      delay(1000);
    }
    Assuming you have a SendCommand() this will work great.
    The main thing is changing the function to take a `Waypoint const &` and then to change the uses of the values to use that struct with `wp.A` instead of just `A`.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •