C++ Structure Intializing Help

Status
Not open for further replies.

mjs513

Senior Member+
Know this is not a real Teensy issue but I am completely stumped on this one. Just a little background - I am making a homebrew pseudo-autopilot using a T3.5 and working on implementing Mavlink protocol interfacing to APM Planner using a ESP8266. Its actually working decently - I am able to transmit IMU and GPS messages to the ground station gui and can even receive joystick commands through the ground station to the autopilot.

Ok so here's my problem, I am trying to set it up so I can modify parameters from the GUI and so I am trying to create a structure array to hold what I need to set the parameters but I keep running into errors on the structure. I tried googling and think I am doing it right but I know I am missing something so any help would be appreciated. Here is the code and the error message:

Code:
struct local_param_set
{
  char param_id[16];  ///< Onboard parameter id, terminated by NULL if the length is less than 16 human-readable chars and WITHOUT null termination (NULL) byte if the length is exactly 16 chars - applications have to provide 16+1 bytes storage if the ID is stored as string
  uint8_t param_type; ///< Onboard parameter type: see the MAV_PARAM_TYPE enum for supported data types.
  float param_value;  ///< Onboard parameter value
};

struct mavlink_param_set local_param[4] =
    { 
      {"ARMING_REQUIRE" , MAV_PARAM_TYPE_UINT8 , 0 },
      {"INITIAL_MODE" , MAV_PARAM_TYPE_UINT8 , 0 },
      {"ARMING_CHECK" , MAV_PARAM_TYPE_UINT8 , 0 },
      {"CRUISE_SPEED" , MAV_PARAM_TYPE_FLOAT, 2.0 }
    };


Error:
Code:
sMavlink.h:39: error: 'local_param' does not name a type
 local_param[] =
 ^

'local_param' does not name a type

I have tried variations on a theme but just gives me the same or similar errors.

Thanks

Mike
 
shouldn’t it be:

Code:
struct mavlink_param_set
      {
......
};

struct mavlink_param_set local_param[4];
 
In cases like this I normally typedef my structure...

This compiles:
Code:
#define MAV_PARAM_TYPE_UINT8 0
#define MAV_PARAM_TYPE_FLOAT 1
typedef struct local_param_set
{
  char param_id[16];  ///< Onboard parameter id, terminated by NULL if the length is less than 16 human-readable chars and WITHOUT null termination (NULL) byte if the length is exactly 16 chars - applications have to provide 16+1 bytes storage if the ID is stored as string
  uint8_t param_type; ///< Onboard parameter type: see the MAV_PARAM_TYPE enum for supported data types.
  float param_value;  ///< Onboard parameter value
} local_params_type;

local_params_type local_param[4] =
    { 
      {"ARMING_REQUIRE" , MAV_PARAM_TYPE_UINT8 , 0 },
      {"INITIAL_MODE" , MAV_PARAM_TYPE_UINT8 , 0 },
      {"ARMING_CHECK" , MAV_PARAM_TYPE_UINT8 , 0 },
      {"CRUISE_SPEED" , MAV_PARAM_TYPE_FLOAT, 2.0 }
    };

void setup() {
  
}

void loop() {
  
}
 
Ok. Here is one for you. I had that structure in the .h file that I use for all the mavlink related stuff in the sketch and I did an include in the main sketch. It would always throw an error in both cases (the one from tonton81 and the one from KurtE). I moved the structure declaration to the .ino (main sketch) and it now compiles fine - just have to do the declares for the mav types :).

This one was driving me crazy all morning. Didn't think about moving it to the main.sketch. Learn something new everyday.

Thank you all very much.
Mike
 
if it's outside the class scope it's considered global
you perhaps got the error because the struct was declared within the class, if so, you would have to access it via the scope of the class
 
That's the strange thing, while its in a .h file its not defined in a class. It should be global ? Probably just don't really understand fully how .h's should work.
 
Yeah, as an example , if you check SPI_MST .h file you will see AsyncMST is a struct outside of the class definition, located just below the #include's
Code:
struct AsyncMST {
  uint16_t packetID = 0;
  uint8_t error = 0, slave = -1, port = 0; // slave identification added by linarism
};

this allows both the callback to include it as an overload and the sketch as well
 
You might be sorry you asked for the whole code, or least the two offending pieces - oh never mind here is the whole zip. I did make one change to get it to work properly - had to move the send_parameter function from the sMavlink.h file to the main sketch. Think it would probably be better to just make them ino files.
 

Attachments

  • MavlinkRoverMavESPv2.zip
    349.9 KB · Views: 87
You might be sorry you asked for the whole code, or least the two offending pieces - oh never mind here is the whole zip. I did make one change to get it to work properly - had to move the send_parameter function from the sMavlink.h file to the main sketch. Think it would probably be better to just make them ino files.

sorry not quite sure here exactly your usage of the .h file versus usage in the .ino file. Especially since there are is code functions defined here. I understand if for example this is a library and you wish for the code to compile with different parameters...

In cases like that, where I wish to have the code to create the array in your .INO file but functions in the header file wish to access the data... Sort of a chicken and egg on how/where to define things.
Maybe in cases like that, you have the structure defined in your .ino. Then have a method/function to your library code to set the parameter list, and then have your library functions work of the pointer...
 
sorry not quite sure here exactly your usage of the .h file versus usage in the .ino file.
To be honest Kurt, absolutely no reason for it not to be an .INO file. Think it was just me trying to group things together to clean up everything in one huge file and not thinking about the ramifications.
 
Status
Not open for further replies.
Back
Top