Ask Your Question
0

catkin_make errors while subscribing to diagnostic topic

asked 2017-11-09 15:38:18 -0500

robo_ninja gravatar image

updated 2017-11-09 15:41:14 -0500

I am trying to subscribe to diagnostic topic and based on the level published by the topic, I am trying to start and stop my operation. I have created 2 functions requestStart and requestNotStart for these purposes. I am getting the CMake errors which I am unable to fix (posted at the end of the post).

Following is my code -

using namespace std;
namespace errorLib
{
  class errorH
  {
    public:
      errorH(EC value, string text, int sec):
        value_(value),
        text_(text),
        is_started_(false),
        sec_(sec)
        {
        }
      EC value_;
      string text_;
      bool is_started_;
      int retry_seconds_;
  };
}
 void diagnosticCallback(const diagnostic_msgs::DiagnosticArrayPtr &diags_msg)
  {
    if (diags_msg[0].level == diagnostic_msgs::DiagnosticStatus::ERROR)
      errorcode = NodeToErrorCode[diags_msg[0].name];
      requestStart();

    else (diags_msg[0].level == diagnostic_msgs::DiagnosticStatus::OK)
      errorcode = NodeToErrorCode[diags_msg[0].name];
      requestNotStart();
  }

int requestStart(errorLib::errorH& c)
{
  if(!c.is_started_)
    retry_seconds_ = 2;
    // Start timer
    // Timer expired
  c.is_started_ = true;
  return 0;
}
int requestNotStart(errorLib::errorH& c)
{
   return 0;
}
int main(int argc, char **argv)
{
  errorLib::errorH c("For starting/ending", 2);
  requestStart(c);
  requestNotStart(c);
  ros::init(argc, argv, "<node_name>");
  ros::NodeHandle nh;
  std::map<std::string, int> NodeToErrorCode =
    {{"rosout", 1},
     {"<Node1>", 2},
     {"<Node2>", 3}};

  ros::Subscriber sub = nh.subscribe("diagnostics", 1000, diagnosticCallback);
  ros::spin();
  return 0;
}

Following are the errors for reference -

In function ‘void diagnosticCallback(const DiagnosticArrayPtr&)’:
/home/default_ws/src/err_pack/src/errors_node.cpp:52:21: error: invalid use of ‘boost::detail::sp_array_access<diagnostic_msgs::DiagnosticArray_<std::allocator<void> > >::type {aka void}’
     if (diags_msg[0].level == diagnostic_msgs::DiagnosticStatus::ERROR)
                     ^
/home/default_ws/src/err_pack/src/errors_node.cpp:54:7: error: ‘errorcode’ was not declared in this scope
       errorcode = NodeToErrorCode[diags_msg[0].name];
       ^
/home/default_ws/src/err_pack/src/errors_node.cpp:54:19: error: ‘NodeToErrorCode’ was not declared in this scope
       errorcode = NodeToErrorCode[diags_msg[0].name];
                   ^
/home/default_ws/src/err_pack/src/errors_node.cpp:54:47: error: invalid use of ‘boost::detail::sp_array_access<diagnostic_msgs::DiagnosticArray_<std::allocator<void> > >::type {aka void}’
       errorcode = NodeToErrorCode[diags_msg[0].name];
                                               ^
/home/default_ws/src/err_pack/src/errors_node.cpp:55:18: error: ‘requestStart’ was not declared in this scope
       requestStart();
                  ^
/home/default_ws/src/err_pack/src/errors_node.cpp:58:26: error: invalid use of ‘boost::detail::sp_array_access<diagnostic_msgs::DiagnosticArray_<std::allocator<void> > >::type {aka void}’
     else if (diags_msg[0].level == diagnostic_msgs::DiagnosticStatus::OK)
                          ^
/home/default_ws/src/err_pack/src/errors_node.cpp:59:47: error: invalid use of ‘boost::detail::sp_array_access<diagnostic_msgs::DiagnosticArray_<std::allocator<void> > >::type {aka void}’
       errorcode = NodeToErrorCode[diags_msg[0].name];
                                               ^
/home/default_ws/src/err_pack/src/errors_node.cpp:60:21: error: ‘requestNotStart’ was not declared in this scope
       requestNotStart();
                     ^
/home/default_ws/src/err_pack/src/errors_node.cpp: In function ‘int requestStart(errorLib::errorH&)’:
/home/default_ws/src/err_pack/src/errors_node.cpp: In function ‘int main(int, char**)’:
/home/default_ws/src/err_pack/src/errors_node.cpp:93:53: error: no matching function for call to ‘errorLib::errorH::errorH(const char [21], int)’
   errorLib::errorH c("For starting/ending", 2);
                                                     ^
