feat(motor): select mixed-protocol groups by motor system ID
Preserve exact single-group selection and allow a distinct MotorConfig.id to select all groups in a dedicated configuration. Share validation with DeviceFactory and reject ambiguous group IDs, duplicate motor IDs and duplicate joint names. All 9 motor-manager shutdown and group-selection tests pass; cmvr_es builds with -j8.
This commit is contained in:
parent
bbcc716195
commit
d6a1635b1b
@ -25,3 +25,5 @@ install(TARGETS motor_manager LIBRARY DESTINATION lib)
|
||||
add_executable(motor_manager_shutdown_test src/motor_manager_shutdown_test.cpp)
|
||||
target_link_libraries(motor_manager_shutdown_test PRIVATE
|
||||
cmvr_es::device::motor_manager cmvr_es::device::lingkong_can_motor_driver cmvr_es::device::canbus gtest gtest_main pthread)
|
||||
|
||||
target_compile_definitions(motor_manager_shutdown_test PRIVATE CMVR_ES_SOURCE_DIR="${PROJECT_SOURCE_DIR}")
|
||||
|
||||
@ -39,6 +39,10 @@ public:
|
||||
DeviceKind kind() const noexcept override { return DeviceKind::MotorSystem; }
|
||||
std::string typeName() const override { return "MotorManager"; }
|
||||
|
||||
// Existing group ID selects one group. A distinct MotorConfig.id selects
|
||||
// all groups in that dedicated configuration as one motor system.
|
||||
static std::vector<const config::MotorGroupConfig*> selectGroups(
|
||||
const config::MotorConfig& config, const std::string& selected_id);
|
||||
bool init() override;
|
||||
bool stop() override;
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
|
||||
#include "common/base/logging/logger.h"
|
||||
@ -45,6 +46,31 @@ MotorManager::MotorManager(std::string id,
|
||||
|
||||
MotorManager::~MotorManager() = default;
|
||||
|
||||
std::vector<const config::MotorGroupConfig*> MotorManager::selectGroups(
|
||||
const config::MotorConfig& cfg, const std::string& selected_id)
|
||||
{
|
||||
if (selected_id.empty()) return {};
|
||||
std::vector<const config::MotorGroupConfig*> exact;
|
||||
for (const auto& group : cfg.motor_groups())
|
||||
if (group.id() == selected_id) exact.push_back(&group);
|
||||
if (exact.size() > 1) return {};
|
||||
if (exact.size() == 1) return exact; // Existing single-group semantics win.
|
||||
if (cfg.id() != selected_id || cfg.motor_groups().empty()) return {};
|
||||
std::vector<const config::MotorGroupConfig*> selected;
|
||||
std::unordered_set<std::string> groups, joints;
|
||||
std::unordered_set<int> ids;
|
||||
for (const auto& group : cfg.motor_groups()) {
|
||||
if (group.id().empty() || !groups.insert(group.id()).second ||
|
||||
!group.has_motors() || group.motors().motors().empty()) return {};
|
||||
for (const auto& motor : group.motors().motors()) {
|
||||
if (motor.id() <= 0 || motor.joint_name().empty() ||
|
||||
!ids.insert(motor.id()).second || !joints.insert(motor.joint_name()).second) return {};
|
||||
}
|
||||
selected.push_back(&group);
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
bool MotorManager::init()
|
||||
{
|
||||
if (initialized_) {
|
||||
@ -71,14 +97,15 @@ bool MotorManager::init()
|
||||
motors_by_joint_.clear();
|
||||
}
|
||||
|
||||
const auto selected_groups = selectGroups(cfg_, selected_group_id_);
|
||||
if (selected_groups.empty()) {
|
||||
CMVR_LOG(ERROR) << "[MotorManager] invalid or ambiguous motor group selection: " << selected_group_id_;
|
||||
return false;
|
||||
}
|
||||
bool all_ok = true;
|
||||
std::size_t selected_group_count = 0;
|
||||
for (const auto& motor_group_cfg : cfg_.motor_groups()) {
|
||||
for (const auto* selected_group : selected_groups) {
|
||||
const auto& motor_group_cfg = *selected_group;
|
||||
const auto& group_name = motor_group_cfg.id();
|
||||
if (group_name != selected_group_id_) {
|
||||
continue;
|
||||
}
|
||||
++selected_group_count;
|
||||
if (!motor_group_cfg.has_motors()) {
|
||||
CMVR_LOG(ERROR) << "[MotorManager] motor group missing motors: " << group_name;
|
||||
all_ok = false;
|
||||
@ -166,11 +193,6 @@ bool MotorManager::init()
|
||||
bus_runtimes_.push_back(std::move(bus_runtime));
|
||||
}
|
||||
|
||||
if (selected_group_count != 1) {
|
||||
CMVR_LOG(ERROR) << "[MotorManager] selected motor group '" << selected_group_id_
|
||||
<< "' must occur exactly once in config: " << id_;
|
||||
all_ok = false;
|
||||
}
|
||||
|
||||
if (!all_ok) {
|
||||
for (auto& bus_runtime : bus_runtimes_) {
|
||||
|
||||
@ -84,6 +84,43 @@ TEST(MotorManagerShutdown, LingkongDisabledOrMissingMotorNeverAbortsGroupStop) {
|
||||
}
|
||||
}
|
||||
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
namespace {
|
||||
config::MotorConfig mixedConfig() {
|
||||
config::MotorConfig cfg;cfg.set_id("mixed");
|
||||
for(int part=0;part<2;++part){
|
||||
auto* group=cfg.add_motor_groups();group->set_id(part==0?"old":"new");
|
||||
group->set_protocol(part==0?config::MOTOR_PROTOCOL_LINGKONG_CAN:config::MOTOR_PROTOCOL_LINGKONG_CAN_V2_38);
|
||||
group->set_bus_type(config::MOTOR_BUS_CAN);group->set_vendor(config::MOTOR_VENDOR_LINGKONG);group->mutable_can()->set_channel_id(3);
|
||||
for(int id=(part==0?1:2);id<=(part==0?1:3);++id){auto* m=group->mutable_motors()->add_motors();m->set_id(id);m->set_joint_name("axis_"+std::to_string(id));}
|
||||
}
|
||||
return cfg;
|
||||
}
|
||||
TEST(MotorGroupSelection, MixedSystemSelectsBothProtocolsInOneManager){
|
||||
auto cfg=mixedConfig();auto groups=MotorManager::selectGroups(cfg,"mixed");ASSERT_EQ(groups.size(),2);
|
||||
EXPECT_EQ(groups[0]->protocol(),config::MOTOR_PROTOCOL_LINGKONG_CAN);EXPECT_EQ(groups[1]->protocol(),config::MOTOR_PROTOCOL_LINGKONG_CAN_V2_38);
|
||||
EXPECT_EQ(groups[0]->motors().motors(0).id(),1);EXPECT_EQ(groups[1]->motors().motors_size(),2);
|
||||
}
|
||||
TEST(MotorGroupSelection, ExistingGroupSelectionStillLoadsOnlyItsOwnGroup){
|
||||
auto cfg=mixedConfig();auto groups=MotorManager::selectGroups(cfg,"old");ASSERT_EQ(groups.size(),1);EXPECT_EQ(groups[0]->id(),"old");
|
||||
cfg.set_id("old");groups=MotorManager::selectGroups(cfg,"old");ASSERT_EQ(groups.size(),1);EXPECT_EQ(groups[0]->id(),"old");
|
||||
}
|
||||
TEST(MotorGroupSelection, UnknownEmptyOrDuplicateGroupsAreRejected){
|
||||
auto cfg=mixedConfig();EXPECT_TRUE(MotorManager::selectGroups(cfg,"missing").empty());EXPECT_TRUE(MotorManager::selectGroups(cfg,"").empty());
|
||||
cfg.mutable_motor_groups(1)->set_id("old");EXPECT_TRUE(MotorManager::selectGroups(cfg,"old").empty());EXPECT_TRUE(MotorManager::selectGroups(cfg,"mixed").empty());
|
||||
}
|
||||
TEST(MotorGroupSelection, AggregateRejectsDuplicateMotorIdsOrJointNames){
|
||||
auto cfg=mixedConfig();cfg.mutable_motor_groups(1)->mutable_motors()->mutable_motors(0)->set_id(1);EXPECT_TRUE(MotorManager::selectGroups(cfg,"mixed").empty());
|
||||
cfg=mixedConfig();cfg.mutable_motor_groups(1)->mutable_motors()->mutable_motors(0)->set_joint_name("axis_1");EXPECT_TRUE(MotorManager::selectGroups(cfg,"mixed").empty());
|
||||
}
|
||||
TEST(MotorGroupSelection, NewDeploymentFileParsesAndHasRequestedProtocolsAndLimits){
|
||||
std::ifstream input(std::string(CMVR_ES_SOURCE_DIR)+"/cmvr-es/config/devices/motor/lingkong_mixed_motors.pb.txt");ASSERT_TRUE(input.good());std::ostringstream text;text<<input.rdbuf();config::MotorRootConfig root;
|
||||
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(text.str(),&root));auto groups=MotorManager::selectGroups(root.motor(),"lingkong_mixed_motors");ASSERT_EQ(groups.size(),2);
|
||||
for(auto* group:groups){EXPECT_EQ(group->can().channel_id(),3);for(const auto& motor:group->motors().motors()){EXPECT_DOUBLE_EQ(motor.limit_q_lb(),-3.14);EXPECT_DOUBLE_EQ(motor.limit_q_ub(),3.14);EXPECT_DOUBLE_EQ(motor.gear_ratio(),10);EXPECT_EQ(group->protocol(),motor.id()==1?config::MOTOR_PROTOCOL_LINGKONG_CAN:config::MOTOR_PROTOCOL_LINGKONG_CAN_V2_38);}}
|
||||
}
|
||||
}
|
||||
#include <future>
|
||||
#include <chrono>
|
||||
namespace {
|
||||
|
||||
@ -241,21 +241,8 @@ DeviceFactory::DeviceFactory()
|
||||
return DeviceRecord{};
|
||||
}
|
||||
|
||||
const config::MotorGroupConfig* selected_group = nullptr;
|
||||
for (const auto& group_cfg : root_cfg.motor().motor_groups()) {
|
||||
if (group_cfg.id() != entry.id()) {
|
||||
continue;
|
||||
}
|
||||
if (selected_group != nullptr) {
|
||||
CMVR_LOG(ERROR) << "[DeviceFactory]: Duplicate motor group id '"
|
||||
<< entry.id() << "' in config: " << entry.config_file();
|
||||
return DeviceRecord{};
|
||||
}
|
||||
selected_group = &group_cfg;
|
||||
}
|
||||
if (selected_group == nullptr) {
|
||||
CMVR_LOG(ERROR) << "[DeviceFactory]: Motor group id '" << entry.id()
|
||||
<< "' not found in config: " << entry.config_file();
|
||||
if (MotorManager::selectGroups(root_cfg.motor(), entry.id()).empty()) {
|
||||
CMVR_LOG(ERROR) << "[DeviceFactory]: Invalid motor group/system selection: " << entry.id();
|
||||
return DeviceRecord{};
|
||||
}
|
||||
CMVR_LOG(INFO) << "[DeviceFactory]: Read device config success";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user