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

unable to publish Path waypoints for pure pursuit

asked 2021-03-09 07:32:50 -0500

krushnal gravatar image

updated 2021-03-10 12:29:00 -0500

gvdhoorn gravatar image

These are the initial line of my rospy code:

if __name__ == "__main__":
    rospy.init_node('path_for_control', anonymous=True)
    pub = rospy.Publisher('path', Path, queue_size=10)
    rate = rospy.Rate(1)  # small amount on purpose (20m in 20 sec)

while not rospy.is_shutdown():
    Trajectory()
    rate.sleep()

This is the launch file in which is being used:

<launch>
    <node pkg="pure_pursuit" name="pure_pursuit" type="pure_pursuit" />
    <node pkg="pure_pursuit" name="waypoint_publisher" type="control_listpath.py" />
</launch>

And this is the error while launching the launch file using roslaunch pure_pursuit pure_pursuit.launch

Traceback (most recent call last):
 File "/home/krush/SDC/src/pure_pursuit/src/control_listpath.py", line 148, in <module>
pub = rospy.Publisher('path', Path, queue_size=10)
 File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 842, in __init__
super(Publisher, self).__init__(name, data_class, Registration.PUB)
 File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 142, in __init__
 raise ValueError("data_class [%s] is not a class"%data_class) 
 ValueError: data_class [<function Path at 0x7f636ecb6280>] is not a class

Edit:

#! /usr/bin/env python3

import math

import numpy as np
import rospy
from geometry_msgs.msg import PoseStamped
from nav_msgs.msg import Path

def Path():
    # path = [(2.0, 0.0), (4.0, 0.0), (6.0, 0.0), (8.0, 0.0), (10.0, 0.0), (13.0, 0.0), (16.0, 1.5), (20.0, 2.5)]
    global i
    point_X, point_Y = path[i]
    i += 1

    if i == 8:
        i = 0

    pitch = 0
    roll = 0
    carPosX, carPosY = (0.0, 0.0)
    track = Path()
    track.header.frame_id = "base_link"
    track.header.stamp = rospy.Time.now()
    # for point_X, point_Y in path:
    # yaw = math.atan((point_Y - carPosY)/(point_X - carPosX))
    # print(yaw)

    dir_vec = np.array([point_X - carPosX, point_Y - carPosY])
    axis = np.array([1.0, 0.0])

    # #do dot product between them
    cos_theta = np.dot(dir_vec, axis) / (np.sqrt((point_X - carPosX) ** 2 + (point_Y - carPosY) ** 2))
    yaw = math.acos(_cos_theta)

    qx = np.sin(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) - np.cos(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)
    qy = np.cos(roll/2) * np.sin(pitch/2) * np.cos(yaw/2) + np.sin(roll/2) * np.cos(pitch/2) * np.sin(yaw/2)
    qz = np.cos(roll/2) * np.cos(pitch/2) * np.sin(yaw/2) - np.sin(roll/2) * np.sin(pitch/2) * np.cos(yaw/2)
    qw = np.cos(roll/2) * np.cos(pitch/2) * np.cos(yaw/2) + np.sin(roll/2) * np.sin(pitch/2) * np.sin(yaw/2)

    pose = PoseStamped()

    pose.pose.position.x = point_X
    pose.pose.position.y = point_Y
    pose.pose.position.z = 0.0
    pose.pose.orientation.x = qx
    pose.pose.orientation.y = qy
    pose.pose.orientation.z = qz
    pose.pose.orientation.w = qw
    track.poses.append(pose)

    pub.publish(track)

    carPosX, carPosY = point_X, point_Y


def interm(k=0):
    path = [
        (2.0, 0.0),
        (4.0, 0.0),
        (6.0, 0.0),
        (8.0, 0.0),
        (10.0, 0.0),
        (13.0, 0.0),
        (16.0, 1.5),
        (20.0, 2.5),
    ]
    point_X, point_Y = path ...
(more)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-03-09 08:26:49 -0500

updated 2021-03-10 15:04:29 -0500

The second argument to the rospy.Publisher constructor is data_class which the docs say should be a message class for serialization. So you need to provide a ROS message class. From the provided code snippet it's impossible for us to tell what Path is (seems like a function defined elsewhere in the Python code), but whatever it is it is not the right type of object. If you look at the rospy Writing a Simple Publisher and Subscriber tutorial you'll see they have a line like:

from std_msgs.msg import String

Then in their publisher constructor

pub = rospy.Publisher('chatter', String, queue_size=10)

This is an example of how to import a ROS message class and use as the data_class argument in the Publisher constructor. You'll need to do something similar.

UPDATE:

@krushnal, now that all of your code has been added to the original question, the issue is exactly as I described in the comments below. This line

from nav_msgs.msg import Path

defines a variable Path that is a ROS message class in the global namespace. Then, the very next line, you have

def Path():

which also creates a variable Path in the global namespace, but this one is a function. The issue you're seeing is because this second variable overwrites the import variable from above. Hence the error I highlighted below:

ValueError: data_class [<function Path at 0x7f636ecb6280>] is not a class

Which tells you the argument that you've provided for data_class is a function called Path with some memory address. It is not the ROS message class that it needs to be. You either need to rename this Path function or change how you import the ROS message. For example, you could simply import nav_msgs.msg module and then access the Path class in that module's namespace. E.g., nav_msgs.msg.Path

edit flag offensive delete link more

Comments

Path is of the type nav_msgs/Path which I have imported at the top using from nav_msgs.msg import Path

krushnal gravatar image krushnal  ( 2021-03-10 03:31:59 -0500 )edit

This part of your error message seems to indicate otherwise:

ValueError: data_class [<function Path at 0x7f636ecb6280>] is not a class

Perhaps updating your question to provide the full Python code would better help diagnose the specific issue. It's possible, for example, that even though you've imported a Path message that somewhere else the variable Path is being redefined.

jarvisschultz gravatar image jarvisschultz  ( 2021-03-10 09:52:50 -0500 )edit

i have added my code as an answer

krushnal gravatar image krushnal  ( 2021-03-10 11:30:06 -0500 )edit

thanks a lot!!!! that solved the problem.... i changed the Path() function name to something else

krushnal gravatar image krushnal  ( 2021-03-10 15:32:29 -0500 )edit

Glad you were able to get things sorted!

jarvisschultz gravatar image jarvisschultz  ( 2021-03-11 09:16:55 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-03-09 07:32:50 -0500

Seen: 296 times

Last updated: Mar 10 '21