Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/sanity-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
apt-get install -y -qq --no-install-recommends \
build-essential cmake git pkg-config ca-certificates \
libserial-dev libgpiod-dev libzmq3-dev libboost-all-dev \
libpistache-dev rapidjson-dev nlohmann-json3-dev cppzmq-dev pps-tools
libpistache-dev rapidjson-dev nlohmann-json3-dev cppzmq-dev pps-tools zlib1g-dev libzstd-dev
update-ca-certificates

- name: Install Clang and clang-tidy
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "3rd/tracy"]
path = 3rd/tracy
url = https://github.com/wolfpld/tracy.git
[submodule "3rd/mlaz"]
path = 3rd/mlaz
url = https://gitlab.com/GitMF/mlaz.git
1 change: 1 addition & 0 deletions 3rd/mlaz
Submodule mlaz added at 3d19ff
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ message("LASzip library")
add_subdirectory(3rd/LASzip)
include_directories(3rd/LASzip/include)

message("mLAZ writer")
# mLAZ writer (3rd/mlaz submodule, pinned to a codec release tag)
include(cmake/Mlaz.cmake)

message("INCBIN")
# INCBIN
include_directories(3rd/incbin)
Expand Down Expand Up @@ -160,13 +164,13 @@ endif()
add_executable(control_program code/main.cpp code/gnss.cpp code/web_page.h
${LIDAR_SOURCES}
${LIDAR_SOURCES}
code/gpios.cpp code/FileSystemClient.cpp code/save_laz.cpp code/save_data.cpp
code/gpios.cpp code/FileSystemClient.cpp code/save_laz.cpp code/save_mlaz.cpp code/save_data.cpp
code/utils/TimeStampReceiver.cpp code/publisher.cpp)

set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -latomic " )


target_link_libraries(control_program pthread ${LIDAR_LIBRARIES} pistache atomic laszip ${LIBSERIAL_LIBRARY} minea gpiod zmq)
target_link_libraries(control_program pthread ${LIDAR_LIBRARIES} pistache atomic laszip mlaz_writer ${LIBSERIAL_LIBRARY} minea gpiod zmq)

if(MANDEYE_USE_TRACY)
target_link_libraries(control_program TracyClient)
Expand Down Expand Up @@ -251,6 +255,6 @@ set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_NAME "mandeye-multisensor")
set(CPACK_PACKAGE_VERSION "${MANDEYE_VERSION}-${GIT_HASH}-${MANDEYE_HARDWARE_HEADER}-${SYSTEM_ARCH}")
set(CPACK_PACKAGE_CONTACT "Michał Pełka <michalpelka@gmail.com>")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "rapidjson-dev,libserial-dev, libgpiod-dev, libzmq3-dev")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "rapidjson-dev,libserial-dev, libgpiod-dev, libzmq3-dev, libzstd1, zlib1g")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/packing/postinst/postinst")
include(CPack)
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The software can run other distros (e.g. Ubuntu), but it is not tested and maint
```bash
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake git rapidjson-dev debhelper build-essential ntfs-3g libserial-dev libgpiod-dev libzmq3-dev libpistache-dev libcamera-dev linuxptp debhelper libopencv-dev pps-tools ppsh nlohmann-json3-dev
sudo apt-get install build-essential cmake git rapidjson-dev debhelper build-essential ntfs-3g libserial-dev libgpiod-dev libzmq3-dev libpistache-dev libcamera-dev linuxptp debhelper libopencv-dev pps-tools ppsh nlohmann-json3-dev libzstd-dev zlib1g-dev
```

