Robotics StackExchange | Archived questions

why my turtlebot is not moving with odometry info?

i was using the below code.This doesnot move my turtlebot.What could be the reason?

#!/usr/bin/env python

import math from math import sin, cos, pi

import rospy import tf from navmsgs.msg import Odometry from geometrymsgs.msg import Point, Pose, Quaternion, Twist, Vector3

rospy.initnode('odometrypublisher')

odompub = rospy.Publisher("/odom", Odometry, queuesize=50) odom_broadcaster = tf.TransformBroadcaster()

x = 0.0 y = 0.0 th = 0.0

vx = 0.1 vy = -0.1 vth = 0.1

currenttime = rospy.Time.now() lasttime = rospy.Time.now()

r = rospy.Rate(1.0) while not rospy.isshutdown(): currenttime = rospy.Time.now()

# compute odometry in a typical way given the velocities of the robot
dt = (current_time - last_time).to_sec()
delta_x = (vx * cos(th) - vy * sin(th)) * dt
delta_y = (vx * sin(th) + vy * cos(th)) * dt
delta_th = vth * dt

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

# since all odometry is 6DOF we'll need a quaternion created from yaw
odom_quat = tf.transformations.quaternion_from_euler(0, 0, th)

# first, we'll publish the transform over tf
odom_broadcaster.sendTransform(
    (x, y, 0.),
    odom_quat,
    current_time,
    "base_link",
    "odom"
)

# next, we'll publish the odometry message over ROS
odom = Odometry()
odom.header.stamp = current_time
odom.header.frame_id = "odom"

# set the position
odom.pose.pose = Pose(Point(x, y, 0.), Quaternion(*odom_quat))

# set the velocity
odom.child_frame_id = "base_link"
odom.twist.twist = Twist(Vector3(vx, vy, 0), Vector3(0, 0, vth))

# publish the message
odom_pub.publish(odom)

last_time = current_time
r.sleep()

Asked by MPR on 2021-10-28 09:19:45 UTC

Comments

Please take a look at this question, similar issue: https://answers.ros.org/question/241602/get-odometry-from-wheels-encoders/

Asked by osilva on 2021-10-28 09:29:31 UTC

could you help me with some python code?

Asked by MPR on 2021-10-28 09:32:41 UTC

Take a look at this discussion: https://gist.github.com/atotto/f2754f75bedb6ea56e3e0264ec405dcf Hope this helps. It's mainly in Python.

Asked by osilva on 2021-10-28 09:36:50 UTC

Answers