Skip to content

Commit

Permalink
修改 OPC UA 模块中方法参数结构,增加参数描述
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxi-scut committed Jul 2, 2024
1 parent 9cc8a5e commit b176af2
Show file tree
Hide file tree
Showing 21 changed files with 86 additions and 68 deletions.
4 changes: 3 additions & 1 deletion doc/tutorials/modules/tools/camera.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ int main()
软触发:

```cpp
using namespace std::chrono_literals;

int main()
{
auto camera_config = rm::CameraConfig::create(rm::GrabMode::Software, rm::RetrieveMode::OpenCV)
Expand All @@ -365,7 +367,7 @@ int main()
std::thread th([&run]() {
while (run)
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(10ms);
capture.set(rm::CAMERA_TRIGGER_SOFT); // 触发
}
});
Expand Down
4 changes: 3 additions & 1 deletion doc/tutorials/modules/tools/opcua.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,15 @@ int main()
// client_1.cpp
#include <rmvl/opcua/client.hpp>

using namespace std::chrono_literals;

int main()
{
rm::Client clt("opc.tcp://127.0.0.1:4840");
auto node = rm::nodeObjectsFolder | clt.find("number");
for (int i = 0; i < 100; ++i)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(1s);
// 写入数据,i + 200 隐式构造成了 rm::Variable
bool success = clt.write(node, i + 200);
if (!success)
Expand Down
29 changes: 24 additions & 5 deletions doc/tutorials/modules/tutorial_rmvlpara.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ string serial_number # 相机序列号

<center>表1:参数类型表

| 数据类型 | 含义 |
| :------: | :----------------------------------------------------------: |
| 基本类型 | 包括 `int``uint8_t``double``float``string` 等<br>对标 C++ 的基础类型和 `std::string` |
| 矩阵类型 | 包括形如 `Matx??``Vec?` 的类型,例如 `Matx22f`<br>对标 OpenCV 的 `cv::Matx``cv::Vec`<br>可使用列表初始化和相关静态函数初始化,例如 `Matx22f::eye()` |
| 复合类型 | 包括 `vector` 和形如 `Point?` 的类型<br>对标 C++ 的 `std::vector` 以及 OpenCV 的`cv::Point2?``cv::Point3?`<br>只能使用列表初始化,例如 `{1, 2, 3}` |
| 数据类型 | 含义 |
| :------: | :----------------------------------------------------------- |
| 基本类型 | 1. 包括 `int``uint8_t``double``float``string` 等<br />2. 对标 C++ 的基础类型和 `std::string` |
| 矩阵类型 | 1. 包括形如 `Matx??``Vec?` 的类型,例如 `Matx22f`<br>2. 对标 OpenCV 的 `cv::Matx``cv::Vec`<br>3. 可使用列表初始化和相关静态函数初始化,例如 `Matx22f::eye()` |
| 复合类型 | 1. 包括 `vector` 和形如 `Point?` 的类型<br>2. 对标 C++ 的 `std::vector` 以及 OpenCV 的`cv::Point2?``cv::Point3?`<br>3. 只能使用列表初始化,例如 `{1, 2, 3}` |
| 枚举类型 | 1. 需要用户自定以 `enum` 开头和 `endenum` 结尾的数据类型声明<br />2. 对标 C++ 的有作用域枚举类型 `enum class`<br />3. 变量的定义上与有作用域枚举类型一致,例如 `Color COLOR_MODE = Color::RED` |

</center>

Expand All @@ -70,6 +71,19 @@ float gain = 64 # 相机数字增益
@note
- `#` 和注释信息之间<u>**不必**</u>使用空格分隔,例如 `#相机曝光`

枚举类型可在每个枚举项后加上具体的值,也能使用 `#` 为枚举项添加注释,例如

```
enum ColorMode # 颜色类型
RED # 红色
GREEN # 绿色
BLUE = 4 # 蓝色
GRAY = 5
endenum
ColorMode COLOR = ColorMode::RED # 颜色信息
```

