get the average () msg from LIDAR Hokuyo and publisher it
hi,
i'am quite new using ROS, and i trying to get and publish the average of 1000 msg received from the LIDAR. I'am working with python code, but i cant get anything out. i checked it in the terminal with rostopic list. if i have a problem or maybe iam getting an error with the code please help me and tell me what is the mistake with the code. till momment when i run it dont have error. maybe it has lack of information or add a new library.
Source code here:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from sensor_msgs.msg import LaserScan # Mensaje que leeremos
import numpy as np
class nodo_control():
max_medidas = 1000
cont_medidas = 0
medidas =list()
def __init__(self):
self.scan_Pub = rospy.Publisher('promedio_laser', LaserScan, queue_size=1)
self.scan_Sub = rospy.Subscriber("/scan", LaserScan, self.laserCallback)
def laserCallback(self,msg):
self.cont_medidas=self.cont_medidas +1
self.medidas.append(msg.ranges)
if self.cont_medidas >= self.max_medidas:
arreglo_medidas= np.ndarray(self.medidas)
promedio = arreglo_medidas.mean(1)
#publicar promedio
msg.ranges=promedio
self.scan_Pub.publish (promedio)
self.cont_medidas=0
self.medidas=list()
if __name__ == '__main__':
rospy.init_node("nodo_control")
rospy.spin()
try:
nodo_control()
except rospy.ROSInterruptException:
pass
Edit: i appreciate your answered, it will help me to reorganize and modify my code. i forgot say that im quite new to python too, but i need to finish it in a record time. tnx again for your time. i'll verify all again y to tell you as soon as possible. greets.
You need to move the
rospy.spin()
line to the end of the your main function. At the moment executing is blocking when it reaches this so that yournodo_control()
class is never getting created.its working, but it has a problem. the next lines: arreglo_medidas= np.ndarray(self.medidas) ValueError: sequence too large; cannot be greater than 32. Do u know how i could fix it?
To initialise a numpy array from a list you need to use
np.array(<your_list>)
notndarray
. I would highly recommend adding some print statements to help you debug this, including checking the shape of the numpy array you're working with.i did some changes and i added some prints like u said. for ndarray i added vstack. so i can say, its working now. tnx for ur time man. greets
Glad you've got this working. If you can accept my answer with the tick icon, then other users can see that this solution works. Thanks.