ros 使用编译好的消息文件直接使用python脚本导入执行 实现通讯

原文链接: ros 使用编译好的消息文件直接使用python脚本导入执行 实现通讯

上一篇: python swig 调用cpp

下一篇: python pydub FFmpeg mp3转wav 显示波形

需要正常使用编译命令编译消息文件,然后在文件夹下将编译好的py文件拷贝出来

edc8d1ed7954b763b8195d52e3abb0e47a4.jpg

需要以包为单位拷贝

9db42741062072745101592e19fdf0bef17.jpg

脚本文件中,只需要引入对应的msg文件,即可以脚本方式执行,而不是使用rosun,会灵活一点(maybe)

但是还是得先执行roscore,启动

69b7aedc43e8df39c1d3b09ab7e4466344a.jpg

client

#! /usr/bin/env python

import rospy
# Brings in the SimpleActionClient
import actionlib
import time
# Brings in the messages used by the fibonacci action, including the
# goal message and the result message.
import demo.msg
def cb(fb):
    print('cb ',fb)
def fibonacci_client():
    # Creates the SimpleActionClient, passing the type of the action
    # (FibonacciAction) to the constructor.
    client = actionlib.SimpleActionClient('fibonacci', demo.msg.FibonacciAction)

    # Waits until the action server has started up and started
    # listening for goals.
    client.wait_for_server()

    # Creates a goal to send to the action server.
    goal = demo.msg.FibonacciGoal(order=10)

    # Sends the goal to the action server.
    client.send_goal(goal)
    client.feedback_cb = cb

    print('send ')

    # Waits for the server to finish performing the action.
    client.wait_for_res""" """  """ """ult()

  
    print('wait')
    # Prints out the result of executing the action
    return client.get_result()  # A FibonacciResult

if __name__ == '__main__':
    try:
        # Initializes a rospy node so that the SimpleActionClient can
        # publish and subscribe over ROS.
        rospy.init_node('fibonacci_client_py')
        result = fibonacci_client()
        print("Result:", ', '.join([str(n) for n in result.sequence]))
    except rospy.ROSInterruptException:
        print("program interrupted before completion")

server

#! /usr/bin/env python

import rospy
import actionlib
import demo.msg
print(actionlib)
class FibonacciAction(object):
    # create messages that are used to publish feedback/result
    _feedback = demo.msg.FibonacciFeedback()
    _result = demo.msg.FibonacciResult()

    def __init__(self, name):
        self._action_name = name
        self._as = actionlib.SimpleActionServer(self._action_name, demo.msg.FibonacciAction, execute_cb=self.execute_cb, auto_start = False)
        self._as.start()
      
    def execute_cb(self, goal):
        # helper variables
        r = rospy.Rate(1)
        success = True
        
        # append the seeds for the fibonacci sequence
        self._feedback.sequence = []
        self._feedback.sequence.append(0)
        self._feedback.sequence.append(1)
        
        # publish info to the console for the user
        rospy.loginfo('%s: Executing, creating fibonacci sequence of order %i with seeds %i, %i' % (self._action_name, goal.order, self._feedback.sequence[0], self._feedback.sequence[1]))
        
        # start executing the action
        for i in range(1, goal.order):
            # check that preempt has not been requested by the client
            if self._as.is_preempt_requested():
                rospy.loginfo('%s: Preempted' % self._action_name)
                self._as.set_preempted()
                success = False
                break
            self._feedback.sequence.append(self._feedback.sequence[i] + self._feedback.sequence[i-1])
            # publish the feedback
            self._as.publish_feedback(self._feedback)
            # this step is not necessary, the sequence is computed at 1 Hz for demonstration purposes
            r.sleep()
          
        if success:
            self._result.sequence = self._feedback.sequence
            rospy.loginfo('%s: Succeeded' % self._action_name)
            self._as.set_succeeded(self._result)
        
if __name__ == '__main__':
    rospy.init_node('fibonacci')
    server = FibonacciAction(rospy.get_name())
    rospy.spin()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值