0% found this document useful (0 votes)
16 views

EE50237 - Robotics Software 4a

The document discusses ROS actions and provides an example of a timer action. ROS actions are similar to services but are designed for long-running tasks. An action has a goal, feedback provided during execution, and a final result. The document defines a timer action and shows the Python code for an action server that waits for a specified time, sending periodic feedback and a final result.

Uploaded by

skylar.skyau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

EE50237 - Robotics Software 4a

The document discusses ROS actions and provides an example of a timer action. ROS actions are similar to services but are designed for long-running tasks. An action has a goal, feedback provided during execution, and a final result. The document defines a timer action and shows the Python code for an action server that waits for a specified time, sending periodic feedback and a final result.

Uploaded by

skylar.skyau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

EE50237 – Robotics

Software
Dr Rob Wortham

Lecture 4a - Developing ROS Nodes #2

Note: This lecture is being recorded using University of Bath Panopto

22/01/2024 EE50237 Robotics Software - Lecture 4a 1


Reminder: Learning Outcomes: Weeks 3 and 4
• Confident in use of ROS architecture and ROS command line tools
• Confident in use of basic Linux commands within the bash shell
• Able to program, debug and explain simple, single threaded nodes
in Python

Topics
• Topic Publisher & Topic Subscriber (listener)
• Service Server & Service Client
• Synchronicity, Performance, Scalability
• Remapping names
• Action Server & Action Client with progress feedback
• Define and use our own message types
• Using a launch file
22/01/2024 EE50237 Robotics Software - Lecture 4a 2
Today’s Lecture

• ROS Actions

• Next Lecture
• ROS Message Types
• ROS Launch Files

22/01/2024 EE50237 Robotics Software - Lecture 4a 3


ROS Actions
• An extension to ROS Services for long running tasks
• Goal – Sent from the client to the server. Defines what work must be done
by the server.
• Feedback during execution - Sent by server to the client during execution.
• Result – sent by server to client once execution is complete.
• Action is defined in a .action file (remember - services are defined in a .srv
file)
• The .action file is in {package_name}/action

22/01/2024 EE50237 Robotics Software - Lecture 4a 4


ROS Actions - Architecture

22/01/2024 EE50237 Robotics Software - Lecture 4a 5


ROS Action Example: Timer
• Purpose – wait a specified time, reporting progress each second
• Goal – time to wait
• Feedback – time elapsed so far (seconds), time remaining (seconds)
• Result – How many feedback updates were provided

22/01/2024 EE50237 Robotics Software - Lecture 4a 6


ROS Action Example: Timer – Action Definition File

Timer.action
# Part 1: the goal, to be sent by the client
duration time_to_wait
---
# Part 2: the result, to be sent by the server upon completion
duration time_elapsed
uint32 updates_sent
---
# Part 3: the feedback, to be sent periodically by the server during
# execution.
duration time_elapsed
duration time_remaining

22/01/2024 EE50237 Robotics Software - Lecture 4a 7


Start
Timer

YES
Have we waited
Finish
long enough?

NO

Send progress to Client

Wait 1 Second

22/01/2024 EE50237 Robotics Software - Lecture 4a 8


Action Server Python Code #! /usr/bin/env python
import rospy
import time
import actionlib
from basics.msg import TimerAction, TimerGoal, TimerResult, TimerFeedback
Import the 4 auto generated
messages for Timer action def do_timer(goal):
start_time = time.time()
update_count = 0

if goal.time_to_wait.to_sec() > 60.0:


result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)

All the work is done in this callback result.updates_sent = update_count


server.set_aborted(result, "Timer aborted due to too-long wait")
return

[See next slide] while (time.time() - start_time) < goal.time_to_wait.to_sec():

if server.is_preempt_requested():
result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
result.updates_sent = update_count
server.set_preempted(result, "Timer preempted")
return

feedback = TimerFeedback()
feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
feedback.time_remaining = goal.time_to_wait - feedback.time_elapsed
server.publish_feedback(feedback)
update_count += 1

time.sleep(1.0)

result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
result.updates_sent = update_count
server.set_succeeded(result, "Timer completed successfully")

rospy.init_node('timer_action_server')
server = actionlib.SimpleActionServer('timer', TimerAction, do_timer, False)
Initialise a ROS Publisher instance server.start()
Register the action server ‘timer’ with auto_start=False rospy.spin()
Start the server, spin until termination.

22/01/2024 EE50237 Robotics Software - Lecture 4a 9


start_time = time.time()

Action Server do_timer Callback update_count = 0

def do_timer(goal): if goal.time_to_wait.to_sec() > 60.0:


result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
result.updates_sent = update_count
Requests for more than 60 seconds server.set_aborted(result, "Timer aborted due to too-long wait")
return
are not accepted
while (time.time() - start_time) < goal.time_to_wait.to_sec():
Stay in this loop until our work if server.is_preempt_requested():
is complete result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
result.updates_sent = update_count
Check if client has aborted the server.set_preempted(result, "Timer preempted")
work return

feedback = TimerFeedback()
Create the feedback info and feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
send it back to the client feedback.time_remaining = goal.time_to_wait - feedback.time_elapsed
server.publish_feedback(feedback)
update_count += 1
Wait one second (or do
time.sleep(1.0)
something useful)
result = TimerResult()
result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time)
Create the result info. Return result.updates_sent = update_count
control to client server.set_succeeded(result, "Timer completed successfully")

22/01/2024 EE50237 Robotics Software - Lecture 4a 10


Action Client Python Code ………(normal preamble stuff)

def feedback_cb(feedback):
print('[Feedback] Time elapsed: %f'%(feedback.time_elapsed.to_sec()))
print('[Feedback] Time remaining: %f'%(feedback.time_remaining.to_sec()))
Callback to print feedback
notifications rospy.init_node('timer_action_client')
client = actionlib.SimpleActionClient('timer', TimerAction)
client.wait_for_server()
Initialise the node, wait until there goal = TimerGoal()
is a server we can use goal.time_to_wait = rospy.Duration.from_sec(5.0)
# Uncomment this line to test server-side abort:
Set a 5 second goal time #goal.time_to_wait = rospy.Duration.from_sec(500.0)

Start the remote Action client.send_goal(goal, feedback_cb=feedback_cb)

# Uncomment these lines to test goal preemption:


#time.sleep(3.0)
To terminate the Action pre-emptively #client.cancel_goal()

Block until the Action completes, fails or client.wait_for_result()


print('[Result] State: %d'%(client.get_state()))
is cancelled print('[Result] Status: %s'%(client.get_goal_status_text()))
print('[Result] Time elapsed: %f'%(client.get_result().time_elapsed.to_sec()))
print('[Result] Updates sent: %d'%(client.get_result().updates_sent))

22/01/2024 EE50237 Robotics Software - Lecture 4a 11


Reading
Essential Reading from Programming Robots with ROS,
Quigley, 2015:

• Chapter 5: Actions

22/01/2024 EE50237 Robotics Software - Lecture 4a 12


Today’s topics were:

• Action Server & Action Client with progress feedback

• Next Lecture
• ROS messages
• User defined message types
• Using a launch file

Questions?
Ask them on moodle!

22/01/2024 EE50237 Robotics Software 13

You might also like