Last active
October 3, 2024 05:44
-
-
Save atotto/f2754f75bedb6ea56e3e0264ec405dcf to your computer and use it in GitHub Desktop.
Publishing Odometry Information over ROS (python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import math | |
from math import sin, cos, pi | |
import rospy | |
import tf | |
from nav_msgs.msg import Odometry | |
from geometry_msgs.msg import Point, Pose, Quaternion, Twist, Vector3 | |
rospy.init_node('odometry_publisher') | |
odom_pub = rospy.Publisher("odom", Odometry, queue_size=50) | |
odom_broadcaster = tf.TransformBroadcaster() | |
x = 0.0 | |
y = 0.0 | |
th = 0.0 | |
vx = 0.1 | |
vy = -0.1 | |
vth = 0.1 | |
current_time = rospy.Time.now() | |
last_time = rospy.Time.now() | |
r = rospy.Rate(1.0) | |
while not rospy.is_shutdown(): | |
current_time = 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#!/usr/bin/env python3
import rospy
import sys
import math
import tf
from nav_msgs.msg import Odometry
from geometry_msgs.msg import Twist
from geometry_msgs.msg import Quaternion
from roboclaw_3 import Roboclaw
from time import sleep
from std_msgs.msg import *
address_right = 0x80
address_left = 0x80
loop_rate = 3
class BaseController(object):
def main():
if name=="main":
main()
this is my code for two motors with roboclaw but i have facing issues that is odometry values not coming
and many errors is there pls help to troubleshooot