update directory
This commit is contained in:
parent
08dfcd82a6
commit
949b5f6676
@ -93,9 +93,8 @@ target_link_libraries(cmvr_es PRIVATE
|
||||
jsoncpp_lib
|
||||
cmvr_es::utils
|
||||
cmvr_es::service
|
||||
cmvr_es::monitor
|
||||
cmvr_es::monitor_manager
|
||||
cmvr_es::hardware
|
||||
cmvr_es::httpclient
|
||||
cmvr_es::device::canbus
|
||||
cmvr_es::device::ti5motor
|
||||
cmvr_es::ctrl::controller
|
||||
|
||||
@ -13,8 +13,8 @@
|
||||
#include <cmath>
|
||||
#include <iomanip>
|
||||
|
||||
#include "utils/dynamics/robot.h"
|
||||
#include "utils/controller/cartesian_controller.h"
|
||||
#include "../src/utils/dynamics/include/robot.h"
|
||||
#include "../src/utils/controller/include/cartesian_controller.h"
|
||||
|
||||
// ---------- CONFIG ---------------------------------------------------------
|
||||
constexpr int DOF = 14; // robot DOF
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include <cstdlib> // for std::atof
|
||||
|
||||
#include <glog/logging.h>
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../src/device_manager/include/device_manager.h"
|
||||
#include <libgen.h>
|
||||
|
||||
using namespace cmvr::device;
|
||||
|
||||
@ -10,8 +10,8 @@
|
||||
#include <pybind11/stl.h>
|
||||
#include <mutex>
|
||||
|
||||
#include "devices/abstract_robot.h"
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../src/devices/robot/abstract_robot.h"
|
||||
#include "../src/device_manager/include/device_manager.h"
|
||||
#include "cmvr/msgs/geometry.pb.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
|
||||
#include "utils/dynamics/robot.h"
|
||||
#include "../src/utils/dynamics/include/robot.h"
|
||||
|
||||
constexpr int DOF = 14;
|
||||
using RobotT = cmvr::dyn::Robot<DOF>;
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
// Created by xtkuang on 2025/7/31.
|
||||
//
|
||||
|
||||
#include "utils/dynamics/robot.h"
|
||||
#include "utils/controller/cartesian_controller.h"
|
||||
#include "../src/utils/dynamics/include/robot.h"
|
||||
#include "../src/utils/controller/include/cartesian_controller.h"
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include <cstdlib> // for std::atof
|
||||
|
||||
#include <glog/logging.h>
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../src/device_manager/include/device_manager.h"
|
||||
#include <libgen.h>
|
||||
|
||||
using namespace cmvr::device;
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
#include <cstdlib> // for std::atof
|
||||
|
||||
#include <glog/logging.h>
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../src/device_manager/include/device_manager.h"
|
||||
#include <libgen.h>
|
||||
|
||||
using namespace cmvr::device;
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
// Created by xtkuang on 2025/7/9.
|
||||
//
|
||||
|
||||
#include "utils/dynamics/robot.h"
|
||||
#include "utils/controller/cartesian_controller.h"
|
||||
#include "../src/utils/dynamics/include/robot.h"
|
||||
#include "../src/utils/controller/include/cartesian_controller.h"
|
||||
|
||||
constexpr int DOF = 14;
|
||||
using RobotT = cmvr::dyn::Robot<DOF>;
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
//
|
||||
// Created by xtkuang on 2025/5/6.
|
||||
//
|
||||
|
||||
#ifndef CMVR_ES_CAN_INTERFACE_H
|
||||
#define CMVR_ES_CAN_INTERFACE_H
|
||||
|
||||
namespace cmvr {
|
||||
|
||||
class can_interface {
|
||||
|
||||
};
|
||||
|
||||
} // cmvr
|
||||
|
||||
#endif //CMVR_ES_CAN_INTERFACE_H
|
||||
@ -1,14 +0,0 @@
|
||||
//
|
||||
// Created by linbo on 2025/9/24.
|
||||
//
|
||||
|
||||
#ifndef CMVR_ES_IO_INTERFACE_H
|
||||
#define CMVR_ES_IO_INTERFACE_H
|
||||
|
||||
|
||||
class io_interface
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
#endif //CMVR_ES_IO_INTERFACE_H
|
||||
@ -1,88 +0,0 @@
|
||||
//
|
||||
// Created by linbo on 2025/6/24.
|
||||
//
|
||||
|
||||
#ifndef HTTPCLIENT_H
|
||||
#define HTTPCLIENT_H
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
// 控制是否使用libcurl的宏
|
||||
#ifdef USE_LIBCURL
|
||||
#include <curl/curl.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace cmvr {
|
||||
|
||||
// HTTP响应结构
|
||||
struct HttpResponse {
|
||||
int statusCode;
|
||||
std::string statusText;
|
||||
std::map<std::string, std::string> headers;
|
||||
std::string body;
|
||||
};
|
||||
|
||||
// HTTP异常类
|
||||
class HttpException : public std::runtime_error {
|
||||
public:
|
||||
HttpException(const std::string& message) : std::runtime_error(message) {}
|
||||
};
|
||||
|
||||
// HTTP客户端接口
|
||||
class HttpClientImpl {
|
||||
public:
|
||||
virtual ~HttpClientImpl() = default;
|
||||
|
||||
virtual void setBaseUrl(const std::string& baseUrl) = 0;
|
||||
virtual void setTimeout(int timeout) = 0;
|
||||
virtual void addHeader(const std::string& key, const std::string& value) = 0;
|
||||
virtual void clearHeaders() = 0;
|
||||
|
||||
virtual HttpResponse get(const std::string& path,
|
||||
const std::map<std::string, std::string>& params = {}) = 0;
|
||||
|
||||
virtual HttpResponse post(const std::string& path,
|
||||
const std::map<std::string, std::string>& params = {},
|
||||
const std::string& body = "",
|
||||
const std::string& contentType = "application/x-www-form-urlencoded") = 0;
|
||||
|
||||
virtual HttpResponse postJson(const std::string& path,
|
||||
const std::map<std::string, std::string>& params = {},
|
||||
const std::string& jsonBody = "") = 0;
|
||||
};
|
||||
|
||||
// HTTP客户端类
|
||||
class HttpClient {
|
||||
public:
|
||||
HttpClient();
|
||||
~HttpClient() = default;
|
||||
|
||||
void setBaseUrl(const std::string& baseUrl);
|
||||
void setTimeout(int timeout);
|
||||
void addHeader(const std::string& key, const std::string& value);
|
||||
void clearHeaders();
|
||||
|
||||
HttpResponse get(const std::string& path,
|
||||
const std::map<std::string, std::string>& params = {});
|
||||
|
||||
HttpResponse post(const std::string& path,
|
||||
const std::map<std::string, std::string>& params = {},
|
||||
const std::string& body = "",
|
||||
const std::string& contentType = "application/x-www-form-urlencoded");
|
||||
|
||||
HttpResponse postJson(const std::string& path,
|
||||
const std::map<std::string, std::string>& params = {},
|
||||
const std::string& jsonBody = "");
|
||||
|
||||
private:
|
||||
std::unique_ptr<HttpClientImpl> m_impl;
|
||||
};
|
||||
|
||||
}
|
||||
#endif //HTTPCLIENT_H
|
||||
@ -1,10 +1,10 @@
|
||||
add_subdirectory(utils)
|
||||
add_subdirectory(hardware)
|
||||
add_subdirectory(devices)
|
||||
add_subdirectory(monitor)
|
||||
add_subdirectory(device_manager)
|
||||
add_subdirectory(monitor)
|
||||
add_subdirectory(monitor_manager)
|
||||
add_subdirectory(service)
|
||||
add_subdirectory(http)
|
||||
add_subdirectory(controller)
|
||||
add_subdirectory(planner)
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ find_package(glog REQUIRED)
|
||||
find_package(protobuf REQUIRED)
|
||||
|
||||
add_library(controller SHARED
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/touch_controller.cpp
|
||||
src/touch_controller.cpp
|
||||
)
|
||||
|
||||
target_include_directories(controller PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
@ -36,7 +36,7 @@ link_directories(
|
||||
|
||||
|
||||
add_executable(touch_controller_test
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/touch_controller_test.cpp
|
||||
src/touch_controller_test.cpp
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -6,9 +6,9 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "devices/abstract_camera.h"
|
||||
#include "devices/abstract_dexhand.h"
|
||||
#include "devices/abstract_robot.h"
|
||||
#include "../../devices/camera/abstract_camera.h"
|
||||
#include "../../devices/dexhand/abstract_dexhand.h"
|
||||
#include "../../devices/robot/abstract_robot.h"
|
||||
#include "cmvr/msgs/geometry.pb.h"
|
||||
#include "librealsense2/rs.h"
|
||||
#include "librealsense2/h/rs_frame.h"
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by lgv on 2025/8/24.
|
||||
//
|
||||
|
||||
#include "controller/touch_controller.h"
|
||||
#include "../include/touch_controller.h"
|
||||
|
||||
using namespace cmvr::ctrl;
|
||||
using namespace cmvr::msgs;
|
||||
@ -3,9 +3,9 @@
|
||||
//
|
||||
|
||||
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../../device_manager/include/device_manager.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "controller/touch_controller.h"
|
||||
#include "../include/touch_controller.h"
|
||||
#include "cmvr/msgs/geometry.pb.h"
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#ifndef CMVR_ES_MOTORS_INFO_H
|
||||
#define CMVR_ES_MOTORS_INFO_H
|
||||
|
||||
#include "devices/abstract_canbus.h"
|
||||
#include "../../devices/canbus/abstract_canbus.h"
|
||||
#include "canbus/can_comm/can_sender.h"
|
||||
#include "canbus/can_comm/can_receiver.h"
|
||||
#include "canbus/can_comm/message_manager.h"
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
add_library(device_manager STATIC
|
||||
device_factory.cpp
|
||||
device_manager.cpp
|
||||
system_monitor.cpp
|
||||
src/device_factory.cpp
|
||||
src/device_manager.cpp
|
||||
src/system_monitor.cpp
|
||||
)
|
||||
|
||||
target_include_directories(device_manager PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
@ -6,15 +6,15 @@
|
||||
#define CMVR_ES_DEVICE_FACTORY_H
|
||||
#pragma once
|
||||
|
||||
#include "devices/abstract_agv.h"
|
||||
#include "devices/abstract_battery.h"
|
||||
#include "devices/abstract_camera.h"
|
||||
#include "devices/abstract_dexhand.h"
|
||||
#include "devices/abstract_gripper.h"
|
||||
#include "devices/abstract_robot.h"
|
||||
#include "devices/abstract_microphone.h"
|
||||
#include "devices/abstract_speaker.h"
|
||||
#include "devices/abstract_biohead.h"
|
||||
#include "../../devices/agv/abstract_agv.h"
|
||||
#include "../../devices/battery/abstract_battery.h"
|
||||
#include "../../devices/camera/abstract_camera.h"
|
||||
#include "../../devices/dexhand/abstract_dexhand.h"
|
||||
#include "../../devices/gripper/abstract_gripper.h"
|
||||
#include "../../devices/robot/abstract_robot.h"
|
||||
#include "../../devices/microphone/abstract_microphone.h"
|
||||
#include "../../devices/speaker/abstract_speaker.h"
|
||||
#include "../../devices/biohead/abstract_biohead.h"
|
||||
|
||||
namespace cmvr::device {
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
#include <unordered_map>
|
||||
#include "device_factory.h"
|
||||
#include "system_monitor.h"
|
||||
#include "../utils/base/thread_pool.h"
|
||||
#include "../../utils/base/include/thread_pool.h"
|
||||
|
||||
namespace cmvr::device {
|
||||
|
||||
@ -13,7 +13,7 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <glog/logging.h>
|
||||
#include "../utils/base/timer.h"
|
||||
#include "../../utils/base/include/timer.h"
|
||||
#include "rapidxml/xml_parser.h"
|
||||
|
||||
namespace cmvr::device {
|
||||
@ -2,15 +2,15 @@
|
||||
// Created by xtkuang on 2025/5/13.
|
||||
//
|
||||
|
||||
#include "device_manager/device_factory.h"
|
||||
#include "biohead/biohead_esp32/biohead_esp32.h"
|
||||
#include "camera/uvc_camera/uvc_camera.h"
|
||||
#include "camera/mechmind/mechmind_camera.h"
|
||||
#include "speaker/ffmpeg_speaker/ffmpeg_speaker.h"
|
||||
#include "microphone/ffmpeg_microphone/ffmpeg_microphone.h"
|
||||
#include "camera/realsense_camera/realsense_camera.h"
|
||||
#include "dexhand/rh56dftp_dexhand/rh56dftp_dexhand.h"
|
||||
#include "robot/humanoid_robot/humanoid_robot.h"
|
||||
#include "../include/device_factory.h"
|
||||
#include "../../devices/biohead/biohead_esp32/include/biohead_esp32.h"
|
||||
#include "../../devices/camera/uvc_camera/include/uvc_camera.h"
|
||||
#include "../../devices/camera/mechmind/include/mechmind_camera.h"
|
||||
#include "../../devices/speaker/ffmpeg_speaker/include/ffmpeg_speaker.h"
|
||||
#include "../../devices/microphone/ffmpeg_microphone/include/ffmpeg_microphone.h"
|
||||
#include "../../devices/camera/realsense_camera/include/realsense_camera.h"
|
||||
#include "../../devices/dexhand/rh56dftp_dexhand/include/rh56dftp_dexhand.h"
|
||||
#include "../../devices/robot/humanoid_robot/include/humanoid_robot.h"
|
||||
//#include "robot/ti5_robot/ti5_robot.h"
|
||||
#include "data_center/include/motors_info.h"
|
||||
using namespace std;
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by xtkuang on 2025/5/6.
|
||||
//
|
||||
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../include/device_manager.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace cmvr::device;
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by xtkuang on 2025/5/13.
|
||||
//
|
||||
|
||||
#include "../../include/device_manager/system_monitor.h"
|
||||
#include "../include/system_monitor.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace cmvr::device;
|
||||
@ -6,7 +6,7 @@
|
||||
#define CMVR_ES_ABSTRACT_AGV_H
|
||||
#pragma once
|
||||
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
|
||||
namespace cmvr::device{
|
||||
class AbstractAGV: public AbstractDevice {
|
||||
@ -6,7 +6,7 @@
|
||||
#define CMVR_ES_ABSTRACT_BATTERY_H
|
||||
#pragma once
|
||||
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
|
||||
namespace cmvr::device{
|
||||
class AbstractBattery: public AbstractDevice {
|
||||
@ -1,7 +1,7 @@
|
||||
#ifndef ABSTRACT_BIOHEAD_H
|
||||
#define ABSTRACT_BIOHEAD_H
|
||||
#pragma once
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
|
||||
namespace cmvr::device {
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
# 定义库目标(名称统一为 head_esp32)
|
||||
add_library(head_esp32 SHARED biohead_esp32.cpp)
|
||||
add_library(head_esp32 SHARED src/biohead_esp32.cpp)
|
||||
|
||||
# 设置头文件包含路径
|
||||
target_include_directories(head_esp32 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#ifndef CMVR_ES_BIOHEAD_ESP32_H
|
||||
#define CMVR_ES_BIOHEAD_ESP32_H
|
||||
|
||||
#include "devices/abstract_biohead.h"
|
||||
#include "hardware/esp32_serial_port.h"
|
||||
#include "../../abstract_biohead.h"
|
||||
#include "../../../../hardware/include/esp32_serial_port.h"
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
@ -1,4 +1,4 @@
|
||||
#include "biohead_esp32.h"
|
||||
#include "../include/biohead_esp32.h"
|
||||
#include <glog/logging.h>
|
||||
#include <cmath>
|
||||
#include <thread>
|
||||
@ -3,7 +3,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
#include <Eigen/Core>
|
||||
|
||||
namespace cmvr::device {
|
||||
@ -1,6 +1,6 @@
|
||||
set(MECH_EYE_SDK_PATH /opt/mech-mind/mech-eye-sdk)
|
||||
|
||||
add_library(mechmind mechmind_camera.cpp)
|
||||
add_library(mechmind src/mechmind_camera.cpp)
|
||||
|
||||
target_include_directories(mechmind PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
|
||||
#include <mutex>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "devices/abstract_camera.h"
|
||||
#include "../../abstract_camera.h"
|
||||
#include "area_scan_3d_camera/Camera.h"
|
||||
#include "area_scan_3d_camera/api_util.h"
|
||||
#include "area_scan_3d_camera/Mapping2DToDepth.h"
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by xtkuang on 2025/6/4.
|
||||
//
|
||||
|
||||
#include "mechmind_camera.h"
|
||||
#include "../include/mechmind_camera.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace cmvr::device;
|
||||
@ -1,4 +1,4 @@
|
||||
add_library(realsense_camera SHARED realsense_camera.cpp)
|
||||
add_library(realsense_camera SHARED src/realsense_camera.cpp)
|
||||
|
||||
target_include_directories(realsense_camera PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
@ -29,7 +29,7 @@ link_directories(
|
||||
|
||||
|
||||
add_executable(realsense_camera_test
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/realsense_camera_test.cpp
|
||||
src/realsense_camera_test.cpp
|
||||
)
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#ifndef REALSENSE_CAMERA_H
|
||||
#define REALSENSE_CAMERA_H
|
||||
|
||||
#include "camera/uvc_camera/uvc_camera.h"
|
||||
#include "../../uvc_camera/include/uvc_camera.h"
|
||||
#include <librealsense2/rs.hpp>
|
||||
#include <librealsense2/hpp/rs_internal.hpp>
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by linbo on 2025/6/18.
|
||||
//
|
||||
|
||||
#include "realsense_camera.h"
|
||||
#include "../include/realsense_camera.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace cmvr::device;
|
||||
@ -2,9 +2,9 @@
|
||||
// Created by lgv on 2025/9/1.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include "devices/abstract_camera.h"
|
||||
#include "../../abstract_camera.h"
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../../../../device_manager/include/device_manager.h"
|
||||
using namespace cmvr::device;
|
||||
// 测试: 实时显示 RGB + Depth
|
||||
TEST(RealsenseCameraRealDeviceTest, SaveFrames) {
|
||||
@ -1,6 +1,6 @@
|
||||
find_package(OpenCV REQUIRED)
|
||||
|
||||
add_library(uvc_camera SHARED uvc_camera.cpp)
|
||||
add_library(uvc_camera SHARED src/uvc_camera.cpp)
|
||||
|
||||
target_include_directories(uvc_camera PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
||||
@ -5,14 +5,14 @@
|
||||
#ifndef CMVR_ES_UVC_CAMERA_H
|
||||
#define CMVR_ES_UVC_CAMERA_H
|
||||
|
||||
#include "../../../../include/utils/base/ring_buffer.h"
|
||||
#include "devices/abstract_camera.h"
|
||||
#include "../../../../utils/base/include/ring_buffer.h"
|
||||
#include "../../abstract_camera.h"
|
||||
|
||||
//使用ffmpeg来编码保存视频文件
|
||||
#define USE_FFMPEG_ENCODER 1
|
||||
|
||||
#if USE_FFMPEG_ENCODER
|
||||
#include "speaker/ffmpeg_speaker/ffmpeg_ptr.h"
|
||||
#include "../../../speaker/ffmpeg_speaker/include/ffmpeg_ptr.h"
|
||||
#endif
|
||||
|
||||
namespace cmvr::device {
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by xtkuang on 2025/5/30.
|
||||
//
|
||||
|
||||
#include "uvc_camera.h"
|
||||
#include "../include/uvc_camera.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace cmvr::device;
|
||||
@ -15,6 +15,7 @@ target_link_libraries(canbus
|
||||
PRIVATE
|
||||
protobuf::libprotobuf
|
||||
glog::glog
|
||||
# proto-objects
|
||||
# PUBLIC pcanbasic
|
||||
)
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
#include "cmvr/msgs/error_code.pb.h"
|
||||
#include "canbus/common/byte.h"
|
||||
namespace cmvr::device {
|
||||
@ -28,7 +28,7 @@
|
||||
#include "cmvr/msgs/can_card_parameter.pb.h"
|
||||
|
||||
#include "gflags/gflags.h"
|
||||
#include "devices/abstract_canbus.h"
|
||||
#include "../../abstract_canbus.h"
|
||||
#include "canbus/common/canbus_consts.h"
|
||||
|
||||
#include <PCANBasic.h>
|
||||
|
||||
@ -24,7 +24,7 @@
|
||||
#include "cmvr/msgs/can_card_parameter.pb.h"
|
||||
|
||||
#include "gflags/gflags.h"
|
||||
#include "devices/abstract_canbus.h"
|
||||
#include "../../abstract_canbus.h"
|
||||
#include "canbus/common/canbus_consts.h"
|
||||
|
||||
namespace cmvr {
|
||||
|
||||
@ -28,7 +28,7 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "canbus/can_comm/protocol_data.h"
|
||||
#include "devices/abstract_canbus.h"
|
||||
#include "../abstract_canbus.h"
|
||||
#include "cmvr/msgs/error_code.pb.h"
|
||||
|
||||
namespace cmvr {
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#define CMVR_ES_ABSTRACT_DEXHAND_H
|
||||
#pragma once
|
||||
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
|
||||
namespace cmvr::device{
|
||||
// 单个触觉点数据(16位无符号整数)
|
||||
@ -1,4 +1,4 @@
|
||||
add_library(rh56dftp_dexhand SHARED rh56dftp_dexhand.cpp)
|
||||
add_library(rh56dftp_dexhand SHARED src/rh56dftp_dexhand.cpp)
|
||||
|
||||
target_include_directories(rh56dftp_dexhand PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
||||
@ -11,9 +11,9 @@
|
||||
#include <condition_variable>
|
||||
#include <modbus/modbus.h>
|
||||
#include <boost/lockfree/spsc_queue.hpp>
|
||||
#include "../../../../include/utils/base/os.h"
|
||||
#include "devices/abstract_dexhand.h"
|
||||
#include "hardware/serial_interface.h"
|
||||
#include "../../../../utils/base/include/os.h"
|
||||
#include "../../abstract_dexhand.h"
|
||||
#include "../../../../hardware/include/serial_interface.h"
|
||||
|
||||
namespace cmvr::device {
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by linbo on 2025/6/20.
|
||||
//
|
||||
|
||||
#include "rh56dftp_dexhand.h"
|
||||
#include "../include/rh56dftp_dexhand.h"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <unistd.h>
|
||||
@ -6,7 +6,7 @@
|
||||
#define CMVR_ES_ABSTRACT_GRIPPER_H
|
||||
#pragma once
|
||||
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
|
||||
namespace cmvr::device{
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
#define CMVR_ES_ABSTRACT_MICROPHONE_H
|
||||
#pragma once
|
||||
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
|
||||
namespace cmvr::device{
|
||||
class AbstractMicrophone: public AbstractDevice {
|
||||
@ -1,4 +1,4 @@
|
||||
add_library(ffmpeg_microphone SHARED ffmpeg_microphone.cpp)
|
||||
add_library(ffmpeg_microphone SHARED src/ffmpeg_microphone.cpp)
|
||||
|
||||
target_include_directories(ffmpeg_microphone PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
||||
@ -7,10 +7,10 @@
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <condition_variable>
|
||||
#include "devices/abstract_microphone.h"
|
||||
#include "../../abstract_microphone.h"
|
||||
#include <boost/lockfree/spsc_queue.hpp>
|
||||
#include "../../../../include/utils/base/os.h"
|
||||
#include "speaker/ffmpeg_speaker/ffmpeg_ptr.h"
|
||||
#include "../../../../utils/base/include/os.h"
|
||||
#include "../../../speaker/ffmpeg_speaker/include/ffmpeg_ptr.h"
|
||||
|
||||
namespace cmvr::device {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#include "ffmpeg_microphone.h"
|
||||
#include "../include/ffmpeg_microphone.h"
|
||||
using namespace std;
|
||||
using namespace cmvr::device;
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "abstract_device.h"
|
||||
#include "utils/dynamics/joint.h"
|
||||
#include "../abstract_device.h"
|
||||
#include "../../utils/dynamics/include/joint.h"
|
||||
#include "motor/motor_protocol_interface.h"
|
||||
#include <mutex>
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "devices/abstract_motor.h"
|
||||
#include "abstract_motor.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#pragma once
|
||||
#include "cmvr/msgs/motor.pb.h"
|
||||
#include "motor/motor_protocol_interface.h"
|
||||
#include "devices/abstract_canbus.h"
|
||||
#include "../../../canbus/abstract_canbus.h"
|
||||
#include "canbus/can_comm/can_receiver.h"
|
||||
#include "canbus/can_comm/can_sender.h"
|
||||
#include "canbus/can_comm/message_manager.h"
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by lgv on 2025/8/1.
|
||||
//
|
||||
|
||||
#include "devices/abstract_motor.h"
|
||||
#include "../abstract_motor.h"
|
||||
#include "motor/motor_protocol_interface.h"
|
||||
#include "motor/ti5_motor/canopen/ti5_motor_canopen_protocol.h"
|
||||
|
||||
|
||||
@ -7,8 +7,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "jsoncpp/json/json.h"
|
||||
#include "abstract_device.h"
|
||||
#include "utils/controller/cartesian_controller.h"
|
||||
#include "../abstract_device.h"
|
||||
#include "../../utils/controller/include/cartesian_controller.h"
|
||||
#include "cmvr/msgs/geometry.pb.h"
|
||||
#include "cmvr/msgs/motor.pb.h"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
find_package(protobuf REQUIRED)
|
||||
|
||||
add_library(humanoid_robot SHARED humanoid_robot.cpp)
|
||||
add_library(humanoid_robot SHARED src/humanoid_robot.cpp)
|
||||
|
||||
|
||||
target_include_directories(humanoid_robot PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
@ -41,7 +41,7 @@ link_directories(
|
||||
)
|
||||
|
||||
add_executable(humanoid_robot_test
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/humanoid_robot_test.cpp
|
||||
src/humanoid_robot_test.cpp
|
||||
)
|
||||
|
||||
target_include_directories(humanoid_robot_test PUBLIC
|
||||
|
||||
@ -5,13 +5,13 @@
|
||||
#ifndef CMVR_ES_HUMANOID_ROBOT_H
|
||||
#define CMVR_ES_HUMANOID_ROBOT_H
|
||||
|
||||
#include "devices/abstract_robot.h"
|
||||
#include "utils/controller/cartesian_controller.h"
|
||||
#include "utils/base/os.h"
|
||||
#include "utils/base/timer.h"
|
||||
#include "utils/base/ring_buffer.h"
|
||||
#include "../../abstract_robot.h"
|
||||
#include "../../../../utils/controller/include/cartesian_controller.h"
|
||||
#include "../../../../utils/base/include/os.h"
|
||||
#include "../../../../utils/base/include/timer.h"
|
||||
#include "../../../../utils/base/include/ring_buffer.h"
|
||||
|
||||
#include "devices/abstract_canbus.h"
|
||||
#include "../../../canbus/abstract_canbus.h"
|
||||
#include "canbus/can_comm/can_sender.h"
|
||||
#include "canbus/can_comm/can_receiver.h"
|
||||
#include "canbus/can_comm/message_manager.h"
|
||||
@ -2,10 +2,10 @@
|
||||
// Created by xtkuang on 2025/7/24.
|
||||
//
|
||||
|
||||
#include "humanoid_robot.h"
|
||||
#include "../include/humanoid_robot.h"
|
||||
#include "motor/ti5_motor/canopen/ti5_motor_canopen_protocol.h"
|
||||
#include "motor/ti5_motor/ti5_motor.h"
|
||||
#include "utils/base/abstract_interpolation.h"
|
||||
#include "../../../../utils/base/include/abstract_interpolation.h"
|
||||
#include "data_center/include/motors_info.h"
|
||||
|
||||
using namespace std;
|
||||
@ -13,13 +13,13 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include <glog/logging.h>
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "../../../../device_manager/include/device_manager.h"
|
||||
#include <libgen.h>
|
||||
|
||||
#include "cmvr/msgs/can_card_parameter.grpc.pb.h"
|
||||
#include "robot/humanoid_robot/humanoid_robot.h"
|
||||
#include "../include/humanoid_robot.h"
|
||||
#include "motor/ti5_motor/canopen/ti5_motor_canopen_protocol.h"
|
||||
#include "utils/base/abstract_interpolation.h"
|
||||
#include "../../../../utils/base/include/abstract_interpolation.h"
|
||||
|
||||
// 定义一个命令行参数 --config_path
|
||||
DEFINE_string(config_path, "../config/cabin_robot.xml", "Path to the robot config XML file");
|
||||
@ -1,5 +0,0 @@
|
||||
#add_library(ti5robot SHARED ti5_robot.cpp)
|
||||
#
|
||||
#target_include_directories(ti5robot PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
#
|
||||
#add_library(cmvr_es::device::ti5robot ALIAS ti5robot)
|
||||
@ -1,343 +0,0 @@
|
||||
//#include "ti5_robot.h"
|
||||
//#include <string>
|
||||
//#include <vector>
|
||||
//#include <memory>
|
||||
//#include <chrono>
|
||||
//#include <thread>
|
||||
//#include <sstream>
|
||||
//#include <cstring>
|
||||
//#include <sys/socket.h>
|
||||
//#include <arpa/inet.h>
|
||||
//#include <unistd.h>
|
||||
//#include <csignal>
|
||||
//#include <mutex>
|
||||
//#include <shared_mutex>
|
||||
//#include <termios.h>
|
||||
//#include <fcntl.h>
|
||||
//#include <algorithm>
|
||||
//#include <sys/socket.h>
|
||||
//#include <nlohmann/json.hpp>
|
||||
//
|
||||
//using json = nlohmann::json;
|
||||
//using namespace std;
|
||||
//using namespace cmvr::device;
|
||||
//// 服务端地址和端口(需与服务端保持一致)
|
||||
//#define SERVER_IP "10.148.108.142" // 服务端IP,本地测试用127.0.0.1
|
||||
//#define SERVER_PORT 8888 // 服务端端口,与服务端PORT一致
|
||||
//// 设置socket超时(单位:毫秒)
|
||||
//void set_socket_timeout(int sock, long timeout_ms) {
|
||||
// struct timeval tv;
|
||||
// tv.tv_sec = timeout_ms / 1000;
|
||||
// tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||
// setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
//}
|
||||
//
|
||||
//// 发送请求并接收响应的函数 正常返回应该是Json字符串
|
||||
//bool send_request(const string& request_str,string& result) {
|
||||
// int sock = 0;
|
||||
// struct sockaddr_in serv_addr;
|
||||
// char buffer[1024] = {0};
|
||||
//
|
||||
// // 创建socket
|
||||
// if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
// LOG(ERROR) << "[Ti5Robot] (send_request): socket create failed";
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// // 设置服务端地址信息
|
||||
// serv_addr.sin_family = AF_INET;
|
||||
// serv_addr.sin_port = htons(SERVER_PORT);
|
||||
// if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
|
||||
// LOG(ERROR) << "[Ti5Robot] (send_request): invaid ip address";
|
||||
// close(sock);
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// // 连接服务端
|
||||
// if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
// LOG(ERROR) << "[Ti5Robot] (send_request): connect server failed";
|
||||
// close(sock);
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// // 发送JSON请求
|
||||
// send(sock, request_str.c_str(), request_str.length(), 0);
|
||||
// LOG(INFO) << "[Ti5Robot] (send_request): request info:" << request_str;
|
||||
// // 设置接收超时为3秒
|
||||
// set_socket_timeout(sock, 2000);
|
||||
// // 接收服务端响应
|
||||
// ssize_t valread = read(sock, buffer, 1024);
|
||||
//
|
||||
// close(sock); // 关闭连接
|
||||
// if (valread == -1 && errno == EAGAIN) {
|
||||
// LOG(ERROR) << "[Ti5Robot] (send_request): timeout";
|
||||
// return false;
|
||||
// }
|
||||
// else if (valread <= 0) {
|
||||
// LOG(ERROR) << "[Ti5Robot] (send_request): read error:" << valread;
|
||||
// return false;
|
||||
// }
|
||||
// else {
|
||||
// result = buffer;
|
||||
// }
|
||||
// return true;
|
||||
//}
|
||||
//
|
||||
//
|
||||
//// Ti5Robot 类的实现
|
||||
//Ti5Robot::Ti5Robot(const XmlNode& cfg) : AbstractRobot(cfg) {
|
||||
// // 从配置中初始化机器人参数
|
||||
// try {
|
||||
// id_ = cfg_.getAttrString("id");
|
||||
// if (id_ == "left_arm") {
|
||||
// arm_ = Left_Arm;
|
||||
// } else if (id_ == "right_arm") {
|
||||
// arm_ = Right_Arm;
|
||||
// } else {
|
||||
// //报错
|
||||
// }
|
||||
// canInd_ = cfg_.getAttrDefault("canInd", 1);
|
||||
// deviceInd_ = cfg_.getAttrDefault("deviceInd", 1);
|
||||
// }
|
||||
// catch (const exception& e) {
|
||||
// LOG(ERROR) << "[Ti5Robot] ([Ti5Robot]): Failed to parse XML: " << e.what();
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//Ti5Robot::~Ti5Robot() {
|
||||
// LOG(INFO) << "Ti5Robot 析构函数被调用";
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::init() {
|
||||
// // 连接socket服务
|
||||
// // 检查是否可以连接到服务端。
|
||||
// // 角度范围应该在[-π,π]之间?
|
||||
// std::vector<double> goal_j(7,0.5);
|
||||
// moveJ(goal_j,0.5,0.1);
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::start() {
|
||||
// LOG(INFO) << "Ti5Robot 启动";
|
||||
// // 启动机器人运行
|
||||
// // 可以添加启动CAN通道等操作
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::stop() {
|
||||
// LOG(INFO) << "Ti5Robot 停止";
|
||||
// // 停止机器人运行
|
||||
// // 可以添加停止CAN通道等操作
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::getState(RobotState &state) {
|
||||
// // 获取机器人当前状态
|
||||
// LOG(INFO) << "[Ti5Robot](getState)";
|
||||
//
|
||||
// // 获取左右臂的关节角度
|
||||
// float currentAngles[7];
|
||||
// get_current_angle(arm_,currentAngles,canInd_,deviceInd_);
|
||||
// for (float currentAngle : currentAngles)
|
||||
// {
|
||||
// state.joint_positions.push_back(currentAngle);
|
||||
// }
|
||||
//
|
||||
// // 获取末端执行器位姿
|
||||
//// float pos[6];
|
||||
//// get_current_pose(Left_Arm, pos, 0, 0);
|
||||
//// state.ee_pose.position.x = pos[0];
|
||||
//// state.ee_pose.position.y = pos[1];
|
||||
//// state.ee_pose.position.z = pos[2];
|
||||
//// state.ee_pose.euler.rx = pos[3];
|
||||
//// state.ee_pose.euler.ry = pos[4];
|
||||
//// state.ee_pose.euler.rz = pos[5];
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::eStop() {
|
||||
// LOG(WARNING) << "[Ti5Robot::eStop]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::eStop]: The current interface is not yet implemented.");
|
||||
// LOG(INFO) << "Ti5Robot 紧急停止";
|
||||
// // 实现紧急停止逻辑
|
||||
// // 例如刹车操作
|
||||
// // brake(LEFT_ARM, 0, 0); // 假设使用左臂,设备索引0,通道索引0
|
||||
// // brake(RIGHT_ARM, 0, 0);
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::moveJ(std::vector<double> &joints, double vel, double acc) {
|
||||
// //LOG(WARNING) << "[Ti5Robot::moveJ]: The current interface is not yet implemented.";
|
||||
// //throw std::runtime_error("[Ti5Robot::moveJ]: The current interface is not yet implemented.");
|
||||
// LOG(INFO) << "Ti5Robot 关节空间运动: 关节角度=";
|
||||
// // 实现关节空间运动控制逻辑
|
||||
// float goal_j[7];
|
||||
// for (int i = 0; i < 7; ++i) {
|
||||
// goal_j[i] = static_cast<float>(joints[i]);
|
||||
// }
|
||||
// joint_to_move(arm_,goal_j,deviceInd_,canInd_);
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::moveJ_IK(math::Pose3d &pose, double vel, double acc) {
|
||||
// float pos[6] = {static_cast<float>(pose.position.x), static_cast<float>(pose.position.y), static_cast<float>(pose.position.z),
|
||||
// static_cast<float>(pose.euler.rx), static_cast<float>(pose.euler.ry), static_cast<float>(pose.euler.rz)};
|
||||
// pos_to_move(arm_, pos, 0, 0);
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::moveL(math::Pose3d &pose, double vel, double acc) {
|
||||
// LOG(WARNING) << "[Ti5Robot::moveL]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::moveL]: The current interface is not yet implemented.");
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::teachJ(RobotJointIndex joint, RobotJointIndexDirection dir, double vel, double acc) {
|
||||
// LOG(WARNING) << "[Ti5Robot::teachJ]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::teachJ]: The current interface is not yet implemented.");
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::teachL(RobotCartesian cart, RobotJointIndexDirection dir, double vel, double acc) {
|
||||
// LOG(WARNING) << "[Ti5Robot::teachL]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::teachL]: The current interface is not yet implemented.");
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::followJointTrajectory(std::vector<std::vector<double>> &traj, double dt) {
|
||||
// LOG(WARNING) << "[Ti5Robot::followPoseTrajectory]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::followPoseTrajectory]: The current interface is not yet implemented.");
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::followPoseTrajectory(std::vector<math::Pose3d> &traj, double dt) {
|
||||
// LOG(WARNING) << "[Ti5Robot::followPoseTrajectory]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::followPoseTrajectory]: The current interface is not yet implemented.");
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::servoJ(std::vector<double> &joints, double dt) {
|
||||
// LOG(WARNING) << "[Ti5Robot::servoJ]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::servoJ]: The current interface is not yet implemented.");
|
||||
//}
|
||||
//
|
||||
//void Ti5Robot::servoL(math::Pose3d &pose, double dt) {
|
||||
// LOG(WARNING) << "[Ti5Robot::servoL]: The current interface is not yet implemented.";
|
||||
// throw std::runtime_error("[Ti5Robot::servoL]: The current interface is not yet implemented.");
|
||||
//}
|
||||
//
|
||||
//bool Ti5Robot::get_current_angle(ArmType arm, float goal_j[7], int canInd, int deviceInd)
|
||||
//{
|
||||
// // 获取关节位置
|
||||
// // 获取当前关节角度
|
||||
// bool success = false;
|
||||
// json req;
|
||||
// req["interface"] = "get_current_angle"; // 接口名
|
||||
// if (arm == Left_Arm) {
|
||||
// req["parameters"]["side"] = "LEFT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// else if (arm == Right_Arm) {
|
||||
// req["parameters"]["side"] = "RIGHT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// req["parameters"]["deviceInd"] = deviceInd; // CAN设备号
|
||||
// req["parameters"]["canInd"] = canInd; // CAN通道号
|
||||
//
|
||||
// string resp_str;
|
||||
// auto ret = send_request(req.dump(),resp_str);
|
||||
// if (ret && !resp_str.empty()) {
|
||||
// json resp = json::parse(resp_str);
|
||||
// if (resp.contains("error")) {
|
||||
// cerr << "接口调用失败: " << resp["error"] << endl;
|
||||
// } else {
|
||||
// std::vector<float> angeles = resp["result"];
|
||||
// for (int i = 0; i < 7; ++i) {
|
||||
// goal_j[i] = angeles[i];
|
||||
// success = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return success;
|
||||
//}
|
||||
//
|
||||
//
|
||||
//bool Ti5Robot::get_current_pose(ArmType arm, float pos[6], int canInd, int deviceInd)
|
||||
//{
|
||||
// // 获取关节位置
|
||||
// // 获取当前关节角度
|
||||
// bool success = false;
|
||||
// json req;
|
||||
// req["interface"] = "get_current_pose"; // 接口名
|
||||
// if (arm == Left_Arm) {
|
||||
// req["parameters"]["side"] = "LEFT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// else if (arm == Right_Arm) {
|
||||
// req["parameters"]["side"] = "RIGHT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// req["parameters"]["deviceInd"] = deviceInd; // CAN设备号
|
||||
// req["parameters"]["canInd"] = canInd; // CAN通道号
|
||||
//
|
||||
// string resp_str;
|
||||
// auto ret = send_request(req.dump(),resp_str);
|
||||
// if (ret && !resp_str.empty()) {
|
||||
// json resp = json::parse(resp_str);
|
||||
// if (resp.contains("error")) {
|
||||
// cerr << "接口调用失败: " << resp["error"] << endl;
|
||||
// } else {
|
||||
// std::vector<float> result = resp["result"];
|
||||
// for (int i = 0; i < 6; ++i) {
|
||||
// pos[i] = result[i];
|
||||
// success = true;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return success;
|
||||
//}
|
||||
//
|
||||
//
|
||||
//bool Ti5Robot::joint_to_move(ArmType side, float *goal_j, int deviceInd, int canInd)
|
||||
//{
|
||||
// bool success = false;
|
||||
//
|
||||
// json req;
|
||||
// req["interface"] = "joint_to_move"; // 接口名
|
||||
// if (arm_ == Left_Arm) {
|
||||
// req["parameters"]["side"] = "LEFT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// else if (arm_ == Right_Arm) {
|
||||
// req["parameters"]["side"] = "RIGHT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// req["parameters"]["deviceInd"] = deviceInd; // CAN设备号
|
||||
// req["parameters"]["canInd"] = canInd; // CAN通道号
|
||||
// // 目标角度数组(7个关节角度,单位:弧度)
|
||||
// req["parameters"]["goal_j"] = *goal_j;
|
||||
//
|
||||
// string resp_str;
|
||||
// auto ret = send_request(req.dump(),resp_str);
|
||||
// if (ret && !resp_str.empty()) {
|
||||
// json resp = json::parse(resp_str);
|
||||
// if (resp.contains("error")) {
|
||||
// success = false;
|
||||
// } else {
|
||||
// success = true;
|
||||
// }
|
||||
// }
|
||||
// return success;
|
||||
//}
|
||||
//
|
||||
//bool Ti5Robot::pos_to_move(ArmType side, float *pos, int deviceInd, int canInd)
|
||||
//{
|
||||
// bool success = false;
|
||||
// json req;
|
||||
// req["interface"] = "pos_to_move"; // 接口名
|
||||
// if (arm_ == Left_Arm) {
|
||||
// req["parameters"]["side"] = "LEFT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// else if (arm_ == Right_Arm) {
|
||||
// req["parameters"]["side"] = "RIGHT_ARM"; // 参数:右臂
|
||||
// }
|
||||
// req["parameters"]["deviceInd"] = deviceInd_; // CAN设备号
|
||||
// req["parameters"]["canInd"] = canInd_; // CAN通道号
|
||||
// req["parameters"]["pos"] = *pos;
|
||||
//
|
||||
// string resp_str;
|
||||
// auto ret = send_request(req.dump(),resp_str);
|
||||
// if (ret && !resp_str.empty()) {
|
||||
// json resp = json::parse(resp_str);
|
||||
// if (resp.contains("error")) {
|
||||
// success = false;
|
||||
// } else {
|
||||
// success = true;
|
||||
// }
|
||||
// }
|
||||
// return success;
|
||||
//}
|
||||
@ -1,49 +0,0 @@
|
||||
//
|
||||
// Created by linbo on 2025/7/1.
|
||||
//
|
||||
|
||||
#ifndef TI5_ROBOT_H
|
||||
#define TI5_ROBOT_H
|
||||
|
||||
#include "devices/abstract_robot.h"
|
||||
|
||||
namespace cmvr::device {
|
||||
class Ti5Robot final : public AbstractRobot
|
||||
{
|
||||
public:
|
||||
explicit Ti5Robot(const XmlNode& cfg);
|
||||
~Ti5Robot() override;
|
||||
void init() override;
|
||||
void start() override;
|
||||
void stop() override;
|
||||
|
||||
void getState(RobotState &state) override;
|
||||
void eStop() override;
|
||||
void moveJ(std::vector<double> &joints, double vel=0.5, double acc=0.1) override;
|
||||
void moveJ_IK(math::Pose3d &pose, double vel=0.5, double acc=0.1) override;
|
||||
void moveL(math::Pose3d &pose, double vel=0.5, double acc=0.1) override;
|
||||
void speedJ(std::string &joint_name, RobotJointIndexDirection dir, double vel, double acc=0.5) override;
|
||||
void speedL(RobotCartesian cart, RobotJointIndexDirection dir, double vel, double acc=0.5) override;
|
||||
void followJointTrajectory(std::vector<std::vector<double>> &traj, double dt) override;
|
||||
void followPoseTrajectory(std::vector<math::Pose3d> &traj, double dt) override;
|
||||
void servoJ(std::vector<double> &joints, double dt) override;
|
||||
void servoL(math::Pose3d &pose, double dt) override;
|
||||
|
||||
|
||||
// 以下为socket调用ti5robot sdk的接口
|
||||
// bool get_current_angle(ArmType arm, float goal_j[7], int canInd,int deviceInd);
|
||||
// bool get_current_pose(ArmType arm, float pos[6], int canInd, int deviceInd);
|
||||
// bool joint_to_move(ArmType side, float *goal_j, int deviceInd, int canInd);
|
||||
// bool pos_to_move(ArmType side, float *pos, int deviceInd, int canInd);
|
||||
private:
|
||||
// ArmType arm_;
|
||||
// int canInd_;
|
||||
// int deviceInd_;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif //TI5_ROBOT_H
|
||||
@ -1,252 +0,0 @@
|
||||
#ifndef Ti5BASIC_H
|
||||
#define Ti5BASIC_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
#include <iomanip>
|
||||
#include <cstring>
|
||||
|
||||
#include "Ti5LOGIC.h"
|
||||
#include "Ti5MOVE.h"
|
||||
|
||||
// #include "can/tcontrolcanfactor.h"
|
||||
|
||||
#define MAX_IP_ADDR_LEN 256 // 存储IP地址的最大长度
|
||||
|
||||
|
||||
// extern float arr_s[6];
|
||||
|
||||
extern bool flag;
|
||||
|
||||
extern string log_path; // log文件
|
||||
|
||||
extern char LogInfo[100]; // 存储写入log文件的信息
|
||||
|
||||
extern char Info_Str[100]; // 定义一个字符数组用于存储 flag 的字符串表示
|
||||
class ArmController; // 声明ArmController类
|
||||
|
||||
extern uint8_t l_id[IDNUM], r_id[IDNUM]; // 声明左臂和右臂的canID数组
|
||||
extern ArmController l_controller; // 声明TH_L对象
|
||||
extern ArmController r_controller; // 声明TH_R对象
|
||||
|
||||
extern class humanoidLeftArm l_solver; // 数学上的解算器
|
||||
extern class humanoidRightArm r_solver; // 解算器
|
||||
|
||||
|
||||
enum ArmSide
|
||||
{
|
||||
LEFT_ARM, // 左臂
|
||||
RIGHT_ARM // 右臂
|
||||
};
|
||||
|
||||
extern "C"
|
||||
{ // 添加extern "C"
|
||||
|
||||
// 写入调试信息到文件
|
||||
void writeDebugInfoToFile(const char *func_name, const char *info);
|
||||
|
||||
// 输出数组的调试信息
|
||||
void printArrayDebugInfo(float arr[], int size, const char *arr_name);
|
||||
|
||||
/* 获取本机IP地址*/
|
||||
std::string ip_address();
|
||||
|
||||
/*std::string query_can();
|
||||
查询can设备号
|
||||
*/
|
||||
std::vector<std::string> query_can();
|
||||
|
||||
void receive_fully(int sock, void *buffer, size_t size);
|
||||
/*socket通信
|
||||
参数:
|
||||
deviceInd:can设备号
|
||||
canInd:can通道
|
||||
port:端口号
|
||||
发送端发送数据格式:{标识位,数据}
|
||||
标识位
|
||||
0x01 左臂pos
|
||||
0x02 右臂pos
|
||||
0x03 左臂joint
|
||||
0x04 右臂joint
|
||||
*/
|
||||
int ti5_socket_server(int deviceInd, int canInd,int port);
|
||||
|
||||
/*获取电机错误状态
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
deviceInd:can设备号
|
||||
canInd:can通道
|
||||
dataList:接收数据的数组,错误状态
|
||||
返回值:为电机错误
|
||||
0:无错误
|
||||
1:软件错误
|
||||
2:过压
|
||||
4:欠压
|
||||
16:启动错误
|
||||
*/
|
||||
// int get_mechanicalarm_Maschinen_status(ArmSide side, int deviceInd, int canInd);
|
||||
void get_mechanicalarm_status(ArmSide side, int deviceInd, int canInd, int32_t *dataList);
|
||||
|
||||
/*清除电机错误
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
deviceInd:can设备号
|
||||
canInd:can通道
|
||||
*/
|
||||
void clear_elc_error(ArmSide side, int deviceInd, int canInd);
|
||||
|
||||
/*机械臂刹车
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
返回值:
|
||||
true:成功
|
||||
false:失败
|
||||
*/
|
||||
bool brake(ArmSide side, int deviceInd, int canInd);
|
||||
|
||||
/*将数据记录下来写入文件
|
||||
参数:
|
||||
pj_flag:角度或者位姿标识,1为角度 ,0为坐标
|
||||
filename:存储文件名
|
||||
array[6]:被保存的值
|
||||
*/
|
||||
void write_value(int pj_flag, string filename, float array[7]);
|
||||
|
||||
/*机械臂回到原点
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
返回值:无
|
||||
*/
|
||||
void mechanical_arm_origin(ArmSide side, int deviceInd, int canInd);
|
||||
|
||||
/*机械臂关节运动
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
goal_j:目标关节角
|
||||
canInd:can通道
|
||||
DeviceInd:can设备号
|
||||
返回值:无*/
|
||||
void joint_to_move(ArmSide side, float *goal_j, int deviceInd, int canInd);
|
||||
void new_joint_to_move(ArmSide side, float *goal_j, int deviceInd, int canInd);
|
||||
|
||||
/*机械臂关节运动,同时获取当前位置
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
goal_j:目标关节角
|
||||
CUrrentJointPosition:存储当前位置
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
返回值:无
|
||||
*/
|
||||
void GetP_joint_to_move(ArmSide side, float *goal_j, float *CUrrentJointPosition, int deviceInd, int canInd);
|
||||
|
||||
/*pos运动
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
pos:目标位置
|
||||
value:dim的值
|
||||
dim:-1~2 代表x,y,z, -1的时候是没有臂角约束,只会接收末端位姿,其他参数忽略
|
||||
absolute:true的时候是绝对位置(以胸部原点位置),false的时候是相对位置(以当前点胳膊轴位置)
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
返回值:无
|
||||
*/
|
||||
// void pos_to_move(ArmSide side, float *pos, float value, int dim, bool absolute, int deviceInd, int canInd);
|
||||
bool pos_to_move(ArmSide side, float *pos, int deviceInd, int canInd);
|
||||
|
||||
/*获取当前角度
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
goal_j:存储角度的数组
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
*/
|
||||
void get_current_angle(ArmSide side, float goal_j[7], int deviceInd, int canInd);
|
||||
|
||||
/*获取当前位姿
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
posz:存储位姿的数组
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
*/
|
||||
void get_current_pose(ArmSide side, float posz[7], int deviceInd, int canInd);
|
||||
|
||||
/*设置为电流模式,并设置目标电流
|
||||
参数:
|
||||
side:左臂或右臂 (LEFT_ARM 左臂,RIGHT_ARM 右臂)
|
||||
current: 目标电流
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
*/
|
||||
void set_current_mode(ArmSide side, uint32_t current[7], int deviceInd, int canInd);
|
||||
|
||||
/*设置键盘控制模式
|
||||
参数:
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
*/
|
||||
void LLL_keyboard_controller_J(int deviceInd, int canInd);
|
||||
void LLL_keyboard_controller_P(int deviceInd, int canInd);
|
||||
void RRR_keyboard_controller_J(int deviceInd, int canInd);
|
||||
void RRR_keyboard_controller_P(int deviceInd, int canInd);
|
||||
// void L_keyboard_controller(int deviceInd, int canInd);
|
||||
// void R_keyboard_controller(int deviceInd, int canInd);
|
||||
|
||||
/*获取电机当前位置
|
||||
参数:
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
MotorsTotal:电机数量
|
||||
MotorsIDlist:电机ID列表
|
||||
data:存储电机位置数据的数组
|
||||
*/
|
||||
void get_motor_position(int deviceInd, int canInd, int MotorsTotal,uint8_t *MotorsIDlist,int32_t *MotorPosition);
|
||||
|
||||
/*设置电机当前位置
|
||||
参数:
|
||||
canInd:can通道
|
||||
deviceInd:can设备号
|
||||
MotorsTotal:电机数量
|
||||
MotorsIDlist:电机ID列表
|
||||
data:存储电机位置数据的数组
|
||||
*/
|
||||
void set_motor_position(int deviceInd, int canInd, int MotorsTotal,uint8_t *MotorsIDlist,int32_t *MotorPosition);
|
||||
|
||||
|
||||
// 设定终端为非阻塞模式
|
||||
void setNonBlocking(bool enable) ;
|
||||
|
||||
// 设定文件描述符为非阻塞
|
||||
void setNonBlockingInput() ;
|
||||
// 清空键盘缓冲区
|
||||
void clearStdinBuffer();
|
||||
|
||||
// 读取键盘输入
|
||||
char ssscanKeyboard();
|
||||
|
||||
} // 添加extern "C"
|
||||
#endif
|
||||
@ -1,171 +0,0 @@
|
||||
#ifndef TI5CAN_DRIVER_H
|
||||
#define TI5CAN_DRIVER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
#include "controlcan.h"
|
||||
#include "Ti5BASIC.h"
|
||||
|
||||
#define GET_MOTOR_PERSIONS 8 //获取电机当前位置指令(1字节),转化为减速机角度公式:(返回值/65536/减速比)*360
|
||||
#define GET_MOTOR_ERROR 10 //获取电机错误指令(1字节)
|
||||
#define CLEAR_ERROR 11 //清除电机错误指令(1字节)
|
||||
#define SET_MOTOR_CURRENTS 28 //设置电机为电流模式,并设置目标电流指令(5字节)
|
||||
#define SET_MOTOR_SPEED 29 //设置电机为速度模式,并设置目标速度指令(5字节),下发参数为:(目标转速(度每秒)*减速比*100)/360
|
||||
#define SET_MOTOR_POSITION 30 //设置电机为位置模式,并设置目标位置指令(5字节),下发参数为:(减速机目标角度/360)*减速比*65536
|
||||
#define SET_MOTOR_MAX_SPEED 36 //设置电机最大正向允许速度指令(5字节),下发参数为:(目标转速(度每秒)*减速比*100)/360
|
||||
#define SET_MOTOR_MIN_SPEED 37 //设置电机最小负向允许速度指令(5字节),下发参数为:(目标转速(度每秒)*减速比*100)/360
|
||||
|
||||
|
||||
extern int CanNum;
|
||||
|
||||
// 查询 CAN 设备并绑定序列号与设备索引
|
||||
std::map<std::string, int> query_can_with_index();
|
||||
/*登录并初始化can设备
|
||||
参数:无
|
||||
返回值
|
||||
true:成功
|
||||
false:失败
|
||||
*/
|
||||
bool Start();
|
||||
// void Start();
|
||||
|
||||
bool compare_serial_numbers(const std::string &a, const std::string &b);
|
||||
std::unordered_map<std::string, int> get_device_map();
|
||||
void operate_multiple_can_devices();
|
||||
|
||||
/*登出can设备
|
||||
参数:无
|
||||
返回值
|
||||
true:成功
|
||||
false:失败
|
||||
*/
|
||||
bool Exit();
|
||||
|
||||
int32_t convertHexArrayToDecimal(const uint8_t hexArray[4]);
|
||||
|
||||
void toIntArray(int number, int *res, int size);
|
||||
|
||||
/*发送1字节指令
|
||||
参数:
|
||||
DeviceInd:can设备索引 (一个为0,2个为1)
|
||||
CANInd:can通道索引 (通道1:0,通道2:1)
|
||||
numOfActuator:发送指令的电机数量
|
||||
canIdList:电机canId列表
|
||||
commandList:指令列表
|
||||
dataList:接收数据列表
|
||||
*/
|
||||
void sendSimpleCanCommand(int DeviceInd, int CANInd,uint8_t numOfActuator, uint8_t *canIdList, uint8_t command,int32_t *dataList);
|
||||
|
||||
void sendCanCommand(int DeviceInd,int CANInd,uint8_t numOfActuator, uint8_t *canIdList, uint8_t command, uint32_t *parameterList);
|
||||
#endif // TEST_CAN_H
|
||||
|
||||
|
||||
|
||||
// // 比较函数,用于按字典序排序设备序列号
|
||||
// bool compare_serial_numbers(const std::string &a, const std::string &b)
|
||||
// {
|
||||
// return a < b; // 字典序比较
|
||||
// }
|
||||
|
||||
// std::unordered_map<std::string, int> get_device_map()
|
||||
// {
|
||||
// VCI_BOARD_INFO pinfo[50];
|
||||
// std::unordered_map<std::string, int> deviceMap;
|
||||
// int num = VCI_FindUsbDevice2(pinfo); // 查询所有CAN设备
|
||||
|
||||
// // 提取所有设备的序列号
|
||||
// std::vector<std::string> serialNumbers;
|
||||
// for (int i = 0; i < num; i++)
|
||||
// {
|
||||
// std::string serialNumber = "";
|
||||
// for (int j = 0; j < 20; j++) // 提取序列号
|
||||
// {
|
||||
// if (pinfo[i].str_Serial_Num[j] == '\0') // 结尾判断
|
||||
// break;
|
||||
// serialNumber += pinfo[i].str_Serial_Num[j];
|
||||
// }
|
||||
// serialNumbers.push_back(serialNumber);
|
||||
// }
|
||||
|
||||
// // 按照字典序对序列号进行排序
|
||||
// std::sort(serialNumbers.begin(), serialNumbers.end(), compare_serial_numbers);
|
||||
|
||||
// // 将排序后的序列号和索引进行绑定
|
||||
// for (int i = 0; i < serialNumbers.size(); i++)
|
||||
// {
|
||||
// deviceMap[serialNumbers[i]] = i;
|
||||
// }
|
||||
|
||||
// return deviceMap; // 返回序列号与设备索引的映射关系
|
||||
// }
|
||||
|
||||
// void operate_multiple_can_devices()
|
||||
// {
|
||||
// auto deviceMap = get_device_map(); // 获取设备序列号与设备索引的映射
|
||||
|
||||
// // 遍历所有设备
|
||||
// for (const auto &pair : deviceMap)
|
||||
// {
|
||||
// const std::string &serialNumber = pair.first; // 设备序列号
|
||||
// int nDeviceInd = pair.second; // 设备索引
|
||||
// int nDeviceType = 4; // 根据实际设备类型设置
|
||||
// DWORD dwRel;
|
||||
// VCI_INIT_CONFIG vic;
|
||||
|
||||
// std::cout << "正在操作设备: " << serialNumber << " (索引: " << nDeviceInd << ")" << std::endl;
|
||||
|
||||
// // 打开设备
|
||||
// dwRel = VCI_OpenDevice(nDeviceType, nDeviceInd, 0);
|
||||
// if (dwRel != 1)
|
||||
// {
|
||||
// std::cout << "无法打开设备 " << serialNumber << std::endl;
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// // 初始化 CAN 通道
|
||||
// vic.AccCode = 0x80000008;
|
||||
// vic.AccMask = 0xFFFFFFFF;
|
||||
// vic.Filter = 1;
|
||||
// vic.Timing0 = 0x00;
|
||||
// vic.Timing1 = 0x14;
|
||||
// vic.Mode = 0;
|
||||
|
||||
// if (VCI_InitCAN(nDeviceType, nDeviceInd, 0, &vic) != 1)
|
||||
// {
|
||||
// std::cout << "初始化设备 " << serialNumber << " 的 CAN 通道 0 失败" << std::endl;
|
||||
// VCI_CloseDevice(nDeviceType, nDeviceInd);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if (VCI_InitCAN(nDeviceType, nDeviceInd, 1, &vic) != 1)
|
||||
// {
|
||||
// std::cout << "初始化设备 " << serialNumber << " 的 CAN 通道 1 失败" << std::endl;
|
||||
// VCI_CloseDevice(nDeviceType, nDeviceInd);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// // 启动 CAN 通道
|
||||
// if (VCI_StartCAN(nDeviceType, nDeviceInd, 0) != 1)
|
||||
// {
|
||||
// std::cout << "启动设备 " << serialNumber << " 的 CAN 通道 0 失败" << std::endl;
|
||||
// VCI_CloseDevice(nDeviceType, nDeviceInd);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if (VCI_StartCAN(nDeviceType, nDeviceInd, 1) != 1)
|
||||
// {
|
||||
// std::cout << "启动设备 " << serialNumber << " 的 CAN 通道 1 失败" << std::endl;
|
||||
// VCI_CloseDevice(nDeviceType, nDeviceInd);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// std::cout << "设备 " << serialNumber << " 的所有 CAN 通道启动成功!" << std::endl;
|
||||
// }
|
||||
// }
|
||||
@ -1,160 +0,0 @@
|
||||
#ifndef _TI5LOGIC_H_
|
||||
#define _TI5LOGIC_H_
|
||||
#include "mathfunc.h"
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#include <cmath>
|
||||
#include "tool.h"
|
||||
using namespace std;
|
||||
|
||||
extern "C"
|
||||
{ // 添加extern "C"
|
||||
class pos_trans
|
||||
{
|
||||
protected:
|
||||
float j[7] = {0, 0, 0, 0, 0, 0, 0};
|
||||
float orij[7];
|
||||
float prcj = 0.017;
|
||||
float scd = sin(prcj) * sin(prcj);
|
||||
float prc1 = float(int(10000 * sin(prcj) + 1)) / 10000;
|
||||
float prc2 = prc1 * (prc1 + 2);
|
||||
float prc3 = prc1 * (3 + prc1 + prc1 * prc1) + prc2;
|
||||
float P_0[19][3] = {{0, 0, 0}}; // 机械臂按线性顺序的点集
|
||||
struct ROD
|
||||
{
|
||||
int i1, i2; // 连杆端点为P_0[i1]和P_0[i2]
|
||||
float r; // 连杆半径
|
||||
float l; // 连杆长度
|
||||
};
|
||||
ROD rod[18]; // 机械臂按线性顺序的连杆集
|
||||
float len[18]; // 数学模型的长度参数(与rod.l独立)
|
||||
int rodnum; // 连杆总数
|
||||
// 检测两连杆是否碰撞,P1、P2构成一根连杆,半径为r1;P3、P4构成另一根连杆,半径为r2
|
||||
bool LLcolsp(float P1[3], float P2[3], float P3[3], float P4[3], float r1, float r2);
|
||||
void init_rodindex();
|
||||
// 若点距和连杆长度一致则认为点坐标值正确
|
||||
bool check_Points();
|
||||
// 根据变换矩阵计算位姿
|
||||
void MatrixT2ypr(float T[4][4]);
|
||||
// 根据位姿计算变换矩阵
|
||||
void Pos2MatrixT(float T[4][4]);
|
||||
// 修正角度并检测关节范围是否合法(相邻连杆间是否碰撞)
|
||||
bool mendjoints();
|
||||
bool checkcalj();
|
||||
// 检测所有连杆间的碰撞
|
||||
bool check_colsp();
|
||||
|
||||
public:
|
||||
float jr1[7] = {-pi, -pi, -pi, -pi, -pi, -pi, -pi}; // 关节静态角度范围
|
||||
float jr2[7] = {pi, pi, pi, pi, pi, pi, pi};
|
||||
float gap0, gap;
|
||||
float ypr[6] = {0, 0, 0};
|
||||
// float j[7] = {0, 0, 0, 0, 0, 0, 0};
|
||||
void showpointsinfo();
|
||||
};
|
||||
|
||||
class humanoidLeftArm : public pos_trans
|
||||
{
|
||||
protected:
|
||||
// 初始化固有的机械结构
|
||||
void init_arm_structure()
|
||||
{
|
||||
rodnum = 3;
|
||||
rod[0].r = 10, rod[1].r = 10, rod[2].r = 10; // 连杆半径 170A
|
||||
rod[0].l = 171, rod[1].l = 250, rod[2].l = 250; // 连杆长度 170A
|
||||
// rod[0].r = 45, rod[1].r = 42, rod[2].r = 37.5; // 连杆半径 T230
|
||||
// rod[0].l = 234.5, rod[1].l = 339, rod[2].l = 301.5; // 连杆长度 T230
|
||||
init_rodindex();
|
||||
}
|
||||
// 初始化数学模型参数
|
||||
void init_model_structure()
|
||||
{
|
||||
float M = 1.57;
|
||||
jr1[6] = -pi/6;
|
||||
// jr1[0] = -M, jr1[1] = -M, jr1[2] = -0.5, jr1[3] = -M, jr1[4] = -M, jr1[5] = -M, jr1[6] = -pi/6;
|
||||
// jr2[0] = M, jr2[1] = M, jr2[2] = 0.5, jr2[3] = 0, jr2[4] = M, jr2[5] = M, jr2[6] = M;
|
||||
gap = abs(jr1[0]);
|
||||
for (int i = 1; i < 7; i++)
|
||||
if (abs(jr1[i]) > gap)
|
||||
gap = abs(jr1[i]);
|
||||
for (int i = 0; i < 7; i++)
|
||||
if (abs(jr2[i]) > gap)
|
||||
gap = abs(jr2[i]);
|
||||
gap0 = gap;
|
||||
len[0] = rod[0].l, len[1] = rod[1].l, len[2] = rod[2].l;
|
||||
P_0[1][0] = 0, P_0[1][1] = len[0], P_0[1][2] = 0;
|
||||
float goal_j[7] = {0, 0, 0, 0, 0, 0, 0};
|
||||
l_forward_move(goal_j); // show_value("pos",pos,6);
|
||||
ypr[0]=ypr[2]=0;
|
||||
}
|
||||
|
||||
// 点或向量在不同坐标系转换(p=true表示点,p=false表示向量)
|
||||
void fromS0toS1(float P0[3], float P[3], bool p);
|
||||
void fromS1toS2(float P0[3], float P[3], bool p);
|
||||
void fromS2toS3(float P0[3], float P[3], bool p);
|
||||
void fromS3toS4(float P0[3], float P[3], bool p);
|
||||
void fromS4toS5(float P0[3], float P[3], bool p);
|
||||
|
||||
void fromS5toS6(float P0[3], float P[3], bool p);
|
||||
void fromS6toS7(float P0[3], float P[3], bool p);
|
||||
|
||||
void fromS1toS0(float P0[3], float P[3], bool p);
|
||||
void fromS2toS1(float P0[3], float P[3], bool p);
|
||||
void fromS3toS2(float P0[3], float P[3], bool p);
|
||||
void fromS4toS3(float P0[3], float P[3], bool p);
|
||||
void fromS5toS4(float P0[3], float P[3], bool p);
|
||||
void fromS6toS5(float P0[3], float P[3], bool p);
|
||||
void fromS7toS6(float P0[3], float P[3], bool p);
|
||||
bool solve_in_S6();
|
||||
bool solve_in_S4();
|
||||
bool solve_in_S2();
|
||||
// 逆运动解算器
|
||||
bool Points2J();
|
||||
// 根据角度计算点变换矩阵
|
||||
void J2MatrixT(float T[4][4]);
|
||||
|
||||
public:
|
||||
humanoidLeftArm();
|
||||
// 检测逆运动解出的角是否满足原始位姿
|
||||
bool checkacc();
|
||||
// 正运动
|
||||
bool l_forward_move(float goal_j[7]);
|
||||
|
||||
// 获取当前角度
|
||||
void get_crt_j(float joints[7]);
|
||||
|
||||
// 获取当前位置
|
||||
void get_crt_pos(float pos[6]);
|
||||
|
||||
/*
|
||||
参数:
|
||||
pos:目标位置
|
||||
value:dim的值
|
||||
dim:-1~2 代表x,y,z,-1的时候是没有臂角约束,只会接收末端位姿,其他参数忽略
|
||||
absolute:true的时候是绝对位置(以胸部原点位置),false的时候是相对位置(以当前点胳膊轴位置)
|
||||
*/
|
||||
// bool l_backward_move(float pos[6], float value, int dim, bool absolute);
|
||||
bool l_backward_move(float pos[6]);
|
||||
// 测试fromSi2Sj函数是否正确(是否抄错)
|
||||
void testj2p();
|
||||
void show_crt_pos();
|
||||
void show_crt_j();
|
||||
};
|
||||
class humanoidRightArm : public humanoidLeftArm
|
||||
{
|
||||
private:
|
||||
void get_r_ypr(float r_ypr[3]);
|
||||
public:
|
||||
humanoidRightArm();
|
||||
bool r_forward_move(float goal_j[7]);
|
||||
void get_crt_j(float joints[7]);
|
||||
void get_crt_pos(float postrue[6]);
|
||||
// bool r_backward_move(float postrue[6], float value, int dim, bool absolute);
|
||||
bool r_backward_move(float postrue[6]);
|
||||
void show_crt_pos();
|
||||
void show_crt_j();
|
||||
void showpointsinfo();
|
||||
};
|
||||
|
||||
} // 添加extern "C"
|
||||
#endif
|
||||
@ -1,46 +0,0 @@
|
||||
#ifndef TI5MOVE_H
|
||||
#define TI5MOVE_H
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstdlib>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
// #include "can/SingleCaninterface.h"
|
||||
// #include "can/motortypehelper.h"
|
||||
#include <vector>
|
||||
#include "tool.h"
|
||||
#include "Ti5LOGIC.h"
|
||||
#include <time.h>
|
||||
#include <mutex>
|
||||
#include "Ti5CAN_Driver.h"
|
||||
|
||||
#define USLEEPTIME 3000
|
||||
|
||||
extern "C"{ //添加:extern C
|
||||
class ArmController{
|
||||
private:
|
||||
float AG = 0.005; //启停时变速的采样间距(秒
|
||||
float scale = 101; //电机内圈与外圈的速度比
|
||||
float n2p = 655.36; //内圈转速到步速的转化系数
|
||||
// float n2p = 655.36/101; //双编码器的 内圈转速到步速的转化系数
|
||||
float mvtime = 0;
|
||||
// float j2p = scale * 65536 / 2 / pi; //电机外圈角度到内圈步数的转化
|
||||
float min_time = 0;
|
||||
bool jstp = false;
|
||||
uint8_t canidList[IDNUM];
|
||||
float nplL[4][4]; //add 用于linear_move函数机械臂直线运动
|
||||
void setn(int npL[IDNUM],int deviceInd,int canInd);
|
||||
void ACTmove(float *a,float *b,float T0,int deviceInd,int canInd);// 实际运动
|
||||
public:
|
||||
ArmController(uint8_t canid[IDNUM]);
|
||||
// void plan_move(float crtj[IDNUM],int canInd,int deviceInd);
|
||||
void plan_move(int deviceInd, int canInd,float crtj[IDNUM]);
|
||||
void new_plan_move(int deviceInd,int canInd,float crtj[IDNUM]);//mfs add 2024-12-3
|
||||
void GETP_plan_move(float crtj[IDNUM],float *CUrrentJointPosition,int deviceInd,int canInd);
|
||||
float NMAX = 3000; //所有电机内核最大转速值((NMAX/100)圈/秒)
|
||||
float j2p = scale * 65536 / 2 / pi; //电机外圈角度到内圈步数的转化
|
||||
// float j2p = 131072/pi; //双编码器 电机外圈角度到内圈步数的转化
|
||||
};
|
||||
}//添加:extern C
|
||||
|
||||
#endif
|
||||
@ -1,104 +0,0 @@
|
||||
#ifndef CONTROLCAN_H
|
||||
#define CONTROLCAN_H
|
||||
|
||||
////文件版本:v2.02 20190609
|
||||
//接口卡类型定义
|
||||
|
||||
#define VCI_USBCAN1 3
|
||||
#define VCI_USBCAN2 4
|
||||
#define VCI_USBCAN2A 4
|
||||
|
||||
#define VCI_USBCAN_E_U 20
|
||||
#define VCI_USBCAN_2E_U 21
|
||||
|
||||
//函数调用返回状态值
|
||||
#define STATUS_OK 1
|
||||
#define STATUS_ERR 0
|
||||
|
||||
#define USHORT unsigned short int
|
||||
#define BYTE unsigned char
|
||||
#define CHAR char
|
||||
#define UCHAR unsigned char
|
||||
#define UINT unsigned int
|
||||
#define DWORD unsigned int
|
||||
#define PVOID void*
|
||||
#define ULONG unsigned int
|
||||
#define INT int
|
||||
#define UINT32 UINT
|
||||
#define LPVOID void*
|
||||
#define BOOL BYTE
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
|
||||
//1.ZLGCAN系列接口卡信息的数据类型。
|
||||
typedef struct _VCI_BOARD_INFO{
|
||||
USHORT hw_Version;
|
||||
USHORT fw_Version;
|
||||
USHORT dr_Version;
|
||||
USHORT in_Version;
|
||||
USHORT irq_Num;
|
||||
BYTE can_Num;
|
||||
CHAR str_Serial_Num[20];
|
||||
CHAR str_hw_Type[40];
|
||||
USHORT Reserved[4];
|
||||
} VCI_BOARD_INFO,*PVCI_BOARD_INFO;
|
||||
|
||||
//2.定义CAN信息帧的数据类型。
|
||||
typedef struct _VCI_CAN_OBJ{
|
||||
UINT ID;
|
||||
UINT TimeStamp;
|
||||
BYTE TimeFlag;
|
||||
BYTE SendType;
|
||||
BYTE RemoteFlag;//是否是远程帧
|
||||
BYTE ExternFlag;//是否是扩展帧
|
||||
BYTE DataLen;
|
||||
BYTE Data[8];
|
||||
BYTE Reserved[3];
|
||||
}VCI_CAN_OBJ,*PVCI_CAN_OBJ;
|
||||
|
||||
//3.定义初始化CAN的数据类型
|
||||
typedef struct _INIT_CONFIG{
|
||||
DWORD AccCode;
|
||||
DWORD AccMask;
|
||||
DWORD Reserved;
|
||||
UCHAR Filter;
|
||||
UCHAR Timing0;
|
||||
UCHAR Timing1;
|
||||
UCHAR Mode;
|
||||
}VCI_INIT_CONFIG,*PVCI_INIT_CONFIG;
|
||||
|
||||
///////// new add struct for filter /////////
|
||||
typedef struct _VCI_FILTER_RECORD{
|
||||
DWORD ExtFrame; //是否为扩展帧
|
||||
DWORD Start;
|
||||
DWORD End;
|
||||
}VCI_FILTER_RECORD,*PVCI_FILTER_RECORD;
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN_C extern "C"
|
||||
#else
|
||||
#define EXTERN_C
|
||||
#endif
|
||||
|
||||
EXTERN_C DWORD VCI_OpenDevice(DWORD DeviceType,DWORD DeviceInd,DWORD Reserved);
|
||||
EXTERN_C DWORD VCI_CloseDevice(DWORD DeviceType,DWORD DeviceInd);
|
||||
EXTERN_C DWORD VCI_InitCAN(DWORD DeviceType, DWORD DeviceInd, DWORD CANInd, PVCI_INIT_CONFIG pInitConfig);
|
||||
|
||||
EXTERN_C DWORD VCI_ReadBoardInfo(DWORD DeviceType,DWORD DeviceInd,PVCI_BOARD_INFO pInfo);
|
||||
|
||||
EXTERN_C DWORD VCI_SetReference(DWORD DeviceType,DWORD DeviceInd,DWORD CANInd,DWORD RefType,PVOID pData);
|
||||
|
||||
EXTERN_C ULONG VCI_GetReceiveNum(DWORD DeviceType,DWORD DeviceInd,DWORD CANInd);
|
||||
EXTERN_C DWORD VCI_ClearBuffer(DWORD DeviceType,DWORD DeviceInd,DWORD CANInd);
|
||||
|
||||
EXTERN_C DWORD VCI_StartCAN(DWORD DeviceType,DWORD DeviceInd,DWORD CANInd);
|
||||
EXTERN_C DWORD VCI_ResetCAN(DWORD DeviceType,DWORD DeviceInd,DWORD CANInd);
|
||||
|
||||
EXTERN_C ULONG VCI_Transmit(DWORD DeviceType,DWORD DeviceInd,DWORD CANInd,PVCI_CAN_OBJ pSend,ULONG Len);
|
||||
EXTERN_C ULONG VCI_Receive(DWORD DeviceType,DWORD DeviceInd,DWORD CANInd,PVCI_CAN_OBJ pReceive,ULONG Len,INT WaitTime);
|
||||
|
||||
EXTERN_C DWORD VCI_UsbDeviceReset(DWORD DevType,DWORD DevIndex,DWORD Reserved);
|
||||
EXTERN_C DWORD VCI_FindUsbDevice2(PVCI_BOARD_INFO pInfo);
|
||||
|
||||
#endif
|
||||
|
||||
@ -1,28 +0,0 @@
|
||||
#ifndef MATHFUNC_H
|
||||
#define MATHFUNC_H
|
||||
|
||||
#include<math.h>
|
||||
#define pi M_PI
|
||||
|
||||
extern "C"{//添加extern "C"
|
||||
float vec_dot(float v1[3],float v2[3]);
|
||||
float vec_length(float v[3]);
|
||||
void vec_rescale(float v[3],float l,float nv[3]);
|
||||
int fx2solv(float a,float b,float c,float *p1,float *p2);
|
||||
float pointsdistance(float P1[3],float P2[3]);
|
||||
void vec_subtraction(float v1[3],float v2[3],float v[3]);
|
||||
void vec_addition(float v1[3],float v2[3],float v[3]);
|
||||
|
||||
float vec_angle(float v1[3],float v2[3],float f);
|
||||
float solve_trangle(float a,float b,float c);
|
||||
void vec_cross(float v1[3],float v2[3],float v[3]);
|
||||
float calcu_angle(float sa,float ca,float cp2);
|
||||
|
||||
|
||||
void MatrixmultPoint(float T[4][4],float P0[3],float P[3]);
|
||||
void Matrixmult(float T1[4][4],float T2[4][4],float T[4][4]);
|
||||
void MatrixT2MatrixI(float T[4][4],float I[4][4]);
|
||||
|
||||
|
||||
}
|
||||
#endif
|
||||
@ -1,35 +0,0 @@
|
||||
#ifndef TOOL_H
|
||||
#define TOOL_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <termio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define IDNUM 7
|
||||
|
||||
|
||||
// extern "C"
|
||||
// { // 添加extern "C"
|
||||
void Mcopy(float (*C)[4], float (*P)[4]);
|
||||
|
||||
void show_value(string name, float (*T)[4]);
|
||||
void copy_value(float *copy, float *paste, int n);
|
||||
void show_value(string name, float *value);
|
||||
void show_value(string name,float* value,int n);
|
||||
void show_value(string name, uint32_t *value);
|
||||
void show_point(string name, float *value);
|
||||
void show_value(string name, uint8_t *value);
|
||||
void get_cmdlist(uint8_t *L, uint8_t c);
|
||||
void get_paralist(uint32_t *L, uint32_t c);
|
||||
|
||||
int getch();
|
||||
char scanKeyboard();
|
||||
|
||||
// void test_demo();
|
||||
// void test();
|
||||
|
||||
// } // 添加extern "C"
|
||||
#endif
|
||||
@ -6,7 +6,7 @@
|
||||
#define CMVR_ES_ABSTRACT_SPEAKER_H
|
||||
#pragma once
|
||||
|
||||
#include "abstract_device.h"
|
||||
#include "../abstract_device.h"
|
||||
|
||||
namespace cmvr::device{
|
||||
class AbstractSpeaker: public AbstractDevice {
|
||||
@ -1,4 +1,4 @@
|
||||
add_library(ffmpeg_speaker SHARED ffmpeg_speaker.cpp)
|
||||
add_library(ffmpeg_speaker SHARED src/ffmpeg_speaker.cpp)
|
||||
|
||||
target_include_directories(ffmpeg_speaker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
||||
@ -6,10 +6,10 @@
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include "devices/abstract_speaker.h"
|
||||
#include "../../abstract_speaker.h"
|
||||
#include <boost/lockfree/spsc_queue.hpp>
|
||||
#include "ffmpeg_ptr.h"
|
||||
#include "../../../../include/utils/base/os.h"
|
||||
#include "../../../../utils/base/include/os.h"
|
||||
|
||||
extern "C" {
|
||||
#include <alsa/asoundlib.h>
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by xtkuang on 2025/6/9.
|
||||
//
|
||||
|
||||
#include "ffmpeg_speaker.h"
|
||||
#include "../include/ffmpeg_speaker.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace cmvr::device;
|
||||
@ -12,7 +12,7 @@
|
||||
#include <unordered_map>
|
||||
#include <set>
|
||||
|
||||
#include "utils/math/geometry.h"
|
||||
#include "../utils/math/include/geometry.h"
|
||||
|
||||
|
||||
namespace cmvr::device{
|
||||
@ -1,8 +1,6 @@
|
||||
add_library(hardware SHARED
|
||||
can_interface.cpp
|
||||
serial_interface.cpp
|
||||
esp32_serial_port.cpp
|
||||
io_interface.cpp
|
||||
src/serial_interface.cpp
|
||||
src/esp32_serial_port.cpp
|
||||
)
|
||||
|
||||
target_include_directories(hardware PUBLIC
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
//
|
||||
// Created by xtkuang on 2025/5/6.
|
||||
//
|
||||
|
||||
#include "../../include/hardware/can_interface.h"
|
||||
|
||||
namespace cmvr {
|
||||
} // cmvr
|
||||
@ -1,5 +0,0 @@
|
||||
//
|
||||
// Created by linbo on 2025/9/24.
|
||||
//
|
||||
|
||||
#include "../../include/hardware/io_interface.h"
|
||||
@ -1,5 +1,5 @@
|
||||
// esp32_serial_port.cpp
|
||||
#include "hardware/esp32_serial_port.h"
|
||||
#include "../include/esp32_serial_port.h"
|
||||
#include <iostream>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by xtkuang on 2025/5/6.
|
||||
//
|
||||
|
||||
#include "hardware/serial_interface.h"
|
||||
#include "../include/serial_interface.h"
|
||||
#include <iostream>
|
||||
#include <bitset>
|
||||
#include <vector>
|
||||
@ -1,40 +0,0 @@
|
||||
# 定义是否使用libcurl的变量
|
||||
set(USE_LIBCURL ON CACHE BOOL "Use libcurl for HTTP requests")
|
||||
|
||||
# 创建共享库 - 先定义目标
|
||||
add_library(httpclient SHARED
|
||||
httpclient.cpp
|
||||
)
|
||||
|
||||
# 设置公共包含目录
|
||||
target_include_directories(httpclient PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
# 如果使用libcurl,查找并配置libcurl
|
||||
if(USE_LIBCURL)
|
||||
# 查找libcurl库
|
||||
find_package(CURL REQUIRED)
|
||||
|
||||
if(CURL_FOUND)
|
||||
message(STATUS "Found libcurl: ${CURL_VERSION_STRING}")
|
||||
message(STATUS "libcurl include dirs: ${CURL_INCLUDE_DIRS}")
|
||||
message(STATUS "libcurl libraries: ${CURL_LIBRARIES}")
|
||||
|
||||
# 添加libcurl的包含目录
|
||||
target_include_directories(httpclient PUBLIC ${CURL_INCLUDE_DIRS})
|
||||
|
||||
# 将USE_LIBCURL宏应用到httpclient库
|
||||
target_compile_definitions(httpclient PUBLIC USE_LIBCURL)
|
||||
|
||||
# 链接libcurl库
|
||||
target_link_libraries(httpclient PUBLIC ${CURL_LIBRARIES})
|
||||
else()
|
||||
message(FATAL_ERROR "libcurl not found. Please install libcurl-dev package.")
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "Using native socket implementation for HTTP requests")
|
||||
endif()
|
||||
|
||||
# 添加别名
|
||||
add_library(cmvr_es::httpclient ALIAS httpclient)
|
||||
@ -1,673 +0,0 @@
|
||||
//
|
||||
// Created by linbo on 2025/6/24.
|
||||
//
|
||||
|
||||
#include "../../include/http/httpclient.h"
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#ifdef USE_LIBCURL
|
||||
#include <curl/curl.h>
|
||||
#endif
|
||||
|
||||
namespace cmvr {
|
||||
|
||||
// URL解析辅助函数
|
||||
struct UrlParts {
|
||||
std::string protocol;
|
||||
std::string host;
|
||||
int port;
|
||||
std::string path;
|
||||
std::map<std::string, std::string> queryParams;
|
||||
};
|
||||
|
||||
UrlParts parseUrl(const std::string& url) {
|
||||
UrlParts parts;
|
||||
|
||||
// 解析协议
|
||||
size_t protocolEnd = url.find("://");
|
||||
if (protocolEnd != std::string::npos) {
|
||||
parts.protocol = url.substr(0, protocolEnd);
|
||||
} else {
|
||||
throw HttpException("Invalid URL: missing protocol");
|
||||
}
|
||||
|
||||
// 解析主机和端口
|
||||
size_t hostStart = protocolEnd + 3;
|
||||
size_t pathStart = url.find("/", hostStart);
|
||||
if (pathStart == std::string::npos) {
|
||||
pathStart = url.length();
|
||||
}
|
||||
|
||||
std::string hostPart = url.substr(hostStart, pathStart - hostStart);
|
||||
size_t portPos = hostPart.find(":");
|
||||
|
||||
if (portPos != std::string::npos) {
|
||||
parts.host = hostPart.substr(0, portPos);
|
||||
parts.port = std::stoi(hostPart.substr(portPos + 1));
|
||||
} else {
|
||||
parts.host = hostPart;
|
||||
if (parts.protocol == "http") {
|
||||
parts.port = 80;
|
||||
} else if (parts.protocol == "https") {
|
||||
parts.port = 443;
|
||||
} else {
|
||||
throw HttpException("Unsupported protocol: " + parts.protocol);
|
||||
}
|
||||
}
|
||||
|
||||
// 解析路径和查询参数
|
||||
if (pathStart < url.length()) {
|
||||
std::string pathAndQuery = url.substr(pathStart);
|
||||
size_t queryStart = pathAndQuery.find("?");
|
||||
|
||||
if (queryStart != std::string::npos) {
|
||||
parts.path = pathAndQuery.substr(0, queryStart);
|
||||
std::string queryString = pathAndQuery.substr(queryStart + 1);
|
||||
|
||||
// 解析查询参数
|
||||
size_t paramStart = 0;
|
||||
while (paramStart < queryString.length()) {
|
||||
size_t paramEnd = queryString.find("&", paramStart);
|
||||
if (paramEnd == std::string::npos) {
|
||||
paramEnd = queryString.length();
|
||||
}
|
||||
|
||||
std::string param = queryString.substr(paramStart, paramEnd - paramStart);
|
||||
size_t equalsPos = param.find("=");
|
||||
|
||||
if (equalsPos != std::string::npos) {
|
||||
std::string key = param.substr(0, equalsPos);
|
||||
std::string value = param.substr(equalsPos + 1);
|
||||
parts.queryParams[key] = value;
|
||||
}
|
||||
|
||||
paramStart = paramEnd + 1;
|
||||
}
|
||||
} else {
|
||||
parts.path = pathAndQuery;
|
||||
}
|
||||
} else {
|
||||
parts.path = "/";
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
// 构建完整URL(baseUrl + path + params)
|
||||
std::string buildUrl(const std::string& baseUrl,
|
||||
const std::string& path,
|
||||
const std::map<std::string, std::string>& params) {
|
||||
std::stringstream urlStream;
|
||||
|
||||
// 添加基础URL
|
||||
if (!baseUrl.empty()) {
|
||||
urlStream << baseUrl;
|
||||
|
||||
// 确保路径以斜杠开始
|
||||
if (baseUrl.back() != '/' && (!path.empty() && path.front() != '/')) {
|
||||
urlStream << '/';
|
||||
}
|
||||
}
|
||||
|
||||
// 添加路径
|
||||
if (!path.empty()) {
|
||||
urlStream << path;
|
||||
} else if (baseUrl.empty()) {
|
||||
urlStream << '/';
|
||||
}
|
||||
|
||||
// 添加查询参数
|
||||
if (!params.empty()) {
|
||||
urlStream << '?';
|
||||
bool first = true;
|
||||
|
||||
for (const auto& param : params) {
|
||||
if (!first) {
|
||||
urlStream << '&';
|
||||
}
|
||||
|
||||
urlStream << param.first << '=' << param.second;
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
return urlStream.str();
|
||||
}
|
||||
|
||||
// libcurl响应数据回调函数
|
||||
#ifdef USE_LIBCURL
|
||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
|
||||
((std::string*)userp)->append((char*)contents, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
#endif
|
||||
// 原生socket实现
|
||||
class SocketHttpClientImpl : public HttpClientImpl {
|
||||
public:
|
||||
SocketHttpClientImpl() : m_baseUrl(), m_timeout(10000) {}
|
||||
~SocketHttpClientImpl() override = default;
|
||||
|
||||
void setBaseUrl(const std::string& baseUrl) override {
|
||||
m_baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
void setTimeout(int timeout) override {
|
||||
m_timeout = timeout;
|
||||
}
|
||||
|
||||
void addHeader(const std::string& key, const std::string& value) override {
|
||||
m_headers[key] = value;
|
||||
}
|
||||
|
||||
void clearHeaders() override {
|
||||
m_headers.clear();
|
||||
}
|
||||
|
||||
HttpResponse get(const std::string& path,
|
||||
const std::map<std::string, std::string>& params) override {
|
||||
return executeRequest("GET", buildUrl(m_baseUrl, path, params), "", "");
|
||||
}
|
||||
|
||||
HttpResponse post(const std::string& path,
|
||||
const std::map<std::string, std::string>& params,
|
||||
const std::string& body,
|
||||
const std::string& contentType) override {
|
||||
return executeRequest("POST", buildUrl(m_baseUrl, path, params), contentType, body);
|
||||
}
|
||||
|
||||
HttpResponse postJson(const std::string& path,
|
||||
const std::map<std::string, std::string>& params,
|
||||
const std::string& jsonBody) override {
|
||||
return executeRequest("POST", buildUrl(m_baseUrl, path, params), "application/json", jsonBody);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_baseUrl;
|
||||
int m_timeout;
|
||||
std::map<std::string, std::string> m_headers;
|
||||
|
||||
HttpResponse executeRequest(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& contentType,
|
||||
const std::string& body) {
|
||||
HttpResponse response;
|
||||
|
||||
try {
|
||||
// 解析URL
|
||||
UrlParts parts = parseUrl(url);
|
||||
|
||||
// 检查协议(原生socket实现不支持HTTPS)
|
||||
if (parts.protocol != "http") {
|
||||
throw HttpException("Unsupported protocol: " + parts.protocol +
|
||||
". Native socket implementation only supports HTTP.");
|
||||
}
|
||||
|
||||
// 创建socket
|
||||
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) {
|
||||
throw HttpException("Failed to create socket");
|
||||
}
|
||||
|
||||
// 设置超时
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = m_timeout / 1000;
|
||||
timeout.tv_usec = (m_timeout % 1000) * 1000;
|
||||
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
|
||||
close(sockfd);
|
||||
throw HttpException("Failed to set receive timeout");
|
||||
}
|
||||
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) {
|
||||
close(sockfd);
|
||||
throw HttpException("Failed to set send timeout");
|
||||
}
|
||||
|
||||
// 获取主机地址
|
||||
struct hostent* server = gethostbyname(parts.host.c_str());
|
||||
if (server == NULL) {
|
||||
close(sockfd);
|
||||
throw HttpException("Failed to resolve host: " + parts.host);
|
||||
}
|
||||
|
||||
// 设置服务器地址
|
||||
struct sockaddr_in serv_addr;
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
|
||||
serv_addr.sin_port = htons(parts.port);
|
||||
|
||||
// 连接到服务器
|
||||
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
close(sockfd);
|
||||
throw HttpException("Failed to connect to server: " + parts.host);
|
||||
}
|
||||
|
||||
// 构建并发送请求
|
||||
std::stringstream request;
|
||||
request << method << " " << parts.path;
|
||||
|
||||
// 添加查询参数
|
||||
if (!parts.queryParams.empty()) {
|
||||
request << '?';
|
||||
bool first = true;
|
||||
|
||||
for (const auto& param : parts.queryParams) {
|
||||
if (!first) {
|
||||
request << '&';
|
||||
}
|
||||
|
||||
request << param.first << '=' << param.second;
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
request << " HTTP/1.1\r\n";
|
||||
request << "Host: " << parts.host;
|
||||
if (parts.port != 80) {
|
||||
request << ":" << parts.port;
|
||||
}
|
||||
request << "\r\n";
|
||||
|
||||
// 添加内容类型头(如果有)
|
||||
if (!contentType.empty()) {
|
||||
request << "Content-Type: " << contentType << "\r\n";
|
||||
}
|
||||
|
||||
// 添加用户代理头
|
||||
request << "User-Agent: SocketHttpClient/1.0\r\n";
|
||||
|
||||
// 添加其他自定义头
|
||||
for (const auto& header : m_headers) {
|
||||
request << header.first << ": " << header.second << "\r\n";
|
||||
}
|
||||
|
||||
// 添加内容长度头(如果有请求体)
|
||||
if (!body.empty()) {
|
||||
request << "Content-Length: " << body.length() << "\r\n";
|
||||
}
|
||||
|
||||
// 添加连接头
|
||||
request << "Connection: close\r\n\r\n";
|
||||
|
||||
// 添加请求体(如果有)
|
||||
if (!body.empty()) {
|
||||
request << body;
|
||||
}
|
||||
|
||||
// 发送请求
|
||||
std::string requestStr = request.str();
|
||||
if (send(sockfd, requestStr.c_str(), requestStr.length(), 0) < 0) {
|
||||
close(sockfd);
|
||||
throw HttpException("Failed to send request");
|
||||
}
|
||||
|
||||
// 接收响应
|
||||
char buffer[4096];
|
||||
std::string responseData;
|
||||
ssize_t bytesRead;
|
||||
|
||||
while ((bytesRead = recv(sockfd, buffer, sizeof(buffer) - 1, 0)) > 0) {
|
||||
buffer[bytesRead] = '\0';
|
||||
responseData += buffer;
|
||||
}
|
||||
|
||||
if (bytesRead < 0) {
|
||||
close(sockfd);
|
||||
throw HttpException("Failed to receive response");
|
||||
}
|
||||
|
||||
// 关闭socket
|
||||
close(sockfd);
|
||||
|
||||
// 解析响应
|
||||
size_t headerEnd = responseData.find("\r\n\r\n");
|
||||
if (headerEnd != std::string::npos) {
|
||||
// 解析状态行
|
||||
size_t statusLineEnd = responseData.find("\r\n");
|
||||
if (statusLineEnd != std::string::npos) {
|
||||
std::string statusLine = responseData.substr(0, statusLineEnd);
|
||||
|
||||
// 解析HTTP版本
|
||||
size_t httpVersionEnd = statusLine.find(' ');
|
||||
if (httpVersionEnd != std::string::npos) {
|
||||
// 解析状态码
|
||||
size_t statusCodeStart = httpVersionEnd + 1;
|
||||
size_t statusCodeEnd = statusLine.find(' ', statusCodeStart);
|
||||
|
||||
if (statusCodeStart != std::string::npos && statusCodeEnd != std::string::npos) {
|
||||
std::string statusCodeStr = statusLine.substr(statusCodeStart, statusCodeEnd - statusCodeStart);
|
||||
response.statusCode = std::stoi(statusCodeStr);
|
||||
|
||||
// 解析状态文本
|
||||
response.statusText = statusLine.substr(statusCodeEnd + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 解析响应头
|
||||
size_t headerStart = responseData.find("\r\n") + 2;
|
||||
std::string headers = responseData.substr(headerStart, headerEnd - headerStart);
|
||||
std::istringstream headerStream(headers);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(headerStream, line) && line.length() > 1) {
|
||||
if (line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
|
||||
size_t colonPos = line.find(':');
|
||||
if (colonPos != std::string::npos) {
|
||||
std::string key = line.substr(0, colonPos);
|
||||
std::string value = line.substr(colonPos + 1);
|
||||
|
||||
// 去除前后空格
|
||||
size_t start = value.find_first_not_of(" \t");
|
||||
size_t end = value.find_last_not_of(" \t");
|
||||
|
||||
if (start != std::string::npos && end != std::string::npos) {
|
||||
value = value.substr(start, end - start + 1);
|
||||
}
|
||||
|
||||
response.headers[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取响应体
|
||||
response.body = responseData.substr(headerEnd + 4);
|
||||
} else {
|
||||
// 没有找到响应头结束标记,可能是不完整的响应
|
||||
response.body = responseData;
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
throw HttpException(std::string("HTTP request failed: ") + e.what());
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
};
|
||||
|
||||
// libcurl实现
|
||||
#ifdef USE_LIBCURL
|
||||
class LibcurlHttpClientImpl : public HttpClientImpl {
|
||||
public:
|
||||
LibcurlHttpClientImpl() : m_baseUrl(), m_timeout(10000) {
|
||||
// 初始化libcurl
|
||||
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to initialize libcurl: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
}
|
||||
|
||||
~LibcurlHttpClientImpl() override {
|
||||
// 清理libcurl
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
void setBaseUrl(const std::string& baseUrl) override {
|
||||
m_baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
void setTimeout(int timeout) override {
|
||||
m_timeout = timeout;
|
||||
}
|
||||
|
||||
void addHeader(const std::string& key, const std::string& value) override {
|
||||
m_headers[key] = value;
|
||||
}
|
||||
|
||||
void clearHeaders() override {
|
||||
m_headers.clear();
|
||||
}
|
||||
|
||||
HttpResponse get(const std::string& path,
|
||||
const std::map<std::string, std::string>& params) override {
|
||||
return executeRequest("GET", buildUrl(m_baseUrl, path, params), "", "");
|
||||
}
|
||||
|
||||
HttpResponse post(const std::string& path,
|
||||
const std::map<std::string, std::string>& params,
|
||||
const std::string& body,
|
||||
const std::string& contentType) override {
|
||||
return executeRequest("POST", buildUrl(m_baseUrl, path, params), contentType, body);
|
||||
}
|
||||
|
||||
HttpResponse postJson(const std::string& path,
|
||||
const std::map<std::string, std::string>& params,
|
||||
const std::string& jsonBody) override {
|
||||
return executeRequest("POST", buildUrl(m_baseUrl, path, params), "application/json", jsonBody);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_baseUrl;
|
||||
int m_timeout;
|
||||
std::map<std::string, std::string> m_headers;
|
||||
|
||||
HttpResponse executeRequest(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& contentType,
|
||||
const std::string& body) {
|
||||
HttpResponse response;
|
||||
CURL* curl = nullptr;
|
||||
CURLcode res;
|
||||
struct curl_slist* headers = nullptr;
|
||||
|
||||
try {
|
||||
// 初始化curl
|
||||
curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
throw HttpException("Failed to initialize libcurl");
|
||||
}
|
||||
|
||||
// 设置URL
|
||||
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set URL: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
// 设置超时
|
||||
res = curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, m_timeout);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set timeout: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
// 设置响应数据回调
|
||||
std::string responseBody;
|
||||
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set write callback: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set write data: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
// 设置响应头回调
|
||||
std::string responseHeaders;
|
||||
res = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WriteCallback);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set header callback: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
res = curl_easy_setopt(curl, CURLOPT_HEADERDATA, &responseHeaders);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set header data: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
// 设置HTTP方法
|
||||
if (method == "GET") {
|
||||
// GET是默认方法,不需要额外设置
|
||||
} else if (method == "POST") {
|
||||
// 设置为POST方法
|
||||
res = curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set POST method: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
// 设置POST数据
|
||||
if (!body.empty()) {
|
||||
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set POST fields: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 对于其他HTTP方法,使用自定义请求
|
||||
res = curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set custom request method: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
// 如果是PUT或PATCH等需要请求体的方法
|
||||
if ((method == "PUT" || method == "PATCH") && !body.empty()) {
|
||||
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to set request body: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加请求头
|
||||
if (!contentType.empty()) {
|
||||
std::string contentTypeHeader = "Content-Type: " + contentType;
|
||||
headers = curl_slist_append(headers, contentTypeHeader.c_str());
|
||||
}
|
||||
|
||||
// 添加用户自定义头
|
||||
for (const auto& header : m_headers) {
|
||||
std::string headerStr = header.first + ": " + header.second;
|
||||
headers = curl_slist_append(headers, headerStr.c_str());
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
if (headers) {
|
||||
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
if (res != CURLE_OK) {
|
||||
curl_slist_free_all(headers);
|
||||
throw HttpException("Failed to set HTTP headers: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
}
|
||||
|
||||
// 执行请求
|
||||
res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to perform HTTP request: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
// 获取HTTP响应码
|
||||
long httpCode = 0;
|
||||
res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
|
||||
if (res != CURLE_OK) {
|
||||
throw HttpException("Failed to get HTTP response code: " + std::string(curl_easy_strerror(res)));
|
||||
}
|
||||
|
||||
response.statusCode = static_cast<int>(httpCode);
|
||||
response.body = responseBody;
|
||||
|
||||
// 解析响应头
|
||||
std::istringstream headerStream(responseHeaders);
|
||||
std::string line;
|
||||
|
||||
while (std::getline(headerStream, line) && line.length() > 1) {
|
||||
if (line.back() == '\r') {
|
||||
line.pop_back();
|
||||
}
|
||||
|
||||
size_t colonPos = line.find(':');
|
||||
if (colonPos != std::string::npos) {
|
||||
std::string key = line.substr(0, colonPos);
|
||||
std::string value = line.substr(colonPos + 1);
|
||||
|
||||
// 去除前后空格
|
||||
size_t start = value.find_first_not_of(" \t");
|
||||
size_t end = value.find_last_not_of(" \t");
|
||||
|
||||
if (start != std::string::npos && end != std::string::npos) {
|
||||
value = value.substr(start, end - start + 1);
|
||||
}
|
||||
|
||||
response.headers[key] = value;
|
||||
}
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
// 清理资源
|
||||
if (headers) {
|
||||
curl_slist_free_all(headers);
|
||||
}
|
||||
|
||||
if (curl) {
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
|
||||
throw HttpException(std::string("Libcurl request failed: ") + e.what());
|
||||
}
|
||||
|
||||
// 清理资源
|
||||
if (headers) {
|
||||
curl_slist_free_all(headers);
|
||||
}
|
||||
|
||||
if (curl) {
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
// HttpClient类实现
|
||||
HttpClient::HttpClient() {
|
||||
#ifdef USE_LIBCURL
|
||||
m_impl = std::make_unique<LibcurlHttpClientImpl>();
|
||||
#else
|
||||
m_impl = std::make_unique<SocketHttpClientImpl>();
|
||||
#endif
|
||||
}
|
||||
|
||||
void HttpClient::setBaseUrl(const std::string& baseUrl) {
|
||||
m_impl->setBaseUrl(baseUrl);
|
||||
}
|
||||
|
||||
void HttpClient::setTimeout(int timeout) {
|
||||
m_impl->setTimeout(timeout);
|
||||
}
|
||||
|
||||
void HttpClient::addHeader(const std::string& key, const std::string& value) {
|
||||
m_impl->addHeader(key, value);
|
||||
}
|
||||
|
||||
void HttpClient::clearHeaders() {
|
||||
m_impl->clearHeaders();
|
||||
}
|
||||
|
||||
HttpResponse HttpClient::get(const std::string& path,
|
||||
const std::map<std::string, std::string>& params) {
|
||||
return m_impl->get(path, params);
|
||||
}
|
||||
|
||||
HttpResponse HttpClient::post(const std::string& path,
|
||||
const std::map<std::string, std::string>& params,
|
||||
const std::string& body,
|
||||
const std::string& contentType) {
|
||||
return m_impl->post(path, params, body, contentType);
|
||||
}
|
||||
|
||||
HttpResponse HttpClient::postJson(const std::string& path,
|
||||
const std::map<std::string, std::string>& params,
|
||||
const std::string& jsonBody) {
|
||||
return m_impl->postJson(path, params, jsonBody);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
|
||||
148
src/main.cpp
148
src/main.cpp
@ -7,19 +7,17 @@
|
||||
#include <libgen.h>
|
||||
#include <iostream>
|
||||
#include <glog/logging.h>
|
||||
#include "device_manager/device_manager.h"
|
||||
#include "monitor/monitor_manager.h"
|
||||
#include "service/grpc_camera_service.h"
|
||||
#include "service/grpc_system_service.h"
|
||||
#include "service/grpc_speaker_service.h"
|
||||
#include "service/grpc_microphone_service.h"
|
||||
#include "service/grpc_dexhand_service.h"
|
||||
#include "utils/base/logger.h"
|
||||
#include "service/grpc_head_service.h"
|
||||
#include "service/grpc_humanoid_robot_service.h"
|
||||
#include "service/grpc_hlc_service.h"
|
||||
|
||||
#include "http/httpclient.h"
|
||||
#include "device_manager/include/device_manager.h"
|
||||
#include "monitor_manager/include/monitor_manager.h"
|
||||
#include "service/include/grpc_camera_service.h"
|
||||
#include "service/include/grpc_system_service.h"
|
||||
#include "service/include/grpc_speaker_service.h"
|
||||
#include "service/include/grpc_microphone_service.h"
|
||||
#include "service/include/grpc_dexhand_service.h"
|
||||
#include "utils/base/include/logger.h"
|
||||
#include "service/include/grpc_head_service.h"
|
||||
#include "service/include/grpc_humanoid_robot_service.h"
|
||||
#include "service/include/grpc_hlc_service.h"
|
||||
#include "json/json.h"
|
||||
#include <grpcpp/ext/proto_server_reflection_plugin.h>
|
||||
void runServer(const XmlNode &cfg){
|
||||
@ -75,130 +73,6 @@ void runServer(const XmlNode &cfg){
|
||||
}
|
||||
}
|
||||
|
||||
int test_httpclient() {
|
||||
try {
|
||||
std::cout << "HTTP Client Example" << std::endl;
|
||||
std::cout << "===================" << std::endl;
|
||||
|
||||
// 创建HTTP客户端
|
||||
cmvr::HttpClient client;
|
||||
|
||||
// 设置基础URL - 使用Reqres.in替代JSONPlaceholder
|
||||
client.setBaseUrl("https://reqres.in/api");
|
||||
|
||||
// 设置超时时间为15秒(增加超时时间以应对网络波动)
|
||||
client.setTimeout(15000);
|
||||
|
||||
// 示例1: GET请求 - 获取用户列表
|
||||
std::cout << "\n=== Example 1: GET Request (List Users) ===" << std::endl;
|
||||
cmvr::HttpResponse response = client.get("/users?page=1");
|
||||
|
||||
std::cout << "Status: " << response.statusCode << " " << response.statusText << std::endl;
|
||||
std::cout << "Content-Type: " << response.headers["Content-Type"] << std::endl;
|
||||
std::cout << "Response Body: " << std::endl << response.body << std::endl;
|
||||
|
||||
// 示例2: POST JSON请求 - 创建用户
|
||||
std::cout << "\n=== Example 2: POST JSON Request (Create User) ===" << std::endl;
|
||||
|
||||
// 使用JsonCpp创建JSON数据
|
||||
Json::Value postData;
|
||||
postData["name"] = "John Doe";
|
||||
postData["job"] = "Software Engineer";
|
||||
|
||||
// 将JSON转换为字符串
|
||||
Json::StreamWriterBuilder writer;
|
||||
std::string jsonString = Json::writeString(writer, postData);
|
||||
|
||||
response = client.postJson("/users", {}, jsonString);
|
||||
|
||||
std::cout << "Status: " << response.statusCode << " " << response.statusText << std::endl;
|
||||
std::cout << "Content-Type: " << response.headers["Content-Type"] << std::endl;
|
||||
std::cout << "Response Body: " << std::endl << response.body << std::endl;
|
||||
|
||||
// 示例3: 解析JSON响应 - 获取单个用户
|
||||
std::cout << "\n=== Example 3: Parsing JSON Response (Single User) ===" << std::endl;
|
||||
response = client.get("/users/2");
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// 使用JsonCpp解析JSON字符串
|
||||
Json::Value user;
|
||||
Json::CharReaderBuilder reader;
|
||||
std::string errors;
|
||||
|
||||
std::istringstream stream(response.body);
|
||||
bool parsingSuccessful = Json::parseFromStream(reader, stream, &user, &errors);
|
||||
|
||||
if (parsingSuccessful) {
|
||||
Json::Value data = user["data"];
|
||||
std::cout << "User ID: " << data["id"].asInt() << std::endl;
|
||||
std::cout << "Email: " << data["email"].asString() << std::endl;
|
||||
std::cout << "First Name: " << data["first_name"].asString() << std::endl;
|
||||
std::cout << "Last Name: " << data["last_name"].asString() << std::endl;
|
||||
} else {
|
||||
std::cerr << "Failed to parse JSON: " << errors << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// 示例4: PUT请求 - 更新用户
|
||||
std::cout << "\n=== Example 4: PUT Request (Update User) ===" << std::endl;
|
||||
|
||||
Json::Value updateData;
|
||||
updateData["name"] = "Jane Doe";
|
||||
updateData["job"] = "Senior Software Engineer";
|
||||
|
||||
std::string updateJson = Json::writeString(writer, updateData);
|
||||
|
||||
response = client.postJson("/users/2", {}, updateJson); // Reqres.in使用POST处理PUT请求
|
||||
|
||||
std::cout << "Status: " << response.statusCode << " " << response.statusText << std::endl;
|
||||
std::cout << "Content-Type: " << response.headers["Content-Type"] << std::endl;
|
||||
std::cout << "Response Body: " << std::endl << response.body << std::endl;
|
||||
|
||||
// 示例5: 测试HttpBin.org - 获取请求头信息
|
||||
std::cout << "\n=== Example 5: HttpBin.org (Request Headers) ===" << std::endl;
|
||||
|
||||
// 切换到HttpBin.org
|
||||
client.setBaseUrl("https://httpbin.org");
|
||||
|
||||
// 添加自定义请求头
|
||||
client.clearHeaders();
|
||||
client.addHeader("X-Test-Header", "Hello from HttpClient");
|
||||
client.addHeader("Accept", "application/json");
|
||||
|
||||
response = client.get("/headers");
|
||||
|
||||
std::cout << "Status: " << response.statusCode << " " << response.statusText << std::endl;
|
||||
std::cout << "Content-Type: " << response.headers["Content-Type"] << std::endl;
|
||||
std::cout << "Response Body: " << std::endl << response.body << std::endl;
|
||||
|
||||
// 示例6: 测试超时与错误处理
|
||||
std::cout << "\n=== Example 6: Timeout & Error Handling ===" << std::endl;
|
||||
|
||||
// 设置较短的超时时间
|
||||
client.setTimeout(2000); // 2秒超时
|
||||
|
||||
try {
|
||||
// 请求一个会延迟响应的端点
|
||||
response = client.get("/delay/5"); // 服务器会延迟5秒响应
|
||||
|
||||
std::cout << "Status: " << response.statusCode << " " << response.statusText << std::endl;
|
||||
} catch (const cmvr::HttpException& e) {
|
||||
std::cout << "Error (Expected Timeout): " << e.what() << std::endl;
|
||||
}
|
||||
|
||||
// 恢复正常超时
|
||||
client.setTimeout(15000);
|
||||
|
||||
} catch (const cmvr::HttpException& e) {
|
||||
std::cerr << "HTTP Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::string config_path;
|
||||
if(argc == 1) {
|
||||
|
||||
@ -1,16 +1,2 @@
|
||||
# 首先添加子目录,确保disk_monitor目标被定义
|
||||
add_subdirectory(diskmonitor)
|
||||
|
||||
# 然后定义monitor静态库并链接依赖
|
||||
add_library(monitor STATIC
|
||||
monitor_factory.cpp
|
||||
monitor_manager.cpp
|
||||
)
|
||||
|
||||
target_include_directories(monitor PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_link_libraries(monitor PRIVATE
|
||||
cmvr_es::monitor::disk_monitor
|
||||
)
|
||||
|
||||
add_library(cmvr_es::monitor ALIAS monitor)
|
||||
@ -8,7 +8,7 @@
|
||||
#include <mutex>
|
||||
#include <glog/logging.h>
|
||||
#include "rapidxml/xml_parser.h"
|
||||
#include "../utils/base/timer.h"
|
||||
#include "../utils/base/include/timer.h"
|
||||
|
||||
namespace cmvr::monitor{
|
||||
class AbstractMonitor {
|
||||
@ -1,4 +1,4 @@
|
||||
add_library(disk_monitor SHARED disk_monitor.cpp)
|
||||
add_library(disk_monitor SHARED src/disk_monitor.cpp)
|
||||
|
||||
target_include_directories(disk_monitor PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
#include <condition_variable>
|
||||
|
||||
#include "monitor/abstract_monitor.h"
|
||||
#include "../../abstract_monitor.h"
|
||||
|
||||
namespace cmvr::monitor {
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// Created by linbo on 2025/6/18.
|
||||
//
|
||||
|
||||
#include "disk_monitor.h"
|
||||
#include "../include/disk_monitor.h"
|
||||
#include <filesystem>
|
||||
#include <queue>
|
||||
#include <sys/stat.h>
|
||||
13
src/monitor_manager/CMakeLists.txt
Normal file
13
src/monitor_manager/CMakeLists.txt
Normal file
@ -0,0 +1,13 @@
|
||||
# 然后定义monitor静态库并链接依赖
|
||||
add_library(monitor_manager STATIC
|
||||
src/monitor_factory.cpp
|
||||
src/monitor_manager.cpp
|
||||
)
|
||||
|
||||
target_include_directories(monitor_manager PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
target_link_libraries(monitor_manager PRIVATE
|
||||
cmvr_es::monitor::disk_monitor
|
||||
)
|
||||
|
||||
add_library(cmvr_es::monitor_manager ALIAS monitor_manager)
|
||||
@ -6,7 +6,7 @@
|
||||
#define MONITOR_FACTORY_H
|
||||
|
||||
|
||||
#include "monitor/abstract_monitor.h"
|
||||
#include "../../monitor/abstract_monitor.h"
|
||||
|
||||
namespace cmvr::monitor
|
||||
{
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
#include <unordered_map>
|
||||
#include "rapidxml/xml_parser.h"
|
||||
#include "monitor/monitor_factory.h"
|
||||
#include "monitor_factory.h"
|
||||
|
||||
namespace cmvr::monitor {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user