ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

I need help, I have a basic error with kabuki and cmd_vel

asked 2022-08-29 10:06:58 -0600

Antonio Avezon gravatar image

updated 2022-08-29 21:28:09 -0600

ravijoshi gravatar image

I want to make the robot move towards a wall. When approaching the wall, the robot must turn left to avoid it. Please see the code below. The build has no errors, but it doesn't run.

topics_quiz_node.cpp

#include "geometry_msgs/Twist.h"
#include "ros/init.h"
#include "ros/node_handle.h"
#include "ros/publisher.h"
#include <ros/ros.h>
#include <sensor_msgs/LaserScan.h>
// float vel_pub;

void FuncCall(const sensor_msgs::LaserScan::ConstPtr &msg) {
  ROS_INFO("LaserScan (val,angle)=(%f,%f", msg->range_min, msg->angle_min);
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "topics_quiz_node");
  ros::NodeHandle robot;
  ros::Publisher vel_pub =
      robot.advertise<geometry_msgs::Twist>("/cmd_vel", 100);
  ros::Subscriber laser_sub =
      robot.subscribe("/kobuki/laser/scan", 10, FuncCall);

  while (ros::ok()) {
    geometry_msgs::Twist msg;
    sensor_msgs::LaserScan lsr;
    msg.linear.x = 0;
    msg.angular.x = 0;

    if (lsr.ranges[0] < 1.0) {
      msg.linear.x = 0;
      msg.angular.x = 0.5;
    } else {
      msg.linear.x = 0.5;
      msg.angular.x = 0;
    }
    msg.linear.y = 0;
    msg.linear.z = 0;
    msg.angular.y = 0;
    msg.angular.z = 0;
    vel_pub.publish(msg);
    ros::spin();
  }
  return 0;
}

topics_quiz.launch

<launch>
     <!--topics quiz launch file-->
     <node pkg="topics_quiz" type="topics_quiz_node" name="topics_quiz_node">
     </node>
</launch>

use:

roslaunch topics_quiz topics_quiz.launch

Error :

user:~/catkin_ws$ roslaunch topics_quiz topics_quiz.launch
... logging to /home/user/.ros/log/331fdf62-27a0-11ed-af3f-0242ac150008/roslaunch-2_xterm-8759.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://2_xterm:41403/

SUMMARY
========

PARAMETERS
 * /rosdistro: noetic
 * /rosversion: 1.15.9

NODES
  /
    topics_quiz_node (topics_quiz/topics_quiz_node)

ROS_MASTER_URI=http://2_simulation:11311

process[topics_quiz_node-1]: started with pid [8767]
[topics_quiz_node-1] process has died [pid 8767, exit code -11, cmd /home/user/catkin_ws/devel/lib/topics_quiz/topics_quiz_node __name:=topics_quiz_node __log:=/home/user/.ros/log/331fdf62-27a0-11ed-af3f-0242ac150008/topics_quiz_node-1.log].
log file: /home/user/.ros/log/331fdf62-27a0-11ed-af3f-0242ac150008/topics_quiz_node-1*.log
all processes on machine have died, roslaunch will exit
shutting down processing monitor...
... shutting down processing monitor complete
done

Compilation shows no error as shown below:

user:~/catkin_ws$ catkin_make
Base path: /home/user/catkin_ws
Source space: /home/user/catkin_ws/src
Build space: /home/user/catkin_ws/build
Devel space: /home/user/catkin_ws/devel
Install space: /home/user/catkin_ws/install
####
#### Running command: "cmake /home/user/catkin_ws/src -DCATKIN_DEVEL_PREFIX=/home/user/catkin_ws/devel -DCMAKE_INSTALL_PREFIX=/home/user/catkin_ws/install -G Unix Makefiles" in "/home/user/catkin_ws/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/user/catkin_ws/devel
-- Using CMAKE_PREFIX_PATH: /home/user/catkin_ws/devel;/home/simulations/public_sim_ws/devel;/opt/ros/noetic
-- This workspace overlays: /home/user/catkin_ws/devel;/home/simulations/public_sim_ws/devel;/opt/ros/noetic
-- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.5", minimum required is "3")
-- Using PYTHON_EXECUTABLE: /usr/bin/python3
-- Using Debian Python package layout
-- Using empy: /usr/lib/python3/dist-packages/em.py
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/user/catkin_ws/build/test_results
-- Forcing gtest/gmock from source, though one was otherwise available.
-- Found gtest sources under '/usr/src/googletest': gtests will be built
-- Found gmock sources under '/usr/src/googletest': gmock will be built ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-08-29 21:54:52 -0600

ravijoshi gravatar image

The build has no errors, but it doesn't run.

For readers, in the above quote, "it" is referring to the robot (not the code).

Anyways, it is better to add a print statement (for debugging purposes only) to see what values are exactly being commanded to the robot.

std::cout << "commanded values: " << msg << std::endl; // for debugging purposes only
vel_pub.publish(msg);

The above print statement should give you a great hint about the problem. Next, let's see the following snippet taken from your code:

geometry_msgs::Twist msg;
sensor_msgs::LaserScan lsr;
msg.linear.x = 0;
msg.angular.x = 0;

if (lsr.ranges[0] < 1.0) {
  msg.linear.x = 0;
  msg.angular.x = 0.5;
} else {
  msg.linear.x = 0.5;
  msg.angular.x = 0;
}

The conditional statement checks the first element of the range attribute in the laser scan message. However, this laser scan message is not received from the sensor. Instead, it is created manually. Thus the lsr variable contains default values (probably all zero or null potentially). This is why the conditional statement is not making much sense.

Let see another issue with the code. Please see the following snippet taken from your code:

while (ros::ok()) {
  geometry_msgs::Twist msg;
  // code removed for brevity
  vel_pub.publish(msg);
  ros::spin();
}

The ros:spin() is being called inside a while statement. This is a red flag. It will block the while loop, and thus twist messages can not be published. Instead, you should move your logic from the while loop to the callback. You can publish the twist directly from the callback.

Finally, it would be worth spending time understanding the ROS simply by going through ROS Tutorials. The authors have put immense effort into making these easy-to-understand tutorials.

edit flag offensive delete link more

Comments

1

Thank you very much. I am making progress in learning ROS. The language complicates me (I speak Spanish), but I am making an effort and I am very motivated!

Antonio Avezon gravatar image Antonio Avezon  ( 2022-08-30 10:04:11 -0600 )edit

Finger crossed!!! I am confident you can learn ROS by giving enough effort and keeping the motivation alive. Furthermore, please do not worry about your English language. By the way, please upvote and mark the answer as accepted. You can see these buttons/icons on the left side of this answer.

ravijoshi gravatar image ravijoshi  ( 2022-08-30 20:48:10 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2022-08-29 10:06:58 -0600

Seen: 188 times

Last updated: Aug 29 '22