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

[python] how the vehilce keep moving, while the if condition doesnot receive any value?! [closed]

asked 2022-09-01 12:47:30 -0500

Delbina gravatar image

updated 2022-09-02 04:36:49 -0500

ravijoshi gravatar image

In the following code, i want the robot keep moving up to the time that the if condition gets True. Now the problem is that, when one of the conditions in if, receives no value (ValueError: min() arg is an empty sequence), the vehicle does not start moving.how can i define such a condition?

(Note: min() is generated by another python code and provides value for self.object_1. when i move the vehicle for far, it does not have value, up to the time that the vehicle gets close to the object.)

#!/usr/bin/env python
import rospy
from find_object_2d.msg import DetectionInfo
from sensor_msgs.msg import LaserScan
from std_msgs.msg import Int32, Float32, String, Time, Int8, Float64
from geometry_msgs.msg import Twist
import numpy as np
from geometry_msgs.msg import Pose


class object_detection_dist_to_goal():
    def __init__(self):
        self.velocity_publisher = rospy.Publisher('/cmd_vel', Twist, queue_size = 1)
        self.distance_to_object_publisher = rospy.Publisher('/distance_to_object', Float64, queue_size = 10)
        rospy.Subscriber('/info', DetectionInfo, self.infocallback)
        rospy.Subscriber('lidar_distance',Pose, self.tfcallback)

        self.move = Twist()
        self.object = Pose()
    def tfcallback(self, msg):
        self.object = msg
        #print(self.object)
        self.object_1 = self.object.position.x
        print("object_1", self.object_1)

    def infocallback(self, msg):
        inlier_id = msg.ids
        self.length = len(inlier_id)

        self.move.linear.x = 0.25 
        if (self.length == 2) and (self.object_1 < 5.4):
            self.move.linear.x = 0.0
            print("vehicle stopped: ", self.move.linear.x)
        self.velocity_publisher.publish(self.move)

    def loop(self):
        rospy.spin()

if __name__ == '__main__':
    rospy.init_node('ontrack_cameralidar', anonymous=False)  #anony=False
    on_track_camer_lidar = object_detection_dist_to_goal()
    on_track_camer_lidar.loop()

Sorry, i have provided the other python code that generates the min value. I think i should solve the error here first:

#!/usr/bin/env python
from roslib import message
import rospy
import sensor_msgs.point_cloud2 as pc2
from sensor_msgs.msg import PointCloud2, PointField
import numpy as np
import ros_numpy
from geometry_msgs.msg import Pose

#listener
def listen():
    rospy.init_node('listen', anonymous=True)
    rospy.Subscriber("/Filtered_points_x", PointCloud2, callback_kinect)


def callback_kinect(data):
    pub = rospy.Publisher('lidar_distance',Pose, queue_size=10)
    data_lidar = Pose()
    xyz_array = ros_numpy.point_cloud2.pointcloud2_to_xyz_array(data)
    print(xyz_array)
    mini_data = min(xyz_array[:,0])
    print("mini_data", mini_data)
    data_lidar.position.x = mini_data  
    pub.publish(data_lidar) 
    print("data_points", data_lidar.position.x)
    height =  int (data.height / 2)
    middle_x = int (data.width / 2)
    middle = read_depth (middle_x, height, data)  # do stuff with middle


def read_depth(width, height, data) :
    if (height >= data.height) or (width >= data.width) :
        return -1
    data_out = pc2.read_points(data, field_names= ('x','y','z'), skip_nans=True, uvs=[[width, height]])
    int_data = next(data_out)
    rospy.loginfo("int_data " + str(int_data))
    return int_data

if __name__ == '__main__':
    try:
        listen()
        rospy.spin()
    except rospy.ROSInterruptException:
        pass

Here, i have limited the input of the subscriber. i mean when the vehicle is near to the objects, it receives a limited data points, and when i start moving the vehicle far from the objects it does not receive any value and shows the following error:

