Publishing and Subscribing using the same node | exercise Topics_4.4

Hi, i’m doing the 4.4 Topics Quiz about avoiding the wall using Turtlebot.
If i got it right i have to write the whole publisher-subscriber behaviour in the same node aka .cpp script so i’m trying something like this.

int main(int argc, char **argv) {
  ros::init(argc, argv, "topics_quiz_node");
  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe("/kobuki/laser/scan", 1000, wallAvoiderCallback);
  ros::spin();
  return 0;
}

while in my wallAvoiderCallback i’m trying to publish the velocity command based on the distance i sense.

void wallAvoiderCallback(const sensor_msgs::LaserScan::ConstPtr &msg) {
  // ROS_INFO("Received %f", msg->ranges[360]); // it works

  // defining the command object
  geometry_msgs::Twist forward_command;

  if (msg->ranges[360] > 1) {
    forward_command.linear.x = 0.1;
  }

  ros::NodeHandle nh;
  ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/cmd_vel", 1000);
  ros::Rate loop_rate(2);
  pub.publish(forward_command);
  ros::spinOnce();
  loop_rate.sleep();
}

it works for the first 5 seconds and then i get this error which terminates the node.

[topics_quiz_node-1] process has died [pid 38458, exit code -11, cmd /home/user/catkin_ws/devel/lib/topics_quiz/wall_avoider __name:=topics_quiz_node __log:=/home/user/.ros/log/3baf523a-f159-11eb-b55f-0242ac170008/topics_quiz_node-1.log].
log file: /home/user/.ros/log/3baf523a-f159-11eb-b55f-0242ac170008/topics_quiz_node-1*.log

all processes on machine have died, roslaunch will exit
shutting down processing monitor...
... shutting down processing monitor complete
done

Anybody has suggestion about how to complete the quiz? Thank you.

1 Like

Hi @micheleciciollabb12ec819e11468e ,
Please post your log files here as well so that it’s easier to help you debug.

For those who might encounter such issue, i managed to solve it reducing a lot the queue size of the publisher and reducing the frequency of the sensor outputs. It worked even with higher values than QUEUE_SIZE = 2; (like 25/30) but 40 was my upperbound. I am not sure if the SLEEP_RATE has helped with that (i’ve reduced just to have the time to read what’s happening in the terminal outputs).

const int QUEUE_SIZE = 2;
const int SLEEP_RATE = 1;

  ros::Publisher pub =
      nh.advertise<geometry_msgs::Twist>("/cmd_vel", QUEUE_SIZE);
  ros::Rate loop_rate(SLEEP_RATE);
1 Like

thank you for your reply, unfortunately i wasnt able to read such log file, i was receiving empty lines doing

vi /home/user/.ros/log/3baf523a-f159-11eb-b55f-0242ac170008/topics_quiz_node-1*.log

I solved the problem reducing the queue size of the publisher command node.

1 Like