### 3. C++ 代码生成

`*.para` 到对应的 C++ 代码生成的过程依赖 RMVL 提供的两条 CMake 函数,分别是
Expand Down Expand Up @@ -159,6 +173,8 @@ rmvl_generate_module_para(m)

### 4. 运行时加载

#### 4.1 加载方式

例如,对于以下的 `test.para` 文件

```
Expand Down Expand Up @@ -197,3 +213,6 @@ inline TestParam test_param;
rm::para::core_param.load(prefix_path + "core.yml");
/* code */
```

#### 4.2 YAML 文件格式

5 changes: 3 additions & 2 deletions modules/camera/src/hik_camera/hik_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

#include <unistd.h>
#include <thread>
#include <unordered_set>

#include <opencv2/imgproc.hpp>
Expand Down Expand Up @@ -269,9 +269,10 @@ bool HikCamera::Impl::read(cv::OutputArray image) noexcept

bool HikCamera::Impl::reconnect() noexcept
{
using namespace std::chrono_literals;
INFO_("hik - camera device reconnect");
release();
sleep(1);
std::this_thread::sleep_for(100ms);
open();
return true;
}
Expand Down
6 changes: 4 additions & 2 deletions modules/camera/src/mv_camera/mv_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

#include <unistd.h>
#include <thread>

#include <opencv2/imgproc.hpp>

Expand Down Expand Up @@ -177,9 +177,11 @@ bool MvCamera::Impl::retrieve(cv::OutputArray image) noexcept

bool MvCamera::Impl::reconnect() noexcept
{
using namespace std::chrono_literals;
INFO_("mv - camera device reconnect");
release();
sleep(1);
std::this_thread::sleep_for(100ms);

// 重置相机数量
_camera_counts = 8;
open();
Expand Down
3 changes: 2 additions & 1 deletion modules/camera/src/opt_camera/opt_camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ void OptCamera::Impl::release() noexcept

bool OptCamera::Impl::reconnect() noexcept
{
using namespace std::chrono_literals;
WARNING_("opt - Reconnecting...");
release();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
std::this_thread::sleep_for(200ms);
return open();
}

Expand Down
6 changes: 3 additions & 3 deletions modules/light/include/rmvl/light/opt_light_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OPTLightController
//! 禁止从 OPTLightController 对象复制构造
OPTLightController(const OPTLightController &) = delete;
//! 移动 OPTLightController 对象
OPTLightController(OPTLightController &&obj);
OPTLightController(OPTLightController &&obj) = default;

//! 析构 OPTLightController 对象
~OPTLightController() { disconnect(); }
Expand Down Expand Up @@ -104,7 +104,7 @@ class OPTLightController
* @param[in] channel 指定通道
* @return 若读取成功,返回 \f$[0, 255]\f$ 的值,否则返回 \f$-1\f$
*/
int getIntensity(int channel);
int getIntensity(int channel) const;

/**
* @brief 设置指定通道的光源亮度
Expand All @@ -122,7 +122,7 @@ class OPTLightController
* @param[in] time 触发时间,单位: 10ms
* @return 是否成功触发?
*/
bool trigger(int channel, int time);
bool trigger(int channel, int time) const;
};

//! @} opt_light_control
Expand Down
15 changes: 4 additions & 11 deletions modules/light/src/opt_light_control/light_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

#include "rmvl/light/opt_light_control.h"

rm::OPTLightController::OPTLightController(rm::OPTLightController &&obj)
{
_handle = obj._handle;
obj._handle = {};
_init = obj._init;
obj._init = false;
}

