Skip to content
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

RSDK-3323 - addSensorReading refactor #102

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions viam-cartographer/src/slam_service/slam_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,34 @@ void SLAMServiceImpl::SaveMapWithTimestamp() {
}
}

bool SLAMServiceImpl::AddSensorReading(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: In the implementation which makes it into CartoFacade you will need to detect the first time AddSensorReading has been called & set the start time on the map builder to be the time that the first lidar reading was taken.

cartographer::sensor::TimedPointCloudData measurement) {
cartographer::transform::Rigid3d tmp_global_pose;
bool update_latest_global_pose = false;

if (map_builder_mutex.try_lock()) {
if (measurement.ranges.size() > 0) {
nicksanford marked this conversation as resolved.
Show resolved Hide resolved
trajectory_builder->AddSensorData(kRangeSensorId.id, measurement);
auto local_poses = map_builder.GetLocalSlamResultPoses();
if (local_poses.size() > 0) {
nicksanford marked this conversation as resolved.
Show resolved Hide resolved
update_latest_global_pose = true;
tmp_global_pose = map_builder.GetGlobalPose(trajectory_id,
nicksanford marked this conversation as resolved.
Show resolved Hide resolved
local_poses.back());
nicksanford marked this conversation as resolved.
Show resolved Hide resolved
}
}
map_builder_mutex.unlock();
if (update_latest_global_pose) {
std::lock_guard<std::mutex> lk(viam_response_mutex);
latest_global_pose = tmp_global_pose;
}
return true;
} else {
return false;
}
}

void SLAMServiceImpl::ProcessDataAndStartSavingMaps(double data_start_time) {
// Prepare the trajectory builder and grab the active trajectory_id
cartographer::mapping::TrajectoryBuilderInterface *trajectory_builder;
int trajectory_id;
{
std::lock_guard<std::mutex> lk(map_builder_mutex);
// Set TrajectoryBuilder
Expand All @@ -664,9 +688,6 @@ void SLAMServiceImpl::ProcessDataAndStartSavingMaps(double data_start_time) {
bool set_start_time = false;
auto file = GetNextDataFile();

// Define tmp_global_pose here so it always has the previous pose
cartographer::transform::Rigid3d tmp_global_pose =
cartographer::transform::Rigid3d();
while (file != "") {
// Ignore files that are not *.pcd files
if (file.find(".pcd") == std::string::npos) {
Expand Down Expand Up @@ -704,25 +725,10 @@ void SLAMServiceImpl::ProcessDataAndStartSavingMaps(double data_start_time) {
LOG(INFO) << "Starting to save maps...";
StartSaveMap();
}
// Add data to the map_builder to add to the map
{
std::lock_guard<std::mutex> lk(map_builder_mutex);
auto measurement = map_builder.GetDataFromFile(file);
if (measurement.ranges.size() > 0) {
trajectory_builder->AddSensorData(kRangeSensorId.id,
measurement);
auto local_poses = map_builder.GetLocalSlamResultPoses();
if (local_poses.size() > 0) {
tmp_global_pose = map_builder.GetGlobalPose(
trajectory_id, local_poses.back());
}
}
}
// Save a copy of the global pose
{
std::lock_guard<std::mutex> lk(viam_response_mutex);
latest_global_pose = tmp_global_pose;
}
// Add data to the map_builder to add to the map and save a copy of the
// global pose
auto measurement = map_builder.GetDataFromFile(file);
AddSensorReading(measurement);

// This log line is needed by rdk integration tests.
VLOG(1) << "Passed sensor data to SLAM " << file;
Expand All @@ -741,6 +747,8 @@ void SLAMServiceImpl::ProcessDataAndStartSavingMaps(double data_start_time) {
if (!use_live_data) {
// We still want to optimize the map in localization mode, but we do not
// need to update the backup of the map
cartographer::transform::Rigid3d tmp_global_pose;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Why did we need to move this inside the loop?
  2. Is there a risk of us leaking memory from creating new tmp_global_poses every iteration of the while loop without ever freeing the one from the previous loop?

Copy link
Collaborator Author

@kim-mishra kim-mishra May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Because we wanted to keep the function signature simple and not have to pass this var into AddSensorReading we wanted to only initialize it if it were going to be used, although now I am getting a little confused why it is better for this to be in the loop. @kkufieta would you mind explaining this again
  2. Based on this doc I think we actually will need to delete this if we continue with this route thanks for pointing that out I don't think so, I think memory leaks only happen when you use dynamic memory (i.e. new or malloc)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is happening outside the loop. This code is only executed in offline mode after we've processed all the data.

bool update_latest_global_pose = false;
if (action_mode != ActionMode::LOCALIZING) BackupLatestMap();
{
std::unique_lock<std::shared_mutex> optimization_lock(
Expand All @@ -754,12 +762,13 @@ void SLAMServiceImpl::ProcessDataAndStartSavingMaps(double data_start_time) {

auto local_poses = map_builder.GetLocalSlamResultPoses();
if (local_poses.size() > 0) {
update_latest_global_pose = true;
tmp_global_pose = map_builder.GetGlobalPose(trajectory_id,
local_poses.back());
}
}

{
if (update_latest_global_pose) {
std::lock_guard<std::mutex> lk(viam_response_mutex);
latest_global_pose = tmp_global_pose;
}
Expand Down
6 changes: 6 additions & 0 deletions viam-cartographer/src/slam_service/slam_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ class SLAMServiceImpl final : public SLAMService::Service {
// string if it fails.
std::string TryFileClose(std::ifstream &file, std::string filename);

// AddSensorReading adds sensor readings to the map builder.
bool AddSensorReading(
cartographer::sensor::TimedPointCloudData measurement);

// ProcessDataAndStartSavingMaps processes the data in the data directory
// that is newer than the provided data_cutoff_time
// and starts the process to save maps in parallel. In offline mode,
Expand Down Expand Up @@ -238,6 +242,8 @@ class SLAMServiceImpl final : public SLAMService::Service {
std::shared_mutex optimization_shared_mutex;
std::mutex map_builder_mutex;
mapping::MapBuilder map_builder;
cartographer::mapping::TrajectoryBuilderInterface *trajectory_builder;
int trajectory_id;

std::atomic<bool> finished_processing_offline{false};
std::thread *thread_save_map_with_timestamp;
Expand Down
Loading