Publishing and subscribing C structures?

asked 2018-02-07 02:42:41 -0500

wojtasso gravatar image

updated 2018-02-07 08:07:18 -0500

lucasw gravatar image

So I want to publish C structure. I have my_pkg.msg

int32 a
float32 b

And C structure:

namespace my {
struct __atribute((packed)) my_pkg {
    int a;
    float b;
};
}

And then I do serialization like in this link

namespace ros
{
namespace message_traits
{
    template<> struct IsFixedSize<my::my_pkg> : public TrueType {};
    template<> struct IsSimple<my::my_pkg> : public TrueType {};

    template<>
        struct MD5Sum<my::my_pkg>
        {
            static const char* value()
            {
                return MD5Sum<my::my_pkg>::value();
            }

            static const char* value(const my::my_pkg& m)
            {
                return MD5Sum<my::my_pkg>::value(m);
            }
        };

    template<>
        struct DataType<my::my_pkg>
        {
            static const char* value()
            {
                return DataType<my::my_pkg>::value();
            }

            static const char* value(const my::my_pkg& m)
            {
                return DataType<my::header_s>::value(m);
            }
        };

    template<>
        struct Definition<my::my_pkg>
        {
            static const char* value()
            {
                return Definition<my::my_pkg>::value();
            }

            static const char* value(const my::my_pkg& m)
            {
                return Definition<my::my_pkg>::value(m);
            }
        };
} // namespace message_traits

namespace serialization
{
    template<>
        struct Serializer<my::my_pkg>
        {
            template<typename Stream, typename T>
                inline static void allInOne(Stream& stream, T item)
                {
                    stream.next(item.a);
                    stream.next(item.b);
                }

            ROS_DECLARE_ALLINONE_SERIALIZER;
        };
} // namespace serialization
} // namespace ros

And I want to publish my message so I do

ros::Publisher pub = n.advertise<my::my_pkg>("test", 1);

And I get Segmentation Fault at this level. Can someone has idea what I'm doing wrong?

EDIT: gdb

Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff1a76700 (LWP 10115)]
[New Thread 0x7ffff1275700 (LWP 10116)]
[New Thread 0x7ffff0a74700 (LWP 10117)]
[New Thread 0x7fffebfff700 (LWP 10122)]

Thread 1 "example" received signal SIGSEGV, Segmentation fault.
0x000055555555b3b9 in ros::message_traits::MD5Sum<my::my_pkg>::value() ()
edit retag flag offensive close merge delete

Comments

1

So what did gdb tell you about the SEGFAULT?

gvdhoorn gravatar image gvdhoorn  ( 2018-02-07 02:51:15 -0500 )edit

How can I use gdb with roscpp to debug code?

wojtasso gravatar image wojtasso  ( 2018-02-07 02:59:15 -0500 )edit
1

wiki/roslaunch - Roslaunch Nodes in Valgrind or GDB.

(we're here to help, but a quick google (ros gdb) returns this as one of the first results for me)

gvdhoorn gravatar image gvdhoorn  ( 2018-02-07 03:50:12 -0500 )edit

I've never run roscpp nodes by roslaunch so it takes me a while.

wojtasso gravatar image wojtasso  ( 2018-02-07 04:19:49 -0500 )edit
1

If you're starting individual nodes, you can also just do something like gdb -ex run --args /path/to/your/node. In a regular Catkin workspace, that could be catkin_ws/devel/lib/$pkg_name/$binary.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-07 04:24:37 -0500 )edit

Thanks, that was really helpful. Here is output:

Thread 1 "example" received signal SIGSEGV, Segmentation fault. 0x000055555555b3b9 in ros::message_traits::MD5Sum<my::my_pkg>::value() ()

wojtasso gravatar image wojtasso  ( 2018-02-07 06:55:41 -0500 )edit
1

Please edit your OP with the new info.

Note that backtraces are really only useful if you compile your program in debug mode.

And the full backtrace is also preferable over just a few lines.

gvdhoorn gravatar image gvdhoorn  ( 2018-02-07 06:57:36 -0500 )edit

Has someone an idea what i'm doing wrong?

wojtasso gravatar image wojtasso  ( 2018-02-07 15:58:58 -0500 )edit