forked from RoboticaUtnFrba/create_autonomy
-
Notifications
You must be signed in to change notification settings - Fork 0
Attached the Color Sensor Plugin #1
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
Open
stevendes
wants to merge
1
commit into
try2
Choose a base branch
from
color_plugin
base: try2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #ifndef GAZEBO_ROS_COLOR_SENSOR_HH | ||
| #define GAZEBO_ROS_COLOR_SENSOR_HH | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
| // library for processing camera data for gazebo / ros conversions | ||
| #include <gazebo/plugins/CameraPlugin.hh> | ||
|
|
||
|
|
||
| #include <gazebo_plugins/gazebo_ros_camera_utils.h> | ||
|
|
||
|
|
||
| namespace gazebo | ||
| { | ||
| class GazeboRosColor : public CameraPlugin, GazeboRosCameraUtils | ||
stevendes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| public: | ||
| ///Constructor | ||
| GazeboRosColor(); | ||
| ///Destructor | ||
| ~GazeboRosColor(); | ||
| ///Load the plugin | ||
| void Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf); | ||
|
|
||
| // Put the RGB limits from the SDF to the rgbmax_ and rgbmin_ vectors | ||
| void LoadRGBLimits(sdf::ElementPtr _sdf); | ||
|
|
||
| // Check if the data received from the sensor is in range with the parameters | ||
| bool InRange(std::vector<int> RGB); | ||
|
|
||
| /// \brief Update the controller | ||
| protected: virtual void OnNewFrame(const unsigned char *_image, | ||
| unsigned int _width, unsigned int _height, | ||
| unsigned int _depth, const std::string &_format); | ||
|
|
||
| private: | ||
| // vector rgbmax_: used to store the maximun values of the RGB range | ||
| std::vector<int> rgbmax_ {0, 0, 0}; | ||
| // vector rgbmin_: used to store the minimun values of the RGB range | ||
| std::vector<int> rgbmin_ {0, 0, 0}; | ||
| //param publisher_name : Name of the publisher topic, received from SDF file | ||
| std::string publisher_name_; | ||
| //param publisher_name : Name of the publisher topic, received from SDF file | ||
| float color_percentage_; | ||
|
|
||
| /// Initialize ROS variables | ||
| ros::NodeHandle nh_; | ||
| ros::Publisher sensor_publisher_; | ||
|
|
||
| }; // GazeboRosColor | ||
| } // gazebo | ||
| #endif // GAZEBO_ROS_COLOR_SENSOR_HH | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| #include "ca_gazebo/color_sensor_plugin.h" | ||
|
|
||
| #include <gazebo/common/Plugin.hh> | ||
| #include <gazebo/sensors/Sensor.hh> | ||
| #include <gazebo/sensors/CameraSensor.hh> | ||
| #include <gazebo/sensors/SensorTypes.hh> | ||
| #include <ros/ros.h> | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "std_msgs/Bool.h" | ||
stevendes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include "std_msgs/Float64.h" | ||
| #include "gazebo_plugins/gazebo_ros_camera.h" | ||
|
|
||
| namespace gazebo | ||
| { | ||
| // Register this plugin with the simulator | ||
| GZ_REGISTER_SENSOR_PLUGIN(GazeboRosColor) | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| // Constructor | ||
| GazeboRosColor::GazeboRosColor(): | ||
| nh_("color_sensor_plugin") | ||
| { | ||
|
|
||
| } | ||
| //////////////////////////////////////////////////////////////////////////////// | ||
| // Destructor | ||
| GazeboRosColor::~GazeboRosColor() | ||
| { | ||
| ROS_DEBUG_STREAM_NAMED("camera","Unloaded"); | ||
| } | ||
|
|
||
| void GazeboRosColor::Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf) | ||
| { | ||
| // Make sure the ROS node for Gazebo has already been initialized | ||
| if (!ros::isInitialized()) | ||
| { | ||
| ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. " | ||
| << "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)"); | ||
| return; | ||
| } | ||
|
|
||
| CameraPlugin::Load(_parent, _sdf); | ||
| GazeboRosCameraUtils::Load(_parent, _sdf); | ||
| // copying from CameraPlugin into GazeboRosCameraUtils | ||
| this->parentSensor_ = this->parentSensor; | ||
|
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. Are all these parameters being used?
Owner
Author
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. Yes, removing one of them makes the plugin misbehave |
||
| this->width_ = this->width; | ||
| this->height_ = this->height; | ||
| this->depth_ = this->depth; | ||
| this->format_ = this->format; | ||
| this->camera_ = this->camera; | ||
| this->LoadRGBLimits(_sdf); | ||
| this->publisher_name_ = _sdf->Get<std::string>("PublisherName"); | ||
| this->sensor_publisher_ = nh_.advertise<std_msgs::Bool>(this->publisher_name_, 1); | ||
| this->color_percentage_=_sdf->Get<double>("ColorPercentage"); | ||
| this->parentSensor->SetActive(true); | ||
|
|
||
| } | ||
|
|
||
| void GazeboRosColor::LoadRGBLimits(sdf::ElementPtr _sdf) | ||
| { | ||
| this->rgbmax_[0] = _sdf->Get<double>("rvalue_max"); | ||
| this->rgbmax_[1] = _sdf->Get<double>("gvalue_max"); | ||
| this->rgbmax_[2] = _sdf->Get<double>("bvalue_max"); | ||
|
|
||
| this->rgbmin_[0] = _sdf->Get<double>("rvalue_min"); | ||
| this->rgbmin_[1] = _sdf->Get<double>("gvalue_min"); | ||
| this->rgbmin_[2] = _sdf->Get<double>("bvalue_min"); | ||
| } | ||
|
|
||
| bool GazeboRosColor::InRange(std::vector<int> RGB) | ||
| { | ||
| bool R_ok_,G_ok_, B_ok_; | ||
| R_ok_ = RGB[0] <= this->rgbmax_[0] and RGB[0] >= this->rgbmin_[0]; | ||
| G_ok_ = RGB[1] <= this-> rgbmax_[1] and RGB[1] >= this->rgbmin_[1]; | ||
| B_ok_ = RGB[2] <= this-> rgbmax_[2] and RGB[2] >= this->rgbmin_[2]; | ||
|
|
||
| return R_ok_ and G_ok_ and B_ok_; | ||
| } | ||
|
|
||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| // Update the controller | ||
| void GazeboRosColor::OnNewFrame(const unsigned char *_image, | ||
| unsigned int _width, unsigned int _height, unsigned int _depth, | ||
| const std::string &_format) | ||
| { | ||
|
|
||
| double goal_color_=0, pixel_quant_=0; | ||
|
|
||
| this->sensor_update_time_ = this->parentSensor_->LastUpdateTime(); | ||
|
|
||
| std_msgs::BoolPtr publ(new std_msgs::Bool); | ||
| const common::Time cur_time = this->world_->SimTime(); | ||
| if (cur_time - this->last_update_time_ >= this->update_period_) | ||
| { | ||
| this->PutCameraData(_image); | ||
| this->PublishCameraInfo(); | ||
| this->last_update_time_ = cur_time; | ||
| std::vector <int> RGB {0 , 0, 0}; | ||
| bool color_detected_; | ||
|
|
||
| for (int i=0; i < _width * _height * 3 ; i = i + 3) | ||
| { | ||
| RGB[0] = _image[i]; | ||
| RGB[1] = _image[i + 1]; | ||
| RGB[2] = _image[i + 2]; | ||
|
|
||
| pixel_quant_=_width*_height; | ||
|
|
||
| if (this->InRange(RGB)) | ||
| { | ||
| goal_color_++; | ||
| } | ||
| } | ||
|
|
||
| color_detected_ = goal_color_/pixel_quant_ > this->color_percentage_; | ||
| publ->data = color_detected_; | ||
| this->sensor_publisher_.publish(publ); | ||
|
|
||
| goal_color_ = 0; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Goal | ||
| int32 amount | ||
|
|
||
| --- | ||
| # Result | ||
| string[] final_result | ||
|
|
||
| --- | ||
| # Feedback | ||
| int32 number_of_minutes |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.