Skip to content

Commit

Permalink
RGBカメラによるライントレースの実装 (#47)
Browse files Browse the repository at this point in the history
* カメラによるライントレース用のファイルを作成

* 実行可能な状態に更新

* ライントレースが実行可能な状態に更新

* スイッチ0を押してcmd_velの配信をオンオフできるように変更

* スイッチの処理を修正

* colcon testが通るように修正

* Copyrightの文面を修正

Co-authored-by: Shota Aoki <[email protected]>

* Copyrightの文面を修正

Co-authored-by: Shota Aoki <[email protected]>

* Copyrightの文面を修正

Co-authored-by: Shota Aoki <[email protected]>

* 使用されていないmsgを削除

Co-authored-by: Shota Aoki <[email protected]>

* usingをnamespace内で使用するように変更

* 変数にconstを付ける

* is_bigendianの行を削除

* 内部変数を変更しない関数にconstを付ける

Co-authored-by: Shota Aoki <[email protected]>

* ソースコードのほうでもconstを付ける

* cmd_velを関数内で宣言する

* constをconstexprに変更

Co-authored-by: Shota Aoki <[email protected]>

* コメントを修正

* スイッチの処理を修正

* following()をdetecting_lineに変更

* 検出結果の文字の色を青から白に変更

* コメントを修正

Co-authored-by: Shota Aoki <[email protected]>

* 返り値をbool型に変更

* 関数にconstを付ける

---------

Co-authored-by: Shota Aoki <[email protected]>
  • Loading branch information
YusukeKato and Shota Aoki authored Dec 6, 2023
1 parent f35d35a commit 153c25c
Show file tree
Hide file tree
Showing 4 changed files with 456 additions and 0 deletions.
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,80 @@
// 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.

#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 enable_following_;
cv::Point2d object_normalized_point_;
double object_normalized_area_;
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(const int mat_type) const;
void convert_frame_to_message(
const cv::Mat & frame,
sensor_msgs::msg::Image & msg) const;

bool detecting_line(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 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

0 comments on commit 153c25c

Please sign in to comment.