Initial commit from DCSP - 2025/12/30 17:02:25
This commit is contained in:
28
AdHS-main.cpp
Normal file
28
AdHS-main.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "AdHS.h"
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
// 初始化AD硬件服务化
|
||||
AdHS_init(0, "AD_Hardware_Service");
|
||||
|
||||
// 测试数据: 0x55, 0xAA, 0x55, 0xAA
|
||||
uint8_t test_data[] = {0x55, 0xAA, 0x55, 0xAA};
|
||||
uint16_t data_len = sizeof(test_data);
|
||||
|
||||
std::string dest = "AD_Service";
|
||||
|
||||
int count = 0;
|
||||
while (true) {
|
||||
count++;
|
||||
|
||||
// 发送遥控数据
|
||||
AdHS_command_Pub(test_data, dest, data_len);
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
83
AdHS.cpp
Normal file
83
AdHS.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "SimMsg.h"
|
||||
#include "AdHS.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
void AdHSWriteLog(const std::string &msg)
|
||||
{
|
||||
std::cout << msg<<std::endl;
|
||||
}
|
||||
|
||||
string topic_name_cmd = "Command";
|
||||
string topic_name_tlm = "Telemetry";
|
||||
|
||||
SimMsg* AdHS_part = nullptr;
|
||||
string servername = "AD_Hardware_Service";
|
||||
|
||||
//初始化
|
||||
void AdHS_init(uint8_t domainid, std::string appname)
|
||||
{
|
||||
std::vector<std::string> parameters;
|
||||
string expression = "dest = '"+ servername + "'";
|
||||
if (nullptr == AdHS_part)
|
||||
{
|
||||
AdHS_part=new SimMsg(domainid, 3000, appname, AdHSWriteLog);
|
||||
AdHS_part->create_pub(topic_name_cmd);
|
||||
AdHS_part->create_pub(topic_name_tlm);
|
||||
|
||||
AdHS_part->create_sub(topic_name_cmd, command_callback, expression, parameters);
|
||||
AdHS_part->create_sub(topic_name_tlm, telemetry_callback, expression, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
// 遥控接收回调函数
|
||||
void command_callback(std::string src, std::string dest, std::string type,
|
||||
std::string reserve1, std::string reserve2,
|
||||
std::vector<uint8_t>& data) {
|
||||
// 打印接收到的数据
|
||||
std::cout << "Command received from " << src << " to " << dest << std::endl;
|
||||
std::cout << "Type: " << type << std::endl;
|
||||
std::cout << "Data (" << data.size() << " bytes): ";
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
std::cout << std::hex << std::uppercase << (int)data[i];
|
||||
if (i < data.size() - 1) {
|
||||
std::cout << ", ";
|
||||
}
|
||||
}
|
||||
std::cout << std::dec << std::endl;
|
||||
}
|
||||
|
||||
// 遥测接收回调函数
|
||||
void telemetry_callback(std::string src, std::string dest, std::string type,
|
||||
std::string reserve1, std::string reserve2,
|
||||
std::vector<uint8_t>& data) {
|
||||
// 打印接收到的数据
|
||||
std::cout << "Telemetry received from " << src << " to " << dest << std::endl;
|
||||
std::cout << "Type: " << type << std::endl;
|
||||
std::cout << "Data (" << data.size() << " bytes): ";
|
||||
for (size_t i = 0; i < data.size(); ++i) {
|
||||
std::cout << std::hex << std::uppercase << (int)data[i];
|
||||
if (i < data.size() - 1) {
|
||||
std::cout << ", ";
|
||||
}
|
||||
}
|
||||
std::cout << std::dec << std::endl;
|
||||
}
|
||||
|
||||
|
||||
// AD硬件服务化遥控发布
|
||||
void AdHS_command_Pub(uint8_t* data, string dest, uint16_t len)
|
||||
{
|
||||
AdHS_part->publish(topic_name_cmd, "AD_Hardware_Service", dest, "command", data, len);
|
||||
}
|
||||
|
||||
// AD硬件服务化遥测发布
|
||||
void AdHS_telemetry_Pub(uint8_t* data, string dest, uint16_t len)
|
||||
{
|
||||
AdHS_part->publish(topic_name_tlm, "AD_Hardware_Service", dest, "telemetry", data, len);
|
||||
}
|
||||
19
AdHS.h
Normal file
19
AdHS.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
void AdHSWriteLog(const std::string &msg);
|
||||
|
||||
void AdHS_init(uint8_t domainid, std::string appname);
|
||||
|
||||
void command_callback(std::string src, std::string dest, std::string type,
|
||||
std::string reserve1, std::string reserve2,
|
||||
std::vector<uint8_t>& data);
|
||||
|
||||
void telemetry_callback(std::string src, std::string dest, std::string type,
|
||||
std::string reserve1, std::string reserve2,
|
||||
std::vector<uint8_t>& data);
|
||||
|
||||
void AdHS_command_Pub(uint8_t* data, std::string dest, uint16_t len);
|
||||
|
||||
void AdHS_telemetry_Pub(uint8_t* data, std::string dest, uint16_t len);
|
||||
46
CMakeLists.txt
Normal file
46
CMakeLists.txt
Normal file
@@ -0,0 +1,46 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(AdHS)
|
||||
|
||||
# 设置C++标准
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# 设置编译选项
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread")
|
||||
|
||||
# 包含头文件路径
|
||||
include_directories(
|
||||
${CMAKE_CURRENT_SOURCE_DIR} # 当前目录
|
||||
/usr/local/include/simmsg # simmsg头文件目录
|
||||
/usr/local/include # 系统本地include
|
||||
/usr/include # 系统include
|
||||
)
|
||||
|
||||
# 链接库路径
|
||||
link_directories(
|
||||
/usr/local/lib # 系统本地lib
|
||||
/usr/lib # 系统lib
|
||||
/usr/lib/x86_64-linux-gnu # 系统64位库
|
||||
)
|
||||
|
||||
# 添加可执行文件
|
||||
add_executable(AdHS
|
||||
AdHS-main.cpp
|
||||
AdHS.cpp
|
||||
)
|
||||
|
||||
# 链接库
|
||||
target_link_libraries(AdHS
|
||||
pthread
|
||||
m
|
||||
rt
|
||||
simmsg
|
||||
fastrtps
|
||||
fastcdr
|
||||
foonathan_memory
|
||||
)
|
||||
|
||||
# 设置输出目录
|
||||
set_target_properties(AdHS PROPERTIES
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
|
||||
)
|
||||
Reference in New Issue
Block a user