This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apex_launchtest_ros add better example
- Loading branch information
Pete Baughman
committed
Mar 19, 2019
1 parent
f8457a4
commit e2ae424
Showing
15 changed files
with
338 additions
and
461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
apex_launchtest_ros/apex_launchtest_ros/data_republisher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright 2019 Apex.AI, 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. | ||
|
||
|
||
class DataRepublisher: | ||
"""Republish mesasges with a transform function applied.""" | ||
|
||
def __init__(self, node, listen_topic, publish_topic, msg_type, transform_fn): | ||
""" | ||
Create a DataRepublisher. | ||
:param node: A rclpy node that will run the publisher and subscriber | ||
:param listen_topic: The topic to listen for incoming messages on | ||
:param publish_topic: The topic to republish messages on | ||
:param msg_type: The type of ROS msg to receive and republish | ||
:param transfor_fn: A function that takes one mesasge and returns a new message to | ||
republish or None to drop the message | ||
""" | ||
self.__num_received = 0 | ||
self.__republished_list = [] | ||
|
||
self.__node = node | ||
self.__subscriber = node.create_subscription( | ||
msg_type, | ||
listen_topic, | ||
callback=self.__cb | ||
) | ||
|
||
self.__publisher = node.create_publisher( | ||
msg_type, | ||
publish_topic | ||
) | ||
|
||
self.__transform_fn = transform_fn | ||
|
||
def shutdown(self): | ||
"""Stop republishing messages.""" | ||
self.__node.destroy_subscription(self.__subscriber) | ||
self.__node.destroy_publisher(self.__publisher) | ||
|
||
def get_num_received(self): | ||
"""Get the number of messages received on the listen_topic.""" | ||
return self.__num_received | ||
|
||
def get_num_republished(self): | ||
""" | ||
Get the number of messages published on publish_topic. | ||
This may be lower than get_num_received if the transform_fn indicated a message | ||
should be dropped | ||
""" | ||
return len(self.__republished_list) | ||
|
||
def get_republished(self): | ||
"""Get a list of all of the transformed messages republished.""" | ||
return self.__republished_list | ||
|
||
def __cb(self, msg): | ||
self.__num_received += 1 | ||
repub = self.__transform_fn(msg) | ||
|
||
if repub: | ||
self.__republished_list.append(msg) | ||
self.__publisher.publish(repub) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,31 @@ | ||
# Examples | ||
|
||
## message_counter.launch.py | ||
## `talker_listener.test.py` | ||
|
||
Usage: | ||
> apex_launchtest examples/message_counter.launch.py | ||
> apex_launchtest examples/talker_listener.test.py | ||
Launches a message counter node and runs a few tests to interact with the node | ||
This test launchtes the talker and listener example nodes from demo_nodes_py and interacts | ||
with them via their ROS interfaces. Remaps are used so that one of the tests can sit in | ||
between the talker and the listener and change the data on the fly. | ||
|
||
Node that in the setUpClass method, the test makes sure that the listener is subscribed and | ||
republishing messages. Since the listener process provides no synchronization mechanism to | ||
inform the outside world that it's up and running, this step is necessary especially in resource | ||
constrained environments where process startup may take a non negligable amount of time. This | ||
is often the cause of "flakyness" in tests on CI systems. A more robust design of the talker and | ||
listener processes might provide some positive feedback that the node is up and running, but these | ||
are simple example nodes. | ||
|
||
#### test_fuzzy_data | ||
This test gives an example of what a test that fuzzes data might look like. A ROS subscriber | ||
and publisher pair encapsulated in a `DataRepublisher` object changes the string "Hello World" to | ||
"Aloha World" as it travles between the talker and the listener | ||
|
||
#### test_listener_receives | ||
This test publishes unique messages on the /chatter topic and asserts that the same messages | ||
go to the stdout of the listener node | ||
|
||
#### test_talker_transmits | ||
This test subscribes to the remapped /talker_chatter topic and makes sure the talker node also | ||
writes the data it's transmitting to stdout |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.