Skip to content

Commit

Permalink
🔧 修改海康光源控制库示例程序
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxi-scut committed Dec 6, 2024
1 parent 4a77239 commit 36bdcc1
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 76 deletions.
14 changes: 8 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ cmake_minimum_required(VERSION 3.16)

# Build Type
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configs" FORCE)
if(NOT DEFINED CMAKE_BUILD_TYPE)
message(STATUS "'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build")
endif()
if(DEFINED CMAKE_BUILD_TYPE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${CMAKE_CONFIGURATION_TYPES}")
if(NOT MSVC)
if(NOT DEFINED CMAKE_BUILD_TYPE)
message(STATUS "'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build")
endif()
if(DEFINED CMAKE_BUILD_TYPE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${CMAKE_CONFIGURATION_TYPES}")
endif()
endif()

project(
Expand Down
8 changes: 6 additions & 2 deletions cmake/RMVLGenConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ __find_imported_modules(optlc OPTLightCtrl)
# all targets have been configured in the corresponding CMakeLists.txt, no need to configure again

# 3rdparty (download)
if(open62541_IN_3RD)
list(APPEND RMVL_MODULES_3RD_DOWNLOAD_CONFIGCMAKE "include(\$\{RMVL_INSTALL_PATH\}/lib/cmake/open62541/open62541Config.cmake)\n")
if(WITH_OPEN62541)
if(open62541_IN_3RD)
list(APPEND RMVL_MODULES_3RD_DOWNLOAD_CONFIGCMAKE "include(\$\{RMVL_INSTALL_PATH\}/lib/cmake/open62541/open62541Config.cmake)\n")
else()
list(APPEND RMVL_MODULES_3RD_DOWNLOAD_CONFIGCMAKE "find_package(open62541 REQUIRED)\n")
endif()
endif()

# --------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions cmake/RMVLModule.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ macro(rmvl_add_module _name)
${the_module}
INTERFACE ${MD_EXTRA_HEADER}
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
$<INSTALL_INTERFACE:${RMVL_INCLUDE_INSTALL_PATH}>
)
foreach(_dep ${MD_DEPENDS})
target_link_libraries(
Expand All @@ -190,7 +190,7 @@ macro(rmvl_add_module _name)
${the_module}
PUBLIC ${MD_EXTRA_HEADER}
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
$<INSTALL_INTERFACE:${RMVL_INCLUDE_INSTALL_PATH}>
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
)
foreach(_dep ${MD_DEPENDS})
Expand Down
4 changes: 3 additions & 1 deletion modules/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
find_package(Threads REQUIRED)

if(WITH_OPENCV)
rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT} ${OpenCV_LIBS})
if(WIN32)
rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT} opencv_world)
install(DIRECTORY "${OpenCV_INCLUDE_DIRS}/opencv2" DESTINATION ${RMVL_INCLUDE_INSTALL_PATH})
install(FILES ${OpenCV_DLL_PATHS} DESTINATION ${RMVL_BIN_INSTALL_PATH})
install(FILES ${OpenCV_LIB_PATHS} DESTINATION ${RMVL_3P_LIB_INSTALL_PATH})
else()
rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT} ${OpenCV_LIBS})
endif()
else()
rmvl_add_module(core EXTERNAL ${CMAKE_THREAD_LIBS_INIT})
Expand Down
2 changes: 1 addition & 1 deletion modules/core/include/rmvl/core/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ struct hash_aggregate
std::size_t retval{};
reflect::for_each(r, [&retval](const auto &val) {
// boost::hash_combine
retval = retval ^ (std::hash<std::remove_cvref_t<decltype(val)>>{}(val) << 1);
retval = retval ^ (std::hash<std::remove_cv_t<std::remove_reference_t<decltype(val)>>>{}(val) << 1);
});
return retval;
}
Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void SerialPort::Impl::open()
0,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0,
nullptr);
if (_handle == INVALID_HANDLE_VALUE)
{
Expand All @@ -113,6 +113,8 @@ void SerialPort::Impl::open()
return;
}

SetupComm(_handle, 1024, 1024);

COMMTIMEOUTS timeouts{};
if (_mode.read_mode == SerialReadMode::BLOCK)
{
Expand All @@ -126,8 +128,6 @@ void SerialPort::Impl::open()
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
}
timeouts.WriteTotalTimeoutConstant = 1;
timeouts.WriteTotalTimeoutMultiplier = 1;
if (!SetCommTimeouts(_handle, &timeouts))
{
WARNING_("Failed to set the serial port timeout.");
Expand Down
12 changes: 10 additions & 2 deletions modules/light/src/hik_light_control/light_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ HikLightController::Impl::Impl(const LightConfig &cfg, std::string_view id)
{
SerialPortMode mode{};
mode.baud_rate = BaudRate::BR_19200;
mode.read_mode = SerialReadMode::BLOCK;
mode.read_mode = SerialReadMode::NONBLOCK;
_sp = std::make_unique<SerialPort>(id, mode);
}
else
Expand Down Expand Up @@ -70,7 +70,15 @@ int HikLightController::Impl::get(int chn) const
return -1;
if (buf == "NG")
return -1;
return std::stoi(std::string(buf.begin() + 1, buf.end()));
try
{
return std::stoi(std::string(buf.begin() + 1, buf.end()));
}
catch (const std::exception &)
{
ERROR_("Failed to obtain brightness, try to increase the 'DELAY_AFTER_WRITE' parameter");
return -1;
}
}

bool HikLightController::Impl::set(int chn, int val)
Expand Down
176 changes: 117 additions & 59 deletions samples/light/hik_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <iostream>

#include <rmvl/light/hik_light_control.h>
#include <rmvlpara/light/hik_light_control.h>

