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

After migration from Kinetic to Melodic my script doesn't work [closed]

asked 2021-04-06 09:47:54 -0500

Elektron97 gravatar image

updated 2021-05-14 03:50:28 -0500

Hi everyone. I'm new in ROS, sorry for my ignorance. I wrote a script in Kinetic that implements a trajectory tracking (track: y=0) for my unicicle in Gazebo. My script subscribe /odom and convert in rpy angles. Then, publishes /cmd_vel with a specific angular rate. In Kinetic, my robot follows the line correctly. After migration in Melodic, unicicle doesn't follow the line and goes round.

EDIT: I've implemented the suggestions. This is my code in C++.

#include "ros/ros.h"
#include "geometry_msgs/Vector3.h"
#include "geometry_msgs/Quaternion.h"
#include "geometry_msgs/Twist.h"
#include "geometry_msgs/Pose2D.h"
#include "nav_msgs/Odometry.h"
#include <tf/transform_datatypes.h>


/*Orientation*/
geometry_msgs::Pose2D pose2d; //x, y, theta

float my_sinc(float theta);
geometry_msgs::Twist control_law(geometry_msgs::Pose2D  pose2d, float lin_vel, float k);
void print_info(geometry_msgs::Pose2D pose2d, geometry_msgs::Twist control);

void pose_callback(const nav_msgs::Odometry::ConstPtr& msg)
{
    //conversione quaternioni -> RPY
    tf::Quaternion q(
        msg->pose.pose.orientation.x,
        msg->pose.pose.orientation.y,
        msg->pose.pose.orientation.z,
        msg->pose.pose.orientation.w);
    tf::Matrix3x3 m(q);
    double roll, pitch, yaw;
    m.getRPY(roll, pitch, yaw);

    //update pose2d
    pose2d.x = msg->pose.pose.position.x;
    pose2d.y = msg->pose.pose.position.y;
    pose2d.theta = yaw;
}

int main(int argc,  char **argv)
{
    //Node init
    ros::init(argc, argv, "feedback2");
    ros::NodeHandle node_obj;

    //Pub
    ros::Publisher pub_obj=node_obj.advertise<geometry_msgs::Twist>("/cmd_vel", 1000);
    //Sub
    ros::Subscriber subscriber_obj=node_obj.subscribe("/odom", 1000, pose_callback);
    ros::Rate loop_rate(100);

    float lin_vel=1;
    float K=2;

    while (ros::ok())
    {
        ros::spinOnce(); //receive odom
        geometry_msgs::Twist vel = control_law(pose2d, lin_vel, K);
        pub_obj.publish(vel);
        print_info(pose2d, vel);
        loop_rate.sleep();
    }

    return 0;
}

float my_sinc(float theta)
{
    if(theta ==0)
    {
        return 1;
    }

    return sin(theta)/theta;
}

geometry_msgs::Twist control_law(geometry_msgs::Pose2D  pose2d, float lin_vel, float k)
{
    geometry_msgs::Twist vel;

    //default 2D constraints
    vel.linear.y=0;
    vel.linear.z=0;
    vel.angular.x=0;
    vel.angular.y=0;

    vel.linear.x=lin_vel;
    vel.angular.z=-(pose2d.y)*(my_sinc(pose2d.theta))*(lin_vel) -k*(pose2d.theta);

    return vel;

}

void print_info(geometry_msgs::Pose2D pose2d, geometry_msgs::Twist control)
{
    ROS_WARN("Information:\n");
    ROS_INFO("Position: [%f, %f]", pose2d.x, pose2d.y);
    ROS_INFO("Theta: %f", pose2d.theta);
    ROS_INFO("Linear Velocity: %f, Angular velocity: %f", control.linear.x, control.angular.z);
}
edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant. Please see http://wiki.ros.org/Support for more details. by Elektron97
close date 2021-11-26 09:51:08.807148

Comments

2

"doesn't work" is simply not enough, consider providing more (relevant) details to get an answer!

abhishek47 gravatar image abhishek47  ( 2021-04-06 11:39:16 -0500 )edit

Please edit your question and post your script (101010 button for formatting). It would be a big help in understanding and troubleshooting your issue.

tryan gravatar image tryan  ( 2021-04-06 12:15:47 -0500 )edit
1

Yes i'm sorry. I just edited the post

Elektron97 gravatar image Elektron97  ( 2021-04-06 15:51:04 -0500 )edit
1

I don't notice anything that should be affected by migrating to Melodic, but maybe someone else will catch something. In the mean time, can you provide your launch files and URDF?

tryan gravatar image tryan  ( 2021-04-06 20:06:42 -0500 )edit

"unicicle doesn't follow the line and goes round" -- when you rostopic echo /cmd_vel is anything out of ordinary?

abhishek47 gravatar image abhishek47  ( 2021-04-07 00:38:40 -0500 )edit

Yes, the launch file and urdf model are in this book https://github.com/PacktPublishing/Ma... . In particular:

Launch file: /chapter4/diff_wheeled_robot_gazebo/launch/diff_wheeled_gazebo_full.launch URDF file: /chapter3/urdf/diff_wheeled_robot_with_laser.xacro

Elektron97 gravatar image Elektron97  ( 2021-04-07 02:33:31 -0500 )edit

For rostopic echo: Yes, nothing. However, i tried to plot /cmd_vel and yaw angle with a second topic (Float64) and the resulting plot is very interesting. When quaternion.z is -1, yaw (and then angular rate) seems like a sawtooth wave. [https://imgur.com/a/5yOmL3C] Is math.atan2()? In Kinetic we have this plot: [https://imgur.com/a/s3GqC0p]

Elektron97 gravatar image Elektron97  ( 2021-04-07 02:40:26 -0500 )edit

I reinstalled ubuntu, melodic, tried in other pc but in melodic this script doesn't work. I really don'know why, please help me

Elektron97 gravatar image Elektron97  ( 2021-04-23 11:25:08 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-04-30 10:04:34 -0500

OzzieTheHead gravatar image

From reading your follow-up comments, it sounds like your quaternion operations are not working right. But what you are trying to do is already defined here: tf.quaternion_from_euler. In fact, you are reinventing the wheel when you are using ROS libraries and then defining classes for Vector and Quaternion. These are fundamental elements and already exist in ROS. Your Vector is just geometry_msgsVector3 and Quaternion is geometry_msgsQuaternion and there are even stamped versions of these too.

If anything, you should extend those classes (def MyVector(Vector3): ...) but if you are adding a name field, sounds like you are trying to identify them so just use a dict of Vectors

But first, you should edit your question to include the info you provided after people asked. Without those, we don't have much to work with, and it is an incomplete question.

edit flag offensive delete link more

Comments

Thanks for the suggestion. I implement this, but still not works. I think that is my control law is sensitive to delay due to my old pc.After installing 18.04 and ros melodic, my old toaster is in difficulty and very slow.

Elektron97 gravatar image Elektron97  ( 2021-05-14 10:15:25 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2021-04-06 09:41:58 -0500

Seen: 270 times

Last updated: May 14 '21