Rostopic pub change subcriber arduino

asked 2022-08-07 15:07:05 -0500

Heqas gravatar image

Hey guys, I'm trying to create an algorytm which will allow to chagne subscriber by typing a rostopic pub in terminal. The code is created on Arduino Portenta and I'm using rosserial library. To allow the connection between arduino and ROS I'm using serial.launch file. Right now I have got a problem with changing the subscriber. When I publish the letter there are no changes but after I restart the launch file sometimes I got the subscriber and sometimes not. I think the problem is with string buffor maybe it is overloaded somehow. The code:

#include <ros.h>
#include <std_msgs/String.h>
#include <geometry_msgs/Twist.h>

ros::NodeHandle nh;

void messageCb( const geometry_msgs::Twist& msg) {

}
 void messageCb2( const geometry_msgs::Twist& msg) {

  }
   ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel", &messageCb );

    ros::Subscriber<geometry_msgs::Twist> sub2("local_nav", &messageCb2 );

      void pwm( const std_msgs::String& cmd_msg)
    {
   if (cmd_msg.data[0]=='q')
    {
        nh.subscribe(sub);
   }

        else if (cmd_msg.data[0]=='a')
   {
      nh.subscribe(sub2);
      }

    else if (cmd_msg.data[0]=='z')
      {

       }
      }
        ros::Subscriber<std_msgs::String> sub1("servo", pwm);


     void setup() 
    {
        Serial.begin(115200);

          nh.initNode();
         nh.subscribe(sub1); 
          }


         void loop() 
            { 
             nh.spinOnce();
             delay(10);
               }
edit retag flag offensive close merge delete

Comments

A new subscriber is created every time a message is available in the "servo" topic. I am really concerned and worried about the CPU. This is not a good design choice. It would be best if you created those subscribers once, and then based on the callback message, you can apply the logic.

ravijoshi gravatar image ravijoshi  ( 2022-08-08 08:26:03 -0500 )edit