bool rm::OPTLightController::connect(const LightIpConfig &ip_config)
{
if (_init)
Expand All @@ -34,12 +26,13 @@ bool rm::OPTLightController::connect(const LightIpConfig &ip_config)

bool rm::OPTLightController::connect(std::string_view SN)
{
using namespace std::chrono_literals;
if (_init)
disconnect();
_init = true;
if (OPTController_CreateEtheConnectionBySN(const_cast<char *>(SN.data()), &_handle) == OPT_SUCCEED)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::this_thread::sleep_for(1ms);
return true;
}
else
Expand Down Expand Up @@ -80,7 +73,7 @@ bool rm::OPTLightController::closeAllChannels()
return OPTController_TurnOffChannel(_handle, 0) == OPT_SUCCEED;
}

int rm::OPTLightController::getIntensity(int channel)
int rm::OPTLightController::getIntensity(int channel) const
{
int intensity;
return OPTController_ReadIntensity(_handle, channel, &intensity) == OPT_SUCCEED ? intensity : -1;
Expand All @@ -91,4 +84,4 @@ bool rm::OPTLightController::setIntensity(int channel, int intensity)
return OPTController_SetIntensity(_handle, channel, intensity) == OPT_SUCCEED;
}

bool rm::OPTLightController::trigger(int channel, int time) { return OPTController_SoftwareTrigger(_handle, channel, time) == OPT_SUCCEED; }
bool rm::OPTLightController::trigger(int channel, int time) const { return OPTController_SoftwareTrigger(_handle, channel, time) == OPT_SUCCEED; }
3 changes: 0 additions & 3 deletions modules/ml/src/ort/classification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
*
*/

#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>

#include "rmvl/core/util.hpp"
#include "rmvl/ml/ort.h"

Expand Down
5 changes: 0 additions & 5 deletions modules/ml/src/ort/ort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
*
*/

#include <algorithm>
#include <numeric>

#include <opencv2/imgproc.hpp>

#include "rmvl/core/util.hpp"
#include "rmvl/ml/ort.h"

Expand Down
20 changes: 5 additions & 15 deletions modules/opcua/include/rmvl/opcua/method.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,18 @@ namespace rm
//! OPC UA 方法参数
struct Argument final
{
std::string name; //!< 方法名
UA_TypeFlag data_type{}; //!< 参数数据类型 @note 形如 `UA_TYPES_<xxx>` 的类型标志位
uint32_t dims{1U}; //!< 参数维数 @warning 不能为 `0`

Argument() = default;

/**
* @brief 构造 `rm::Argument` 方法参数
*
* @param[in] n 方法名
* @param[in] dt 参数数据类型
* @param[in] dm 参数维数(默认为 `1`)
*/
Argument(std::string_view n, UA_TypeFlag dt, uint32_t dm = 1) : name(n), data_type(dt), dims(dm) {}
std::string name; //!< 参数名称
UA_TypeFlag data_type{}; //!< 参数数据类型 @note 形如 `UA_TYPES_<xxx>` 的类型标志位
uint32_t dims{1U}; //!< 参数维数,单数据则是 `1`,数组则是数组长度 @warning 不能为 `0`
std::string description{}; //!< 参数描述
};

