odometry problem

asked 2015-04-29 11:40:49 -0500

kintaxs gravatar image

Hello , I want to move my robot with the keyboard. I write a node that subscribe to the cmd_vel topic and pulish to the odom. But I get an error : ‘odom_trans’ does not name a type

Please help me to solve the problem, thanks :))

#include<string>
#include<ros/ros.h>
#include<geometry_msgs/Twist.h>
#include<sensor_msgs/JointState.h>
#include<tf/transform_broadcaster.h>
#include<nav_msgs/Odometry.h>


class subandpub
{
    public:
        subandpub()
        {
            pub = n.advertise<nav_msgs::Odometry>("odom",10);

            sub = n.subscribe("cmd_vel",1,&subandpub::callback,this);
        }

        void callback(const geometry_msgs::Twist::ConstPtr& msg)
        {

            geometry_msgs::TransformStamped odom_trans;
                        odom_trans.header.frame_id = "odom";
                        odom_trans.child_frame_id = "base_bootprint";

            current_time = ros::Time::now();

            double x;
                    double y;
                    double th;

                    double vx;
                    double vy;
                    double vth;

            vx = 0;
            double dt = 1;

            if(msg->linear.x == 0.5)
                vx = 2;
            else if(msg->linear.x == -0.5)
                vx = -2;

            double delta_x = vx * dt;
            double delta_y = vy * dt;
            double delta_th= vth * dt;

            x += delta_x;
            y += delta_y;
            th += delta_th;

            geometry_msgs::Quaternion odom_quat;
            odom_quat = tf::createQuaternionMsgFromRollPitchYaw(0,0,th);

            // update transform
            odom_trans.header.stamp = current_time; 
                odom_trans.transform.translation.x = x; 
                odom_trans.transform.translation.y = y; 
                odom_trans.transform.translation.z = 0.0;
                odom_trans.transform.rotation = tf::createQuaternionMsgFromYaw(th);

            //filling the odometry
                nav_msgs::Odometry odom;
                odom.header.stamp = current_time;
                odom.header.frame_id = "odom";
                odom.child_frame_id = "base_footprint";

                // position
                odom.pose.pose.position.x = x;
                odom.pose.pose.position.y = y;
                odom.pose.pose.position.z = 0.0;
                odom.pose.pose.orientation = odom_quat;

            //velocity
                odom.twist.twist.linear.x = vx;
                odom.twist.twist.linear.y = vy;
                odom.twist.twist.linear.z = 0.0;
                odom.twist.twist.angular.x = 0.0;
                odom.twist.twist.angular.y = 0.0;
                odom.twist.twist.angular.z = vth;

                last_time = current_time;

                // publishing the odometry and the new tf
                broadcaster.sendTransform(odom_trans);
                //odom_pub.publish(odom);


            pub.publish(odom);
        }

    private:

        ros::NodeHandle n;
        ros::Publisher pub;
        ros::Subscriber sub;

        ros::Time current_time;
        ros::Time last_time;

        tf::TransformBroadcaster broadcaster;

        geometry_msgs::TransformStamped odom_trans;
        odom_trans.header.frame_id = "odom";
        odom_trans.child_frame_id = "base_bootprint";

};

int main(int argc,char **argv)
{
    ros::init(argc,argv,"odometry");
    subandpub sap;
    ros::spin();

}
edit retag flag offensive close merge delete

Comments

try removing geometry_msgs::TransformStamped odom_trans; in callback, also move x,y,theta to private variables.

bvbdort gravatar imagebvbdort ( 2015-04-30 04:23:19 -0500 )edit

Thanks @bvbdort . I have removed odom_trans in callback, but the error still exist.

kintaxs gravatar imagekintaxs ( 2015-04-30 09:42:21 -0500 )edit

#include <geometry_msgs/TransformStamped.h>?

yigit gravatar imageyigit ( 2015-04-30 10:02:11 -0500 )edit

Hi @yigit . I included the header file , but the problem is same.

kintaxs gravatar imagekintaxs ( 2015-04-30 10:23:15 -0500 )edit

Change the order of private part and public part of class. Refer variable after defining.

bvbdort gravatar imagebvbdort ( 2015-04-30 11:08:46 -0500 )edit