10 #include "mf_sensors_simulator/CameraOutput.h" 11 #include "mf_sensors_simulator/MultiPoses.h" 13 #include "mf_farm_simulator/Algae.h" 14 #include "reactphysics3d.h" 15 #include <visualization_msgs/Marker.h> 16 #include <geometry_msgs/TransformStamped.h> 17 #include <geometry_msgs/Pose.h> 18 #include <geometry_msgs/Point.h> 19 #include <tf2_geometry_msgs/tf2_geometry_msgs.h> 20 #include <tf2_ros/transform_listener.h> 21 #include <pluginlib/class_list_macros.h> 23 #include <eigen3/Eigen/Dense> 39 sig_atomic_t
volatile CameraNodelet::b_sigint_ = 0;
40 ros::Timer CameraNodelet::main_timer_ = ros::Timer();
45 CameraNodelet::CameraNodelet():
46 tf_listener_(tf_buffer_),
58 nh_ = getNodeHandle();
62 struct sigaction sigIntHandler;
64 sigemptyset(&sigIntHandler.sa_mask);
65 sigIntHandler.sa_flags = 0;
66 sigaction(SIGINT, &sigIntHandler, NULL);
94 out_pub_ =
nh_.advertise<mf_sensors_simulator::CameraOutput>(
"camera_out", 0);
95 rviz_pub_ =
nh_.advertise<visualization_msgs::Marker>(
"camera_markers", 0);
114 if (!ros::ok() || ros::isShuttingDown() ||
b_sigint_)
158 fov_shape_ = unique_ptr<rp3d::BoxShape>(
new rp3d::BoxShape(fov_half_extents));
161 rp3d::Quaternion fov_orient(0, 0, 0, 1);
162 rp3d::Transform fov_transform(fov_pos, fov_orient);
171 for (
unsigned int k = 0; k <
n; k++) {
175 rp3d::Vector3 pos(al->position.x, al->position.y, al->position.z);
176 rp3d::Quaternion orient(al->orientation.x, al->orientation.y,
177 al->orientation.z, al->orientation.w);
179 rp3d::Transform transform(pos, orient);
180 rp3d::CollisionBody* body =
coll_world_.createCollisionBody(transform);
184 rp3d::Vector3 half_extents(al->dimensions.x/2, al->dimensions.y/2,
186 algae_shapes_[k] = unique_ptr<rp3d::BoxShape>(
new rp3d::BoxShape(half_extents));
187 body->addCollisionShape(
algae_shapes_[k].
get(), rp3d::Transform::identity());
190 unsigned int a = al->disease_heatmap.size();
191 unsigned int b = al->disease_heatmap[0].data.size();
192 heatmaps_[k].resize(a, vector<float>(b));
194 for (
unsigned int i = 0; i < a; i++) {
195 for (
unsigned int j = 0; j < b; j++) {
196 heatmaps_[k][i][j] = al->disease_heatmap[i].data[j];
207 for (
unsigned int k = 0; k <
n; k++) {
211 rp3d::Vector3 pos(al->position.x, al->position.y, al->position.z);
212 rp3d::Quaternion orient(al->orientation.x, al->orientation.y,
213 al->orientation.z, al->orientation.w);
215 rp3d::Transform transform(pos, orient);
223 geometry_msgs::TransformStamped tf1, tf2, tf3, tf4;
231 catch (tf2::TransformException &ex) {
232 NODELET_WARN(
"[camera_nodelet] %s",ex.what());
245 mf_sensors_simulator::MultiPoses::Response &res)
249 res.is_success =
false;
254 int n_pxl_h = req.n_pxl_height;
255 int n_pxl_w = req.n_pxl_width;
256 if (n_pxl_h <= 0 || n_pxl_w <= 0) {
261 int nbr_poses = req.pose_array.poses.size();
262 res.results.resize(nbr_poses);
264 if (nbr_poses == 0) {
265 NODELET_WARN(
"[camera_nodelet] Called ray_multi_cb with 0 pose");
266 res.is_success =
false;
272 rp3d::CollisionBody* body =
coll_world_.createCollisionBody(rp3d::Transform::identity());
273 unique_ptr<rp3d::BoxShape> shape;
274 success =
multi_fov_body(req.pose_array.poses, body, shape, req.stamp);
278 res.is_success = success;
287 for (
int k = 0; k < nbr_poses; k++) {
288 bool ret =
raycast_wall(req.pose_array.poses[k], n_pxl_h, n_pxl_w, res.results[k], req.stamp);
299 res.is_success = success;
float noise_std_
Maximum standard deviation of the measurement noise.
bool world_init_
Whether the collision world has been initialised.
bool algae_msg_received_
Whether an algae message has been received.
float focal_length_
Focal length of the camera.
void publish_output()
Publishes camera output.
void algae_cb(const mf_farm_simulator::AlgaeConstPtr msg)
Callback for the algae of the farm.
int n_pxl_width_
Nbr of pixels along sensor width.
static ros::Timer main_timer_
Timer callback for the main function.
ros::NodeHandle nh_
Node handler (for topics and services)
void overlap_fov(rp3d::CollisionBody *body)
Selects algae that are in field of view of the camera.
rp3d::CollisionWorld coll_world_
World with all algae and camera FOV.
float camera_freq_
Frequency of the sensor.
std::vector< std::vector< std::vector< float > > > heatmaps_
Disease heatmatps for all the algae.
box_shape_ptr fov_shape_
Collision shapes of the FOV body.
virtual void onInit()
Function called at beginning of nodelet execution.
Declaration of common Rviz display functions.
geometry_msgs::TransformStamped fixed_camera_tf_
Transform from fixed frame to camera.
ros::NodeHandle private_nh_
Private node handler (for parameters)
static sig_atomic_t volatile b_sigint_
Whether SIGINT signal has been received.
bool raycast_wall(const geometry_msgs::Pose &vp_pose, int n_pxl_h, int n_pxl_w, mf_sensors_simulator::CameraOutput &pxl_output, ros::Time stamp=ros::Time(0))
tf2_ros::Buffer tf_buffer_
std::vector< float > fov_color_
Color of the camera field of view Rviz marker.
void update_algae()
Updates algae in the collision world.
void init_coll_world()
Initialise the collision world.
void main_cb(const ros::TimerEvent &timer_event)
Main callback which is called by a timer.
float fov_distance_
Maximum distance detected by the camera.
std::string robot_frame_
Frame of the robot.
static void sigint_handler(int s)
SINGINT (Ctrl+C) callback to stop the nodelet properly.
float sensor_height_
Height of the camera sensor.
rp3d::WorldSettings world_settings_
Collision world settings.
mf_farm_simulator::AlgaeConstPtr last_algae_msg_
Last algae message.
geometry_msgs::TransformStamped robot_camera_tf_
Transform from robot to camera frame.
std::string fixed_frame_
Frame in which the pose is expressed.
geometry_msgs::TransformStamped camera_robot_tf_
Transform from camera to robot frame.
std::mutex coll_mutex_
Mutex to control access to collision variables.
ros::Publisher rviz_pub_
Publisher for Rviz markers.
bool get_tf()
Gets tf transforms.
ros::Publisher out_pub_
Publisher for the camera output.
bool noise_meas_
Whether to noise measurements on disease heatmap.
ros::ServiceServer ray_multi_serv_
Service for raycasting from several camera poses.
bool ray_multi_cb(mf_sensors_simulator::MultiPoses::Request &req, mf_sensors_simulator::MultiPoses::Response &res)
Service callback for raycasting from several camera poses.
ros::Subscriber algae_sub_
Subscriber for the algae of the farm.
std::string camera_frame_
Frame of the camera.
void publish_rviz_fov()
Publishes a Rviz marker for the camera field of view.
std::vector< rp3d::CollisionBody * > algae_bodies_
Collision bodies of the algae.
int n_pxl_height_
Nbr of pixels along sensor height.
geometry_msgs::TransformStamped camera_fixed_tf_
Transform from camera to fixed frame.
float sensor_width_
Width of the camera sensor.
Nodelet for a simulated camera.
std::vector< box_shape_ptr > algae_shapes_
Collision shapes of the algae bodies.
bool multi_fov_body(const std::vector< geometry_msgs::Pose > &poses, rp3d::CollisionBody *body, std::unique_ptr< rp3d::BoxShape > &shape, ros::Time stamp=ros::Time(0))
Fill a collision body for multi-pose FOV overlap.
Declaration of a nodelet simulating a camera.
rp3d::CollisionBody * fov_body_
Collision body of the camera FOV.