//! OPC UA 方法
struct Method final
{
//! 命名空间索引,默认为 `1`
uint16_t ns{1U};

/**
* @brief 浏览名称 BrowseName
* @brief
Expand Down
2 changes: 1 addition & 1 deletion modules/opcua/src/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ UA_Argument cvtArgument(const Argument &arg)
UA_Argument argument;
UA_Argument_init(&argument);
argument.name = UA_STRING(to_char(arg.name));
argument.description = UA_LOCALIZEDTEXT(zh_CN(), to_char(arg.name));
argument.description = UA_LOCALIZEDTEXT(zh_CN(), to_char(arg.description));
argument.dataType = UA_TYPES[arg.data_type].typeId;
RMVL_Assert(arg.dims);
if (arg.dims == 1)
Expand Down
2 changes: 1 addition & 1 deletion modules/opcua/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Server::Server(uint16_t port, std::string_view name, const std::vector<UserConfi
UA_ServerConfig *config = UA_Server_getConfig(_server);
// 修改日志
#if OPCUA_VERSION < 10400
config->logger = UA_Log_Stdout_withLevel(UA_LOGLEVEL_INFO);
config->logger = UA_Log_Stdout_withLevel(UA_LOGLEVEL_ERROR);
#else
config->logging = UA_Log_Stdout_new(UA_LOGLEVEL_ERROR);
#endif
Expand Down
8 changes: 5 additions & 3 deletions modules/opcua/test/test_opcua_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
namespace rm_test
{

using namespace std::chrono_literals;

void setSvr(rm::Server &svr)
{
// 添加单变量节点
Expand Down Expand Up @@ -50,7 +52,7 @@ void setSvr(rm::Server &svr)
svr.addMethodNode(method);
// 添加对象节点,包含字符串变量和乘法方法
svr.start();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(10ms);
}

// 路径搜索
Expand Down Expand Up @@ -122,7 +124,7 @@ TEST(OPC_UA_ClientTest, variable_monitor)
EXPECT_TRUE(client.monitor(node_id, onChange, 5));
// 数据更新
client.write(node_id, 66);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(10ms);
client.spinOnce();
EXPECT_EQ(type_name, "Int32");
EXPECT_EQ(receive_data, 66);
Expand Down Expand Up @@ -166,7 +168,7 @@ TEST(OPC_UA_ClientTest, event_monitor)
event["aaa"] = 66;
svr.triggerEvent(rm::nodeServer, event);
client.spinOnce();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::this_thread::sleep_for(10ms);
EXPECT_EQ(source_name, "GtestServer");
EXPECT_EQ(aaa, 66);
svr.stop();
Expand Down
4 changes: 3 additions & 1 deletion modules/opcua/test/test_opcua_pubsub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
namespace rm_test
{

using namespace std::chrono_literals;

TEST(OPC_UA_PubSub, pubsub_config)
{
// 创建发布者
Expand All @@ -36,7 +38,7 @@ TEST(OPC_UA_PubSub, pubsub_config)
EXPECT_EQ(nodes.size(), 1);

pub.write(node_id, 3.4);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(100ms);
auto sub_val = sub.read(nodes[0]);
EXPECT_EQ(sub_val.cast<double>(), 3.4);

Expand Down
6 changes: 4 additions & 2 deletions samples/camera/hik/sample_hik_manual_calib.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <iostream>
#include <unistd.h>
#include <thread>

#include <opencv2/calib3d.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include "rmvl/camera/hik_camera.h"

using namespace std::chrono_literals;

// 内参矩阵
static cv::Matx<double, 3, 3> cameraMatrix = {1250, 0, 640,
0, 1250, 512,
Expand Down Expand Up @@ -98,7 +100,7 @@ int main()
cv::createTrackbar("畸变 4", "控制面板", nullptr, 1000, distCoeffCallBack, &(dist_pose.at(4)));
cv::setTrackbarPos("畸变 4", "控制面板", distCoeffs(4, 0) * 5000 + 500);

sleep(1);
std::this_thread::sleep_for(1s);

printf("Press the 's' key to save the parameters to the yaml file: \033[33m%s\033[0m\n", file_name);

Expand Down
6 changes: 4 additions & 2 deletions samples/camera/hik/sample_hik_mono.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <unistd.h>
#include <thread>

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

#include "rmvl/camera/hik_camera.h"

using namespace std::chrono_literals;

static int exposure = 1000;
static int gain = 0;
static int r_gain = 1200;
Expand Down Expand Up @@ -56,7 +58,7 @@ int main()
cv::createTrackbar("蓝通道", "控制面板", nullptr, 3000, bGainCallBack, nullptr);
cv::setTrackbarPos("蓝通道", "控制面板", b_gain);

sleep(1);
std::this_thread::sleep_for(1s);

[[maybe_unused]] int ch = system("clear");

Expand Down
Loading

0 comments on commit b176af2

Please sign in to comment.