compscistudent
compscistudent

Reputation: 127

How to Automate terminal commands of ROS using a python script

I am currently trying to write an executable program in python that runs the following ROS command: rostopic echo dvrk/PSM1/position_cartesian_current however despite reading the ROS Tutorials, I am unsure how to go about doing this. Within a file called arm.py the following subscriber and definition already exist:

rospy.Subscriber(self.__full_ros_namespace + '/position_cartesian_current', PoseStamped, self.__position_cartesian_current_cb)

def __position_cartesian_current_cb(self, data):
        self.__position_cartesian_current = posemath.fromMsg(data.pose)

Am I supposed to reuse this subscriber and definition in the new automated python script? After obtaining the current Cartesian position, the robot will subsequently be moved to a different position, which can currently be accomplished using ROS commands in the terminal, however, the aim is to write a python script that automates these commands. Any help would be greatly appreciated!

import rospy
from tf import transformations
from tf_conversions import posemath
from std_msgs.msg import String, Bool, Float32, Empty, Float64MultiArray
from geometry_msgs.msg import Pose, PoseStamped, Vector3, Quaternion, Wrench, WrenchStamped, TwistStamped

def callback(data):
  rospy.loginfo(rospy.get_caller_id() + data.data)

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber(self.__full_ros_namespace + '/position_cartesian_current', PoseStamped, callback)
    rospy.spin()

if __name__ == '__main__':
    listener()

Upvotes: 2

Views: 2487

Answers (3)

Benyamin Jafari
Benyamin Jafari

Reputation: 33956

According to this doc PoseStamped message contained from Pose and Header messages, so to obtain to the Pose values try it:

from geometry_msgs.msg import PoseStamped,

def callback(data):
  print(data.pose)

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber(self.__full_ros_namespace + '/position_cartesian_current', 
    PoseStamped, callback
)
    rospy.spin()

Upvotes: 0

Sıddık Açıl
Sıddık Açıl

Reputation: 967

This is not a Python automation script, it is a Python ROS program which can be executed as a ROS node (using rosrun). What you are accomplish by doing this is the same with publishing manual messages through ROS built-in terminal commands. This program will execute callback whenever a message is published at "/position_cartesian_current" topic.

If you do not want to execute bunch of commands at different terminals whenever you want to try something, ROS offers you the ability to create launch files (roslaunch) which reduces your execution pipeline to a single roslaunch command. This is heavily used in ROS community to automate processes.

Upvotes: 0

bracco23
bracco23

Reputation: 2211

What you want to do is write a Python ROS Node to subscribe to the topic and implement your logic.

You can do so by following this guide.

The main idea is to subscribe to the position topic, get the relevant data in the callback function and publish, in that same callback, the commands you usually perform by command line.

To reproduce a simple rostopic echo, you can just print the values you receive in the callback.

Upvotes: 1

Related Questions