diff --git a/json_tf_bridge/CMakeLists.txt b/json_tf_bridge/CMakeLists.txt new file mode 100644 index 0000000..4fcdb95 --- /dev/null +++ b/json_tf_bridge/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 3.8) +project(json_tf_bridge) + +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-Wall -Wextra -Wpedantic) +endif() + +# find dependencies +find_package(ament_cmake REQUIRED) +find_package(nlohmann_json 3.2.0 REQUIRED) +find_package(geometry_msgs REQUIRED) +find_package(std_msgs REQUIRED) +find_package(rclcpp REQUIRED) +find_package(tf2 REQUIRED) +find_package(tf2_ros REQUIRED) + +# uncomment the following section in order to fill in +# further dependencies manually. +# find_package( REQUIRED) + +add_executable(json_tf_bridge_node src/json_tf_bridge_node.cpp) +target_link_libraries(json_tf_bridge_node nlohmann_json::nlohmann_json) +ament_target_dependencies( + json_tf_bridge_node + geometry_msgs + std_msgs + rclcpp + tf2 + tf2_ros +) +target_include_directories(json_tf_bridge_node PUBLIC + $ + $) +target_compile_features(json_tf_bridge_node PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17 + + +install(TARGETS json_tf_bridge_node + DESTINATION lib/${PROJECT_NAME}) + +if(BUILD_TESTING) + find_package(ament_lint_auto REQUIRED) + # the following line skips the linter which checks for copyrights + # comment the line when a copyright and license is added to all source files + set(ament_cmake_copyright_FOUND TRUE) + # the following line skips cpplint (only works in a git repo) + # comment the line when this package is in a git repo and when + # a copyright and license is added to all source files + set(ament_cmake_cpplint_FOUND TRUE) + ament_lint_auto_find_test_dependencies() +endif() + +ament_package() diff --git a/json_tf_bridge/package.xml b/json_tf_bridge/package.xml new file mode 100644 index 0000000..40010f9 --- /dev/null +++ b/json_tf_bridge/package.xml @@ -0,0 +1,22 @@ + + + + json_tf_bridge + 0.0.0 + TODO: Package description + pac48 + TODO: License declaration + + ament_cmake + + geometry_msgs + std_msgs + rclcpp + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/json_tf_bridge/src/json_tf_bridge_node.cpp b/json_tf_bridge/src/json_tf_bridge_node.cpp new file mode 100644 index 0000000..360d0a2 --- /dev/null +++ b/json_tf_bridge/src/json_tf_bridge_node.cpp @@ -0,0 +1,181 @@ +#include +#include + +#include + +#include "geometry_msgs/msg/transform_stamped.hpp" +#include "rclcpp/rclcpp.hpp" +#include "tf2/LinearMath/Quaternion.h" +#include "tf2_ros/transform_broadcaster.h" +#include "std_msgs/msg/string.hpp" + + +namespace nlohmann { + +/////////////////////////////////////////////////////////////////////////////// +// std::variant +/////////////////////////////////////////////////////////////////////////////// +// Try to set the value of type T into the variant data if it fails, do nothing + template + void variant_from_json(const nlohmann::json &j, std::variant &data) { + try { + data = j.get(); + } catch (...) { + } + } + + template + struct adl_serializer> { + static void to_json(nlohmann::json &j, const std::variant &data) { + // Will call j = v automatically for the right type + std::visit([&j](const auto &v) { j = v; }, data); + } + + static void from_json(const nlohmann::json &j, std::variant &data) { + // Call variant_from_json for all types, only one will succeed + (variant_from_json(j, data), ...); + } + }; + +/////////////////////////////////////////////////////////////////////////////// +// std::optional +/////////////////////////////////////////////////////////////////////////////// + template + void optional_to_json(nlohmann::json &j, const char *name, const std::optional &value) { + if (value) + j[name] = *value; + } + + template + void optional_from_json(const nlohmann::json &j, const char *name, std::optional &value) { + const auto it = j.find(name); + if (it != j.end()) + value = it->get(); + else + value = std::nullopt; + } + +/////////////////////////////////////////////////////////////////////////////// +// all together +/////////////////////////////////////////////////////////////////////////////// + template + constexpr bool is_optional = false; + template + constexpr bool is_optional> = true; + + template + void extended_to_json(const char *key, nlohmann::json &j, const T &value) { + if constexpr (is_optional) + nlohmann::optional_to_json(j, key, value); + else + j[key] = value; + } + + template + void extended_from_json(const char *key, const nlohmann::json &j, T &value) { + if constexpr (is_optional) + nlohmann::optional_from_json(j, key, value); + else + j.at(key).get_to(value); + } + +} + +#define EXTEND_JSON_TO(v1) extended_to_json(#v1, nlohmann_json_j, nlohmann_json_t.v1); +#define EXTEND_JSON_FROM(v1) extended_from_json(#v1, nlohmann_json_j, nlohmann_json_t.v1); + +#define NLOHMANN_JSONIFY_ALL_THINGS(Type, ...) \ + inline void to_json(nlohmann::json &nlohmann_json_j, const Type &nlohmann_json_t) { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_TO, __VA_ARGS__)) \ + } \ + inline void from_json(const nlohmann::json &nlohmann_json_j, Type &nlohmann_json_t) { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(EXTEND_JSON_FROM, __VA_ARGS__)) \ + } + + + +// for convenience +using json = nlohmann::json; + +struct TransformData { + double posX; + double posY; + double posZ; + double quatW; + double quatX; + double quatY; + double quatZ; + int sec; + unsigned int nanosec; + std::string frameID; +}; + +NLOHMANN_JSONIFY_ALL_THINGS(TransformData, posX, posY, posZ, quatW, quatX, quatY, quatZ, sec, nanosec, frameID) + +class FramePublisher : public rclcpp::Node { +public: + FramePublisher() + : Node("json_tf_bridge") { + + // Initialize the transform broadcaster + tf_broadcaster_ = + std::make_unique(*this); + + + subscription_ = this->create_subscription( + "unity_tf_bridge", 10, + std::bind(&FramePublisher::callback, this, std::placeholders::_1)); + + clock_ = rclcpp::Clock(rcl_clock_type_e::RCL_ROS_TIME); + } + +private: + void callback(const std::shared_ptr msg) { + auto json_msg = json::parse(msg->data); + auto transform_data_vec = json_msg.get>(); + + for (auto transform_data: transform_data_vec) { + + geometry_msgs::msg::TransformStamped t; + + // Read message content and assign it to + // corresponding tf variables +// t.header.stamp = clock_.now(); + t.header.stamp.sec = transform_data.sec; + t.header.stamp.nanosec = transform_data.nanosec; + t.header.frame_id = "odom"; + t.child_frame_id = transform_data.frameID; + + // Turtle only exists in 2D, thus we get x and y translation + // coordinates from the message and set the z coordinate to 0 + t.transform.translation.y = -transform_data.posX; + t.transform.translation.z = transform_data.posY; + t.transform.translation.x = transform_data.posZ; + + // For the same reason, turtle can only rotate around one axis + // and this why we set rotation in x and y to 0 and obtain + // rotation in z axis from the message + tf2::Quaternion q; + t.transform.rotation.w = -transform_data.quatW; + t.transform.rotation.y = -transform_data.quatX; + t.transform.rotation.z = transform_data.quatY; + t.transform.rotation.x = transform_data.quatZ; + + // Send the transformation + tf_broadcaster_->sendTransform(t); + } + } + + rclcpp::Subscription::SharedPtr subscription_; + std::unique_ptr tf_broadcaster_; + rclcpp::Clock clock_; +}; + +int main(int argc, char **argv) { + + rclcpp::init(argc, argv); + rclcpp::spin(std::make_shared()); + rclcpp::shutdown(); + + return 0; +} diff --git a/ros_tcp_endpoint/.gitignore b/ros_tcp_endpoint/.gitignore new file mode 100644 index 0000000..57905cc --- /dev/null +++ b/ros_tcp_endpoint/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +*.pyc +.idea +.coverage +test-results/ +*~ +build +devel diff --git a/ros_tcp_endpoint/.pre-commit-config.yaml b/ros_tcp_endpoint/.pre-commit-config.yaml new file mode 100644 index 0000000..f27883e --- /dev/null +++ b/ros_tcp_endpoint/.pre-commit-config.yaml @@ -0,0 +1,12 @@ +repos: +- repo: https://github.com/python/black + rev: 19.3b0 + hooks: + - id: black + args: [--line-length=100] +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.4.0 + hooks: + - id: trailing-whitespace + name: trailing-whitespace-markdown + types: [markdown] \ No newline at end of file diff --git a/ros_tcp_endpoint/.yamato/sonar.yml b/ros_tcp_endpoint/.yamato/sonar.yml new file mode 100644 index 0000000..0fe0f42 --- /dev/null +++ b/ros_tcp_endpoint/.yamato/sonar.yml @@ -0,0 +1,17 @@ +name: Sonarqube Standard Scan +agent: + type: Unity::metal::macmini + image: package-ci/mac + flavor: m1.mac +variables: + SONARQUBE_PROJECT_KEY: ai-robotics-endpoint-ros2 +commands: + - curl https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472-macosx.zip -o sonar-scanner-macosx.zip -L + - unzip sonar-scanner-macosx.zip -d ~/sonar-scanner + - ~/sonar-scanner/sonar-scanner-4.6.2.2472-macosx/bin/sonar-scanner -Dsonar.projectKey=$SONARQUBE_PROJECT_KEY -Dsonar.sources=. -Dsonar.host.url=$SONARQUBE_ENDPOINT_URL_PRD -Dsonar.login=$SONARQUBE_TOKEN_PRD +triggers: + cancel_old_ci: true + expression: | + ((pull_request.target eq "main" OR pull_request.target eq "dev-ros2") + AND NOT pull_request.push.changes.all match "**/*.md") OR + (push.branch eq "main" OR push.branch eq "dev-ros2") \ No newline at end of file diff --git a/ros_tcp_endpoint/.yamato/yamato-config.yml b/ros_tcp_endpoint/.yamato/yamato-config.yml new file mode 100644 index 0000000..b25389c --- /dev/null +++ b/ros_tcp_endpoint/.yamato/yamato-config.yml @@ -0,0 +1,33 @@ +name: Endpoint Unit Tests +agent: + type: Unity::VM + image: robotics/ci-ros2-galactic-ubuntu20:v0.0.2-916903 + flavor: i1.large +variables: + # Coverage measured as a percentage (out of 100) + COVERAGE_EXPECTED: 3.5 +commands: + # run unit tests and save test results in /home/bokken/build/output/Unity-Technologies/ROS-TCP-Endpoint + - command: | + source /opt/ros/galactic/setup.bash && echo "ROS_DISTRO == galactic" + cd .. && mkdir -p ros_ws/src && cp -r ./ROS-TCP-Endpoint ros_ws/src + cd ros_ws && colcon build && source install/local_setup.bash + cd src/ROS-TCP-Endpoint + python3 -m pytest --cov=. --cov-report xml:./test-results/coverage.xml --cov-report html:./test-results/coverage.html test/ + # check the test coverage + - command: | + linecoverage=$(head -2 test-results/coverage.xml | grep -Eo 'line-rate="[0-9]+([.][0-9]+)?"' | grep -Eo "[0-9]+([.][0-9]+)?") + echo "Line coverage: $linecoverage" + if (( $(echo "$linecoverage * 100.0 < $COVERAGE_EXPECTED" | bc -l) )); + then echo "ERROR: Code Coverage is under threshold of $COVERAGE_EXPECTED%" && exit 1 + fi +triggers: + cancel_old_ci: true + expression: | + ((pull_request.target eq "main-ros2" OR pull_request.target eq "dev-ros2") + AND NOT pull_request.push.changes.all match "**/*.md") OR + (push.branch eq "main-ros2" OR push.branch eq "dev-ros2") +artifacts: + logs: + paths: + - "test-results/**/*" diff --git a/ros_tcp_endpoint/CHANGELOG.md b/ros_tcp_endpoint/CHANGELOG.md new file mode 100644 index 0000000..725692b --- /dev/null +++ b/ros_tcp_endpoint/CHANGELOG.md @@ -0,0 +1,101 @@ +# Changelog + +All notable changes to this repository will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). + +## Unreleased + +### Upgrade Notes + +### Known Issues + +### Added + +Added Sonarqube scanner + +### Changed + +### Deprecated + +### Removed + +### Fixed + + +## [0.7.0] - 2022-02-01 + +### Added + +Added Sonarqube scanner + +Send information during hand shaking for ros and package version checks + +Send service response as one queue item + + +## [0.6.0] - 2021-09-30 + +Add the [Close Stale Issues](https://github.com/marketplace/actions/close-stale-issues) action + +### Upgrade Notes + +### Known Issues + +### Added + +Support for queue_size and latch for publishers. (https://github.com/Unity-Technologies/ROS-TCP-Endpoint/issues/82) + +### Changed + +### Deprecated + +### Removed + +### Fixed + +## [0.5.0] - 2021-07-15 + +### Upgrade Notes + +Upgrade the ROS communication to support ROS2 with Unity + +### Known Issues + +### Added + +### Changed + +### Deprecated + +### Removed + +### Fixed + +## [0.4.0] - 2021-05-27 + +Note: the logs only reflects the changes from version 0.3.0 + +### Upgrade Notes + +RosConnection 2.0: maintain a single constant connection from Unity to the Endpoint. This is more efficient than opening one connection per message, and it eliminates a whole bunch of user issues caused by ROS being unable to connect to Unity due to firewalls, proxies, etc. + +### Known Issues + +### Added + +Add a link to the Robotics forum, and add a config.yml to add a link in the Github Issues page + +Add linter, unit tests, and test coverage reporting + +### Changed + +Improving the performance of the read_message in client.py, This is done by receiving the entire message all at once instead of reading 1024 byte chunks and stitching them together as you go. + +### Deprecated + +### Removed + +Remove outdated handshake references + +### Fixed diff --git a/ros_tcp_endpoint/CODE_OF_CONDUCT.md b/ros_tcp_endpoint/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..a7a4c57 --- /dev/null +++ b/ros_tcp_endpoint/CODE_OF_CONDUCT.md @@ -0,0 +1,74 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, gender identity and expression, level of experience, +nationality, personal appearance, race, religion, or sexual identity and +orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or + advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic + address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at [unity-robotics@unity3d.com](mailto:unity-robotics@unity3d.com). All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 1.4, available at +https://www.contributor-covenant.org/version/1/4/code-of-conduct/ + +[homepage]: https://www.contributor-covenant.org \ No newline at end of file diff --git a/ros_tcp_endpoint/CONTRIBUTING.md b/ros_tcp_endpoint/CONTRIBUTING.md new file mode 100644 index 0000000..c0d4360 --- /dev/null +++ b/ros_tcp_endpoint/CONTRIBUTING.md @@ -0,0 +1,64 @@ +# Contribution Guidelines + +Thank you for your interest in contributing to Unity Robotics! To facilitate your +contributions, we've outlined a brief set of guidelines to ensure that your extensions +can be easily integrated. + +## Communication + +First, please read through our +[code of conduct](CODE_OF_CONDUCT.md), +as we expect all our contributors to follow it. + +Second, before starting on a project that you intend to contribute to any of our +Unity Robotics packages or tutorials, we **strongly** recommend posting on the repository's +[Issues page](https://github.com/Unity-Technologies/ROS-TCP-Endpoint/issues) and +briefly outlining the changes you plan to make. This will enable us to provide +some context that may be helpful for you. This could range from advice and +feedback on how to optimally perform your changes or reasons for not doing it. + +## Git Branches + +The `main` branch corresponds to the most recent stable version of the project. The `dev` branch +contains changes that are staged to be merged into `main` as the team sees fit. + +When contributing to the project, please make sure that your Pull Request (PR) +does the following: + +- Is up-to-date with and targets the `dev` branch +- Contains a detailed description of the changes performed +- Has corresponding changes to documentation, unit tests and sample environments (if + applicable) +- Contains a summary of the tests performed to validate your changes +- Links to issue numbers that the PR resolves (if any) + + + +## Code style + +All Python code should follow the [PEP 8 style guidelines](https://pep8.org/). + +All C# code should follow the [Microsoft C# Coding Conventions](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions). +Additionally, the [Unity Coding package](https://docs.unity3d.com/Packages/com.unity.coding@0.1/manual/index.html) +can be used to format, encode, and lint your code according to the standard Unity +development conventions. Be aware that these Unity conventions will supersede the +Microsoft C# Coding Conventions where applicable. + +Please note that even if the code you are changing does not adhere to these guidelines, +we expect your submissions to follow these conventions. + +## Contributor License Agreements + +When you open a pull request, you will be asked to acknowledge our Contributor +License Agreement. We allow both individual contributions and contributions made +on behalf of companies. We use an open source tool called CLA assistant. If you +have any questions on our CLA, please +[submit an issue](https://github.com/Unity-Technologies/ROS-TCP-Endpoint/issues) or +email us at [unity-robotics@unity3d.com](mailto:unity-robotics@unity3d.com). + +## Contribution review + +Once you have a change ready following the above ground rules, simply make a +pull request in GitHub. \ No newline at end of file diff --git a/ros_tcp_endpoint/LICENSE b/ros_tcp_endpoint/LICENSE new file mode 100644 index 0000000..7507bc9 --- /dev/null +++ b/ros_tcp_endpoint/LICENSE @@ -0,0 +1,190 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + Copyright 2020 Unity Technologies + + 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. diff --git a/ros_tcp_endpoint/README.md b/ros_tcp_endpoint/README.md new file mode 100644 index 0000000..40f60cb --- /dev/null +++ b/ros_tcp_endpoint/README.md @@ -0,0 +1,26 @@ +# ROS TCP Endpoint + +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) + +## Introduction + +[ROS](https://www.ros.org/) package used to create an endpoint to accept ROS messages sent from a Unity scene using the [ROS TCP Connector](https://github.com/Unity-Technologies/ROS-TCP-Connector) scripts. + +Instructions and examples on how to use this ROS package can be found on the [Unity Robotics Hub](https://github.com/Unity-Technologies/Unity-Robotics-Hub/blob/master/tutorials/ros_unity_integration/README.md) repository. + +## Community and Feedback + +The Unity Robotics projects are open-source and we encourage and welcome contributions. +If you wish to contribute, be sure to review our [contribution guidelines](CONTRIBUTING.md) +and [code of conduct](CODE_OF_CONDUCT.md). + +## Support +For questions or discussions about Unity Robotics package installations or how to best set up and integrate your robotics projects, please create a new thread on the [Unity Robotics forum](https://forum.unity.com/forums/robotics.623/) and make sure to include as much detail as possible. + +For feature requests, bugs, or other issues, please file a [GitHub issue](https://github.com/Unity-Technologies/ROS-TCP-Endpoint/issues) using the provided templates and the Robotics team will investigate as soon as possible. + +For any other questions or feedback, connect directly with the +Robotics team at [unity-robotics@unity3d.com](mailto:unity-robotics@unity3d.com). + +## License +[Apache License 2.0](LICENSE) \ No newline at end of file diff --git a/ros_tcp_endpoint/launch/endpoint.launch.py b/ros_tcp_endpoint/launch/endpoint.launch.py new file mode 100644 index 0000000..2e1bf8c --- /dev/null +++ b/ros_tcp_endpoint/launch/endpoint.launch.py @@ -0,0 +1,15 @@ +from launch import LaunchDescription +from launch_ros.actions import Node + + +def generate_launch_description(): + return LaunchDescription( + [ + Node( + package="ros_tcp_endpoint", + executable="default_server_endpoint", + emulate_tty=True, + parameters=[{"ROS_IP": "0.0.0.0"}, {"ROS_TCP_PORT": 10000}], + ) + ] + ) diff --git a/ros_tcp_endpoint/package.xml b/ros_tcp_endpoint/package.xml new file mode 100644 index 0000000..7919782 --- /dev/null +++ b/ros_tcp_endpoint/package.xml @@ -0,0 +1,20 @@ + + + + ros_tcp_endpoint + 0.7.0 + ROS TCP Endpoint Unity Integration (ROS2 version) + Acts as the bridge between Unity messages sent via TCP socket and ROS messages. + + Unity Robotics + Apache 2.0 + + ament_copyright + ament_flake8 + ament_pep257 + python3-pytest + + + ament_python + + diff --git a/ros_tcp_endpoint/requirements.txt b/ros_tcp_endpoint/requirements.txt new file mode 100644 index 0000000..5ad32fb --- /dev/null +++ b/ros_tcp_endpoint/requirements.txt @@ -0,0 +1,3 @@ +black +pre-commit +pytest-cov \ No newline at end of file diff --git a/ros_tcp_endpoint/resource/ros_tcp_endpoint b/ros_tcp_endpoint/resource/ros_tcp_endpoint new file mode 100644 index 0000000..e69de29 diff --git a/ros_tcp_endpoint/ros_tcp_endpoint/__init__.py b/ros_tcp_endpoint/ros_tcp_endpoint/__init__.py new file mode 100644 index 0000000..381ca8e --- /dev/null +++ b/ros_tcp_endpoint/ros_tcp_endpoint/__init__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Unity Technologies +# +# 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. + +from .server import TcpServer diff --git a/ros_tcp_endpoint/ros_tcp_endpoint/client.py b/ros_tcp_endpoint/ros_tcp_endpoint/client.py new file mode 100644 index 0000000..1783fb8 --- /dev/null +++ b/ros_tcp_endpoint/ros_tcp_endpoint/client.py @@ -0,0 +1,228 @@ +# Copyright 2020 Unity Technologies +# +# 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 rclpy +import struct + +import threading +import json + +from rclpy.serialization import deserialize_message +from rclpy.serialization import serialize_message + +from .exceptions import TopicOrServiceNameDoesNotExistError + + +class ClientThread(threading.Thread): + """ + Thread class to read all data from a connection and pass along the data to the + desired source. + """ + + def __init__(self, conn, tcp_server, incoming_ip, incoming_port): + """ + Set class variables + Args: + conn: + tcp_server: server object + incoming_ip: connected from this IP address + incoming_port: connected from this port + """ + self.conn = conn + self.tcp_server = tcp_server + self.incoming_ip = incoming_ip + self.incoming_port = incoming_port + threading.Thread.__init__(self) + + @staticmethod + def recvall(conn, size, flags=0): + """ + Receive exactly bufsize bytes from the socket. + """ + buffer = bytearray(size) + view = memoryview(buffer) + pos = 0 + while pos < size: + read = conn.recv_into(view[pos:], size - pos, flags) + if not read: + raise IOError("No more data available") + pos += read + return bytes(buffer) + + @staticmethod + def read_int32(conn): + """ + Reads four bytes from socket connection and unpacks them to an int + + Returns: int + + """ + raw_bytes = ClientThread.recvall(conn, 4) + num = struct.unpack(" 0 and not data: + self.logerr("No data for a message size of {}, breaking!".format(full_message_size)) + return + + destination = destination.rstrip("\x00") + return destination, data + + @staticmethod + def serialize_message(destination, message): + """ + Serialize a destination and message class. + + Args: + destination: name of destination + message: message class to serialize + + Returns: + serialized destination and message as a list of bytes + """ + dest_bytes = destination.encode("utf-8") + length = len(dest_bytes) + dest_info = struct.pack(" 1: + if node is not None: + self.tcp_server.get_logger().warning( + "Only one message type per topic is supported, but found multiple types for topic {}; maintaining {} as the subscribed type.".format( + i[0], self.parse_message_name(node.msg) + ) + ) + topic_list.types = [ + item[1][0].replace("/msg/", "/") + if (len(item[1]) <= 1) + else self.parse_message_name(node.msg) + for item in topics_and_types + ] + serialized_bytes = ClientThread.serialize_command("__topic_list", topic_list) + self.queue.put(serialized_bytes) + + def start_sender(self, conn, halt_event): + sender_thread = threading.Thread( + target=self.sender_loop, args=(conn, self.sender_id, halt_event) + ) + self.sender_id += 1 + + # Exit the server thread when the main thread terminates + sender_thread.daemon = True + sender_thread.start() + + def sender_loop(self, conn, tid, halt_event): + s = None + local_queue = Queue() + + # send a handshake message to confirm the connection and version number + handshake_metadata = SysCommand_Handshake_Metadata() + handshake = SysCommand_Handshake(handshake_metadata) + local_queue.put(ClientThread.serialize_command("__handshake", handshake)) + + with self.queue_lock: + self.queue = local_queue + + try: + while not halt_event.is_set(): + try: + item = local_queue.get(timeout=self.time_between_halt_checks) + except Empty: + # I'd like to just wait on the queue, but we also need to check occasionally for the connection being closed + # (otherwise the thread never terminates.) + continue + + # print("Sender {} sending an item".format(tid)) + + try: + conn.sendall(item) + except Exception as e: + self.tcp_server.logerr("Exception {}".format(e)) + break + finally: + halt_event.set() + with self.queue_lock: + if self.queue is local_queue: + self.queue = None + + def parse_message_name(self, name): + try: + # Example input string: + names = (str(type(name))).split(".") + module_name = names[0][8:] + class_name = names[-1].split("_")[-1][:-2] + return "{}/{}".format(module_name, class_name) + except (IndexError, AttributeError, ImportError) as e: + self.tcp_server.logerr("Failed to resolve message name: {}".format(e)) + return None + + +class SysCommand_Log: + def __init__(self): + text = "" + + +class SysCommand_Service: + def __init__(self): + srv_id = 0 + + +class SysCommand_TopicsResponse: + def __init__(self): + topics = [] + types = [] + + +class SysCommand_Handshake: + def __init__(self, metadata): + self.version = "v0.7.0" + self.metadata = json.dumps(metadata.__dict__) + + +class SysCommand_Handshake_Metadata: + def __init__(self): + self.protocol = "ROS2" diff --git a/ros_tcp_endpoint/ros_tcp_endpoint/thread_pauser.py b/ros_tcp_endpoint/ros_tcp_endpoint/thread_pauser.py new file mode 100644 index 0000000..ed98cab --- /dev/null +++ b/ros_tcp_endpoint/ros_tcp_endpoint/thread_pauser.py @@ -0,0 +1,15 @@ +import threading + +class ThreadPauser: + def __init__(self): + self.condition = threading.Condition() + self.result = None + + def sleep_until_resumed(self): + with self.condition: + self.condition.wait() + + def resume_with_result(self, result): + self.result = result + with self.condition: + self.condition.notify() diff --git a/ros_tcp_endpoint/ros_tcp_endpoint/unity_service.py b/ros_tcp_endpoint/ros_tcp_endpoint/unity_service.py new file mode 100644 index 0000000..c23b2fa --- /dev/null +++ b/ros_tcp_endpoint/ros_tcp_endpoint/unity_service.py @@ -0,0 +1,65 @@ +# Copyright 2020 Unity Technologies +# +# 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 rclpy +import socket +import re + +from .communication import RosReceiver +from .client import ClientThread + + +class UnityService(RosReceiver): + """ + Class to register a ROS service that's implemented in Unity. + """ + + def __init__(self, topic, service_class, tcp_server, queue_size=10): + """ + + Args: + topic: Topic name to publish messages to + service_class: The message class in catkin workspace + queue_size: Max number of entries to maintain in an outgoing queue + """ + strippedTopic = re.sub("[^A-Za-z0-9_]+", "", topic) + node_name = f"{strippedTopic}_service" + RosReceiver.__init__(self, node_name) + + self.topic = topic + self.node_name = node_name + self.service_class = service_class + self.tcp_server = tcp_server + self.queue_size = queue_size + + self.service = self.create_service(self.service_class, self.topic, self.send) + + def send(self, request, response): + """ + Connect to TCP endpoint on client, pass along message and get reply + Args: + data: message data to send outside of ROS network + + Returns: + The response message + """ + return self.tcp_server.send_unity_service(self.topic, self.service_class, request) + + def unregister(self): + """ + + Returns: + + """ + self.destroy_node() diff --git a/ros_tcp_endpoint/setup.cfg b/ros_tcp_endpoint/setup.cfg new file mode 100644 index 0000000..1525691 --- /dev/null +++ b/ros_tcp_endpoint/setup.cfg @@ -0,0 +1,4 @@ +[develop] +script-dir=$base/lib/ros_tcp_endpoint +[install] +install-scripts=$base/lib/ros_tcp_endpoint diff --git a/ros_tcp_endpoint/setup.py b/ros_tcp_endpoint/setup.py new file mode 100644 index 0000000..fa1a1f0 --- /dev/null +++ b/ros_tcp_endpoint/setup.py @@ -0,0 +1,29 @@ +import os + +from setuptools import setup + +package_name = "ros_tcp_endpoint" +share_dir = os.path.join("share", package_name) + +setup( + name=package_name, + version="0.0.1", + packages=[package_name], + data_files=[ + ("share/ament_index/resource_index/packages", ["resource/" + package_name]), + (share_dir, ["package.xml"]), + (os.path.join(share_dir, "launch"), ["launch/endpoint.launch.py"]), + ], + install_requires=["setuptools"], + zip_safe=True, + maintainer="Unity Robotics", + maintainer_email="unity-robotics@unity3d.com", + description="ROS TCP Endpoint Unity Integration (ROS2 version)", + license="Apache 2.0", + tests_require=["pytest"], + entry_points={ + "console_scripts": [ + "default_server_endpoint = ros_tcp_endpoint.default_server_endpoint:main" + ] + }, +) diff --git a/ros_tcp_endpoint/test/test_copyright.py b/ros_tcp_endpoint/test/test_copyright.py new file mode 100644 index 0000000..0f04262 --- /dev/null +++ b/ros_tcp_endpoint/test/test_copyright.py @@ -0,0 +1,24 @@ +# Copyright 2015 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. + +from ament_copyright.main import main +import pytest + + +@pytest.mark.copyright +@pytest.mark.linter +def test_copyright(): + rc = main(argv=[".", "test"]) + if rc != 0: + print("WARNING: Copyright linter found problems.") diff --git a/ros_tcp_endpoint/test/test_flake8.py b/ros_tcp_endpoint/test/test_flake8.py new file mode 100644 index 0000000..e908441 --- /dev/null +++ b/ros_tcp_endpoint/test/test_flake8.py @@ -0,0 +1,24 @@ +# Copyright 2017 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. + +from ament_flake8.main import main_with_errors +import pytest + + +@pytest.mark.flake8 +@pytest.mark.linter +def test_flake8(): + rc, errors = main_with_errors(argv=[]) + if rc != 0: + print("Found %d code style errors / warnings:\n" % len(errors) + "\n".join(errors)) diff --git a/ros_tcp_endpoint/test/test_pep257.py b/ros_tcp_endpoint/test/test_pep257.py new file mode 100644 index 0000000..74fdc2a --- /dev/null +++ b/ros_tcp_endpoint/test/test_pep257.py @@ -0,0 +1,24 @@ +# Copyright 2015 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. + +from ament_pep257.main import main +import pytest + + +@pytest.mark.linter +@pytest.mark.pep257 +def test_pep257(): + rc = main(argv=[".", "test"]) + if rc != 0: + print("Found code style errors / warnings") diff --git a/unity_binary/package.xml b/unity_binary/package.xml new file mode 100644 index 0000000..a2a64f0 --- /dev/null +++ b/unity_binary/package.xml @@ -0,0 +1,18 @@ + + + + unity_binary + 0.0.0 + TODO: Package description + pac48 + TODO: License declaration + + ament_copyright + ament_flake8 + ament_pep257 + python3-pytest + + + ament_python + + diff --git a/unity_binary/resource/unity_binary b/unity_binary/resource/unity_binary new file mode 100644 index 0000000..e69de29 diff --git a/unity_binary/setup.cfg b/unity_binary/setup.cfg new file mode 100644 index 0000000..a0d493f --- /dev/null +++ b/unity_binary/setup.cfg @@ -0,0 +1,4 @@ +[develop] +script_dir=$base/lib/unity_binary +[install] +install_scripts=$base/lib/unity_binary diff --git a/unity_binary/setup.py b/unity_binary/setup.py new file mode 100644 index 0000000..717ef00 --- /dev/null +++ b/unity_binary/setup.py @@ -0,0 +1,26 @@ +from setuptools import setup + +package_name = 'unity_binary' + +setup( + name=package_name, + version='0.0.0', + packages=[package_name], + data_files=[ + ('share/ament_index/resource_index/packages', + ['resource/' + package_name]), + ('share/' + package_name, ['package.xml']), + ], + install_requires=['setuptools'], + zip_safe=True, + maintainer='pac48', + maintainer_email='pac48@wildcats.unh.edu', + description='TODO: Package description', + license='TODO: License declaration', + tests_require=['pytest'], + entry_points={ + 'console_scripts': [ + 'download_and_run_binary = unity_binary.download_and_run_binary:main' + ], + }, +) diff --git a/unity_binary/test/test_copyright.py b/unity_binary/test/test_copyright.py new file mode 100644 index 0000000..97a3919 --- /dev/null +++ b/unity_binary/test/test_copyright.py @@ -0,0 +1,25 @@ +# Copyright 2015 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. + +from ament_copyright.main import main +import pytest + + +# Remove the `skip` decorator once the source file(s) have a copyright header +@pytest.mark.skip(reason='No copyright header has been placed in the generated source file.') +@pytest.mark.copyright +@pytest.mark.linter +def test_copyright(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found errors' diff --git a/unity_binary/test/test_flake8.py b/unity_binary/test/test_flake8.py new file mode 100644 index 0000000..27ee107 --- /dev/null +++ b/unity_binary/test/test_flake8.py @@ -0,0 +1,25 @@ +# Copyright 2017 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. + +from ament_flake8.main import main_with_errors +import pytest + + +@pytest.mark.flake8 +@pytest.mark.linter +def test_flake8(): + rc, errors = main_with_errors(argv=[]) + assert rc == 0, \ + 'Found %d code style errors / warnings:\n' % len(errors) + \ + '\n'.join(errors) diff --git a/unity_binary/test/test_pep257.py b/unity_binary/test/test_pep257.py new file mode 100644 index 0000000..b234a38 --- /dev/null +++ b/unity_binary/test/test_pep257.py @@ -0,0 +1,23 @@ +# Copyright 2015 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. + +from ament_pep257.main import main +import pytest + + +@pytest.mark.linter +@pytest.mark.pep257 +def test_pep257(): + rc = main(argv=['.', 'test']) + assert rc == 0, 'Found code style errors / warnings' diff --git a/unity_binary/unity_binary/__init__.py b/unity_binary/unity_binary/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/unity_binary/unity_binary/download_and_run_binary.py b/unity_binary/unity_binary/download_and_run_binary.py new file mode 100644 index 0000000..133186e --- /dev/null +++ b/unity_binary/unity_binary/download_and_run_binary.py @@ -0,0 +1,34 @@ +import requests +import zipfile +import os + + +def main(): + URL = "https://universitysystemnh-my.sharepoint.com/:u:/g/personal/mb1215_usnh_edu/EVM_16qj-QJDsMFiaw6DGYYBQEtt6hvDj211F_HHqjruzg?download=1" + home_path = os.path.expanduser('~') + install_path = os.path.join(home_path, 'unity_binary') + installed = os.path.exists(install_path) + + if not installed: + with requests.get(URL, stream=True) as r: + print(f'downloading file: total size: {len(r.content)}') + downloaded = 0 + with open("sim.zip", 'wb') as f: + for chunk in r.iter_content(chunk_size=1024 * 64 * 16): + # writing one chunk at a time to pdf file + if chunk: + f.write(chunk) + downloaded += 1024 * 64 * 16 + print(f'downloading [% {100 * (downloaded / len(r.content))}]') + + print('download complete') + + with zipfile.ZipFile('sim.zip', 'r') as zip_ref: + zip_ref.extractall(install_path) + + os.system('chmod +x ' + os.path.join(install_path, 'sim', 'smart_home_unity_binary.x86_64')) + os.system(os.path.join(install_path, 'sim', 'smart_home_unity_binary.x86_64')) + + +if __name__ == '__main__': + main() diff --git a/unity_launch/CMakeLists.txt b/unity_launch/CMakeLists.txt new file mode 100644 index 0000000..469a664 --- /dev/null +++ b/unity_launch/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.8) +project(unity_launch) + +# find dependencies +find_package(ament_cmake REQUIRED) + + +install( + DIRECTORY launch rviz + DESTINATION share/unity_launch +) + +ament_package() diff --git a/unity_launch/launch/launch.launch.py b/unity_launch/launch/launch.launch.py new file mode 100644 index 0000000..392adc0 --- /dev/null +++ b/unity_launch/launch/launch.launch.py @@ -0,0 +1,59 @@ +from ament_index_python.packages import get_package_share_directory +from launch import LaunchDescription +from launch.actions import IncludeLaunchDescription +from launch.launch_description_sources import PythonLaunchDescriptionSource +from launch.substitutions import PathJoinSubstitution +from launch_ros.actions import Node +from launch_ros.substitutions import FindPackageShare + +import os + + +def generate_launch_description(): + ld = LaunchDescription() + + download_and_run_binary = Node( + package="unity_binary", + executable="download_and_run_binary", + name="download_and_run_binary", + output="log" + ) + ld.add_action(download_and_run_binary) + + # teleop_twist_keyboard = Node( + # package="teleop_twist_keyboard", + # executable="teleop_twist_keyboard", + # name="teleop_twist_keyboard", + # output="log" + # ) + # ld.add_action(teleop_twist_keyboard) + + rviz_config_file = PathJoinSubstitution( + [FindPackageShare("unity_launch"), "rviz", "unity.rviz"] + ) + rviz_node = Node( + package="rviz2", + executable="rviz2", + name="rviz2", + output="log", + arguments=["-d", rviz_config_file], + ) + ld.add_action(rviz_node) + + json_tf_bridge_node = Node( + package="json_tf_bridge", + executable="json_tf_bridge_node", + name="json_tf_bridge_node", + output="log" + ) + ld.add_action(json_tf_bridge_node) + + path = get_package_share_directory('pioneer_description') + ld.add_action(IncludeLaunchDescription( + PythonLaunchDescriptionSource(os.path.join(path, 'launch', 'robot_state_publisher.launch.py')))) + + path = get_package_share_directory('ros_tcp_endpoint') + ld.add_action(IncludeLaunchDescription( + PythonLaunchDescriptionSource(os.path.join(path, 'launch', 'endpoint.launch.py')))) + + return ld diff --git a/unity_launch/launch/launch_local.launch.py b/unity_launch/launch/launch_local.launch.py new file mode 100644 index 0000000..e63853e --- /dev/null +++ b/unity_launch/launch/launch_local.launch.py @@ -0,0 +1,66 @@ +from ament_index_python.packages import get_package_share_directory +from launch import LaunchDescription +from launch.actions import IncludeLaunchDescription +from launch.launch_description_sources import PythonLaunchDescriptionSource +from launch.substitutions import PathJoinSubstitution +from launch_ros.actions import Node +from launch_ros.substitutions import FindPackageShare + +import os + + +def generate_launch_description(): + ld = LaunchDescription() + + # download_and_run_binary = Node( + # package="unity_binary", + # executable="download_and_run_binary", + # name="download_and_run_binary", + # output="log" + # ) + # ld.add_action(download_and_run_binary) + + # teleop_twist_keyboard = Node( + # package="teleop_twist_keyboard", + # executable="teleop_twist_keyboard", + # name="teleop_twist_keyboard", + # output="log" + # ) + # ld.add_action(teleop_twist_keyboard) + + rviz_config_file = PathJoinSubstitution( + [FindPackageShare("unity_launch"), "rviz", "unity.rviz"] + ) + rviz_node = Node( + package="rviz2", + executable="rviz2", + name="rviz2", + output="log", + arguments=["-d", rviz_config_file],) + + ld.add_action(rviz_node) + + json_tf_bridge_node = Node( + package="json_tf_bridge", + executable="json_tf_bridge_node", + name="json_tf_bridge_node", + output="log" + ) + ld.add_action(json_tf_bridge_node) + + # yolo_cmd = IncludeLaunchDescription( + # PythonLaunchDescriptionSource(PathJoinSubstitution([ + # get_package_share_directory('yolostate'), 'launch', 'detecthuman.launch.py'])) + # ) + + # ld.add_action(yolo_cmd) + + path = get_package_share_directory('pioneer_description') + ld.add_action(IncludeLaunchDescription( + PythonLaunchDescriptionSource(os.path.join(path, 'launch', 'robot_state_publisher.launch.py')))) + + path = get_package_share_directory('ros_tcp_endpoint') + ld.add_action(IncludeLaunchDescription( + PythonLaunchDescriptionSource(os.path.join(path, 'launch', 'endpoint.launch.py')))) + + return ld diff --git a/unity_launch/package.xml b/unity_launch/package.xml new file mode 100644 index 0000000..491b21c --- /dev/null +++ b/unity_launch/package.xml @@ -0,0 +1,18 @@ + + + + unity_launch + 0.0.0 + TODO: Package description + pac48 + TODO: License declaration + + ament_cmake + + ament_lint_auto + ament_lint_common + + + ament_cmake + + diff --git a/unity_launch/rviz/unity.rviz b/unity_launch/rviz/unity.rviz new file mode 100644 index 0000000..3e92b30 --- /dev/null +++ b/unity_launch/rviz/unity.rviz @@ -0,0 +1,357 @@ +Panels: + - Class: rviz_common/Displays + Help Height: 0 + Name: Displays + Property Tree Widget: + Expanded: + - /Global Options1 + - /Status1 + - /Image1 + - /Image2 + - /LaserScan1 + - /RobotModel1 + - /TF1 + - /TF1/Frames1 + Splitter Ratio: 0.5 + Tree Height: 325 + - Class: rviz_common/Selection + Name: Selection + - Class: rviz_common/Tool Properties + Expanded: + - /2D Goal Pose1 + - /Publish Point1 + Name: Tool Properties + Splitter Ratio: 0.5886790156364441 + - Class: rviz_common/Views + Expanded: + - /Current View1 + Name: Views + Splitter Ratio: 0.5 + - Class: rviz_common/Time + Experimental: false + Name: Time + SyncMode: 0 + SyncSource: LaserScan +Visualization Manager: + Class: "" + Displays: + - Alpha: 0.5 + Cell Size: 1 + Class: rviz_default_plugins/Grid + Color: 160; 160; 164 + Enabled: true + Line Style: + Line Width: 0.029999999329447746 + Value: Lines + Name: Grid + Normal Cell Count: 0 + Offset: + X: 0 + Y: 0 + Z: 0 + Plane: XY + Plane Cell Count: 10 + Reference Frame: + Value: true + - Class: rviz_default_plugins/Image + Enabled: true + Max Value: 1 + Median window: 5 + Min Value: 0 + Name: Image + Normalize Range: true + Topic: + Depth: 5 + Durability Policy: Volatile + History Policy: Keep Last + Reliability Policy: Reliable + Value: /smart_home/camera/depth/image_raw + Value: true + - Class: rviz_default_plugins/Image + Enabled: true + Max Value: 1 + Median window: 5 + Min Value: 0 + Name: Image + Normalize Range: true + Topic: + Depth: 5 + Durability Policy: Volatile + History Policy: Keep Last + Reliability Policy: Reliable + Value: /smart_home/camera/color/image_raw + Value: true + - Alpha: 1 + Autocompute Intensity Bounds: true + Autocompute Value Bounds: + Max Value: 10 + Min Value: -10 + Value: true + Axis: Z + Channel Name: intensity + Class: rviz_default_plugins/LaserScan + Color: 255; 255; 255 + Color Transformer: Intensity + Decay Time: 0 + Enabled: true + Invert Rainbow: false + Max Color: 255; 255; 255 + Max Intensity: 3.5961287021636963 + Min Color: 0; 0; 0 + Min Intensity: 0.04797770455479622 + Name: LaserScan + Position Transformer: XYZ + Selectable: true + Size (Pixels): 3 + Size (m): 0.05000000074505806 + Style: Flat Squares + Topic: + Depth: 5 + Durability Policy: Volatile + Filter size: 10 + History Policy: Keep Last + Reliability Policy: Reliable + Value: /scan + Use Fixed Frame: true + Use rainbow: true + Value: true + - Alpha: 1 + Class: rviz_default_plugins/RobotModel + Collision Enabled: false + Description File: "" + Description Source: Topic + Description Topic: + Depth: 5 + Durability Policy: Volatile + History Policy: Keep Last + Reliability Policy: Reliable + Value: /robot_description + Enabled: true + Links: + All Links Enabled: true + Expand Joint Details: false + Expand Link Details: false + Expand Tree: false + Link Tree Style: Links in Alphabetic Order + back_sonar: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + base_link: + Alpha: 1 + Show Axes: false + Show Trail: false + camera_link: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + center_hubcap: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + center_wheel: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + chassis: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + front_sonar: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + laser: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + left_hub: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + left_wheel: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + right_hub: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + right_wheel: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + swivel: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + top_plate: + Alpha: 1 + Show Axes: false + Show Trail: false + Value: true + Mass Properties: + Inertia: false + Mass: false + Name: RobotModel + TF Prefix: "" + Update Interval: 0 + Value: true + Visual Enabled: true + - Class: rviz_default_plugins/TF + Enabled: true + Frame Timeout: 15 + Frames: + All Enabled: false + back_sonar: + Value: false + base_link: + Value: true + camera_link: + Value: false + center_hubcap: + Value: false + center_wheel: + Value: false + chassis: + Value: false + front_sonar: + Value: false + laser: + Value: false + left_hub: + Value: false + left_wheel: + Value: false + odom: + Value: true + right_hub: + Value: false + right_wheel: + Value: false + scan_link: + Value: false + swivel: + Value: false + top_plate: + Value: false + Marker Scale: 1 + Name: TF + Show Arrows: true + Show Axes: true + Show Names: false + Tree: + odom: + base_link: + back_sonar: + {} + chassis: + {} + front_sonar: + {} + top_plate: + camera_link: + laser: + {} + scan_link: + {} + Update Interval: 0 + Value: true + Enabled: true + Global Options: + Background Color: 48; 48; 48 + Fixed Frame: odom + Frame Rate: 30 + Name: root + Tools: + - Class: rviz_default_plugins/Interact + Hide Inactive Objects: true + - Class: rviz_default_plugins/MoveCamera + - Class: rviz_default_plugins/Select + - Class: rviz_default_plugins/FocusCamera + - Class: rviz_default_plugins/Measure + Line color: 128; 128; 0 + - Class: rviz_default_plugins/SetInitialPose + Covariance x: 0.25 + Covariance y: 0.25 + Covariance yaw: 0.06853891909122467 + Topic: + Depth: 5 + Durability Policy: Volatile + History Policy: Keep Last + Reliability Policy: Reliable + Value: /initialpose + - Class: rviz_default_plugins/SetGoal + Topic: + Depth: 5 + Durability Policy: Volatile + History Policy: Keep Last + Reliability Policy: Reliable + Value: /goal_pose + - Class: rviz_default_plugins/PublishPoint + Single click: true + Topic: + Depth: 5 + Durability Policy: Volatile + History Policy: Keep Last + Reliability Policy: Reliable + Value: /clicked_point + Transformation: + Current: + Class: rviz_default_plugins/TF + Value: true + Views: + Current: + Class: rviz_default_plugins/Orbit + Distance: 31.150453567504883 + Enable Stereo Rendering: + Stereo Eye Separation: 0.05999999865889549 + Stereo Focal Distance: 1 + Swap Stereo Eyes: false + Value: false + Focal Point: + X: -5.593347072601318 + Y: -0.601751446723938 + Z: -1.1487257480621338 + Focal Shape Fixed Size: true + Focal Shape Size: 0.05000000074505806 + Invert Z Axis: false + Name: Current View + Near Clip Distance: 0.009999999776482582 + Pitch: 1.564796805381775 + Target Frame: + Value: Orbit (rviz) + Yaw: 3.133578062057495 + Saved: ~ +Window Geometry: + Displays: + collapsed: false + Height: 1376 + Hide Left Dock: false + Hide Right Dock: true + Image: + collapsed: false + QMainWindow State: 000000ff00000000fd000000040000000000000371000004c2fc020000000afb0000001200530065006c0065006300740069006f006e00000001e10000009b0000005c00fffffffb0000001e0054006f006f006c002000500072006f007000650072007400690065007302000001ed000001df00000185000000a3fb000000120056006900650077007300200054006f006f02000001df000002110000018500000122fb000000200054006f006f006c002000500072006f0070006500720074006900650073003203000002880000011d000002210000017afb000000100044006900730070006c006100790073010000003d00000182000000c900fffffffb0000002000730065006c0065006300740069006f006e00200062007500660066006500720200000138000000aa0000023a00000294fb00000014005700690064006500530074006500720065006f02000000e6000000d2000003ee0000030bfb0000000c004b0069006e0065006300740200000186000001060000030c00000261fb0000000a0049006d00610067006501000001c50000014e0000002800fffffffb0000000a0049006d0061006700650100000319000001e60000002800ffffff000000010000016a0000023dfc0200000003fb0000001e0054006f006f006c002000500072006f00700065007200740069006500730100000041000000780000000000000000fb0000000a00560069006500770073000000003d0000023d000000a400fffffffb0000001200530065006c0065006300740069006f006e010000025a000000b200000000000000000000000200000490000000a9fc0100000001fb0000000a00560069006500770073030000004e00000080000002e10000019700000003000009b60000003efc0100000002fb0000000800540069006d00650100000000000009b6000002fb00fffffffb0000000800540069006d006501000000000000045000000000000000000000063f000004c200000004000000040000000800000008fc0000000100000002000000010000000a0054006f006f006c00730100000000ffffffff0000000000000000 + Selection: + collapsed: false + Time: + collapsed: false + Tool Properties: + collapsed: false + Views: + collapsed: true + Width: 2486 + X: 74 + Y: 27