0
$\begingroup$

Rosanswers logo

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 text, int sec):
       ^
/home/default_ws/src/err_pack/src/errors_node.cpp:21:7: note:   candidate expects 3 arguments, 2 provided
/home/default_ws/src/err_pack/src/errors_node.cpp:18:9: note: errorLib::errorH::errorH(const errorLib::errorH&)
   class errorH
         ^
/home/default_ws/src/err_pack/src/errors_node.cpp:18:9: note:   candidate expects 1 argument, 2 provided
/home/default_ws/src/err_pack/src/errors_node.cpp:18:9: note: errorLib::errorH::errorH(errorLib::errorH&&)
/home/default_ws/src/err_pack/src/errors_node.cpp:18:9: note:   candidate expects 1 argument, 2 provided

I apologize for the long post. I couldn't figure out a better way to present my problem.


Originally posted by robo_ninja on ROS Answers with karma: 55 on 2017-11-09

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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.


Originally posted by gvdhoorn with karma: 86574 on 2017-11-10

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by robo_ninja on 2017-11-10:
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 ?

Comment by gvdhoorn on 2017-11-10:\

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?

$\endgroup$

Your Answer

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