26 #include <mav_msgs/Actuators.h> 41 gzdbg << __FUNCTION__ <<
"() called." << std::endl;
49 if (_sdf->HasElement(
"robotNamespace")) {
50 namespace_ = _sdf->GetElement(
"robotNamespace")->Get<std::string>();
52 gzerr <<
"[gazebo_bag_plugin] Please specify a robotNamespace.\n";
56 if (_sdf->HasElement(
"bagFileName")) {
57 bag_filename_ = _sdf->GetElement(
"bagFileName")->Get<std::string>();
59 gzerr <<
"[gazebo_bag_plugin] Please specify a bagFileName.\n";
62 if (_sdf->HasElement(
"linkName")) {
63 link_name_ = _sdf->GetElement(
"linkName")->Get<std::string>();
65 gzwarn <<
"[gazebo_bag_plugin] No linkName specified, using default " 72 gzthrow(
"[gazebo_bag_plugin] Couldn't find specified link \"" <<
link_name_ 78 getSdfParam<std::string>(_sdf,
"commandAttitudeThrustTopic",
81 getSdfParam<std::string>(_sdf,
"commandMotorSpeedTopic",
84 getSdfParam<std::string>(_sdf,
"commandRateThrustTopic",
87 getSdfParam<std::string>(_sdf,
"windSpeedTopic",
94 getSdfParam<std::string>(_sdf,
"wrenchesTopic",
wrench_topic_,
102 getSdfParam<std::string>(_sdf,
"recordingServiceName",
105 getSdfParam<double>(_sdf,
"rotorVelocitySlowdownSim",
117 for (
unsigned int i = 0; i <
child_links_.size(); i++) {
118 std::string link_name =
child_links_[i]->GetScopedName();
121 int pos = link_name.find(
"rotor_");
122 if (pos != link_name.npos) {
123 std::string motor_number_str = link_name.substr(pos + 6);
124 unsigned int motor_number = std::stoi(motor_number_str);
125 std::string joint_name =
child_links_[i]->GetName() +
"_joint";
126 physics::JointPtr joint =
model_->GetJoint(joint_name);
132 std::vector<std::string> collisions;
134 for (
unsigned int i = 0; i <
link_->GetCollisions().size(); ++i) {
135 physics::CollisionPtr collision =
link_->GetCollision(i);
136 collisions.push_back(collision->GetScopedName());
138 for (
unsigned int j = 0; j <
child_links_.size(); ++j) {
139 unsigned int zero = 0;
140 for (
unsigned int i = 0; i <
child_links_[j]->GetCollisions().size(); ++i) {
141 collisions.push_back(
child_links_[j]->GetCollision(i)->GetScopedName());
145 if (!collisions.empty()) {
156 gzdbg << __FUNCTION__ <<
"() called." << std::endl;
160 common::Time now =
world_->SimTime();
172 timeinfo = localtime(&rawtime);
174 strftime(buffer, 80,
"%Y-%m-%d-%H-%M-%S", timeinfo);
175 std::string date_time_str(buffer);
177 std::string key(
".bag");
179 if (pos != std::string::npos) {
182 std::string full_bag_filename =
bag_filename_ +
"_" + date_time_str +
".bag";
231 ROS_INFO(
"GazeboBagPlugin START recording bagfile %s",
232 full_bag_filename.c_str());
255 ROS_INFO(
"GazeboBagPlugin STOP recording bagfile");
259 common::Time now =
world_->SimTime();
265 const geometry_msgs::WrenchStampedConstPtr& force_msg) {
266 common::Time now =
world_->SimTime();
272 const trajectory_msgs::MultiDOFJointTrajectoryConstPtr& trajectory_msg) {
273 common::Time now =
world_->SimTime();
279 const geometry_msgs::PoseStampedConstPtr& pose_msg) {
280 common::Time now =
world_->SimTime();
286 const mav_msgs::AttitudeThrustConstPtr& control_msg) {
287 common::Time now =
world_->SimTime();
294 const mav_msgs::ActuatorsConstPtr& control_msg) {
295 common::Time now =
world_->SimTime();
301 const mav_msgs::RateThrustConstPtr& control_msg) {
302 common::Time now =
world_->SimTime();
308 const rotors_comm::WindSpeedConstPtr& wind_speed_msg) {
309 common::Time now =
world_->SimTime();
317 mav_msgs::Actuators rot_velocities_msg;
318 rot_velocities_msg.angular_velocities.resize(
motor_joints_.size());
320 MotorNumberToJointMap::iterator
m;
322 double motor_rot_vel =
324 rot_velocities_msg.angular_velocities[m->first] = motor_rot_vel;
326 rot_velocities_msg.header.stamp.sec = now.sec;
327 rot_velocities_msg.header.stamp.nsec = now.nsec;
335 geometry_msgs::PoseStamped pose_msg;
336 geometry_msgs::TwistStamped twist_msg;
339 ignition::math::Pose3d pose =
link_->WorldPose();
341 pose_msg.header.stamp.sec = now.sec;
342 pose_msg.header.stamp.nsec = now.nsec;
343 pose_msg.pose.position.x = pose.Pos().X();
344 pose_msg.pose.position.y = pose.Pos().Y();
345 pose_msg.pose.position.z = pose.Pos().Z();
346 pose_msg.pose.orientation.w = pose.Rot().W();
347 pose_msg.pose.orientation.x = pose.Rot().X();
348 pose_msg.pose.orientation.y = pose.Rot().Y();
349 pose_msg.pose.orientation.z = pose.Rot().Z();
354 ignition::math::Vector3d linear_veloctiy =
link_->WorldLinearVel();
355 ignition::math::Vector3d angular_veloctiy =
link_->WorldAngularVel();
357 twist_msg.header.stamp.sec = now.sec;
358 twist_msg.header.stamp.nsec = now.nsec;
359 twist_msg.twist.linear.x = linear_veloctiy.X();
360 twist_msg.twist.linear.y = linear_veloctiy.Y();
361 twist_msg.twist.linear.z = linear_veloctiy.Z();
362 twist_msg.twist.angular.x = angular_veloctiy.X();
363 twist_msg.twist.angular.y = angular_veloctiy.Y();
364 twist_msg.twist.angular.z = angular_veloctiy.Z();
370 geometry_msgs::WrenchStamped wrench_msg;
371 std::vector<physics::Contact*> contacts =
contact_mgr_->GetContacts();
372 for (
int i = 0; i <
contact_mgr_->GetContactCount(); ++i) {
373 std::string collision2_name =
374 contacts[i]->collision2->GetLink()->GetScopedName();
375 double body1_force = contacts[i]->wrench->body1Force.Length();
378 if (body1_force < 1
e-10)
continue;
382 std::string collision1_name =
383 contacts[i]->collision1->GetLink()->GetScopedName();
384 wrench_msg.header.frame_id = collision1_name +
"--" + collision2_name;
385 wrench_msg.header.stamp.sec = now.sec;
386 wrench_msg.header.stamp.nsec = now.nsec;
387 wrench_msg.wrench.force.x = contacts[i]->wrench->body1Force.X();
388 wrench_msg.wrench.force.y = contacts[i]->wrench->body1Force.Y();
389 wrench_msg.wrench.force.z = contacts[i]->wrench->body1Force.Z();
390 wrench_msg.wrench.torque.x = contacts[i]->wrench->body1Torque.X();
391 wrench_msg.wrench.torque.y = contacts[i]->wrench->body1Torque.Y();
392 wrench_msg.wrench.torque.z = contacts[i]->wrench->body1Torque.Z();
399 rotors_comm::RecordRosbag::Request& req,
400 rotors_comm::RecordRosbag::Response& res) {
405 gzwarn <<
"[gazebo_bag_plugin] Already recording rosbag, ignoring start " 412 gzwarn <<
"[gazebo_bag_plugin] Already not recording rosbag, ignoring stop " virtual ~GazeboBagPlugin()
void ExternalForceCallback(const geometry_msgs::WrenchStampedConstPtr &force_msg)
Called when an WrenchStamped message is received.
void WindSpeedCallback(const rotors_comm::WindSpeedConstPtr &wind_speed_msg)
Called when a WindSpeed message is received.
void CommandPoseCallback(const geometry_msgs::PoseStampedConstPtr &pose_msg)
Called when a PoseStamped message is received.
void LogMotorVelocities(const common::Time now)
Log all the motor velocities.
ros::NodeHandle * node_handle_
std::string ground_truth_pose_topic_
void open(std::string const &filename, uint32_t mode=bagmode::Read)
void WaypointCallback(const trajectory_msgs::MultiDOFJointTrajectoryConstPtr &trajectory_msg)
Called when a MultiDOFJointTrajectoryPoint message is received.
std::pair< const unsigned int, const physics::JointPtr > MotorNumberToJointPair
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::string command_pose_topic_
ros::Subscriber control_rate_thrust_sub_
void ActuatorsCallback(const mav_msgs::ActuatorsConstPtr &control_msg)
Called when a Actuators message is received.
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
Load the plugin.
void LogGroundTruth(const common::Time now)
Log the ground truth pose and twist.
ros::Subscriber control_attitude_thrust_sub_
ros::Subscriber waypoint_sub_
physics::ContactManager * contact_mgr_
Pointer to the ContactManager to get all collisions of this link and its children.
std::string recording_service_name_
std::string control_rate_thrust_topic_
ros::Subscriber wind_speed_sub_
std::string bag_filename_
static const bool kPrintOnPluginLoad
void AttitudeThrustCallback(const mav_msgs::AttitudeThrustConstPtr &control_msg)
Called when a AttitudeThrust message is received.
std::string external_force_topic_
ros::Subscriber command_pose_sub_
void StopRecording()
Stop recording the rosbag.
This plugin is used to create rosbag files from within gazebo.
void StartRecording()
Starting recording the rosbag.
ros::Subscriber external_force_sub_
std::string control_motor_speed_topic_
void writeBag(const std::string &topic, const ros::Time &time, const T &msg)
std::string wind_speed_topic_
MotorNumberToJointMap motor_joints_
ros::ServiceServer recording_service_
void RateThrustCallback(const mav_msgs::RateThrustConstPtr &control_msg)
Called when a RateThrust message is received.
std::string control_attitude_thrust_topic_
event::ConnectionPtr update_connection_
Pointer to the update event connection.
static const bool kPrintOnUpdates
std::string wrench_topic_
void OnUpdate(const common::UpdateInfo &)
Called when the world is updated.
GZ_REGISTER_MODEL_PLUGIN(GazeboRosP3D)
std::string ground_truth_twist_topic_
ros::Subscriber control_motor_speed_sub_
void ImuCallback(const sensor_msgs::ImuConstPtr &imu_msg)
Called when an IMU message is received.
std::string waypoint_topic_
bool is_recording_
Whether the plugin is currenly recording a rosbag.
double rotor_velocity_slowdown_sim_
bool RecordingServiceCallback(rotors_comm::RecordRosbag::Request &req, rotors_comm::RecordRosbag::Response &res)
Called when a request to start or stop recording is received.
void LogWrenches(const common::Time now)
Log all the wrenches.
bool wait_to_record_
Whether the plugin should wait for user command to start recording.
physics::Link_V child_links_