callback function doesnt printout the subscribe msg
hello guys iam new at ROS and iam trying to print the msg and some how it doesnt print it and i cant find where is the problem this is my code
#include <moveit/move_group_interface/move_group_interface.h>
#include <moveit/planning_scene_interface/planning_scene_interface.h>
#include <moveit_msgs/DisplayRobotState.h>
#include <moveit_msgs/DisplayTrajectory.h>
#include <moveit_msgs/AttachedCollisionObject.h>
#include <moveit_msgs/CollisionObject.h>
#include <moveit_visual_tools/moveit_visual_tools.h>
#include <iostream>
#include <moveit/robot_model/robot_model.h>
#include <moveit/robot_state/robot_state.h>
#include <ros/ros.h>
#include "geometry_msgs/PointStamped.h"
#include <geometry_msgs/PoseStamped.h>
#include <geometry_msgs/Quaternion.h>
#include <tf/transform_broadcaster.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <sensor_msgs/PointCloud.h>
#include <sensor_msgs/PointCloud2.h>
#include <sensor_msgs/point_cloud_conversion.h>
#include <tf/LinearMath/Quaternion.h>
#include <tf/transform_datatypes.h>
using namespace message_filters;
using namespace sensor_msgs;
using namespace std;
using namespace geometry_msgs;
namespace rvt = rviz_visual_tools;
class kuka
{
private:
ros::Subscriber sub;
ros::NodeHandle nh_;
public:
kuka(ros::NodeHandle &nh) //constructor
{
nh_=nh;
}
void status() //func showing the actual position (case0)
{
sub = nh_.subscribe("/robposi", 1000, &kuka::callback, this);
cout<<"status";
}
void callback(const geometry_msgs::PointStamped::ConstPtr& rob)
{
cout<<"callback ";
ROS_INFO_STREAM(endl << "RobCoor.x: "<< rob->point.x << endl << "RobCoor.y: "<< rob->point.y << endl << "RobCoor.z: "<< rob->point.z << endl);
}
};
int main(int argc, char** argv)
{
ros::init(argc, argv, "kuka");
int j;char loop='*';
ros::NodeHandle nh;
do
{
cout<<"-------------------------------------------"<<std::endl;
cout<<"|0 |check position of robot |"<<std::endl;
cout<<"|1 |go to home |"<<std::endl;
cout<<"|2 |go to stored position |"<<std::endl;
cout<<"|3 |enter the position w,x,y,z |"<<std::endl;
cout<<"|4 |enter the position of joints |"<<std::endl;
cout<<"|5 | exit |"<<std::endl;
cout<<"-------------------------------------------"<<std::endl;
cin>>j;
kuka listen_0(nh);
cout<<"i = "<<j;
switch (j){
case(0):{
cout<<"i hekkddddddddddd ";
listen_0.status();
cout<<"i hekk.lj,hk.... ";
}
break;
}
cout <<"would you like to enter new choice y/n?"<<endl;
cin>> loop;
}while (loop == 'y');
return 0;
}
Hello ! Can you put the whole code ? Because we only this part, it look like you forgot to call
ros::spin()
orros::spinOnce()
.These 2 functions are used by roscpp to check if any message has been received by your subscribers
Based on OP's "answer" that indeed looks like the solution.
its the hole program , that my supervisor recommended me to make do while instead of ros spin because it make problems with switch case but i will try to use ros spin and see the result
i tried ros::spinOnce() and also i add do{ /somecode/ }while(loop=='y' && ros::ok()) at the end but the problem still happening callback function doesn't call though when i make rostopic echo i can see the msg that i publish