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 15 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 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.
Expand Down Expand Up @@ -46,24 +46,22 @@ class Camera_Follower : public rclcpp_lifecycle::LifecycleNode
private:
cv::VideoCapture cap_;
bool object_is_detected_;
bool can_publish_cmdvel_;
bool enable_following_;
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);
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);
void detecting_line(const cv::Mat & input_frame, cv::Mat & result_frame);

rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn
on_configure(const rclcpp_lifecycle::State &);
Expand Down
2 changes: 1 addition & 1 deletion launch/camera_line_follower.launch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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.
Expand Down
65 changes: 30 additions & 35 deletions src/camera_line_follower_component.cpp
Copy link
Contributor

Choose a reason for hiding this comment

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

cmd_vel_ は他の関数から参照されていないため、メンバ変数にする必要がないです。
関数内で宣言してください

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 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.
Expand All @@ -24,14 +24,15 @@
#include <opencv2/opencv.hpp>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "geometry_msgs/msg/twist.hpp"
#include "lifecycle_msgs/srv/change_state.hpp"
#include "cv_bridge/cv_bridge.h"

using namespace std::chrono_literals;
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

namespace camera_line_follower
{
using namespace std::chrono_literals;
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

Camera_Follower::Camera_Follower(const rclcpp::NodeOptions & options)
: rclcpp_lifecycle::LifecycleNode("camera_follower", options),
Expand All @@ -41,64 +42,59 @@ Camera_Follower::Camera_Follower(const rclcpp::NodeOptions & options)

void Camera_Follower::image_callback(const sensor_msgs::msg::Image::SharedPtr msg_image)
{
auto cv_img = cv_bridge::toCvShare(msg_image, msg_image->encoding);
auto msg = std::make_unique<sensor_msgs::msg::Image>();
const auto cv_img = cv_bridge::toCvShare(msg_image, msg_image->encoding);
auto result_msg = std::make_unique<sensor_msgs::msg::Image>();
msg->is_bigendian = false;
result_msg->is_bigendian = false;

cv::Mat frame, result_frame;
cv::cvtColor(cv_img->image, frame, CV_RGB2BGR);

if (!frame.empty()) {
following(frame, result_frame);
detecting_line(frame, result_frame);
convert_frame_to_message(result_frame, *result_msg);
result_image_pub_->publish(std::move(result_msg));
}
}

void Camera_Follower::callback_switches(const raspimouse_msgs::msg::Switches::SharedPtr msg)
{
switches_ = *msg;
if (msg->switch0) {
RCLCPP_INFO(this->get_logger(), "Stop following.");
enable_following_ = false;
} else if (msg->switch2) {
RCLCPP_INFO(this->get_logger(), "Start following.");
enable_following_ = true;
}
}

void Camera_Follower::on_cmd_vel_timer()
{
const double LINEAR_VEL = 0.2; // unit: m/s
const double ANGULAR_VEL = -0.8; // unit: rad/s
const double TARGET_AREA = 0.1; // 0.0 ~ 1.0
const double OBJECT_AREA_THRESHOLD = 0.01; // 0.0 ~ 1.0
constexpr double LINEAR_VEL = 0.2; // unit: m/s
constexpr double ANGULAR_VEL = -0.8; // unit: rad/s
constexpr double TARGET_AREA = 0.1; // 0.0 ~ 1.0
constexpr double OBJECT_AREA_THRESHOLD = 0.01; // 0.0 ~ 1.0
geometry_msgs::msg::Twist cmd_vel;

// Detects an object and tracks it
// Detect and follow the line
// when the number of pixels of the object is greater than the threshold.
Copy link
Contributor

Choose a reason for hiding this comment

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

ここではDetectしてないので訂正してほしいです。(画像処理もここで行われている と勘違いします)

Suggested change
// Detect and follow the line
// when the number of pixels of the object is greater than the threshold.
// Follow the line
// when the number of pixels of the object is greater than the threshold.

if (object_is_detected_ && object_normalized_area_ > OBJECT_AREA_THRESHOLD) {
cmd_vel_.linear.x = LINEAR_VEL * (object_normalized_area_ - TARGET_AREA);
cmd_vel_.angular.z = ANGULAR_VEL * object_normalized_point_.x;
cmd_vel.linear.x = LINEAR_VEL * (object_normalized_area_ - TARGET_AREA);
cmd_vel.angular.z = ANGULAR_VEL * object_normalized_point_.x;
} else {
cmd_vel_.linear.x = 0.0;
cmd_vel_.angular.z = 0.0;
}

if (switches_.switch0) {
RCLCPP_INFO(this->get_logger(), "Stop following.");
can_publish_cmdvel_ = false;
} else if (switches_.switch2) {
RCLCPP_INFO(this->get_logger(), "Start following.");
can_publish_cmdvel_ = true;
cmd_vel.linear.x = 0.0;
cmd_vel.angular.z = 0.0;
}
switches_ = raspimouse_msgs::msg::Switches(); // Reset switch values

if (!can_publish_cmdvel_) {
cmd_vel_.linear.x = 0.0;
cmd_vel_.angular.z = 0.0;
if (!enable_following_) {
cmd_vel.linear.x = 0.0;
cmd_vel.angular.z = 0.0;
}

auto msg = std::make_unique<geometry_msgs::msg::Twist>(cmd_vel_);
auto msg = std::make_unique<geometry_msgs::msg::Twist>(cmd_vel);
cmd_vel_pub_->publish(std::move(msg));
}

// Ref: https://github.com/ros2/demos/blob/dashing/image_tools/src/cam2image.cpp
std::string Camera_Follower::mat_type2encoding(int mat_type)
std::string Camera_Follower::mat_type2encoding(const int mat_type) const
{
switch (mat_type) {
case CV_8UC1:
Expand Down Expand Up @@ -129,7 +125,7 @@ void Camera_Follower::convert_frame_to_message(
msg.header.frame_id = "camera_frame";
}

void Camera_Follower::following(const cv::Mat & input_frame, cv::Mat & result_frame)
void Camera_Follower::detecting_line(const cv::Mat & input_frame, cv::Mat & result_frame)
{
// Specific colors are extracted from the input image and converted to binary values.
cv::Mat gray;
Expand Down Expand Up @@ -179,7 +175,7 @@ void Camera_Follower::following(const cv::Mat & input_frame, cv::Mat & result_fr
cv::circle(result_frame, mt_point, 30, cv::Scalar(0, 0, 255), 2, cv::LINE_4);
cv::putText(
result_frame, text, cv::Point(0, 30),
cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 0, 0), 2);
cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255), 2);
} else {
object_is_detected_ = false;
}
Expand Down Expand Up @@ -234,7 +230,6 @@ CallbackReturn Camera_Follower::on_deactivate(const rclcpp_lifecycle::State &)
cmd_vel_timer_->cancel();

object_is_detected_ = false;
cmd_vel_ = geometry_msgs::msg::Twist();

return CallbackReturn::SUCCESS;
}
Expand Down