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

get the average () msg from LIDAR Hokuyo and publisher it

asked 2018-10-19 15:10:53 -0500

asmigox gravatar image

updated 2018-10-25 05:52:52 -0500


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

        if  self.cont_medidas >= self.max_medidas:
            arreglo_medidas= np.ndarray(self.medidas)
            promedio = arreglo_medidas.mean(1)  
            #publicar promedio
            self.scan_Pub.publish (promedio)

if __name__ == '__main__':

   except rospy.ROSInterruptException:

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.

edit retag flag offensive close merge delete


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 your nodo_control() class is never getting created.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-10-25 05:58:30 -0500 )edit

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?

asmigox gravatar image asmigox  ( 2018-10-25 06:42:20 -0500 )edit

To initialise a numpy array from a list you need to use np.array(<your_list>) not ndarray. 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.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-10-25 06:57:34 -0500 )edit

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

asmigox gravatar image asmigox  ( 2018-10-26 06:27:45 -0500 )edit

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.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-10-26 09:39:57 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-10-20 11:15:25 -0500

Okay, there a few thing you need to fix in your code to get it to perform the way you want.

At the moment, the node doesn't do anything, the main function initialises the node then spins forever. You're not creating an instance of you nodo_control class so none of the code in there is getting executed. This is the first thing to fix.

Your laserCallback function includes the following lines:

while not rospy.is_shutdown():
          current_time =

This is going to loop forever until the node shuts down setting the current_time variable, meaning the callback function will block the first time it is run until you kill the node.

You'll want to create your publisher at the same time as you register the callback function for the laser scans, you can't create it and instantly use it in the same function. These publishers should be setup once and used for the lifetime of the node.

I'd recommend not using a class for this until you're more familiar with python, maybe copy and paste the subscriber example here and add the global variables to store the averages as a start.

Hope this helps.

edit flag offensive delete link more


i wanna ask you another thing. as we both know the Hokuyo LIDAR works to x, y axis. the question. is there any basic code or a library to allow get axis z? becouse i need to move the LIDAR with a servo up and down and i want to know what angle is it in a specific time. tnx

asmigox gravatar image asmigox  ( 2018-10-21 12:21:00 -0500 )edit

There is a package for exactly this. Have a look at the Laser Assembler this will help you make 3D scans.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-10-22 04:12:00 -0500 )edit

tnx pal!!!

asmigox gravatar image asmigox  ( 2018-10-22 07:22:16 -0500 )edit

i've been working in the same code and beside to modify the code i read something else about the class and python, i believe the code is fine, but i cant watch nothing out yet. Anybody could say me what its the problem?.

asmigox gravatar image asmigox  ( 2018-10-24 17:20:55 -0500 )edit

Question Tools

1 follower


Asked: 2018-10-19 14:50:59 -0500

Seen: 248 times

Last updated: Oct 25 '18