mirror of
https://github.com/Jieshoudaxue/ros_senior.git
synced 2026-08-29 16:40:44 +08:00
[fix] up action sample
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
# Goal
|
||||
uint32 dishwasher_id
|
||||
---
|
||||
# Result
|
||||
uint32 total_dishes_cleaned
|
||||
---
|
||||
# Feedback
|
||||
float32 percent_complete
|
||||
@@ -0,0 +1,19 @@
|
||||
<launch>
|
||||
|
||||
<node
|
||||
pkg="action_sample"
|
||||
type="DoDishes_client.py"
|
||||
name="do_dishes_client"
|
||||
required="true"
|
||||
output="screen"
|
||||
/>
|
||||
|
||||
<node
|
||||
pkg="action_sample"
|
||||
type="DoDishes_server.py"
|
||||
name="do_dishes_server"
|
||||
respawn="true"
|
||||
output="screen"
|
||||
/>
|
||||
|
||||
</launch>
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
import rospy
|
||||
import actionlib
|
||||
|
||||
from action_sample.msg import DoDishesAction, DoDishesGoal
|
||||
|
||||
def done_cb(state, result):
|
||||
rospy.loginfo("%d dishes is clean, state is %s" %(result.total_dishes_cleaned, str(state)))
|
||||
rospy.signal_shutdown('Normal shutdown')
|
||||
|
||||
def active_cb():
|
||||
rospy.loginfo("job is action")
|
||||
|
||||
def feedback_cb(feedback):
|
||||
rospy.loginfo("percent is %f" %feedback.percent_complete)
|
||||
|
||||
def main():
|
||||
rospy.init_node("do_dishes_client")
|
||||
|
||||
client = actionlib.SimpleActionClient("do_dishes", DoDishesAction)
|
||||
|
||||
rospy.loginfo("waiting for action server to start")
|
||||
client.wait_for_server()
|
||||
|
||||
rospy.loginfo("action server started, sending goal")
|
||||
goal = DoDishesGoal()
|
||||
goal.dishwasher_id = 1
|
||||
|
||||
client.send_goal(goal, done_cb=done_cb, active_cb=active_cb, feedback_cb=feedback_cb)
|
||||
|
||||
rospy.sleep(4)
|
||||
client.cancel_goal()
|
||||
|
||||
rospy.spin()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+53
@@ -0,0 +1,53 @@
|
||||
#! /usr/bin/env python3
|
||||
|
||||
import rospy
|
||||
import actionlib
|
||||
from action_sample.msg import DoDishesAction, DoDishesGoal, DoDishesFeedback, DoDishesResult
|
||||
|
||||
def execute_cb(goal, server):
|
||||
rate = rospy.Rate(1)
|
||||
|
||||
rospy.loginfo("dishwasher %d is working" %goal.dishwasher_id)
|
||||
|
||||
class DishWasherServer(object):
|
||||
def __init__(self, action_name):
|
||||
self._action_name = action_name
|
||||
self._server = actionlib.SimpleActionServer(self._action_name, DoDishesAction, execute_cb=self.execute_cb, auto_start=False)
|
||||
|
||||
def start_server(self):
|
||||
self._server.start()
|
||||
|
||||
def execute_cb(self, goal):
|
||||
rate = rospy.Rate(1)
|
||||
|
||||
rospy.loginfo("dishwasher %d is working" %goal.dishwasher_id)
|
||||
|
||||
feedback = DoDishesFeedback()
|
||||
result = DoDishesResult()
|
||||
|
||||
for i in range(10):
|
||||
feedback.percent_complete = i * 10
|
||||
self._server.publish_feedback(feedback)
|
||||
rate.sleep()
|
||||
|
||||
if self._server.is_preempt_requested():
|
||||
rospy.loginfo("the goal is canceled")
|
||||
result.total_dishes_cleaned = i * 10
|
||||
self._server.set_preempted(result)
|
||||
return
|
||||
|
||||
rospy.loginfo("dishwasher %d finish working" %goal.dishwasher_id)
|
||||
result.total_dishes_cleaned = (i+1) * 10
|
||||
self._server.set_succeeded(result)
|
||||
|
||||
|
||||
def main():
|
||||
rospy.init_node("do_dishes_server")
|
||||
|
||||
dish_washer_server = DishWasherServer("do_dishes")
|
||||
dish_washer_server.start_server()
|
||||
|
||||
rospy.spin()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user