## Static IP for eth0
Expand Down Expand Up @@ -225,6 +225,55 @@ You can set the SDK to use in two ways:
}
```

## Point-cloud format: LAZ or lossless mLAZ

By default every chunk is saved as `lidarNNNN.laz`, exactly as before. The
controller can also save chunks as `lidarNNNN.mlaz`, a lossless format from the
mLAZ codec (https://gitlab.com/GitMF/mlaz, submodule `3rd/mlaz`). Choose the
format with `point_format` in `/media/usb/mandeye_config.json` and restart the
controller:

| `point_format` | Files per chunk |
|-------------------|------------------------------------------------------|
| missing or `laz` | `lidarNNNN.laz` (default) |
| `mlaz` | `lidarNNNN.mlaz` |
| `both` | `lidarNNNN.laz` and `lidarNNNN.mlaz` from one buffer |

```json
{ "point_format": "mlaz" }
```

To go back to LAZ, remove the key or set it to `laz`. mLAZ is enabled only for
`LIVOX_SDK2`; with another LiDAR SDK, or with an unknown value, the controller
saves LAZ and the web page shows a warning. If an mLAZ file cannot be saved,
the chunk is saved as LAZ instead.

`lidarNNNN.mlaz` holds the same LAS 1.2 file the controller would save as
uncompressed `lidarNNNN.las` (same points, decimation and header), except the
header's generating software is `Mandeye mLAZ <codec version>`. The serial
numbers from `lidarNNNN.sn` are stored inside the file as well. Decode it with
the `mlaz` tool from the codec repository:

```bash
mlaz decompress lidar0001.mlaz lidar0001.las --sn lidar0001.sn
mlaz verify lidar0001.mlaz
```

Each `statusNNNN.json` reports `point_format` (`configured`, `effective`,
`warning` and `last_chunk`, the format the chunk was saved in) and, when mLAZ
is on, `lastMlazStatus`. The web status table shows the format.

`test/mlaz` checks the mLAZ writer against the LAZ writer without LiDAR SDKs
(needs `libzstd-dev zlib1g-dev nlohmann-json3-dev` and Python with laspy/lazrs):

```bash
git submodule update --init 3rd/LASzip 3rd/tracy 3rd/mlaz
make -C 3rd/mlaz mlaz # the mlaz tool, decodes .mlaz
pip install "laspy[lazrs]"
cmake -S test/mlaz -B build-mlaz-test && cmake --build build-mlaz-test -j
python3 test/mlaz/check_save_mlaz.py --test build-mlaz-test/test_save_mlaz --mlaz 3rd/mlaz/mlaz
```

# Installation and usage of the package
To install the package, you need to copy it to the target device and install it with `dpkg`:
```bash
Expand Down
39 changes: 39 additions & 0 deletions cmake/Mlaz.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# mLAZ writer (lossless point-cloud codec, https://gitlab.com/GitMF/mlaz) built from the 3rd/mlaz
# submodule pinned to a release tag. Defines:
# mlaz_writer static library with mlaz::Writer (3rd/mlaz/mlaz_writer.h)
# MLAZ_VERSION codec version read from 3rd/mlaz/mlaz_core.h (also passed to users as MANDEYE_MLAZ_VERSION)
# Needs zstd and zlib (Debian/Raspberry Pi OS: apt install libzstd-dev zlib1g-dev).
# Other locations: -DCMAKE_PREFIX_PATH="<zstd prefix>;<zlib prefix>".

set(MLAZ_DIR ${CMAKE_CURRENT_LIST_DIR}/../3rd/mlaz)
if(NOT EXISTS ${MLAZ_DIR}/mlaz_writer.cpp)
message(FATAL_ERROR "3rd/mlaz is empty. Run: git submodule update --init 3rd/mlaz")
endif()