/home/default_ws/src/err_pack/src/errors_node.cpp:93:53: note: candidates are:
/home/default_ws/src/err_pack/src/errors_node.cpp:21:7: note: errorLib::errorH::errorH(EC, std::string, int)
       errorH(EC value, string ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2017-11-10 01:17:51 -0500

gvdhoorn gravatar image

updated 2017-11-10 01:19:59 -0500

First: this is all basic C++ and should have been asked on a more suitable forum (like Stack Overflow or similar). If you're new with C++ (and ROS), I would really recommend not trying to learn the two at the same time, as it's going to complicate things.

Having said that, see below for some comments on why the compiler is complaining.

In function ‘void diagnosticCallback(const DiagnosticArrayPtr&)’:
/home/default_ws/src/err_pack/src/errors_node.cpp:52:21: error: invalid use of ‘boost::detail::sp_array_access<diagnostic_msgs::DiagnosticArray_<std::allocator<void> > >::type {aka void}’
     if (diags_msg[0].level == diagnostic_msgs::DiagnosticStatus::ERROR)
                     ^

diag_msgs is of pointer type, so right now you're indexing against the raw pointer itself, not what it is pointing to (which is a diagnostic_msgs/DiagnosticArray).

If you want access to the array with messsages, I believe you should also be accessing the status field.

/home/default_ws/src/err_pack/src/errors_node.cpp:54:7: error: ‘errorcode’ was not declared in this scope
       errorcode = NodeToErrorCode[diags_msg[0].name];
       ^

There is no errorcode variable anywhere in the code you show, so this is the compiler telling you that.

/home/default_ws/src/err_pack/src/errors_node.cpp:54:19: error: ‘NodeToErrorCode’ was not declared in this scope
       errorcode = NodeToErrorCode[diags_msg[0].name];
                   ^

There is no ‘NodeToErrorCode’ variable anywhere in the code you show, so this is the compiler telling you that.

This statement is also confusing: from the capitalisation I expect this to be a constructor, but the [ make it seem like this is a std::map or something similar.

Suggestion: use lower case for variable names only (ROS uses the Google C++ code style convention).

/home/default_ws/src/err_pack/src/errors_node.cpp:54:47: error: invalid use of ‘boost::detail::sp_array_access<diagnostic_msgs::DiagnosticArray_<std::allocator<void> > >::type {aka void}’
       errorcode = NodeToErrorCode[diags_msg[0].name];
                                               ^

This is the same problem as on line 54.

(I'm going to skip the other occurrences of this)

/home/default_ws/src/err_pack/src/errors_node.cpp:55:18: error: ‘requestStart’ was not declared in this scope
       requestStart();
                  ^

That function is defined later, so the compiler can't find it at this point.

Additionally requestStart(..) takes an errorLib::errorH& argument that you are not providing.

/home/default_ws/src/err_pack/src/errors_node.cpp:60:21: error: ‘requestNotStart’ was not declared in this scope
       requestNotStart();
                     ^

Same problem as on line 55.

/home/default_ws/src/err_pack/src/errors_node.cpp: In function ‘int requestStart(errorLib::errorH&)’:
/home/default_ws/src/err_pack/src/errors_node.cpp: In function ‘int main(int, char**)’:
/home/default_ws/src/err_pack/src/errors_node.cpp:93:53: error: no matching function for call to ‘errorLib::errorH::errorH(const char [21], int)’
   errorLib::errorH c("For starting/ending", 2);
                                                     ^

The ctor you've defined takes three args (EC value, string text, int sec), you're supplying it with two.

edit flag offensive delete link more

Comments

Thank you @gvdhoorn for specifying each errors. I understand, I should have posted it in a more specific way. I was able to rectify most of them. I have a follow up question - In line 52, when I am trying to access the array is this the best way to do that - diags_msg->status[0].level ?

robo_ninja gravatar image robo_ninja  ( 2017-11-10 09:07:38 -0500 )edit

I am trying to access the array is this the best way to do that - diags_msg->status[0].level ?

It's one way to do that yes. I don't know whether it is the best way.

Do you always only want to access the first message?

gvdhoorn gravatar image gvdhoorn  ( 2017-11-10 14:05:12 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2017-11-09 15:38:18 -0500

Seen: 138 times

Last updated: Nov 10 '17