upload
This commit is contained in:
175
libs/vslam_slam/src/occupancy_export.cc
Normal file
175
libs/vslam_slam/src/occupancy_export.cc
Normal file
@@ -0,0 +1,175 @@
|
||||
#include "vslam/occupancy_export.h"
|
||||
|
||||
#include <rtabmap/core/DBDriver.h>
|
||||
#include <rtabmap/core/Memory.h>
|
||||
#include <rtabmap/core/LocalGrid.h>
|
||||
#include <rtabmap/core/Parameters.h>
|
||||
#include <rtabmap/core/Rtabmap.h>
|
||||
#include <rtabmap/core/SensorData.h>
|
||||
#include <rtabmap/core/global_map/OccupancyGrid.h>
|
||||
|
||||
#include <opencv2/core/core.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
|
||||
#include "vslam/log.h"
|
||||
#include "vslam/node_constants.h"
|
||||
#include "vslam/sensor_data_builder.h"
|
||||
|
||||
namespace vslam {
|
||||
namespace {
|
||||
|
||||
// cv::Mat 8SC1 cua RTAB-Map -> vector<int8_t> theo quy uoc map_server:
|
||||
// -1 chua biet, 0 trong, 100 bi chiem
|
||||
// RTAB-Map dung 0=trong, 100=chiem, -1=chua biet o ma tran 8S, nen chi phai
|
||||
// lat truc y (RTAB-Map goc o duoi trai, giong ROS -- KHONG lat).
|
||||
void CopyGrid(const cv::Mat& m, OccupancyGrid* out) {
|
||||
out->width = static_cast<uint32_t>(m.cols);
|
||||
out->height = static_cast<uint32_t>(m.rows);
|
||||
out->data.resize(static_cast<size_t>(m.cols) * m.rows);
|
||||
for (int y = 0; y < m.rows; ++y) {
|
||||
const int8_t* row = m.ptr<int8_t>(y);
|
||||
std::copy(row, row + m.cols,
|
||||
out->data.begin() + static_cast<size_t>(y) * m.cols);
|
||||
}
|
||||
}
|
||||
|
||||
StatusResponse BuildFromCache(rtabmap::LocalGridCache& cache,
|
||||
const std::map<int, rtabmap::Transform>& poses,
|
||||
double cell_size, double range_max,
|
||||
OccupancyGrid* out) {
|
||||
if (poses.empty()) {
|
||||
return StatusResponse::Error(StatusCode::kFailedPrecondition,
|
||||
"khong co pose nao de dung grid");
|
||||
}
|
||||
rtabmap::ParametersMap params;
|
||||
params.insert(rtabmap::ParametersPair(rtabmap::Parameters::kGridCellSize(),
|
||||
std::to_string(cell_size)));
|
||||
params.insert(rtabmap::ParametersPair(rtabmap::Parameters::kGrid3D(), "false"));
|
||||
params.insert(rtabmap::ParametersPair(rtabmap::Parameters::kGridRangeMax(),
|
||||
std::to_string(range_max)));
|
||||
|
||||
rtabmap::OccupancyGrid grid(&cache, params);
|
||||
grid.update(poses);
|
||||
|
||||
float xmin = 0.0f, ymin = 0.0f;
|
||||
const cv::Mat m = grid.getMap(xmin, ymin);
|
||||
if (m.empty()) {
|
||||
return StatusResponse::Error(StatusCode::kInternal, "grid rong sau update()");
|
||||
}
|
||||
|
||||
CopyGrid(m, out);
|
||||
out->resolution = static_cast<float>(cell_size);
|
||||
out->origin = Pose{};
|
||||
out->origin.position.x = xmin;
|
||||
out->origin.position.y = ymin;
|
||||
out->origin.orientation.w = 1.0;
|
||||
out->header.frame_id = kMapFrame;
|
||||
return StatusResponse::Ok();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DbFacts InspectDatabase(const std::string& path) {
|
||||
DbFacts f;
|
||||
// Duong dan rong = DB trong RAM, chua ton tai gi. `openConnection("")` van
|
||||
// THANH CONG (no tao DB tam), nen khong chan o day thi moi kiem dieu kien
|
||||
// tien quyet deu chay tren mot DB rong va bao nham.
|
||||
if (path.empty()) return f;
|
||||
std::unique_ptr<rtabmap::DBDriver> driver(rtabmap::DBDriver::create());
|
||||
if (driver == nullptr) return f;
|
||||
|
||||
// openConnection() NEM UException (qua UFATAL) khi duong dan khong mo duoc,
|
||||
// chu khong tra false. Khong bat o day thi mot duong dan go sai trong
|
||||
// slam.yaml lam SAP ca tien trinh thay vi tra ve mot thong bao doc duoc.
|
||||
try {
|
||||
if (!driver->openConnection(path, false)) return f;
|
||||
} catch (const std::exception&) {
|
||||
return f; // khong mo duoc = coi nhu chua ton tai; Init() se tao moi
|
||||
}
|
||||
f.exists = true;
|
||||
|
||||
std::set<int> ids;
|
||||
driver->getAllNodeIds(ids);
|
||||
f.nodes = static_cast<int>(ids.size());
|
||||
|
||||
// So tu vung. 0 nghia la ban do nay duoc quet voi visual_loop=false.
|
||||
// Dung id tu vung CUOI lam so dem: DBDriver khong cho dem truc tiep, va
|
||||
// cai ta can chi la phan biet "co tu vung" voi "khong co".
|
||||
int last_word = 0;
|
||||
driver->getLastWordId(last_word);
|
||||
f.words = last_word;
|
||||
|
||||
const rtabmap::ParametersMap db_params = driver->getLastParameters();
|
||||
auto it = db_params.find(rtabmap::Parameters::kOptimizerStrategy());
|
||||
if (it != db_params.end()) f.optimizer_strategy = it->second;
|
||||
|
||||
driver->closeConnection(false);
|
||||
return f;
|
||||
}
|
||||
|
||||
StatusResponse ExportOccupancyGrid(rtabmap::Rtabmap& slam, OccupancyGrid* out) {
|
||||
const std::map<int, rtabmap::Transform> poses = slam.getLocalOptimizedPoses();
|
||||
if (poses.empty()) {
|
||||
return StatusResponse::Error(StatusCode::kFailedPrecondition,
|
||||
"do thi chua co pose toi uu nao");
|
||||
}
|
||||
|
||||
rtabmap::LocalGridCache cache;
|
||||
for (const auto& kv : poses) {
|
||||
const rtabmap::Memory* mem = slam.getMemory();
|
||||
if (mem == nullptr) break;
|
||||
rtabmap::SensorData data = mem->getNodeData(kv.first, false, false, false, true);
|
||||
// BAT BUOC: du lieu trong DB la du lieu NEN. Khong goi cai nay thi
|
||||
// gridGroundCellsRaw() tra ma tran rong, im lang.
|
||||
data.uncompressData();
|
||||
if (data.gridCellSize() <= 0.0f) continue;
|
||||
cache.add(kv.first, data.gridGroundCellsRaw(), data.gridObstacleCellsRaw(),
|
||||
data.gridEmptyCellsRaw(), data.gridCellSize(), data.gridViewPoint());
|
||||
}
|
||||
return BuildFromCache(cache, poses, 0.05, 12.0, out);
|
||||
}
|
||||
|
||||
StatusResponse ExportOccupancyGridFromDb(const std::string& db_path,
|
||||
double cell_size, double range_max,
|
||||
OccupancyGrid* out) {
|
||||
std::unique_ptr<rtabmap::DBDriver> driver(rtabmap::DBDriver::create());
|
||||
bool opened = false;
|
||||
try {
|
||||
opened = driver != nullptr && driver->openConnection(db_path, false);
|
||||
} catch (const std::exception& e) {
|
||||
return StatusResponse::Error(StatusCode::kNotFound,
|
||||
"khong mo duoc DB " + db_path + ": " + e.what());
|
||||
}
|
||||
if (!opened) {
|
||||
return StatusResponse::Error(StatusCode::kNotFound, "khong mo duoc DB: " + db_path);
|
||||
}
|
||||
|
||||
const std::map<int, rtabmap::Transform> poses = driver->loadOptimizedPoses();
|
||||
rtabmap::LocalGridCache cache;
|
||||
int with_grid = 0;
|
||||
|
||||
for (const auto& kv : poses) {
|
||||
rtabmap::SensorData data;
|
||||
driver->getNodeData(kv.first, data, false, false, false, true);
|
||||
// Xem ghi chu o dau file: KHONG co dong nay thi cache rong va grid ra
|
||||
// 0x0, khong loi nao duoc bao.
|
||||
data.uncompressData();
|
||||
if (data.gridCellSize() <= 0.0f) continue;
|
||||
cache.add(kv.first, data.gridGroundCellsRaw(), data.gridObstacleCellsRaw(),
|
||||
data.gridEmptyCellsRaw(), data.gridCellSize(), data.gridViewPoint());
|
||||
with_grid++;
|
||||
}
|
||||
driver->closeConnection(false);
|
||||
|
||||
if (with_grid == 0) {
|
||||
return StatusResponse::Error(
|
||||
StatusCode::kFailedPrecondition,
|
||||
"khong node nao co local grid -- ban do quet voi Grid/FromDepth sai?");
|
||||
}
|
||||
return BuildFromCache(cache, poses, cell_size, range_max, out);
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
Reference in New Issue
Block a user