file(STRINGS ${MLAZ_DIR}/mlaz_core.h MLAZ_VERSION_LINE REGEX "constexpr const char\\* VERSION = ")
string(REGEX REPLACE ".*VERSION = \"([0-9.]+)\".*" "\\1" MLAZ_VERSION "${MLAZ_VERSION_LINE}")
if(NOT MLAZ_VERSION MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+$")
message(FATAL_ERROR "Cannot read the mLAZ version from ${MLAZ_DIR}/mlaz_core.h")
endif()
message(STATUS "mLAZ codec version: ${MLAZ_VERSION}")

find_package(Threads REQUIRED)
find_package(ZLIB REQUIRED)
find_path(ZSTD_INCLUDE_DIR zstd.h)
find_library(ZSTD_LIBRARY NAMES zstd)
if(NOT ZSTD_INCLUDE_DIR OR NOT ZSTD_LIBRARY)
message(FATAL_ERROR "zstd not found. Install: apt install libzstd-dev")
endif()

add_library(mlaz_writer STATIC ${MLAZ_DIR}/mlaz_writer.cpp)
set_property(TARGET mlaz_writer PROPERTY POSITION_INDEPENDENT_CODE ON)
target_include_directories(mlaz_writer PUBLIC ${MLAZ_DIR})
target_include_directories(mlaz_writer PRIVATE ${ZSTD_INCLUDE_DIR})
target_link_libraries(mlaz_writer PRIVATE ${ZSTD_LIBRARY} ZLIB::ZLIB Threads::Threads)
target_compile_options(mlaz_writer PRIVATE -O3)
# Every Raspberry Pi 4/5 and CM4/CM5 core (Cortex-A72/A76) has the ARMv8 CRC32 instructions; NEON is
# part of AArch64. Without +crc the codec falls back to the slower zlib CRC.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
target_compile_options(mlaz_writer PRIVATE -march=armv8-a+crc)
endif()
target_compile_definitions(mlaz_writer INTERFACE MANDEYE_MLAZ_VERSION="${MLAZ_VERSION}")
95 changes: 83 additions & 12 deletions code/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "save_data.h"
#include "save_laz.h"
#include "save_mlaz.h"
#include "state.h"
#include <FileSystemClient.h>

Expand Down Expand Up @@ -48,6 +49,10 @@ std::shared_ptr<GNSSClient> gnssClientPtr;
std::shared_ptr<FileSystemClient> fileSystemClientPtr;
std::shared_ptr<Publisher> publisherPtr;
mandeye::LazStats lastFileSaveStats; // updated by savePointcloudData return value
std::mutex lastMlazMutex; // guards the three below, read by the web status thread
mandeye::PointFormatSetting pointFormat; // "point_format" from mandeye_config.json, set at start before stateWatcher runs
mandeye::LazStats lastMlazSaveStats;
std::string lastChunkFormat; // what the last chunk was saved as: laz, mlaz, both, laz (mlaz failed), ...
double usbWriteSpeed10Mb = 0.0;
double usbWriteSpeed1Mb = 0.0;

Expand Down Expand Up @@ -149,6 +154,17 @@ std::string produceReport(bool reportUSB = true)
}

j["lastLazStatus"] = lastFileSaveStats.produceStatus();
{
std::lock_guard<std::mutex> lock(lastMlazMutex);
j["point_format"]["configured"] = pointFormat.configured;
j["point_format"]["effective"] = toString(pointFormat.format);
j["point_format"]["warning"] = pointFormat.warning;
j["point_format"]["last_chunk"] = lastChunkFormat;
if(pointFormat.format != PointFormat::Laz)
{
j["lastMlazStatus"] = lastMlazSaveStats.produceStatus();
}
}

std::ostringstream s;
s << std::setw(4) << j;
Expand Down Expand Up @@ -260,6 +276,57 @@ void saveStatusData(const std::string& directory, int chunk)
system("sync");
}

// Saves the chunk's points in the configured point format. With "laz" the calls are the same as before
// mLAZ existed. Returns the LiDAR list stored in the .mlaz file (if one was written), so that
// lidarNNNN.sn gets the same list.
std::optional<std::unordered_map<uint32_t, std::string>> savePoints(LidarPointsBufferPtr buffer, const std::string& directory, int chunk)
{
if(pointFormat.format == PointFormat::Laz)
{
auto [fn, saveStats] = savePointcloudData(buffer, directory, chunk);
if(saveStats)
lastFileSaveStats = *saveStats;
std::lock_guard<std::mutex> lock(lastMlazMutex);
lastChunkFormat = saveStats ? "laz" : "none";
return std::nullopt;
}

auto lidarList = lidarClientPtr->getSerialNumberToLidarIdMapping();
bool lazSaved = false;
if(pointFormat.format == PointFormat::Both)
{
// LAZ first: an mLAZ error never costs the LAZ file
auto [fn, saveStats] = savePointcloudData(buffer, directory, chunk);
if(saveStats)
lastFileSaveStats = *saveStats;
lazSaved = saveStats.has_value();
}
auto [mlazFn, mlazStats] = savePointcloudMlaz(buffer, directory, chunk, lidarListText(lidarList));
std::string written;
if(pointFormat.format == PointFormat::Both)
{
written = lazSaved && mlazStats ? "both" : lazSaved ? "laz (mlaz failed)" : mlazStats ? "mlaz (laz failed)" : "none";
}
else if(mlazStats)
{
hardware::OnSavedLaz(mlazFn);
written = "mlaz";
}
else
{
// mLAZ only, but it failed: keep the chunk as LAZ
auto [fn, saveStats] = savePointcloudData(buffer, directory, chunk);
if(saveStats)
lastFileSaveStats = *saveStats;
written = saveStats ? "laz (mlaz failed)" : "none";
}
std::lock_guard<std::mutex> lock(lastMlazMutex);
if(mlazStats)
lastMlazSaveStats = *mlazStats;
lastChunkFormat = written;
return lidarList;
}

