Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

RGBカメラによるライントレースの実装 #47

Merged
merged 24 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
004eac5
カメラによるライントレース用のファイルを作成
YusukeKato Nov 21, 2023
31c1e2a
実行可能な状態に更新
YusukeKato Nov 22, 2023
d524acc
ライントレースが実行可能な状態に更新
YusukeKato Nov 22, 2023
a607ff5
スイッチ0を押してcmd_velの配信をオンオフできるように変更
YusukeKato Nov 22, 2023
b72a1c9
スイッチの処理を修正
YusukeKato Nov 22, 2023
a9ad829
colcon testが通るように修正
YusukeKato Nov 22, 2023
125abf2
Copyrightの文面を修正
YusukeKato Dec 1, 2023
1dad0d4
Copyrightの文面を修正
YusukeKato Dec 1, 2023
1fb0729
Copyrightの文面を修正
YusukeKato Dec 1, 2023
52f33a5
使用されていないmsgを削除
YusukeKato Dec 5, 2023
02ed75e
usingをnamespace内で使用するように変更
YusukeKato Dec 5, 2023
d8099b5
変数にconstを付ける
YusukeKato Dec 5, 2023
349b400
is_bigendianの行を削除
YusukeKato Dec 5, 2023
4969c4b
内部変数を変更しない関数にconstを付ける
YusukeKato Dec 5, 2023
442878b
ソースコードのほうでもconstを付ける
YusukeKato Dec 5, 2023
d8891c0
cmd_velを関数内で宣言する
YusukeKato Dec 5, 2023
445f6db
constをconstexprに変更
YusukeKato Dec 5, 2023
e4d4f16
コメントを修正
YusukeKato Dec 5, 2023
48c00f5
スイッチの処理を修正
YusukeKato Dec 5, 2023
a1a8154
following()をdetecting_lineに変更
YusukeKato Dec 5, 2023
b1b3ceb
検出結果の文字の色を青から白に変更
YusukeKato Dec 5, 2023
8f559e8
コメントを修正
YusukeKato Dec 6, 2023
f6067af
返り値をbool型に変更
YusukeKato Dec 6, 2023
5a17a7d
関数にconstを付ける
YusukeKato Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ ament_target_dependencies(object_tracking_component
cv_bridge)
rclcpp_components_register_nodes(object_tracking_component "object_tracking::Tracker")

add_library(camera_line_follower_component SHARED
src/camera_line_follower_component.cpp)
target_compile_definitions(camera_line_follower_component
PRIVATE "RASPIMOUSE_ROS2_EXAMPLES_BUILDING_DLL")
ament_target_dependencies(camera_line_follower_component
rclcpp
rclcpp_components
rclcpp_lifecycle
std_msgs
std_srvs
sensor_msgs
geometry_msgs
OpenCV
cv_bridge
raspimouse_msgs)
rclcpp_components_register_nodes(camera_line_follower_component "camera_line_follower::Camera_Follower")

