cmvr-es/third_party/mech-eye-sdk/samples/cpp/profiler/TriggerNonStopAcquisition/TriggerNonStopAcquisition.cpp

199 lines
7.3 KiB
C++

/*******************************************************************************
*BSD 3-Clause License
*
*Copyright (c) 2016-2025, Mech-Mind Robotics
*All rights reserved.
*
*Redistribution and use in source and binary forms, with or without
*modification, are permitted provided that the following conditions are met:
*
*1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
*2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
*AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
*IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
*FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
*DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
*SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
/*
With this sample, you can acquire profile data triggered non-stop with software, and save the
resulting intensity images and depth maps.
*/
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <thread>
#include <cstdio>
#include <cmath>
#include <chrono>
#include <mutex>
#include "profiler/Profiler.h"
#include "profiler/api_util.h"
#include "profiler/parameters/ProfileProcessingParameters.h"
#include "profiler/parameters/ProfileExtractionParameters.h"
#include "profiler/parameters/RawImageParameters.h"
#include "profiler/parameters/ScanParameters.h"
namespace {
std::mutex kMutex;
constexpr int kRetrieveFrameCount = 3;
void setParameters(mmind::eye::UserSet& userSet)
{
// Set the "Data Acquisition Method" parameter to "Nonstop"
showError(userSet.setEnumValue(
mmind::eye::trigger_settings::DataAcquisitionMethod::name,
static_cast<int>(mmind::eye::trigger_settings::DataAcquisitionMethod::Value::Nonstop)));
// // Set the "Data Acquisition Trigger Source" parameter to "Software"
// showError(userSet.setEnumValue(
// mmind::eye::trigger_settings::DataAcquisitionTriggerSource::name,
// static_cast<int>(
// mmind::eye::trigger_settings::DataAcquisitionTriggerSource::Value::Software)));
// // Set the "Line Scan Trigger Source" parameter to "Fixed rate"
// showError(userSet.setEnumValue(
// mmind::eye::trigger_settings::LineScanTriggerSource::name,
// static_cast<int>(mmind::eye::trigger_settings::LineScanTriggerSource::Value::FixedRate)));
// // Set the "Software Trigger Rate" to 1000 Hz
// showError(userSet.setFloatValue(mmind::eye::trigger_settings::SoftwareTriggerRate::name,
// 1000));
}
void saveDepthMap(const mmind::eye::ProfileBatch& batch, const std::string& path)
{
if (batch.isEmpty()) {
std::cout
<< "The depth map cannot be saved because the batch does not contain any profile data."
<< std::endl;
return;
}
cv::imwrite(path, cv::Mat(batch.height(), batch.width(), CV_32FC1, batch.getDepthMap().data()));
}
void saveIntensityImage(const mmind::eye::ProfileBatch& batch, const std::string& path)
{
if (batch.isEmpty()) {
std::cout << "The intensity image cannot be saved because the batch does not contain any "
"profile data."
<< std::endl;
return;
}
cv::imwrite(path,
cv::Mat(batch.height(), batch.width(), CV_8UC1, batch.getIntensityImage().data()));
}
// Define the callback function for retrieving the profile data
void callbackFunc(const mmind::eye::ProfileBatch& batch, void* pUser)
{
std::unique_lock<std::mutex> lock(kMutex);
auto& callbackCounter = *static_cast<int*>(pUser);
if (callbackCounter >= kRetrieveFrameCount)
return;
const auto status = batch.getErrorStatus();
if (!status.isOK()) {
std::cout << "Callback batch data with error:\n";
showError(status);
}
if (batch.checkFlag(mmind::eye::ProfileBatch::BatchFlag::Incomplete))
std::cout << "Part of the batch's data is lost, the number of valid profiles is: "
<< batch.validHeight() << "." << std::endl;
std::cout << "Save the depth map and intensity image." << std::endl;
saveDepthMap(batch, "DepthMap_" + std::to_string(callbackCounter) + ".tiff");
saveIntensityImage(batch, "IntensityImage_" + std::to_string(callbackCounter) + ".png");
++callbackCounter;
}
bool acquireProfileDataUsingCallback(mmind::eye::Profiler& profiler, bool isSoftwareTrigger)
{
int callbackCounter = 0;
// Register the callback function
auto status = profiler.registerAcquisitionCallback(callbackFunc, &callbackCounter);
if (!status.isOK()) {
showError(status);
return false;
}
// Call startAcquisition() to enter the laser profiler into the acquisition ready status
std::cout << "Start data acquisition." << std::endl;
status = profiler.startAcquisition();
if (!status.isOK()) {
showError(status);
return false;
}
// Call triggerSoftware() to start the data acquisition
if (isSoftwareTrigger) {
status = profiler.triggerSoftware();
if (!status.isOK()) {
showError(status);
return false;
}
}
while (true) {
std::unique_lock<std::mutex> lock(kMutex);
if (callbackCounter >= kRetrieveFrameCount)
break;
lock.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << "Stop data acquisition." << std::endl;
status = profiler.stopAcquisition();
if (!status.isOK())
showError(status);
return status.isOK();
}
} // namespace
int main()
{
mmind::eye::Profiler profiler;
if (!findAndConnect(profiler))
return -1;
if (!confirmCapture()) {
profiler.disconnect();
return -1;
}
mmind::eye::UserSet userSet = profiler.currentUserSet();
// Set the parameters
setParameters(userSet);
int dataAcquisitionTriggerSource{};
showError(userSet.getEnumValue(mmind::eye::trigger_settings::DataAcquisitionTriggerSource::name,
dataAcquisitionTriggerSource));
bool isSoftwareTrigger =
dataAcquisitionTriggerSource ==
static_cast<int>(
mmind::eye::trigger_settings::DataAcquisitionTriggerSource::Value::Software);
// Acquire the profile data using the callback function
if (!acquireProfileDataUsingCallback(profiler, isSoftwareTrigger))
return -1;
// Disconnect from the laser profiler
profiler.disconnect();
return 0;
}