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

publish cmd_vel !!

asked 2017-05-11 10:08:57 -0500

zubair gravatar image

updated 2017-05-12 03:13:11 -0500

hello guys

how can i publish cmd_vel at for instance 20 times per sec ?? where should this be included ??

velPub = nh.advertise<geometry_msgs::Twist>("/agv1/cmd_vel", 20);

this way ??

int main(int argc, char** argv)
{

    ros::init(argc, argv, "image_processor");
    ros::NodeHandle nh;
    image_transport::ImageTransport it(nh);

    cv::namedWindow("blob");
    cv::createTrackbar("LowerH", "blob", &LowerH, 179, NULL);
    cv::createTrackbar("UpperH", "blob", &UpperH, 179, NULL);
    cv::createTrackbar("LowerS", "blob", &LowerS, 255, NULL);
    cv::createTrackbar("UpperS", "blob", &UpperS, 255, NULL);
    cv::createTrackbar("LowerV", "blob", &LowerV, 255, NULL);
    cv::createTrackbar("UpperV", "blob", &UpperV, 255, NULL);

    //cv::namedWindow(WINDOW, CV_WINDOW_AUTOSIZE);
    //cv::namedWindow(RESULT, CV_WINDOW_AUTOSIZE);

    ros::Subscriber dep;

    dep = nh.subscribe("/camera/depth_registered/points", 1, depthcallback);
    image_transport::Subscriber sub = it.subscribe("/camera/rgb/image_rect_color", 1, blobDetectionCallback);

    followmemsg_pub = nh.advertise<blobdetection::Followmemsg>("/agv1/bt/followmemsg", 10);
    //follow_msg.person == 1;


    ros::ServiceServer service = nh.advertiseService("followmeservice", followmesrv);
    velPub = nh.advertise<geometry_msgs::Twist>("/agv1/cmd_vel", 20);
    pub = it.advertise("camera/image_processed", 1);

    ros::Rate rate(20.0);
    geometry_msgs::Twist speed;

    int count = 1;
    while (nh.ok()) 
    {
        std_msgs::String msg;
        ros::Publisher velPub;
        //std::stringstream ss;

        if (hasNewPcl && isStop == true || isStart == true) 
        {
            getXYZ(posX, posY);
            hasNewPcl = false;
        }
        ros::spinOnce();
        rate.sleep();
    }
}

please help..

thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-05-11 16:57:10 -0500

Publishers only publish when you actually call the publish method. Note that in the C++ Publisher example they enter a while loop that sleeps for 1/10 seconds and then calls the publish method. The last argument that you tried to change is the queue_size.

Here's a slightly modified example from the aforementioned tutorial:

#include "ros/ros.h"
#include "geometry_msgs/Twist.h"

int main(int argc, char **argv)
{
    ros::init(argc, argv, "talker");
    ros::NodeHandle n;

    ros::Publisher velPub = n.advertise<geometry_msgs::Twist>("/agv1/cmd_vel", 1);
    ros::Rate loop_rate(20);

    geometry_msgs::Twist msg;
    while (ros::ok())
    {
        msg.linear.x += 0.1;
        velPub.publish(msg);
        ros::spinOnce();
        loop_rate.sleep();
    }
    return 0;
}
edit flag offensive delete link more

Comments

hi thanks for answering ,, please check the edited part above,, its giving me error, speed is not declared in this scope, and vel pub is not declared in this scope.. beacsue i am using this in a function in my other part of program,, apart from main

zubair gravatar image zubair  ( 2017-05-12 03:13:43 -0500 )edit
1

You are asking a general C++ question, and this is not the place to do that. I encourage you to learn the basics of C++ before diving into writing ROS nodes. You could also possibly switch to Python which is a bit easier to get started with, IMO.

jarvisschultz gravatar image jarvisschultz  ( 2017-05-12 07:09:51 -0500 )edit

Your velPub issue is because in the advertise line you don't name a type and you haven't yet declared the velPub variable. You should probably delete the ros::Publisher velPub; line from your while loop and add the ros::Publisher before your advertise line, just like in my example.

jarvisschultz gravatar image jarvisschultz  ( 2017-05-12 07:12:34 -0500 )edit

I'm guessing the speed issue is actually coming from a different function that you didn't post. You must be using that variable in some other function, but never declaring it with a line like geometry_msgs::Twist speed;.

jarvisschultz gravatar image jarvisschultz  ( 2017-05-12 07:15:27 -0500 )edit

Check out this variable declaration tutorial and this variable scope tutorial to understand why what you're doing is not okay.

jarvisschultz gravatar image jarvisschultz  ( 2017-05-12 07:16:08 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2017-05-11 10:08:57 -0500

Seen: 2,375 times

Last updated: May 12 '17