0
$\begingroup$

Rosanswers logo

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[k]
    path(point_X, point_Y)

    # k = path.index((point_X, point_Y))
    print(k)
    if k < 8:
        k += 1
    else:
        k = 0


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

    while not rospy.is_shutdown():
        Path()
        rate.sleep()
    # rospy.spin()

Originally posted by krushnal on ROS Answers with karma: 5 on 2021-03-09

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by jarvisschultz with karma: 9031 on 2021-03-09

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by krushnal on 2021-03-10:
Path is of the type nav_msgs/Path which I have imported at the top using from nav_msgs.msg import Path

Comment by jarvisschultz on 2021-03-10:
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.

Comment by krushnal on 2021-03-10:
i have added my code as an answer

Comment by krushnal on 2021-03-10:
thanks a lot!!!! that solved the problem.... i changed the Path() function name to something else

Comment by jarvisschultz on 2021-03-11:
Glad you were able to get things sorted!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.