cmvr-es/src/devices/robot/controller/jointpositioncontroller.cpp

109 lines
3.9 KiB
C++
Raw Normal View History

2025-10-24 14:37:17 +08:00
//
// Created by linbo on 2025/10/24.
//
#include "jointpositioncontroller.h"
2025-10-29 16:26:15 +08:00
#include "hardware/can/can_manager.h"
#include "hardware_manager/hardware_manager.h"
2025-10-24 14:37:17 +08:00
using namespace std;
using namespace cmvr::device;
2025-10-29 16:26:15 +08:00
using namespace cmvr::hardware;
2025-10-24 14:37:17 +08:00
JointPositionController::JointPositionController(const XmlNode& cfg):AbstractController(cfg)
{
}
2025-10-24 17:06:17 +08:00
void JointPositionController::call(const Json::Value& json)
2025-10-24 14:37:17 +08:00
{
2025-11-07 11:28:18 +08:00
try
{
if (state_ != ControllerState_Idle)
return;
2025-10-29 16:26:15 +08:00
2025-11-07 11:28:18 +08:00
//解析json
/*
*
"params":{
"canGroupId":["leftArm","rightArm"],
"motors":[{"joint_name":"","position":0.01,"velocity"0.8}]
},
**/
std::string protocolType;
if (json.isMember("params"))
2025-11-06 15:58:32 +08:00
{
2025-11-07 11:28:18 +08:00
std::vector<std::string> canGroupIds;
const Json::Value& params = json["params"];
if (params.isMember("canGroupId"))
{
for (const auto & canGroupId : params["canGroupId"])
{
canGroupIds.emplace_back(canGroupId.asString());
}
}
//先把所有电机信息都找到
if (params.isMember("motors"))
2025-11-06 15:58:32 +08:00
{
2025-11-07 11:28:18 +08:00
for (int i = 0; i < params["motors"].size(); i++)
{
const Json::Value& motorJson = params["motors"][i];
std::string joint_name = motorJson["joint_name"].asString();
double pos = motorJson["position"].asDouble();
double vel = motorJson["velocity"].asDouble();
for (const auto& canGroupId : canGroupIds)
{
try
{
auto canGroup = HardWareManager::getInstance().getCanGroup(canGroupId);
auto motorInfo = canGroup->getMotorInfo(joint_name);
motorInfo.motorProtocol->setMode(motorInfo.node_id, msgs::RUN_MODE_PROFILE_POSITION);
motorInfo.motorProtocol->setQd(motorInfo.node_id, vel);
//考虑限位
if (pos > motorInfo.limitQUb)
{
pos = motorInfo.limitQUb;
LOG(WARNING) << "JointPositionController[call]: Joint [" << joint_name
<< "] target position exceeds upper limit! Current target: " << pos
<< ", upper limit: " << motorInfo.limitQUb;
}
else if (pos < motorInfo.limitQLb) // Assume lower limit field exists (limitQLb)
{
pos = motorInfo.limitQLb;
LOG(WARNING) << "JointPositionController[call]: Joint [" << joint_name
<< "] target position exceeds lower limit! Current target: " << pos
<< ", lower limit: " << motorInfo.limitQLb;
}
motorInfo.motorProtocol->setQ(motorInfo.node_id, pos);
break;
}
catch (const std::exception& e)
{
continue; // 捕获异常,继续尝试下一个 canGroup
}
}
}
2025-11-06 15:58:32 +08:00
}
}
2025-10-29 16:26:15 +08:00
2025-11-07 11:28:18 +08:00
}
catch (const std::exception& e)
2025-11-06 15:58:32 +08:00
{
2025-11-07 11:28:18 +08:00
LOG(ERROR)<<"[JointPositionController](call):" <<e.what();
throw runtime_error(e.what());
2025-11-06 15:58:32 +08:00
}
2025-10-29 16:26:15 +08:00
2025-10-24 14:37:17 +08:00
}
2025-10-29 16:26:15 +08:00
void JointPositionController::interrupt()
2025-10-24 14:37:17 +08:00
{
2025-10-29 16:26:15 +08:00
state_ = ControllerState_Switching;
2025-10-24 14:37:17 +08:00
2025-10-29 16:26:15 +08:00
state_ = ControllerState_Idle;
}
void JointPositionController::stop()
{
state_ = ControllerState_Idle;
2025-10-24 14:37:17 +08:00
}