Skip to content

Instantly share code, notes, and snippets.

@argahsuknesib
Created July 15, 2021 22:15
Show Gist options
  • Save argahsuknesib/e945a48472e3fb320c35f902e8238266 to your computer and use it in GitHub Desktop.
Save argahsuknesib/e945a48472e3fb320c35f902e8238266 to your computer and use it in GitHub Desktop.
Log Generation in CSV
#! /usr/bin/env python
import rospy
from nav_msgs.msg import Odometry, OccupancyGrid, Path, GridCells
from move_base_msgs.msg import MoveBaseActionGoal
from rosgraph_msgs.msg import Clock
from geometry_msgs.msg import Twist
def cmd_vel_callback(msg):
linear_velocity_x = msg.linear.x
linear_velocity_y = msg.linear.y
linear_velocity_z = msg.linear.z
angular_velocity_x = msg.angular.x
angular_velocity_y = msg.angular.y
angular_velocity_z = msg.angular.z
linear_velocity_list = []
linear_velocity_list.append(linear_velocity_x)
linear_velocity_list.append(linear_velocity_y)
linear_velocity_list.append(linear_velocity_z)
angular_velocity_list = []
angular_velocity_list.append(angular_velocity_x)
angular_velocity_list.append(angular_velocity_y)
angular_velocity_list.append(angular_velocity_z)
return linear_velocity_list, angular_velocity_list
def odom_callback(msg):
odom_x = msg.pose.pose.position.x
odom_y = msg.pose.pose.position.y
odometry_list = []
odometry_list.append(odom_x)
odometry_list.append(odom_y)
return odometry_list
def global_costmap_callback(msg):
global_position_x = msg.info.origin.position.x
global_position_y = msg.info.origin.position.y
global_orientation_y = msg.info.origin.position.y
global_orientation_x = msg.info.origin.position.x
global_costmap_list = []
global_costmap_list.append(global_position_x)
global_costmap_list.append(global_position_y)
global_costmap_list.append(global_orientation_x)
global_costmap_list.append(global_orientation_y)
return global_costmap_list
def goal_callback(msg):
goal = msg.goal
return goal
def global_plan_callback(msg):
global_path = msg.poses
return global_path
def clock_callback(msg):
clock_time = msg.clock
return clock_time
def main():
rospy.init_node('log_generation_csv')
rospy.Subscriber('/odom', Odometry, odom_callback)
rospy.Subscriber('/move_base/global_costmap/costmap', OccupancyGrid, global_costmap_callback)
rospy.Subscriber('/move_base/goal', MoveBaseActionGoal, goal_callback)
rospy.Subscriber('/move_base/DWAPlannerROS/global_plan', Path, global_plan_callback)
rospy.Subscriber('/clock', Clock, clock_callback)
rospy.Subscriber('/cmd_vel', Twist, cmd_vel_callback)
'''
How should I subscribe to many topics in the same line?
I wish to print all of those values in a .csv file, and I am not able to.
'''
rospy.spin()
if __name__ == '__main__':
main()
@rwbot
Copy link

rwbot commented Jul 16, 2021

The variables you create inside the callback only exist until the last line of the callback (locally scoped variable). You should wrap everything in a python class, and have the logger as a member variable that each of the callbacks can call on. But the quick and dirty solution is just to declare the list as a global variable. Example

# imports
# imports

linear_velocity_list = []
angular_velocity_list = []

def cmd_vel_callback(msg):
    global   linear_velocity_list.append(msg.linear.x)
    #etc

    global   angular_velocity_list.append(msg.angular.x)
    # etc

    #end cmd_vel_callback

I'm on mobile so probably type it out instead of copy and paste
See https://stackoverflow.com/a/423596 for explanation of assigning to a global.

@argahsuknesib
Copy link
Author

Thank you so much! I will try it right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment