Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script for dispatching tasks from a json description #215

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rmf_demos_tasks/rmf_demos_tasks/dispatch_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ def __init__(self, argv=sys.argv):
'-F',
'--fleet',
type=str,
help='Fleet name, should define tgt with robot',
help='Fleet name, should define together with robot',
)
parser.add_argument(
'-R',
'--robot',
type=str,
help='Robot name, should define tgt with fleet',
help='Robot name, should define together with fleet',
)
parser.add_argument(
'--use_sim_time',
Expand Down
4 changes: 2 additions & 2 deletions rmf_demos_tasks/rmf_demos_tasks/dispatch_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ def __init__(self, argv=sys.argv):
'-F',
'--fleet',
type=str,
help='Fleet name, should define tgt with robot',
help='Fleet name, should define together with robot',
)
parser.add_argument(
'-R',
'--robot',
type=str,
help='Robot name, should define tgt with fleet',
help='Robot name, should define together with fleet',
)
parser.add_argument(
'-st',
Expand Down
4 changes: 2 additions & 2 deletions rmf_demos_tasks/rmf_demos_tasks/dispatch_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def __init__(self, argv=sys.argv):
'-F',
'--fleet',
type=str,
help='Fleet name, should define tgt with robot',
help='Fleet name, should define together with robot',
)
parser.add_argument(
'-R',
'--robot',
type=str,
help='Robot name, should define tgt with fleet',
help='Robot name, should define together with fleet',
)
parser.add_argument(
'-st',
Expand Down
164 changes: 164 additions & 0 deletions rmf_demos_tasks/rmf_demos_tasks/dispatch_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/usr/bin/env python3

# Copyright 2024 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import asyncio
import json
import sys
import uuid

import rclpy
from rclpy.node import Node
from rclpy.parameter import Parameter
from rclpy.qos import QoSDurabilityPolicy as Durability
from rclpy.qos import QoSHistoryPolicy as History
from rclpy.qos import QoSProfile
from rclpy.qos import QoSReliabilityPolicy as Reliability

from rmf_task_msgs.msg import ApiRequest, ApiResponse


###############################################################################

class TaskRequester(Node):

def __init__(self, argv=sys.argv):
super().__init__('task_requester')
parser = argparse.ArgumentParser()
parser.add_argument(
'-c', '--category',
type=str,
default='compose',
help='Set the category of the task'
)
parser.add_argument(
'-f', '--file',
required=True,
type=str,
help='File containing a task description formatted in json'
)
parser.add_argument(
'-F', '--fleet',
type=str,
help='Fleet name, should define together with robot'
)
parser.add_argument(
'-R', '--robot',
type=str,
help='Robot name, should define together with fleet'
)
parser.add_argument(
'-st', '--start_time',
type=int,
default=0,
help='Start time from now in secs, default: 0'
)
parser.add_argument(
'-pt', '--priority',
type=int,
default=0,
help='Priority value for this request'
)
parser.add_argument(
'--use_sim_time',
action='store_true',
help='Use sim time, default: false'
)

self.args = parser.parse_args(argv[1:])
self.response = asyncio.Future()

with open(self.args.file) as f:
description = json.load(f)

transient_qos = QoSProfile(
history=History.KEEP_LAST,
depth=1,
reliability=Reliability.RELIABLE,
durability=Durability.TRANSIENT_LOCAL)
self.pub = self.create_publisher(
ApiRequest, 'task_api_requests', transient_qos
)

# enable ros sim time
if self.args.use_sim_time:
self.get_logger().info('Using Sim Time')
param = Parameter('use_sim_time', Parameter.Type.BOOL, True)
self.set_parameters([param])

# Construct task
msg = ApiRequest()
msg.request_id = "task_" + str(uuid.uuid4())
payload = {}

if self.args.robot and self.args.fleet:
self.get_logger().info("Using 'robot_task_request'")
payload['type'] = 'robot_task_request'
payload['robot'] = self.args.robot
payload['fleet'] = self.args.fleet
else:
self.get_logger().info("Using 'dispatch_task_request'")
payload['type'] = 'dispatch_task_request'

request = {}

# Set task request start time
now = self.get_clock().now().to_msg()
now.sec = now.sec + self.args.start_time
start_time = now.sec * 1000 + round(now.nanosec/10**6)
request['unix_millis_earliest_start_time'] = start_time
# todo(YV): Fill priority after schema is added

# Define task request category
request['category'] = self.args.category

# Define task request description
request['description'] = description
payload['request'] = request
msg.json_msg = json.dumps(payload)

def receive_response(response_msg: ApiResponse):
if response_msg.request_id == msg.request_id:
self.response.set_result(json.loads(response_msg.json_msg))

transient_qos.depth = 10
self.sub = self.create_subscription(
ApiResponse, 'task_api_responses', receive_response, transient_qos
)

print(f'Json msg payload: \n{json.dumps(payload, indent=2)}')
self.pub.publish(msg)


###############################################################################


def main(argv=sys.argv):
rclpy.init(args=sys.argv)
args_without_ros = rclpy.utilities.remove_ros_args(sys.argv)

task_requester = TaskRequester(args_without_ros)
rclpy.spin_until_future_complete(
task_requester, task_requester.response, timeout_sec=5.0)
if task_requester.response.done():
print(f'Got response:\n{task_requester.response.result()}')
else:
print('Did not get a response')
rclpy.shutdown()


if __name__ == '__main__':
main(sys.argv)
4 changes: 2 additions & 2 deletions rmf_demos_tasks/rmf_demos_tasks/dispatch_patrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ def __init__(self, argv=sys.argv):
'-F',
'--fleet',
type=str,
help='Fleet name, should define tgt with robot',
help='Fleet name, should define together with robot',
)
parser.add_argument(
'-R',
'--robot',
type=str,
help='Robot name, should define tgt with fleet',
help='Robot name, should define together with fleet',
)
parser.add_argument(
'-st',
Expand Down
1 change: 1 addition & 0 deletions rmf_demos_tasks/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
'dispatch_teleop = rmf_demos_tasks.dispatch_teleop:main',
'mock_docker = rmf_demos_tasks.mock_docker:main',
'teleop_robot = rmf_demos_tasks.teleop_robot:main',
'dispatch_json = rmf_demos_tasks.dispatch_json:main',
],
},
)
Loading