-
Notifications
You must be signed in to change notification settings - Fork 9
カメラライントレースを修正 #49
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
カメラライントレースを修正 #49
Changes from 12 commits
f38c2e7
9bb88a0
e327463
48a1765
72d8965
7d4f392
a136048
7179643
cd26368
5787842
a507be8
376a3fb
e3d772d
5c2a276
5df6d4a
3d3198a
75634bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ $ source ~/ros2_ws/install/setup.bash | |
- [joystick_control](#joystick_control) | ||
- [object_tracking](#object_tracking) | ||
- [line_follower](#line_follower) | ||
- [camera_line_follower](#camera_line_follower) | ||
- [SLAM](#slam) | ||
- [direction_controller](#direction_controller) | ||
|
||
|
@@ -164,7 +165,7 @@ $ ros2 launch raspimouse_ros2_examples object_tracking.launch.py video_device:=/ | |
|
||
カメラ画像は`camera/color/image_raw`、物体検出画像は`result_image`というトピックとして発行されます。 | ||
これらの画像は[RViz](https://index.ros.org/r/rviz/) | ||
や[rqt_image_view](https://index.ros.org/doc/ros2/Tutorials/RQt-Overview-Usage/) | ||
や[rqt_image_view](https://index.ros.org/p/rqt_image_view/) | ||
で表示できます。 | ||
|
||
**画像を表示するとノードの動作が不安定になり、cmd_velや画像トピックが発行されないことがあります。** | ||
|
@@ -253,6 +254,59 @@ void Follower::publish_cmdvel_for_line_following(void) | |
|
||
--- | ||
|
||
### camera_line_follower | ||
|
||
<img src=https://rt-net.github.io/images/raspberry-pi-mouse/mouse_camera_line_trace_2.png width=500 /> | ||
|
||
RGBカメラによるライントレースのコード例です。 | ||
|
||
#### Requirements | ||
|
||
- Webカメラ | ||
- [Logicool HD WEBCAM C310N](https://www.logicool.co.jp/ja-jp/product/hd-webcam-c310n) | ||
- カメラマウント | ||
- [Raspberry Pi Mouse オプションキット No.4 \[Webカメラマウント\]](https://www.rt-shop.jp/index.php?main_page=product_info&cPath=1299_1395&products_id=3584) | ||
|
||
#### Installation | ||
|
||
Raspberry Pi Mouseにカメラマウントを取り付け、WebカメラをRaspberry Piに接続します。 | ||
|
||
#### How to use | ||
|
||
次のコマンドでノードを起動します。 | ||
|
||
```sh | ||
$ ros2 launch raspimouse_ros2_examples camera_line_follower.launch.py video_device:=/dev/video0 | ||
``` | ||
|
||
ShotaAk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ライン上にRaspberry Pi Mouseを置き、SW2を押してライントレースを開始します。 | ||
停止させる場合はSW0を押します。 | ||
|
||
カメラ画像は`camera/color/image_raw`、物体検出画像は`result_image`というトピックとして発行されます。 | ||
これらの画像は[RViz](https://index.ros.org/r/rviz/) | ||
や[rqt_image_view](https://index.ros.org/p/rqt_image_view/) | ||
で表示できます。 | ||
|
||
**画像を表示するとノードの動作が不安定になり、cmd_velや画像トピックが発行されないことがあります。** | ||
ShotaAk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<img src=https://rt-net.github.io/images/raspberry-pi-mouse/camera_line_trace.png width=500 /> | ||
|
||
#### Configure | ||
|
||
追跡対象の色を変更するには | ||
[`./src/camera_line_follower_component.cpp`](./src/camera_line_follower_component.cpp) | ||
を編集します。 | ||
|
||
物体検出精度が悪い時にはカメラの露光や関数内のパラメータを調整して下さい。 | ||
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. ROSパラメータが用意されたので、パラメータを変更して調整できることを説明してください。 raspimouse2 パッケージのREADMEのように、パラメータ一覧を記載するとわかりやすいです。 |
||
|
||
```cpp | ||
cv::inRange(gray, 0, 100, extracted_bin); | ||
``` | ||
ShotaAk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[back to example list](#how-to-use-examples) | ||
|
||
--- | ||
|
||
### SLAM | ||
|
||
<img src=https://rt-net.github.io/images/raspberry-pi-mouse/slam_toolbox_ros2.png width=500 /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ | |
#include "lifecycle_msgs/srv/change_state.hpp" | ||
#include "cv_bridge/cv_bridge.h" | ||
|
||
constexpr auto BRIGHTNESS_MIN_VAL_PARAM = "brightness_min_value"; | ||
constexpr auto BRIGHTNESS_MAX_VAL_PARAM = "brightness_max_value"; | ||
|
||
|
||
namespace camera_line_follower | ||
{ | ||
|
@@ -68,17 +71,16 @@ void Camera_Follower::callback_switches(const raspimouse_msgs::msg::Switches::Sh | |
|
||
void Camera_Follower::on_cmd_vel_timer() | ||
{ | ||
constexpr double LINEAR_VEL = 0.2; // unit: m/s | ||
constexpr double LINEAR_VEL = 0.05; // 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; | ||
|
||
// 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; | ||
cmd_vel.angular.z = ANGULAR_VEL * object_normalized_point_.x * 2.0; | ||
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. ここの2.0 がどういう意味なのかわかるように定数にしてください。 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. この2.0はデバッグ時に使用していた係数なので削除しました。 2.0を削除したことで回転方向の速度が変わりますが、ANGULAR_VELを調整することで対応できると思います。 constexpr double ANGULAR_VEL = -0.8; // unit: rad/s |
||
} else { | ||
cmd_vel.linear.x = 0.0; | ||
cmd_vel.angular.z = 0.0; | ||
|
@@ -131,7 +133,11 @@ bool Camera_Follower::detecting_line(const cv::Mat & input_frame, cv::Mat & resu | |
cv::Mat gray; | ||
cv::cvtColor(input_frame, gray, cv::COLOR_BGR2GRAY); | ||
cv::Mat extracted_bin; | ||
cv::inRange(gray, 0, 100, extracted_bin); | ||
cv::inRange( | ||
gray, | ||
get_parameter(BRIGHTNESS_MIN_VAL_PARAM).get_value<int>(), | ||
get_parameter(BRIGHTNESS_MAX_VAL_PARAM).get_value<int>(), | ||
extracted_bin); | ||
input_frame.copyTo(result_frame, extracted_bin); | ||
|
||
// Remove noise with morphology transformation | ||
|
@@ -141,7 +147,7 @@ bool Camera_Follower::detecting_line(const cv::Mat & input_frame, cv::Mat & resu | |
// Extracting contours | ||
std::vector<std::vector<cv::Point>> contours; | ||
std::vector<cv::Vec4i> hierarchy; | ||
cv::findContours(morph_bin, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE); | ||
cv::findContours(morph_bin, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); | ||
|
||
// Extracting the largest contours | ||
double max_area = 0; | ||
|
@@ -197,6 +203,10 @@ CallbackReturn Camera_Follower::on_configure(const rclcpp_lifecycle::State &) | |
switches_sub_ = create_subscription<raspimouse_msgs::msg::Switches>( | ||
"switches", 1, std::bind(&Camera_Follower::callback_switches, this, std::placeholders::_1)); | ||
|
||
// Set parameter defaults | ||
declare_parameter(BRIGHTNESS_MIN_VAL_PARAM, 0); | ||
declare_parameter(BRIGHTNESS_MAX_VAL_PARAM, 100); | ||
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. 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. READMEに記載している数値も変更お願いします。 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. 私の環境でもライントレースに失敗していました。 しきい値のデフォルト値を90に変更しました。 |
||
|
||
return CallbackReturn::SUCCESS; | ||
} | ||
|
||
|
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.
Parametersの説明が追加されたので、Configureの説明は削除してOKです
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.
日本語READMEも同様にお願いします。