feat: add all code
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import rclpy
|
||||
from rclpy.node import Node
|
||||
from tf2_ros import TransformListener, Buffer
|
||||
from tf_transformations import euler_from_quaternion
|
||||
|
||||
|
||||
class TFListener(Node):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__('tf2_listener')
|
||||
self.buffer = Buffer()
|
||||
self.listener = TransformListener(self.buffer, self)
|
||||
self.timer = self.create_timer(1, self.get_transform)
|
||||
|
||||
def get_transform(self):
|
||||
try:
|
||||
tf = self.buffer.lookup_transform(
|
||||
'map', 'base_footprint', rclpy.time.Time(seconds=0), rclpy.time.Duration(seconds=1))
|
||||
transform = tf.transform
|
||||
rotation_euler = euler_from_quaternion([
|
||||
transform.rotation.x,
|
||||
transform.rotation.y,
|
||||
transform.rotation.z,
|
||||
transform.rotation.w
|
||||
])
|
||||
self.get_logger().info(
|
||||
f'平移:{transform.translation},旋转四元数:{transform.rotation}:旋转欧拉角:{rotation_euler}')
|
||||
except Exception as e:
|
||||
self.get_logger().warn(f'不能够获取坐标变换,原因: {str(e)}')
|
||||
|
||||
|
||||
def main():
|
||||
rclpy.init()
|
||||
node = TFListener()
|
||||
rclpy.spin(node)
|
||||
rclpy.shutdown()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,21 @@
|
||||
from geometry_msgs.msg import PoseStamped
|
||||
from nav2_simple_commander.robot_navigator import BasicNavigator
|
||||
import rclpy
|
||||
|
||||
|
||||
def main():
|
||||
rclpy.init()
|
||||
navigator = BasicNavigator()
|
||||
initial_pose = PoseStamped()
|
||||
initial_pose.header.frame_id = 'map'
|
||||
initial_pose.header.stamp = navigator.get_clock().now().to_msg()
|
||||
initial_pose.pose.position.x = 0.0
|
||||
initial_pose.pose.position.y = 0.0
|
||||
initial_pose.pose.orientation.w = 1.0
|
||||
navigator.setInitialPose(initial_pose)
|
||||
navigator.waitUntilNav2Active()
|
||||
rclpy.spin(navigator)
|
||||
rclpy.shutdown()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,40 @@
|
||||
from geometry_msgs.msg import PoseStamped
|
||||
from nav2_simple_commander.robot_navigator import BasicNavigator, TaskResult
|
||||
import rclpy
|
||||
from rclpy.duration import Duration
|
||||
|
||||
|
||||
def main():
|
||||
rclpy.init()
|
||||
navigator = BasicNavigator()
|
||||
# 等待导航启动完成
|
||||
navigator.waitUntilNav2Active()
|
||||
# 设置目标点坐标
|
||||
goal_pose = PoseStamped()
|
||||
goal_pose.header.frame_id = 'map'
|
||||
goal_pose.header.stamp = navigator.get_clock().now().to_msg()
|
||||
goal_pose.pose.position.x = 1.0
|
||||
goal_pose.pose.position.y = 1.0
|
||||
goal_pose.pose.orientation.w = 1.0
|
||||
# 发送目标接收反馈结果
|
||||
navigator.goToPose(goal_pose)
|
||||
while not navigator.isTaskComplete():
|
||||
feedback = navigator.getFeedback()
|
||||
navigator.get_logger().info(
|
||||
f'预计: {Duration.from_msg(feedback.estimated_time_remaining).nanoseconds / 1e9} s 后到达')
|
||||
# 超时自动取消
|
||||
if Duration.from_msg(feedback.navigation_time) > Duration(seconds=600.0):
|
||||
navigator.cancelTask()
|
||||
# 最终结果判断
|
||||
result = navigator.getResult()
|
||||
if result == TaskResult.SUCCEEDED:
|
||||
navigator.get_logger().info('导航结果:成功')
|
||||
elif result == TaskResult.CANCELED:
|
||||
navigator.get_logger().warn('导航结果:被取消')
|
||||
elif result == TaskResult.FAILED:
|
||||
navigator.get_logger().error('导航结果:失败')
|
||||
else:
|
||||
navigator.get_logger().error('导航结果:返回状态无效')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,52 @@
|
||||
from geometry_msgs.msg import PoseStamped
|
||||
from nav2_simple_commander.robot_navigator import BasicNavigator, TaskResult
|
||||
import rclpy
|
||||
from rclpy.duration import Duration
|
||||
|
||||
def main():
|
||||
rclpy.init()
|
||||
navigator = BasicNavigator()
|
||||
navigator.waitUntilNav2Active()
|
||||
# 创建点集
|
||||
goal_poses = []
|
||||
goal_pose1 = PoseStamped()
|
||||
goal_pose1.header.frame_id = 'map'
|
||||
goal_pose1.header.stamp = navigator.get_clock().now().to_msg()
|
||||
goal_pose1.pose.position.x = 0.0
|
||||
goal_pose1.pose.position.y = 0.0
|
||||
goal_pose1.pose.orientation.w = 1.0
|
||||
goal_poses.append(goal_pose1)
|
||||
goal_pose2 = PoseStamped()
|
||||
goal_pose2.header.frame_id = 'map'
|
||||
goal_pose2.header.stamp = navigator.get_clock().now().to_msg()
|
||||
goal_pose2.pose.position.x = 2.0
|
||||
goal_pose2.pose.position.y = 0.0
|
||||
goal_pose2.pose.orientation.w = 1.0
|
||||
goal_poses.append(goal_pose2)
|
||||
goal_pose3 = PoseStamped()
|
||||
goal_pose3.header.frame_id = 'map'
|
||||
goal_pose3.header.stamp = navigator.get_clock().now().to_msg()
|
||||
goal_pose3.pose.position.x = 2.0
|
||||
goal_pose3.pose.position.y = 2.0
|
||||
goal_pose3.pose.orientation.w = 1.0
|
||||
goal_poses.append(goal_pose3)
|
||||
# 调用路点导航服务
|
||||
navigator.followWaypoints(goal_poses)
|
||||
# 判断结束及获取反馈
|
||||
while not navigator.isTaskComplete():
|
||||
feedback = navigator.getFeedback()
|
||||
navigator.get_logger().info(
|
||||
f'当前目标编号:{feedback.current_waypoint}')
|
||||
# 最终结果判断
|
||||
result = navigator.getResult()
|
||||
if result == TaskResult.SUCCEEDED:
|
||||
navigator.get_logger().info('导航结果:成功')
|
||||
elif result == TaskResult.CANCELED:
|
||||
navigator.get_logger().warn('导航结果:被取消')
|
||||
elif result == TaskResult.FAILED:
|
||||
navigator.get_logger().error('导航结果:失败')
|
||||
else:
|
||||
navigator.get_logger().error('导航结果:返回状态无效')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
18
chapt8/chapt8_ws/src/fishbot_application/package.xml
Normal file
18
chapt8/chapt8_ws/src/fishbot_application/package.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="3">
|
||||
<name>fishbot_application</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="87068644+fishros@users.noreply.github.com">fishros</maintainer>
|
||||
<license>TODO: License declaration</license>
|
||||
|
||||
<test_depend>ament_copyright</test_depend>
|
||||
<test_depend>ament_flake8</test_depend>
|
||||
<test_depend>ament_pep257</test_depend>
|
||||
<test_depend>python3-pytest</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_python</build_type>
|
||||
</export>
|
||||
</package>
|
||||
4
chapt8/chapt8_ws/src/fishbot_application/setup.cfg
Normal file
4
chapt8/chapt8_ws/src/fishbot_application/setup.cfg
Normal file
@@ -0,0 +1,4 @@
|
||||
[develop]
|
||||
script_dir=$base/lib/fishbot_application
|
||||
[install]
|
||||
install_scripts=$base/lib/fishbot_application
|
||||
26
chapt8/chapt8_ws/src/fishbot_application/setup.py
Normal file
26
chapt8/chapt8_ws/src/fishbot_application/setup.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from setuptools import find_packages, setup
|
||||
|
||||
package_name = 'fishbot_application'
|
||||
|
||||
setup(
|
||||
name=package_name,
|
||||
version='0.0.0',
|
||||
packages=find_packages(exclude=['test']),
|
||||
data_files=[
|
||||
('share/ament_index/resource_index/packages',
|
||||
['resource/' + package_name]),
|
||||
('share/' + package_name, ['package.xml']),
|
||||
],
|
||||
install_requires=['setuptools'],
|
||||
zip_safe=True,
|
||||
maintainer='fishros',
|
||||
maintainer_email='87068644+fishros@users.noreply.github.com',
|
||||
description='TODO: Package description',
|
||||
license='TODO: License declaration',
|
||||
tests_require=['pytest'],
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'init_robot_pose=fishbot_application.init_robot_pose:main',
|
||||
],
|
||||
},
|
||||
)
|
||||
@@ -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'
|
||||
25
chapt8/chapt8_ws/src/fishbot_application/test/test_flake8.py
Normal file
25
chapt8/chapt8_ws/src/fishbot_application/test/test_flake8.py
Normal file
@@ -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)
|
||||
23
chapt8/chapt8_ws/src/fishbot_application/test/test_pep257.py
Normal file
23
chapt8/chapt8_ws/src/fishbot_application/test/test_pep257.py
Normal file
@@ -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'
|
||||
Reference in New Issue
Block a user