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

how can i solve this error regarding Float and vector3?

asked 2022-09-16 10:38:21 -0500

PBT gravatar image

updated 2022-09-17 00:29:37 -0500

gvdhoorn gravatar image

hey guys i am using a Jetson nano and a Jetracer Ai Kit as my robot for implementing ros navigation stack. I am able to control the robot with ros using teleop_gamepad.py. by the way the robot is a 4 wheel drive with two rare dc motors without encoders and servo for the steering in front.

on the python script for my robot i tried subscribing to the topic /cmd_vel from move_base but i keep getting these errors...

[INFO] [1663341529.061724]: /cmd_vel: x: -0.1
y: 0.0
z: 0.0
[ERROR] [1663341529.091765]: bad callback: <function callback_cmd at 0x7f8fdd17b8>
Traceback (most recent call last):
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/user/catkin_ws/src/cytron_jetracer/scripts/racecar.py", line 32, in callback_cmd
    car.throttle = cmd.linear
  File "/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py", line 585, in __set__
    self.set(obj, value)
  File "/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py", line 559, in set
    new_value = self._validate(obj, value)
  File "/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py", line 591, in _validate
    value = self.validate(obj, value)
  File "/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py", line 1976, in validate
    self.error(obj, value)
  File "/usr/local/lib/python3.6/dist-packages/traitlets/traitlets.py", line 625, in error
    raise TraitError(e)
traitlets.traitlets.TraitError: The 'throttle' trait of a NvidiaRacecar instance must be a float, but a value of x: -0.1
y: 0.0
z: 0.0 <class 'geometry_msgs.msg._Vector3.Vector3'> was specified.

This is what the python code for my robot looks like...

import rospy
from jetracer.nvidia_racecar import NvidiaRacecar
from std_msgs.msg import Float64
from nav_msgs.msg import Odometry
from geometry_msgs.msg import Point, Twist, Vector3
from math import atan2


#Initialize car variable and tune settings
car = NvidiaRacecar()
car.steering_gain = 0.65
car.steering_offset = 0.0
car.throttle_gain = 0.8
car.throttle = 0.0

#Throttle
def callback_throttle(throt):
    car.throttle = throt.data
    rospy.loginfo("Throttle: %s", str(throt.data))

#Steering
def callback_steering(steer):
    car.steering = steer.data
    rospy.loginfo("Steering: %s", str(steer.data))


#cmd_vel
def callback_cmd(cmd):
    rospy.loginfo("/cmd_vel: %s", str(cmd.linear))
    car.throttle = cmd.linear




#Setup node and topics subscription
def racecar():
    rospy.init_node('racecar', anonymous=True)
    rospy.Subscriber("throttle", Float64, callback_throttle)
    rospy.Subscriber("steering", Float64, callback_steering)
    rospy.Subscriber("/cmd_vel", Twist, callback_cmd)


    rospy.spin()

if __name__ == '__main__':
    print("Running racecar.py")
    racecar()

Because of this i haven't been able to move the robot after clicking 2D Nav Goal on Rviz. Any idea how to solve this issue?

Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-17 00:37:50 -0500

gvdhoorn gravatar image

updated 2022-09-17 00:39:35 -0500

You have this:

#cmd_vel
def callback_cmd(cmd):
    rospy.loginfo("/cmd_vel: %s", str(cmd.linear))
    car.throttle = cmd.linear

As you can see in the documentation, a geometry_msgs/Twist message has the following structure:

Vector3  linear
Vector3  angular

So linear is not a simple value, but a geometry_msgs/Vector3 object, which contains (from here):

float64 x
float64 y
float64 z

The error message you show states:

The 'throttle' trait of a NvidiaRacecar instance must be a float, but a [..] <class 'geometry_msgs.msg._vector3.vector3'=""> was specified.

this is correct: cmd.linear is indeed a (Python version of a) geometry_msgs/Vector3. It's not a float.

To solve your problem, you'll have to convert the cmd_vel.linear.x into an appropriate value for NvidiaRacecar.throttle, expressed as a float.

If NvidiaRacecar.throttle accepts values representing metres/second of the race car, that would be easy. Otherwise, a conversion would be important, as cmd_vel.linear is a vector and encodes the commanded translational velocity in metres/second. It's not a normalised value from [min, max) or something like that.

edit flag offensive delete link more

Comments

In addition to what @gvdhoorn said, running a python3 module in python2 is asking for trouble. Your PYTHONPATH should contain only python2 directories.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-09-17 10:29:40 -0500 )edit

Hm, yes. I hadn't paid any attention to that.

Mixing Python 2 and Python 3 like this is not a good idea.

gvdhoorn gravatar image gvdhoorn  ( 2022-09-17 11:33:33 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-09-16 10:38:21 -0500

Seen: 204 times

Last updated: Sep 17 '22