void stateWatcher()
{
using namespace std::chrono_literals;
Expand Down Expand Up @@ -510,12 +577,10 @@ void stateWatcher()
}
else
{
auto [fn, saveStats] = savePointcloudData(lidarBuffer, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
if(saveStats)
lastFileSaveStats = *saveStats;
auto mlazLidarList = savePoints(lidarBuffer, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
saveImuData(imuBuffer, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
saveStatusData(continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
auto lidarList = lidarClientPtr->getSerialNumberToLidarIdMapping();
auto lidarList = mlazLidarList ? *mlazLidarList : lidarClientPtr->getSerialNumberToLidarIdMapping();
saveLidarList(lidarList, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
if(gnssClientPtr)
{
Expand Down Expand Up @@ -607,12 +672,10 @@ void stateWatcher()
}
else
{
auto [fn, saveStats] = savePointcloudData(lidarBuffer, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
if(saveStats)
lastFileSaveStats = *saveStats;
auto mlazLidarList = savePoints(lidarBuffer, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
saveImuData(imuBuffer, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
saveStatusData(continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
auto lidarList = lidarClientPtr->getSerialNumberToLidarIdMapping();
auto lidarList = mlazLidarList ? *mlazLidarList : lidarClientPtr->getSerialNumberToLidarIdMapping();
saveLidarList(lidarList, continousScanDirectory, chunksInExperimentCS + chunksInExperimentSS);

if(gnssClientPtr)
Expand Down Expand Up @@ -702,13 +765,11 @@ void stateWatcher()
}
else
{
auto [fn, saveStats] = savePointcloudData(lidarBuffer, stopScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
if(saveStats)
lastFileSaveStats = *saveStats;
auto mlazLidarList = savePoints(lidarBuffer, stopScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
saveImuData(imuBuffer, stopScanDirectory, chunksInExperimentCS + chunksInExperimentSS);
saveStatusData(stopScanDirectory, chunksInExperimentCS + chunksInExperimentSS);

auto lidarList = lidarClientPtr->getSerialNumberToLidarIdMapping();
auto lidarList = mlazLidarList ? *mlazLidarList : lidarClientPtr->getSerialNumberToLidarIdMapping();
saveLidarList(lidarList, stopScanDirectory, chunksInExperimentCS + chunksInExperimentSS);

if(gnssClientPtr)
Expand Down Expand Up @@ -834,6 +895,16 @@ int main(int argc, char** argv)
mandeye::lidarSDKToUse = mandeye::configJson["lidar_sdk"].get<std::string>();
}
std::cout << "Lidar SDK to use from config: " << mandeye::lidarSDKToUse << std::endl;
{
// the web server thread already reads it
std::lock_guard<std::mutex> lock(mandeye::lastMlazMutex);
mandeye::pointFormat = mandeye::resolvePointFormat(mandeye::configJson, mandeye::lidarSDKToUse);
}
std::cout << "Point format: " << mandeye::toString(mandeye::pointFormat.format) << " (mLAZ " << MANDEYE_MLAZ_VERSION << ")" << std::endl;
if(!mandeye::pointFormat.warning.empty())
{
std::cout << "WARNING: " << mandeye::pointFormat.warning << std::endl;
}

std::thread thLivox([&]() {
{
Expand Down
36 changes: 33 additions & 3 deletions code/save_data.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "save_data.h"
#include "hardware_config/mandeye.h"
#include "save_laz.h"
#include "save_mlaz.h"
#include <chrono>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -46,12 +47,41 @@ void saveLidarList(const std::unordered_map<uint32_t, std::string>& lidars, cons
std::cout << "Savig lidar list of size " << lidars.size() << " to " << lidarFilePath << std::endl;

std::ofstream lidarStream(lidarFilePath);
lidarStream << lidarListText(lidars);
system("sync");
return;
}

std::string lidarListText(const std::unordered_map<uint32_t, std::string>& lidars)
{
std::ostringstream text;
for(const auto& [id, sn] : lidars)
{
lidarStream << id << " " << sn << "\n";
text << id << " " << sn << "\n";
}
system("sync");
return;
return text.str();
}

std::pair<std::string, std::optional<LazStats>>
savePointcloudMlaz(LidarPointsBufferPtr buffer, const std::string& directory, int chunk, const std::string& lidarList)
{
char lidarName[256];
const auto start = std::chrono::steady_clock::now();
snprintf(lidarName, 256, "lidar%04d.mlaz", chunk);
std::filesystem::path lidarFilePath = std::filesystem::path(directory) / std::filesystem::path(lidarName);
std::cout << "Savig lidar buffer of size " << buffer->size() << " to " << lidarFilePath << std::endl;
auto saveStatus = saveMlaz(lidarFilePath.string(), buffer, lidarList); // fsync + rename inside
const auto end = std::chrono::steady_clock::now();
const std::chrono::duration<float> elapsed_seconds = end - start;
if(saveStatus)
{
saveStatus->m_saveDurationSec2 = elapsed_seconds.count(); // no OnSavedLaz: one signal per chunk, the caller decides
}
else
{
std::cout << "Error saving mlaz file " << lidarFilePath << std::endl;
}
return {lidarFilePath.string(), saveStatus};
}

void saveImuData(LidarIMUBufferPtr buffer, const std::string& directory, int chunk)
Expand Down
5 changes: 5 additions & 0 deletions code/save_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
namespace mandeye
{
std::pair<std::string, std::optional<LazStats>> savePointcloudData(LidarPointsBufferPtr buffer, const std::string& directory, int chunk);
// lidarNNNN.mlaz (lossless mLAZ); lidarList = lidarListText() of the chunk, stored in the file
std::pair<std::string, std::optional<LazStats>>
savePointcloudMlaz(LidarPointsBufferPtr buffer, const std::string& directory, int chunk, const std::string& lidarList);
void saveLidarList(const std::unordered_map<uint32_t, std::string>& lidars, const std::string& directory, int chunk);
// content of lidarNNNN.sn: "<lidar id> <serial number>\n" per LiDAR
std::string lidarListText(const std::unordered_map<uint32_t, std::string>& lidars);
void saveImuData(LidarIMUBufferPtr buffer, const std::string& directory, int chunk);
void saveGnssData(std::deque<std::string>& buffer, const std::string& directory, int chunk);
void saveGnssRawData(std::deque<std::string>& buffer, const std::string& directory, int chunk);
Expand Down
Loading
Loading