Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroXRCEDDSGen pollutes global defines with constants #81

Open
Ryanf55 opened this issue Dec 3, 2024 · 0 comments
Open

MicroXRCEDDSGen pollutes global defines with constants #81

Ryanf55 opened this issue Dec 3, 2024 · 0 comments

Comments

@Ryanf55
Copy link
Contributor

Ryanf55 commented Dec 3, 2024

Hello,

I am looking to see if there an alternative to message constants that aren't global defines.

Sample Message from ROS REP-147:

This generates GlobalPosition.h

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*!
 * @file GlobalPosition.h
 * This header file contains the declaration of the described types in the IDL file.
 *
 * This file was generated by the tool microxrceddsgen.
 */

#ifndef _GlobalPosition_H_
#define _GlobalPosition_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>
#include <stdbool.h>
#include "geometry_msgs/msg/Twist.h"
#include "std_msgs/msg/Header.h"

#define FRAME_GLOBAL_INT 5
#define FRAME_GLOBAL_REL_ALT 6
#define FRAME_GLOBAL_TERRAIN_ALT 11
#define IGNORE_LATITUDE 1
#define IGNORE_LONGITUDE 2
#define IGNORE_ALTITUDE 4
#define IGNORE_VX 8
#define IGNORE_VY 16
#define IGNORE_VZ 32
#define IGNORE_AFX 64
#define IGNORE_AFY 128
#define IGNORE_AFZ 256
#define FORCE 512
#define IGNORE_YAW 1024
#define IGNORE_YAW_RATE 2048
typedef struct ardupilot_msgs_msg_GlobalPosition
{
    std_msgs_msg_Header header;

    uint8_t coordinate_frame;

    uint16_t type_mask;

    double latitude;

    double longitude;

    float altitude;

    geometry_msgs_msg_Twist velocity;

    geometry_msgs_msg_Twist acceleration_or_force;

    float yaw;

} ardupilot_msgs_msg_GlobalPosition;

struct ucdrBuffer;
bool ardupilot_msgs_msg_GlobalPosition_serialize_topic(struct ucdrBuffer* writer, const ardupilot_msgs_msg_GlobalPosition* topic);
bool ardupilot_msgs_msg_GlobalPosition_deserialize_topic(struct ucdrBuffer* reader, ardupilot_msgs_msg_GlobalPosition* topic);
uint32_t ardupilot_msgs_msg_GlobalPosition_size_of_topic(const ardupilot_msgs_msg_GlobalPosition* topic, uint32_t size);

#ifdef __cplusplus
}
#endif

#endif // _GlobalPosition_H_

AND

GlobalPosition.c

// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*!
 * @file GlobalPosition.h
 * This header file contains the declaration of the described types in the IDL file.
 *
 * This file was generated by the tool microxrceddsgen.
 */

#include "GlobalPosition.h"

#include <ucdr/microcdr.h>
#include <string.h>


bool ardupilot_msgs_msg_GlobalPosition_serialize_topic(ucdrBuffer* writer, const ardupilot_msgs_msg_GlobalPosition* topic)
{
    bool success = true;

        success &= std_msgs_msg_Header_serialize_topic(writer, &topic->header);

        success &= ucdr_serialize_uint8_t(writer, topic->coordinate_frame);

        success &= ucdr_serialize_uint16_t(writer, topic->type_mask);

        success &= ucdr_serialize_double(writer, topic->latitude);

        success &= ucdr_serialize_double(writer, topic->longitude);

        success &= ucdr_serialize_float(writer, topic->altitude);

        success &= geometry_msgs_msg_Twist_serialize_topic(writer, &topic->velocity);

        success &= geometry_msgs_msg_Twist_serialize_topic(writer, &topic->acceleration_or_force);

        success &= ucdr_serialize_float(writer, topic->yaw);


    return success && !writer->error;
}

bool ardupilot_msgs_msg_GlobalPosition_deserialize_topic(ucdrBuffer* reader, ardupilot_msgs_msg_GlobalPosition* topic)
{
    bool success = true;

        success &= std_msgs_msg_Header_deserialize_topic(reader, &topic->header);

        success &= ucdr_deserialize_uint8_t(reader, &topic->coordinate_frame);

        success &= ucdr_deserialize_uint16_t(reader, &topic->type_mask);

        success &= ucdr_deserialize_double(reader, &topic->latitude);

        success &= ucdr_deserialize_double(reader, &topic->longitude);

        success &= ucdr_deserialize_float(reader, &topic->altitude);

        success &= geometry_msgs_msg_Twist_deserialize_topic(reader, &topic->velocity);

        success &= geometry_msgs_msg_Twist_deserialize_topic(reader, &topic->acceleration_or_force);

        success &= ucdr_deserialize_float(reader, &topic->yaw);


    return success && !reader->error;
}

uint32_t ardupilot_msgs_msg_GlobalPosition_size_of_topic(const ardupilot_msgs_msg_GlobalPosition* topic, uint32_t size)
{
    uint32_t previousSize = size;
        size += std_msgs_msg_Header_size_of_topic(&topic->header, size);

        size += ucdr_alignment(size, 1) + 1;

        size += ucdr_alignment(size, 2) + 2;

        size += ucdr_alignment(size, 8) + 8;

        size += ucdr_alignment(size, 8) + 8;

        size += ucdr_alignment(size, 4) + 4;

        size += geometry_msgs_msg_Twist_size_of_topic(&topic->velocity, size);

        size += geometry_msgs_msg_Twist_size_of_topic(&topic->acceleration_or_force, size);

        size += ucdr_alignment(size, 4) + 4;


    return size - previousSize;
}

The defines in header then conflict with an enum field in another library:

https://github.com/ArduPilot/ardupilot/blob/eaf20db6ea635eabfc1a8b85d5dca63399a174e6/libraries/AP_Servo_Telem/AP_Servo_Telem.h#L45

Are there any other alternatives to global defines for these constants that could be adopted?

In ArduPilot, we can use C++, so we can use static constexpr and wrap things in namespace declarations. Any ideas? On a large codebase, the usage of defines for constants that are normally scoped to the namespace of the message is not feasible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant