Skip to content

Commit

Permalink
Test rosbag c++
Browse files Browse the repository at this point in the history
Signed-off-by: Nate Koenig <[email protected]>
  • Loading branch information
Nate Koenig committed Sep 30, 2020
1 parent f6267e3 commit eff2b2c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docker/cloudsim_sim/run_sim.bash
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ bwm-ng -o csv -c 0 -t 1000 -T rate -I eth0 >> $FILE &
# atop process monitoring
atop -R -w /tmp/ign/logs/atop_log &

# trap "rosnode kill /cloudsim_bagger" SIGINT
# trap "rosnode kill /cloudsim_bagger" SIGTERM

export ROS_LOG_DIR=/tmp/ign/logs/ros
rosbag record --split --size=1000 -O /tmp/ign/logs/cloudsim.bag -e '/subt/.*' &
# rosbag record --split --size=1000 -O /tmp/ign/logs/cloudsim.bag -e '/subt/.*' __name:=cloudsim_bagger &
ign launch -v 4 $@
2 changes: 1 addition & 1 deletion docker/json2docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
# In this example, two robots are started with the names X1 and X2.
sim:
image: #{options['sim_image']}
command: cloudsim_sim.ign headless:=#{submission['headless']} circuit:=#{submission['circuit']} worldName:=#{submission['world']} #{robotStr}
command: cloudsim_sim.ign headless:=#{submission['headless']} circuit:=#{submission['circuit']} ros:=true worldName:=#{submission['world']} #{robotStr}
networks:
sim_net:
ipv4_address: 172.28.1.1
Expand Down
1 change: 1 addition & 0 deletions subt_ign/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ find_package(catkin REQUIRED
subt_communication_model
subt_communication_broker
subt_ros
rosbag
)

find_package(ignition-gazebo2 2.1 QUIET REQUIRED)
Expand Down
1 change: 1 addition & 0 deletions subt_ign/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<depend>libccd-dev</depend>
<depend>libfcl-dev</depend>
<depend>rosbag</depend>

<test_depend>rostest</test_depend>
</package>
30 changes: 30 additions & 0 deletions subt_ign/src/GameLogicPlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <yaml-cpp/yaml.h>
#include <ros/ros.h>
#include <rosbag/recorder.h>

#include <ignition/msgs/boolean.pb.h>
#include <ignition/msgs/float.pb.h>
Expand Down Expand Up @@ -161,6 +162,7 @@ class subt::GameLogicPluginPrivate
/// \brief Publish the score.
/// \param[in] _event Unused.
public: void PublishScore();
public: void RosBag();

/// \brief Log robot pos data
public: void LogRobotPosData();
Expand Down Expand Up @@ -314,6 +316,9 @@ class subt::GameLogicPluginPrivate
/// \brief Thread on which scores are published
public: std::unique_ptr<std::thread> publishThread = nullptr;

/// \brief Thread on which scores are published
public: std::unique_ptr<std::thread> bagThread = nullptr;

/// \brief Whether the task has started.
public: bool started = false;

Expand Down Expand Up @@ -556,6 +561,7 @@ class subt::GameLogicPluginPrivate
public: ros::Publisher rosRegionEventPub;
public: std::map<std::string, ros::Publisher> rosRobotPosePubs;
public: std::map<std::string, ros::Publisher> rosRobotKinematicPubs;
public: rosbag::Recorder *rosRecorder;
public: std::string prevPhase = "";

/// \brief Counter to create unique id for events
Expand All @@ -578,6 +584,11 @@ GameLogicPlugin::~GameLogicPlugin()
this->dataPtr->finished = true;
if (this->dataPtr->publishThread)
this->dataPtr->publishThread->join();

// Shutdown ros
ros::shutdown();
if (this->dataPtr->bagThread)
this->dataPtr->bagThread->join();
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -665,6 +676,17 @@ void GameLogicPlugin::Configure(const ignition::gazebo::Entity & /*_entity*/,
this->dataPtr->rosRegionEventPub =
this->dataPtr->rosnode->advertise<subt_ros::RegionEvent>(
"region_event", 100);

// Setup a ros bag recorder.
rosbag::RecorderOptions recorderOptions;
recorderOptions.append_date=false;
recorderOptions.prefix="/tmp/ign/logs/cloudsim";
recorderOptions.regex=true;
recorderOptions.topics.push_back("/subt/.*");

this->dataPtr->rosRecorder = new rosbag::Recorder(recorderOptions);
this->dataPtr->bagThread.reset(new std::thread(
&GameLogicPluginPrivate::RosBag, this->dataPtr.get()));
}
}

Expand Down Expand Up @@ -2117,6 +2139,14 @@ void GameLogicPluginPrivate::ParseArtifacts(
}
}

/////////////////////////////////////////////////
void GameLogicPluginPrivate::RosBag()
{
this->rosRecorder->run();
delete this->rosRecorder;
this->rosRecorder = nullptr;
}

/////////////////////////////////////////////////
void GameLogicPluginPrivate::PublishScore()
{
Expand Down

0 comments on commit eff2b2c

Please sign in to comment.