Line-Following-Robot/AutonomousMode/autonomous_mode_main.cpp

67 lines
2.4 KiB
C++
Raw Normal View History

#include "lfr.h"
#include <opencv2/core/utils/logger.hpp>
2022-11-03 11:18:18 +01:00
int main(void)
{
//Disable opencv logging messages
2022-12-22 23:13:53 +01:00
//cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_WARNING);
2022-11-14 11:37:38 +01:00
const int thresholdBinary = 140;
2022-11-30 18:53:50 +01:00
const int videoHeight = 720;
const int videoWidth = 1280;
const int gaussKernelSize = 11;
2022-12-22 23:13:53 +01:00
std::mutex mutex;
LFR lfr(videoHeight, videoWidth, thresholdBinary, gaussKernelSize, [&](std::exception const &ex)
{
std::unique_lock<std::mutex> lock(mutex);
std::cerr<<"camera exception:"<<ex.what()<<std::endl;
2023-01-04 16:52:42 +01:00
return false;
2022-12-22 23:13:53 +01:00
});
//To calculate the frame rate
2023-01-04 16:08:38 +01:00
std::chrono::milliseconds last = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
std::chrono::milliseconds now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
2022-12-22 23:13:53 +01:00
cv::Mat img;
lfr.addListener([&](LFR_Result result)
{
std::unique_lock<std::mutex> lock(mutex);
if (!result.rawImage.empty())
{
2023-01-04 16:52:42 +01:00
img = result.rawImage;
//Resize to minimize latency using raspi over ssh
//cv::resize(result.rawImage, img, cv::Size(128, 64+32));
2022-12-22 23:13:53 +01:00
//Calculate frame rate
2023-01-04 16:08:38 +01:00
now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
unsigned int deltaMs = static_cast<unsigned int>((now-last).count());
double delta = static_cast<double>(deltaMs) / 1000.0;
double frameRate = 1.0 / static_cast<double>(delta);
2022-12-22 23:13:53 +01:00
2023-01-05 10:39:48 +01:00
if (result.validLane)
{
std::cout << "Frame rate: " << frameRate << " angle: " << result.data.angle
<< " motor 1: " << result.motorSignals[0] << " motor 2: " << result.motorSignals[1]
<< " motor 3: " << result.motorSignals[2] << " motor 4: " << result.motorSignals[3] <<std::endl;
}
else
{
std::cout << "No lane found." << std::endl;;
}
2022-12-22 23:13:53 +01:00
last = now;
}
}, &mutex);
lfr.startLoop();
2022-12-22 23:13:53 +01:00
for(int finished = false; finished != 'q';){
2023-01-04 16:52:42 +01:00
finished = std::tolower(cv::waitKey(1));
2022-12-22 23:13:53 +01:00
std::unique_lock<std::mutex> lock(mutex);
if(!img.empty()){
cv::imshow("frame", img);
}
}
2022-11-30 18:53:50 +01:00
}