add_library(line_follower_component SHARED
src/line_follower_component.cpp)
target_compile_definitions(line_follower_component
Expand Down Expand Up @@ -112,6 +129,7 @@ ament_export_dependencies(OpenCV)
ament_export_include_directories(include)
ament_export_libraries(
object_tracking_component
camera_line_follower_component
line_follower_component
direction_controller_component)

Expand All @@ -122,6 +140,7 @@ install(

install(TARGETS
object_tracking_component
camera_line_follower_component
line_follower_component
direction_controller_component
EXPORT export_${PROJECT_NAME}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2020 RT Corporation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright 2020 RT Corporation
// Copyright 2023 RT Corporation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ソースファイルも同様に対応してください。

//
// 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.

#ifndef RASPIMOUSE_ROS2_EXAMPLES__CAMERA_LINE_FOLLOWER_COMPONENT_HPP_
#define RASPIMOUSE_ROS2_EXAMPLES__CAMERA_LINE_FOLLOWER_COMPONENT_HPP_

#include <memory>
#include <string>

#include "raspimouse_msgs/msg/switches.hpp"
#include "raspimouse_ros2_examples/visibility_control.h"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/lifecycle_node.hpp"
#include "rclcpp_lifecycle/lifecycle_publisher.hpp"
#include "std_msgs/msg/string.hpp"
#include "std_srvs/srv/set_bool.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "geometry_msgs/msg/twist.hpp"
#include "opencv2/highgui/highgui.hpp"

namespace camera_line_follower
{

class Camera_Follower : public rclcpp_lifecycle::LifecycleNode
{
public:
RASPIMOUSE_ROS2_EXAMPLES_PUBLIC
explicit Camera_Follower(const rclcpp::NodeOptions & options);

protected:
void image_callback(const sensor_msgs::msg::Image::SharedPtr msg_image);
void callback_switches(const raspimouse_msgs::msg::Switches::SharedPtr msg);
void on_cmd_vel_timer();

private:
cv::VideoCapture cap_;
bool object_is_detected_;
bool can_publish_cmdvel_;
cv::Point2d object_normalized_point_;
double object_normalized_area_;
geometry_msgs::msg::Twist cmd_vel_;
raspimouse_msgs::msg::Switches switches_;
std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<sensor_msgs::msg::Image>> result_image_pub_;
std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<geometry_msgs::msg::Twist>> cmd_vel_pub_;
std::shared_ptr<rclcpp::Client<std_srvs::srv::SetBool>> motor_power_client_;
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr image_sub_;
rclcpp::Subscription<raspimouse_msgs::msg::Switches>::SharedPtr switches_sub_;
rclcpp::TimerBase::SharedPtr cmd_vel_timer_;

std::string mat_type2encoding(int mat_type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

内部変数を変えない場合はconstを付けてください。
また、引数にもconstを付けてください。

Suggested change
std::string mat_type2encoding(int mat_type);
std::string mat_type2encoding(const int mat_type) const;

void convert_frame_to_message(
const cv::Mat & frame,
sensor_msgs::msg::Image & msg);

void following(const cv::Mat & input_frame, cv::Mat & result_frame);

rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
on_configure(const rclcpp_lifecycle::State &);
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
on_activate(const rclcpp_lifecycle::State &);
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
on_deactivate(const rclcpp_lifecycle::State &);
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
on_cleanup(const rclcpp_lifecycle::State &);
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
on_shutdown(const rclcpp_lifecycle::State &);
};

} // namespace camera_line_follower

#endif // RASPIMOUSE_ROS2_EXAMPLES__CAMERA_LINE_FOLLOWER_COMPONENT_HPP_
90 changes: 90 additions & 0 deletions launch/camera_line_follower.launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright 2020 RT Corporation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Copyright 2020 RT Corporation
# Copyright 2023 RT Corporation

#
# 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 launch
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch.conditions import IfCondition
from launch_ros.actions import ComposableNodeContainer
from launch_ros.actions import Node
from launch_ros.descriptions import ComposableNode


def generate_launch_description():
declare_mouse = DeclareLaunchArgument(
'mouse',
default_value="true",
description='Launch raspimouse node'
)
declare_use_camera_node = DeclareLaunchArgument(
'use_camera_node',
default_value='true',
description='Use camera node.'
)
declare_video_device = DeclareLaunchArgument(
'video_device',
default_value='/dev/video0',
description='Set video device.'
)

"""Generate launch description with multiple components."""
container = ComposableNodeContainer(
name='camera_line_follower_container',
namespace='',
package='rclcpp_components',
executable='component_container_mt',
composable_node_descriptions=[
ComposableNode(
package='raspimouse_ros2_examples',
plugin='camera_line_follower::Camera_Follower',
name='camera_follower',
extra_arguments=[{'use_intra_process_comms': True}]),
ComposableNode(
package='raspimouse',
plugin='raspimouse::Raspimouse',
name='raspimouse',
parameters=[{'use_light_sensors': False}],
extra_arguments=[{'use_intra_process_comms': True}],
condition=IfCondition(LaunchConfiguration('mouse'))),
ComposableNode(
package='usb_cam',
plugin='usb_cam::UsbCamNode',
name='usb_cam',
remappings=[('image_raw', 'camera/color/image_raw')],
parameters=[
{'video_device': LaunchConfiguration('video_device')},
{'frame_id': 'camera_color_optical_frame'},
{'pixel_format': 'yuyv2rgb'}
],
extra_arguments=[{'use_intra_process_comms': True}],
condition=IfCondition(LaunchConfiguration('use_camera_node'))),
],
output='screen',
)

manager = Node(
name='manager',
package='raspimouse_ros2_examples',
executable='lifecycle_node_manager',
output='screen',
parameters=[{'components': ['raspimouse', 'camera_follower']}]
)

return launch.LaunchDescription([
declare_mouse,
declare_use_camera_node,
declare_video_device,
container,
manager
])
Loading