[python] how the vehilce keep moving, while the if condition doesnot receive any value?! [closed]
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 ...
We do not see any
min
function in your code.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.
Dear @ravijoshi i have provided the other python code, and the error relared. would please help me to find the solution? Thanks in advance
Dear @ravijoshi , this is the xyz_array:
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