Rosanswers logo


I'm trying to enhance my skills on ROS with C++. I'm already wrote programs on many languages but since strict C++ architecture is a little bit different for me so that I'm having problems with what I'm trying to.

My aim is to make a communication node that manages serial communication between my mcu and Ubuntu PC. I wrote the communication node in C++ and the simplized version looks like this:

namespace FF
  class SerialComm

                    void ManagePackages();
                    void PrintState();
                    bool WriteMotorPackage(FF::MotorDrive::Request &req,
                        FF::MotorDrive::Response &res);

                    ros::NodeHandle         node;


                    void InitPublishers();
                    void OpenConnection();
                    void SearchPackages();
                    void CatchPackage();
                    void DecodePackage();
                    void PublishFState();
                    void PublishMotorState();

                    float BytesToFloat(unsigned char b0,
                                        unsigned char b1,
                                        unsigned char b2,
                                        unsigned char b3);

                    ros::Publisher          FStatePublisher;
                    ros::Publisher          motorStatePublisher;

                    cereal::CerealPort      device;

                    char                    reply[4096];
                    std::vector<unsigned char>      validData;
                    int                     dataSize;
        struct Motors
          unsigned char _1;
          unsigned char _2;

        struct FState_
          char errors;
                      int distance

                   FState_  FState;
           Motors   FMotors;



What this code doing is First open a serial connection and then in each while cycle to read the serial port with SearchPackages and CatchPackages, finally decode package with DecodePackages function. After package decode I'm using Publish functions. What I do is write the values to FMotors variable like FMotors._1 = incomingData and then publish it with a ROS publisher.

But the problem occurs here. First time it reads the value and write it to variables and publish it. And I can see it from the topic echo. But after first read it never updates the variables. Topic Variables always have the value of the first read. But I'm sure there is no problem with reading and catching true values from serial port. Problem is updating the variables. Maybe because of I'm not very familiar with pointer and static concepts in C++ object style. Whatever it is your ideas and helps will be very useful to solve this. problem.

Originally posted by cheour on ROS Answers with karma: 18 on 2012-08-04

Post score: 0

Original comments

Comment by MarkyMark2012 on 2012-08-05:
hard to say what the problem is from your class description. Are you able to confirm that the serial reading and decoding is working correctly every time? If so then, given what you say sounds like you migh that some static object/variable in your code causing the once only execution.

Comment by Lorenz on 2012-08-06:
How does your main look like? Do you call ros::spin or ros::spinOnce?

Comment by cheour on 2012-10-19:
I solved it! Well it


1 Answer 1


Rosanswers logo

I solved it! Well it looks like I did not familiar with const function parameter. I set function parameters to constant parameters like myFunction(const uint8[] msg) so it is working great now.

Originally posted by cheour with karma: 18 on 2012-10-19

This answer was ACCEPTED on the original site

Post score: 0


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.