-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
004eac5
カメラによるライントレース用のファイルを作成
YusukeKato 31c1e2a
実行可能な状態に更新
YusukeKato d524acc
ライントレースが実行可能な状態に更新
YusukeKato a607ff5
スイッチ0を押してcmd_velの配信をオンオフできるように変更
YusukeKato b72a1c9
スイッチの処理を修正
YusukeKato a9ad829
colcon testが通るように修正
YusukeKato 125abf2
Copyrightの文面を修正
YusukeKato 1dad0d4
Copyrightの文面を修正
YusukeKato 1fb0729
Copyrightの文面を修正
YusukeKato 52f33a5
使用されていないmsgを削除
YusukeKato 02ed75e
usingをnamespace内で使用するように変更
YusukeKato d8099b5
変数にconstを付ける
YusukeKato 349b400
is_bigendianの行を削除
YusukeKato 4969c4b
内部変数を変更しない関数にconstを付ける
YusukeKato 442878b
ソースコードのほうでもconstを付ける
YusukeKato d8891c0
cmd_velを関数内で宣言する
YusukeKato 445f6db
constをconstexprに変更
YusukeKato e4d4f16
コメントを修正
YusukeKato 48c00f5
スイッチの処理を修正
YusukeKato a1a8154
following()をdetecting_lineに変更
YusukeKato b1b3ceb
検出結果の文字の色を青から白に変更
YusukeKato 8f559e8
コメントを修正
YusukeKato f6067af
返り値をbool型に変更
YusukeKato 5a17a7d
関数にconstを付ける
YusukeKato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||||||||||
|
@@ -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), | ||||||||||
|
@@ -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. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここではDetectしてないので訂正してほしいです。(画像処理もここで行われている と勘違いします)
Suggested change
|
||||||||||
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: | ||||||||||
|
@@ -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; | ||||||||||
|
@@ -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; | ||||||||||
} | ||||||||||
|
@@ -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; | ||||||||||
} | ||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd_vel_ は他の関数から参照されていないため、メンバ変数にする必要がないです。
関数内で宣言してください