使用boost::mp11::mp_transform_q的示例程序(C/C++)

175 篇文章 ¥59.90 ¥99.00
本文介绍了一个使用Boost库中的mp_transform_q功能进行元编程的C/C++示例。通过修改结构体成员值,展示了如何在类型转换过程中实现特定操作。程序通过编译和运行,验证了mp_transform_q的正确性和在元编程任务中的应用潜力。

使用boost::mp11::mp_transform_q的示例程序(C/C++)

#include <iostream>
#include <boost/mp11.hpp>

// 要在转换过程中修改的结构体
struct MyStruct {
   
   
    int value;
}
我将你给我的代码加到了原本的代码里,解决了VS code里红线报错的错误后,进行编译报错:ld returned 1 exit status。下面完整代码,请你帮我看看哪里有错误。 #include <booster/robot/b1/b1_loco_client.hpp> #include <booster/idl/b1/RemoteControllerState.h> #include <booster/robot/channel/channel_subscriber.hpp> #include <chrono> #include <iostream> #include <thread> #include <sstream> #include <string> #include <vector> #include <boost/iterator/detail/config_def.hpp> // 假设 MoveHandEndEffectorWithAux 已在其他模块定义 extern bool MoveHandEndEffectorWithAux(double x, double y, double z, const std::string&amp; aux_data = ""); // 常量定义默认位置 constexpr double DEFAULT_X = 0.0; constexpr double DEFAULT_Y = 0.0; constexpr double DEFAULT_Z = 0.0; // 命令解析函数 void handleArmCommand(const std::string&amp; input) { std::istringstream iss(input); std::string command; iss >> command; // 提取主命令 if (command == "tp") { // 执行 tp 命令:移动到默认位置 (0,0,0) if (MoveHandEndEffectorWithAux(DEFAULT_X, DEFAULT_Y, DEFAULT_Z)) { std::cout << "Arm moved to default position (0,0,0)." << std::endl; } else { std::cerr << "Error: Failed to move arm to default position." << std::endl; } } else if (command == "move") { // 解析 move 命令参数:move x y z [aux_data] double x, y, z; std::string aux_data; if (iss >> x >> y >> z) { // 可选解析辅助参数 if (iss.peek() != EOF) { std::getline(iss >> std::ws, aux_data); // 读取剩余内容作为 aux_data } if (MoveHandEndEffectorWithAux(x, y, z, aux_data)) { std::cout << "Arm moved to position: (" << x << ", " << y << ", " << z << ")" << std::endl; } else { std::cerr << "Error: Failed to execute move command." << std::endl; } } else { std::cerr << "Invalid arguments. Usage: move <x> <y> <z> [aux_data]" << std::endl; } } else { std::cerr << "Unknown command. Supported: 'tp', 'move x y z'." << std::endl; } } void HandRock(booster::robot::b1::B1LocoClient &amp;client) { // Example: open hand (rock gesture) client.WaveHand(booster::robot::b1::HandAction::kHandOpen); } void HandScissor(booster::robot::b1::B1LocoClient &amp;client) { // Example: close hand (scissor gesture) client.WaveHand(booster::robot::b1::HandAction::kHandClose); } void HandPaper(booster::robot::b1::B1LocoClient &amp;client) { // Example: handshake open (paper gesture) client.Handshake(booster::robot::b1::HandAction::kHandOpen); } void ControlArmOnly(booster::robot::b1::B1LocoClient &amp;client) { int32_t res = 0; // ===== 手臂控制参数区 =====(小白修改这里) // 位置参数 (xyz坐标,单位:米) booster::robot::Position high_position(0.15, 0.15, 0.2); // 举高位置 booster::robot::Position low_position(0.0, 0.0, 0.0); // 放低位置 // 方向参数 (欧拉角) booster::robot::Orientation high_orientation(0.0, 1.0, 0.0); // 举高朝向 booster::robot::Orientation low_orientation(0.0, 0.0, 0.0); // 放低朝向 // 时间参数 (单位:毫秒) int move_duration = 1500; // 手臂移动时间 ↓↓调小值加速运动 int hold_duration = 6000; // 姿势保持时间 // 力矩参数 (示例接口,具体实现需查SDK) ↓↓调大值增加力量 float force_level = 15.0f; // 牛顿·米 std::cout << "=== 启动手臂控制程序 ===" << std::endl; // 启用末端控制模式 std::cout << "启用手臂精细控制..." << std::endl; res = client.SwitchHandEndEffectorControlMode(true); if (res != 0) { std::cout << "控制模式启用失败: error = " << res << std::endl; return; } std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // ===== 右手控制段 ===== std::cout << ">> 右手上举动作..." << std::endl; booster::robot::Posture right_hand_up; right_hand_up.position_ = high_position; // 修改位置坐标 right_hand_up.orientation_ = high_orientation; // 修改方向角度 // 设置末端力矩 (接口示例) // client.SetHandForce(booster::robot::b1::HandIndex::kRightHand, force_level); // 此接口不存在,已注释 res = client.MoveHandEndEffector( right_hand_up, move_duration, // 修改运动速度(时间越短越快) booster::robot::b1::HandIndex::kRightHand ); if (res != 0) std::cout << "上举失败: " << res << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(hold_duration)); // 保持姿势 // ===== 左手控制段 ===== std::cout << ">> 左手上举动作..." << std::endl; booster::robot::Posture left_hand_up; left_hand_up.position_ = booster::robot::Position( high_position.x_, -high_position.y_, // y坐标取反(镜像到左侧) high_position.z_ ); left_hand_up.orientation_ = high_orientation; // client.SetHandForce(booster::robot::b1::HandIndex::kLeftHand, force_level); // 此接口不存在,已注释 res = client.MoveHandEndEffector( left_hand_up, move_duration, booster::robot::b1::HandIndex::kLeftHand ); if (res != 0) std::cout << "上举失败: " << res << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(hold_duration)); // 复位手臂 std::cout << ">> 手臂复位..." << std::endl; booster::robot::Posture tar_posture; tar_posture.position_ = low_position; tar_posture.orientation_ = low_orientation; res = client.MoveHandEndEffectorV2(tar_posture, 3000, booster::robot::b1::HandIndex::kRightHand); // 关闭控制模式 client.SwitchHandEndEffectorControlMode(false); std::cout << "=== 手臂控制完成 ===" << std::endl; } int main(int argc, char const *argv[]) { if (argc < 2) { std::cout << "Usage: " << argv[0] << " networkInterface" << std::endl; exit(-1); } booster::robot::ChannelFactory::Instance()->Init(0, argv[1]); booster::robot::b1::B1LocoClient client; client.Init(); float x, y, z, yaw, pitch; int32_t res = 0; std::string input; int32_t hand_action_count = 0; while (true) { bool need_print = false; std::getline(std::cin, input); if (!input.empty()) { if (input == "mp") { res = client.ChangeMode(booster::robot::RobotMode::kPrepare); } else if (input == "md") { res = client.ChangeMode(booster::robot::RobotMode::kDamping); } else if (input == "mw") { res = client.ChangeMode(booster::robot::RobotMode::kWalking); } else if (input == "mc") { res = client.ChangeMode(booster::robot::RobotMode::kCustom); } else if (input == "w") { x = 1.0; y = 0.0; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "ww") { x = 3.0; y = 0.0; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "l") { x = 0.0; y = 0.0; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "a") { x = 0.0; y = 0.5; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "aa") { x = 0.0; y = 1.5; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "s") { x = -1.0; y = 0.0; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "ss") { x = -2.5; y = 0.0; z = 0.0; need_print = true; res = client.Move(x, y, z); } if (input == "d") { x = 0.0; y = -0.5; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "dd") { x == 0.0; y = -1.5; z = 0.0; need_print = true; res = client.Move(x, y, z); } else if (input == "q") { x = 0.0; y = 0.0; z = 1.0; need_print = true; res = client.Move(x, y, z); } else if (input == "e") { x = 0.0; y = 0.0; z = -1.0; need_print = true; res = client.Move(x, y, z); } else if (input == "hd") { yaw = 0.0; pitch = 1.0; need_print = true; res = client.RotateHead(pitch, yaw); } else if (input == "hu") { yaw = 0.0; pitch = -0.3; need_print = true; res = client.RotateHead(pitch, yaw); } else if (input == "hr") { yaw = -0.785; pitch = 0.0; need_print = true; res = client.RotateHead(pitch, yaw); } else if (input == "hl") { yaw = 0.785; pitch = 0.0; need_print = true; res = client.RotateHead(pitch, yaw); } else if (input == "ho") { yaw = 0.0; pitch = 0.0; need_print = true; res = client.RotateHead(pitch, yaw); } else if (input == "wh") { res = client.WaveHand(booster::robot::b1::HandAction::kHandOpen); } else if (input == "ch") { res = client.WaveHand(booster::robot::b1::HandAction::kHandClose); } else if (input =="hso") { res = client.Handshake(booster::robot::b1::HandAction::kHandOpen); } else if (input =="hsc") { res = client.Handshake(booster::robot::b1::HandAction::kHandClose); } else if (input == "ld") { res = client.LieDown(); } else if (input == "gu") { res = client.GetUp(); } else if (input == "mhel") { booster::robot::Posture tar_posture; tar_posture.position_ = booster::robot::Position(0.35, 0.25, 0.1); tar_posture.orientation_ = booster::robot::Orientation(-1.57, -1.57, 0.); res = client.MoveHandEndEffectorV2( tar_posture, 2000, booster::robot::b1::HandIndex::kLeftHand); tar_posture.position_ = booster::robot::Position(0.35, -0.2, 0.1); tar_posture.orientation_ = booster::robot::Orientation(1.57, -1.57, 0.); res = client.MoveHandEndEffectorV2( tar_posture, 2000, booster::robot::b1::HandIndex::kRightHand); } else if (input == "gopenl") { booster::robot::b1::GripperMotionParameter motion_param; motion_param.position_ = 500; motion_param.force_ = 100; motion_param.speed_ = 100; res = client.ControlGripper( motion_param, booster::robot::b1::GripperControlMode::kPosition, booster::robot::b1::HandIndex::kLeftHand); } else if (input == "gft") { booster::robot::Frame src = booster::robot::Frame::kBody; booster::robot::Frame dst = booster::robot::Frame::kRightHand; booster::robot::Transform transform; res = client.GetFrameTransform(src, dst, transform); if (res == 0) { std::cout << "pos:" << transform.position_.x_ << " " << transform.position_.y_ << " " << transform.position_.z_ << std::endl; std::cout << "ori:" << transform.orientation_.x_ << " " << transform.orientation_.y_ << " " << transform.orientation_.z_ << " " << transform.orientation_.w_ << std::endl; } } else if (input == "hcm-start") { res = client.SwitchHandEndEffectorControlMode(true); } else if (input == "hcm-stop") { res = client.SwitchHandEndEffectorControlMode(false); } else if (input == "hand-down") { booster::robot::Posture tar_posture; tar_posture.position_ = booster::robot::Position(0.28, -0.25, 0.05); tar_posture.orientation_ = booster::robot::Orientation(0., 0., 0.); res = client.MoveHandEndEffector( tar_posture, 1000, booster::robot::b1::HandIndex::kRightHand); std::this_thread::sleep_for(std::chrono::milliseconds(300)); hand_action_count++; int random = rand() % 3; if (random == 0) { HandRock(client); } else if (random == 1) { HandScissor(client); } else { HandPaper(client); } } else if (input == "handl-down") { booster::robot::Posture tar_posture; tar_posture.position_ = booster::robot::Position(0.28, 0.25,0.05); tar_posture.orientation_ = booster::robot::Orientation(0.,0.,0.); res = client.MoveHandEndEffector( tar_posture, 1000, booster::robot::b1::HandIndex::kLeftHand); } else if (input == "hand-up") { booster::robot::Posture tar_posture; tar_posture.position_ = booster::robot::Position(0.25, -0.3, 0.25); tar_posture.orientation_ = booster::robot::Orientation(0., -1.0, 0.); res = client.MoveHandEndEffector( tar_posture, 1000, booster::robot::b1::HandIndex::kRightHand); std::this_thread::sleep_for(std::chrono::milliseconds(300)); HandPaper(client); } else if (input == "handl-up") { booster::robot::Posture tar_posture; tar_posture.position_ = booster::robot::Position(0.25, 0.3, 0.25); tar_posture.orientation_ = booster::robot::Orientation(0., -1.0, 0.); res = client.MoveHandEndEffector( tar_posture, 1000, booster::robot::b1::HandIndex::kLeftHand); } else if (input == "arm") { ControlArmOnly(client); } if (need_print) { std::cout << "Param: " << x << " " << y << " " << z << std::endl; std::cout << "Head param: " << pitch << " " << yaw << std::endl; } if (res != 0) { std::cout << "Request failed: error = " << res << std::endl; } if (input == "exit") break; handleArmCommand(input); // 调用命令处理函数 } } return 0; }
10-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值