static void program_help()
{
Expand All @@ -22,13 +23,17 @@ static void program_help()

static void cmd_help()
{
constexpr const char *usage = "\nCommands Usage:\n"
" help, ?, usage \033[32m# show this help message\033[0m\n"
constexpr const char *usage = "\n\033[34mCommon functions usage:\033[0m\n"
" h, help, ?, usage \033[32m# show this help message\033[0m\n"
" exit, quit, q \033[32m# exit the program\033[0m\n"
"\n\033[34mParameters control usage:\033[0m\n"
" ctl get delay \033[32m# get the delay time after writing\033[0m\n"
" ctl set delay <val> \033[32m# set the delay time after writing\033[0m\n"
"\n\033[34mCommands usage:\033[0m\n"
" open \033[32m# open all the channels\033[0m\n"
" close \033[32m# close all the channels\033[0m\n"
" get <chn> \033[32m# get the brightness of the specified channel\033[0m\n"
" set <chn> <val> \033[32m# set the brightness of the specified channel\033[0m\n"
" exit, quit, q \033[32m# exit the program\033[0m\n";
" set <chn> <val> \033[32m# set the brightness of the specified channel\033[0m\n";
std::cout << usage << std::endl;
}

Expand All @@ -54,6 +59,102 @@ static std::vector<std::string> split(std::string_view str, char delim)
return res;
}

static void parametersCtl(std::vector<std::string> &cmds)
{
if (cmds.size() < 3)
{
warning();
return;
}
if (cmds.size() == 3)
{
if (cmds[1] == "get")
{
if (cmds[2] == "delay")
std::cout << rm::para::hik_light_control_param.DELAY_AFTER_WRITE << '\n';
else
warning();
}
else
warning();
}
else if (cmds.size() == 4)
{
if (cmds[1] == "set")
{
if (cmds[2] == "delay")
{
uint32_t val = static_cast<uint32_t>(std::stoi(cmds[3]));
rm::para::hik_light_control_param.DELAY_AFTER_WRITE = val;
std::cout << "'Success'\n";
}
else
warning();
}
else
warning();
}
else
warning();
}

static void functionCtl(rm::HikLightController &lc, bool &stop, std::vector<std::string> &cmds)
{
if (cmds.size() == 1)
{
if (cmds[0] == "h" || cmds[0] == "help" || cmds[0] == "?" || cmds[0] == "usage")
cmd_help();
else if (cmds[0] == "open")
{
if (!lc.open())
std::cout << "'Failed to open all the channels.'\n";
else
std::cout << "'Success'\n";
}
else if (cmds[0] == "close")
{
if (!lc.close())
std::cout << "'Failed to close all the channels.'\n";
else
std::cout << "'Success'\n";
}
else if (cmds[0] == "exit" || cmds[0] == "quit" || cmds[0] == "q")
stop = true;
else
warning();
}
else if (cmds.size() == 2)
{
if (cmds[0] == "get")
{
int chn = std::stoi(cmds[1]);
int val = lc.get(chn);
if (val < 0)
std::cout << "'Failed to get the brightness of channel " << chn << "'\n";
else
std::cout << val << '\n';
}
else
warning();
}
else if (cmds.size() == 3)
{
if (cmds[0] == "set")
{
int chn = std::stoi(cmds[1]);
int val = std::stoi(cmds[2]);
if (!lc.set(chn, val))
std::cout << "'Failed to set the brightness of channel " << chn << "'\n";
else
std::cout << "'Success'\n";
}
else
warning();
}
else if (cmds.size() > 3)
warning();
}

int main(int argc, char *argv[])
{
if (argc != 2)
Expand All @@ -64,71 +165,28 @@ int main(int argc, char *argv[])

rm::LightConfig cfg{};
cfg.handle_mode = rm::LightHandleMode::Serial;
auto lc = rm::HikLightController(cfg, argv[1]);
std::string id{argv[1]};
auto lc = rm::HikLightController(cfg, id);
if (!lc.isOpened())
{
ERROR_("Failed to open the light controller.");
return -1;
}

while (true)
bool stop{};
while (!stop)
{
std::cout << ">>> ";
std::string cmd{};
std::getline(std::cin, cmd);
auto cmds = split(cmd, ' ');
if (cmds.size() == 1)
{
if (cmds[0] == "help" || cmds[0] == "?" || cmds[0] == "usage")
cmd_help();
else if (cmds[0] == "open")
{
if (!lc.open())
std::cout << "'Failed to open all the channels.'\n";
else
std::cout << "'Success'\n";
}
else if (cmds[0] == "close")
{
if (!lc.close())
std::cout << "'Failed to close all the channels.'\n";
else
std::cout << "'Success'\n";
}
else if (cmds[0] == "exit" || cmds[0] == "quit" || cmds[0] == "q")
break;
else
warning();
}
else if (cmds.size() == 2)
{
if (cmds[0] == "get")
{
int chn = std::stoi(cmds[1]);
int val = lc.get(chn);
if (val < 0)
std::cout << "'Failed to get the brightness of channel " << chn << "'\n";
else
std::cout << val << '\n';
}
else
warning();
}
else if (cmds.size() == 3)
{
if (cmds[0] == "set")
{
int chn = std::stoi(cmds[1]);
int val = std::stoi(cmds[2]);
if (!lc.set(chn, val))
std::cout << "'Failed to set the brightness of channel " << chn << "'\n";
else
std::cout << "'Success'\n";
}
else
warning();
}
else if (cmds.size() > 3)
warning();
if (cmds.empty())
continue;
// Parameters control
if (cmds[0] == "ctl")
parametersCtl(cmds);
// Function commands
else
functionCtl(lc, stop, cmds);
}
}

0 comments on commit 36bdcc1

Please sign in to comment.