[[ 7.99410915  1.36072445 -0.99567264]]
('mini_data', 7.994109153747559)
('data_points', 7.994109153747559)
[INFO] [1662109961.035894]: int_data (7.994109153747559, 1.3607244491577148, -0.9956726431846619 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by Delbina
close date 2022-09-07 05:26:50.971166

Comments

We do not see any min function in your code.

ravijoshi gravatar image ravijoshi  ( 2022-09-01 21:19:03 -0500 )edit

min() is generated by another python code and provides value for self.object_1.

Delbina gravatar image Delbina  ( 2022-09-02 03:34:23 -0500 )edit

min() is generated by another python code and provides value for self.object_1.

We can not see hidden things. Thus we have no idea what you are saying about. Ideally, you should post the minimal reproducible example in an online forum but not the entire project. This not only helps you to dig down into the issue but also saves our time.

ravijoshi gravatar image ravijoshi  ( 2022-09-02 04:22:23 -0500 )edit

Dear @ravijoshi i have provided the other python code, and the error relared. would please help me to find the solution? Thanks in advance

Delbina gravatar image Delbina  ( 2022-09-02 04:25:17 -0500 )edit

Dear @ravijoshi , this is the xyz_array:

[[ 5.68112087  1.09555054 -0.91642445]
 [ 5.66322517  0.86893958 -0.90752858]
 [ 5.31835747  1.20884693 -0.86386597]]

the number of these data points keep changing, i mean it depends to the input. the error is not resolving, but at least the vehicle starts moving!

Sorry, here you mentioned that: "The snippet you have posted in the answer above, has incorrect indentation. Furthermore, it is using else without using if. It will not run!"

the indentation of which part?? and i have not used "else" in my code, which part you mean?? thanks

Delbina gravatar image Delbina  ( 2022-09-02 05:08:17 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-09-01 21:18:17 -0500

ravijoshi gravatar image

updated 2022-09-02 04:41:24 -0500

  1. Initialize your Twist and Pose object before creating subscribers. Therefore you should move the following lines just below def __init__(self):

    self.move = Twist()
    self.object = Pose()
    
  2. Please initialize self.object_1 to None just below the initialization of the Twist and Pose objects,

    self.object_1 = None
    
  3. Please use the conditional statement if (self.length == 2) and (self.object_1 < 5.4): only after ensuring that self.object_1 is not None.

On a side note, if lidar_distance and /info topics are running at a similar frequency, you may couple and receive them together in a single callback. Thus your logic will become much cleaner.

Update

The question has been updated completely. I am not sure if it is good practice to modify it entirely. Never mind, following is the error:

ValueError: min() arg is an empty sequence

The error is coming from this line mini_data = min(xyz_array[:,0]). So, please check the shape of xyz_array before indexing over it.

edit flag offensive delete link more

Comments

@ravijoshi , Thanks a lot for the comment, just i did not get the point of number 3. how should i write this? thanks

Delbina gravatar image Delbina  ( 2022-09-02 03:31:40 -0500 )edit

in this way is it True:

while (self.object_1 == None):
            self.move.linear.x = 0.25    
        else:
            if (self.length == 2) and (self.object_1 < 5.4):
                self.move.linear.x = 0.0
Delbina gravatar image Delbina  ( 2022-09-02 03:33:42 -0500 )edit

how should i write this?

I am worried about the self.object_1 variable. If it is None (just think if /info topic arrives before lidar_distance topic), then your if condition will crash the program. The simplest way is to wrap it as shown below:

if self.object_1 is not None:
  if (self.length == 2) and (self.object_1 < 5.4):
    self.move.linear.x = 0.0
    print("vehicle stopped: ", self.move.linear.x)
ravijoshi gravatar image ravijoshi  ( 2022-09-02 04:28:12 -0500 )edit

The snippet you have posted in the answer above, has incorrect indentation. Furthermore, it is using else without using if. It will not run!

ravijoshi gravatar image ravijoshi  ( 2022-09-02 04:30:30 -0500 )edit

Please see the update section under the answer. Moreover, you have changed the question entirely. I suggest discussing only one problem in a question. Nevermind, please upvote and mark the answer as accepted if the ValueError: min() ... is fixed.

ravijoshi gravatar image ravijoshi  ( 2022-09-02 04:43:59 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-09-01 12:47:30 -0500

Seen: 103 times

Last updated: Sep 02 '22