

add teleop_twist_joy
@c76e70af9487541dbefd3f53b73c40137da611e1
--- .vscode/c_cpp_properties.json
+++ .vscode/c_cpp_properties.json
... | ... | @@ -1,23 +1,24 @@ |
1 | 1 |
{ |
2 |
- "configurations": [ |
|
3 |
- { |
|
4 |
- "browse": { |
|
5 |
- "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db", |
|
6 |
- "limitSymbolsToIncludedHeaders": false |
|
7 |
- }, |
|
8 |
- "includePath": [ |
|
9 |
- "/opt/ros/galactic/include/**", |
|
10 |
- "/usr/include/**", |
|
11 |
- "{workspaceFolder}/p9n_interface/include/**", |
|
12 |
- "{workspaceFolder}/p9n_example/include/**", |
|
13 |
- "{workspaceFolder}/p9n_test/include/**" |
|
14 |
- ], |
|
15 |
- "name": "ROS", |
|
16 |
- "intelliSenseMode": "clang-x64", |
|
17 |
- "compilerPath": "/usr/bin/gcc", |
|
18 |
- "cStandard": "c99", |
|
19 |
- "cppStandard": "c++17" |
|
20 |
- } |
|
21 |
- ], |
|
22 |
- "version": 4 |
|
23 |
-} |
|
2 |
+ "configurations": [ |
|
3 |
+ { |
|
4 |
+ "browse": { |
|
5 |
+ "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db", |
|
6 |
+ "limitSymbolsToIncludedHeaders": false |
|
7 |
+ }, |
|
8 |
+ "includePath": [ |
|
9 |
+ "/opt/ros/galactic/include/**", |
|
10 |
+ "/usr/include/**", |
|
11 |
+ "{workspaceFolder}/p9n_interface/include/**", |
|
12 |
+ "{workspaceFolder}/p9n_example/include/**", |
|
13 |
+ "{workspaceFolder}/p9n_test/include/**", |
|
14 |
+ "${workspaceFolder}/p9n_node/include/**" |
|
15 |
+ ], |
|
16 |
+ "name": "ROS", |
|
17 |
+ "intelliSenseMode": "clang-x64", |
|
18 |
+ "compilerPath": "/usr/bin/gcc", |
|
19 |
+ "cStandard": "c99", |
|
20 |
+ "cppStandard": "c++17" |
|
21 |
+ } |
|
22 |
+ ], |
|
23 |
+ "version": 4 |
|
24 |
+}(파일 끝에 줄바꿈 문자 없음) |
+++ p9n_node/CMakeLists.txt
... | ... | @@ -0,0 +1,30 @@ |
1 | +cmake_minimum_required(VERSION 3.8) | |
2 | +project(p9n_node) | |
3 | + | |
4 | +if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") | |
5 | + add_compile_options(-Wall -Wextra -Wpedantic) | |
6 | +endif() | |
7 | + | |
8 | +find_package(ament_cmake_auto REQUIRED) | |
9 | +ament_auto_find_build_dependencies() | |
10 | + | |
11 | +# Display Node ====================================================== | |
12 | +set(TARGET teleop_twist_joy_node) | |
13 | +ament_auto_add_library( | |
14 | + ${TARGET} | |
15 | + SHARED | |
16 | + src/${TARGET}.cpp) | |
17 | +rclcpp_components_register_node( | |
18 | + ${TARGET} | |
19 | + PLUGIN "p9n_node::TeleopTwistJoyNode" | |
20 | + EXECUTABLE ${TARGET}_exec) | |
21 | +# End Display Node ================================================== | |
22 | + | |
23 | +if(BUILD_TESTING) | |
24 | + find_package(ament_lint_auto REQUIRED) | |
25 | + set(ament_cmake_copyright_FOUND TRUE) | |
26 | + set(ament_cmake_cpplint_FOUND TRUE) | |
27 | + ament_lint_auto_find_test_dependencies() | |
28 | +endif() | |
29 | + | |
30 | +ament_auto_package(INSTALL_TO_SHARE launch) |
+++ p9n_node/include/p9n_node/teleop_twist_joy_node.hpp
... | ... | @@ -0,0 +1,44 @@ |
1 | +// Copyright 2022 HarvestX Inc. | |
2 | +// | |
3 | +// Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | +// you may not use this file except in compliance with the License. | |
5 | +// You may obtain a copy of the License at | |
6 | +// | |
7 | +// http://www.apache.org/licenses/LICENSE-2.0 | |
8 | +// | |
9 | +// Unless required by applicable law or agreed to in writing, software | |
10 | +// distributed under the License is distributed on an "AS IS" BASIS, | |
11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | +// See the License for the specific language governing permissions and | |
13 | +// limitations under the License. | |
14 | + | |
15 | +#pragma once | |
16 | + | |
17 | +#include <string> | |
18 | +#include <memory> | |
19 | +#include <sensor_msgs/msg/joy.hpp> | |
20 | +#include <geometry_msgs/msg/twist.hpp> | |
21 | +#include <rclcpp/rclcpp.hpp> | |
22 | + | |
23 | +#include <p9n_interface/p9n_interface.hpp> | |
24 | + | |
25 | + | |
26 | +namespace p9n_node | |
27 | +{ | |
28 | +class TeleopTwistJoyNode : public rclcpp::Node | |
29 | +{ | |
30 | +private: | |
31 | + p9n_interface::HW_TYPE hw_type_; | |
32 | + std::unique_ptr<p9n_interface::PlayStationInterface> p9n_if_; | |
33 | + | |
34 | + rclcpp::Subscription<sensor_msgs::msg::Joy>::SharedPtr joy_sub_; | |
35 | + rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr twist_pub_; | |
36 | + | |
37 | +public: | |
38 | + TeleopTwistJoyNode(const rclcpp::NodeOptions & options); | |
39 | + void onJoy(sensor_msgs::msg::Joy::ConstSharedPtr joy_msg); | |
40 | +}; | |
41 | +} | |
42 | + | |
43 | +#include "rclcpp_components/register_node_macro.hpp" | |
44 | +RCLCPP_COMPONENTS_REGISTER_NODE(p9n_node::TeleopTwistJoyNode) |
+++ p9n_node/launch/teleop.launch.py
... | ... | @@ -0,0 +1,60 @@ |
1 | +# Copyright 2022 HarvestX Inc. | |
2 | +# | |
3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | +# you may not use this file except in compliance with the License. | |
5 | +# You may obtain a copy of the License at | |
6 | +# | |
7 | +# http://www.apache.org/licenses/LICENSE-2.0 | |
8 | +# | |
9 | +# Unless required by applicable law or agreed to in writing, software | |
10 | +# distributed under the License is distributed on an "AS IS" BASIS, | |
11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | +# See the License for the specific language governing permissions and | |
13 | +# limitations under the License. | |
14 | + | |
15 | +from launch import LaunchDescription | |
16 | +from launch.actions import DeclareLaunchArgument | |
17 | +from launch.substitutions import ( | |
18 | + LaunchConfiguration, | |
19 | + TextSubstitution, | |
20 | +) | |
21 | + | |
22 | +from launch_ros.actions import ComposableNodeContainer | |
23 | +from launch_ros.descriptions import ComposableNode | |
24 | + | |
25 | + | |
26 | +def generate_launch_description(): | |
27 | + """Generate launch description.""" | |
28 | + launch_args = [ | |
29 | + DeclareLaunchArgument( | |
30 | + 'hw_type', | |
31 | + default_value=TextSubstitution(text='DualSense') | |
32 | + ) | |
33 | + ] | |
34 | + nodes = [ | |
35 | + ComposableNodeContainer( | |
36 | + name='joy_container', | |
37 | + package='rclcpp_components', | |
38 | + executable='component_container', | |
39 | + namespace='', | |
40 | + composable_node_descriptions=[ | |
41 | + ComposableNode( | |
42 | + package='joy', | |
43 | + plugin='joy::Joy', | |
44 | + name='joy', | |
45 | + namespace='', | |
46 | + ), | |
47 | + ComposableNode( | |
48 | + package='p9n_node', | |
49 | + plugin='p9n_node::TeleopTwistJoyNode', | |
50 | + name='teleop_twist_joy_node', | |
51 | + namespace='', | |
52 | + parameters=[{ | |
53 | + 'hw_type': LaunchConfiguration('hw_type') | |
54 | + }], | |
55 | + ) | |
56 | + ], | |
57 | + ), | |
58 | + ] | |
59 | + | |
60 | + return LaunchDescription(launch_args + nodes) |
+++ p9n_node/package.xml
... | ... | @@ -0,0 +1,23 @@ |
1 | +<?xml version="1.0"?> | |
2 | +<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> | |
3 | +<package format="3"> | |
4 | + <name>p9n_node</name> | |
5 | + <version>0.0.0</version> | |
6 | + <description>PlayStation JoyInterface example package.</description> | |
7 | + <maintainer email="m12watanabe1a@gmail.com">m12watanabe1a</maintainer> | |
8 | + <license>Apache License 2.0</license> | |
9 | + | |
10 | + <buildtool_depend>ament_cmake_auto</buildtool_depend> | |
11 | + | |
12 | + <depend>rclcpp_components</depend> | |
13 | + <depend>p9n_interface</depend> | |
14 | + <depend>geometry_msgs</depend> | |
15 | + <exec_depend>joy</exec_depend> | |
16 | + | |
17 | + <test_depend>ament_lint_auto</test_depend> | |
18 | + <test_depend>ament_lint_common</test_depend> | |
19 | + | |
20 | + <export> | |
21 | + <build_type>ament_cmake</build_type> | |
22 | + </export> | |
23 | +</package>(파일 끝에 줄바꿈 문자 없음) |
+++ p9n_node/src/teleop_twist_joy_node.cpp
... | ... | @@ -0,0 +1,79 @@ |
1 | +// Copyright 2022 HarvestX Inc. | |
2 | +// | |
3 | +// Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | +// you may not use this file except in compliance with the License. | |
5 | +// You may obtain a copy of the License at | |
6 | +// | |
7 | +// http://www.apache.org/licenses/LICENSE-2.0 | |
8 | +// | |
9 | +// Unless required by applicable law or agreed to in writing, software | |
10 | +// distributed under the License is distributed on an "AS IS" BASIS, | |
11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | +// See the License for the specific language governing permissions and | |
13 | +// limitations under the License. | |
14 | + | |
15 | + | |
16 | +#include "p9n_node/teleop_twist_joy_node.hpp" | |
17 | + | |
18 | +namespace p9n_node | |
19 | +{ | |
20 | + | |
21 | +TeleopTwistJoyNode::TeleopTwistJoyNode(const rclcpp::NodeOptions & options) | |
22 | +: rclcpp::Node("teleop_twist_joy_node", options) | |
23 | +{ | |
24 | + | |
25 | + const std::string hw_name = this->declare_parameter<std::string>( | |
26 | + "hw_type", p9n_interface::HW_NAME::DUALSENSE); | |
27 | + | |
28 | + try { | |
29 | + this->hw_type_ = p9n_interface::getHwType(hw_name); | |
30 | + } catch (std::runtime_error & e) { | |
31 | + RCLCPP_ERROR( | |
32 | + this->get_logger(), | |
33 | + e.what()); | |
34 | + RCLCPP_ERROR( | |
35 | + this->get_logger(), | |
36 | + "Please select hardware from %s", | |
37 | + p9n_interface::getAllHwName().c_str()); | |
38 | + rclcpp::shutdown(); | |
39 | + return; | |
40 | + } | |
41 | + | |
42 | + this->p9n_if_ = | |
43 | + std::make_unique<p9n_interface::PlayStationInterface>(this->hw_type_); | |
44 | + | |
45 | + this->joy_sub_ = this->create_subscription<sensor_msgs::msg::Joy>( | |
46 | + "joy", rclcpp::SensorDataQoS(rclcpp::KeepLast(1)), | |
47 | + std::bind(&TeleopTwistJoyNode::onJoy, this, std::placeholders::_1)); | |
48 | + | |
49 | + this->twist_pub_ = this->create_publisher<geometry_msgs::msg::Twist>( | |
50 | + "cmd_vel", rclcpp::QoS(10)); | |
51 | + | |
52 | + | |
53 | + if (this->joy_sub_->get_publisher_count() == 0) { | |
54 | + RCLCPP_WARN( | |
55 | + this->get_logger(), | |
56 | + "Joy node not launched"); | |
57 | + } | |
58 | +} | |
59 | + | |
60 | +void TeleopTwistJoyNode::onJoy(sensor_msgs::msg::Joy::ConstSharedPtr joy_msg) | |
61 | +{ | |
62 | + this->p9n_if_->setJoyMsg(joy_msg); | |
63 | + if ( | |
64 | + std::abs(this->p9n_if_->tiltedStickLX()) > 1e-2 || | |
65 | + std::abs(this->p9n_if_->tiltedStickLY()) > 1e-2) | |
66 | + { | |
67 | + float l_x = this->p9n_if_->tiltedStickLX(); | |
68 | + float l_y = this->p9n_if_->tiltedStickLY(); | |
69 | + | |
70 | + auto twist = geometry_msgs::msg::Twist(); | |
71 | + twist.linear.x = l_y; | |
72 | + twist.angular.z = l_x; | |
73 | + this->twist_pub_->publish(twist); | |
74 | + } else { | |
75 | + using namespace std::chrono_literals; | |
76 | + rclcpp::sleep_for(100ms); | |
77 | + } | |
78 | +} | |
79 | +} // namespace p9n_node |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?