0
$\begingroup$

Rosanswers logo

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.


Originally posted by asmigox on ROS Answers with karma: 5 on 2018-10-19

Post score: 0


Original comments

Comment by PeteBlackerThe3rd on 2018-10-25:
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.

Comment by asmigox on 2018-10-25:
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?

Comment by PeteBlackerThe3rd on 2018-10-25:
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.

Comment by asmigox on 2018-10-26:
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

Comment by PeteBlackerThe3rd on 2018-10-26:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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 = rospy.Time.now()

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.


Originally posted by PeteBlackerThe3rd with karma: 9529 on 2018-10-20

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by asmigox on 2018-10-21:
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

Comment by PeteBlackerThe3rd on 2018-10-22:
There is a package for exactly this. Have a look at the Laser Assembler this will help you make 3D scans.

Comment by asmigox on 2018-10-22:
tnx pal!!!

Comment by asmigox on 2018-10-24:
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?.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.