Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/canbus/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cc_library(
"//cyber",
"//modules/canbus/common:canbus_common",
"//modules/canbus/vehicle:vehicle_factory",
"//modules/canbus/vehicle:vehicle_plugins",
"//modules/common/adapters:adapter_gflags",
"//modules/common/monitor_log",
"//modules/common/status",
Expand Down
1 change: 0 additions & 1 deletion modules/canbus/canbus_component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ bool CanbusComponent::Init() {
AINFO << "Can client is successfully created.";

VehicleFactory vehicle_factory;
vehicle_factory.RegisterVehicleFactory();
auto vehicle_object =
vehicle_factory.CreateVehicle(canbus_conf_.vehicle_parameter());
if (!vehicle_object) {
Expand Down
85 changes: 85 additions & 0 deletions modules/canbus/common/vehicle_detail_any.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2026 WheelOS. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Created Date: 2026-02-17
// Author: daohu527

#pragma once

#include <utility>

#include "modules/common_msgs/chassis_msgs/chassis_detail.pb.h"

namespace apollo {
namespace canbus {
namespace vehicle_detail {

template <typename T>
bool Has(const ChassisDetail& chassis) {
return chassis.has_vehicle_detail() && chassis.vehicle_detail().Is<T>();
}

template <typename T>
const T& Get(const ChassisDetail& chassis) {
thread_local T cached;
cached.Clear();
if (Has<T>(chassis)) {
chassis.vehicle_detail().UnpackTo(&cached);
}
return cached;
}

template <typename T>
bool UnpackTo(const ChassisDetail& chassis, T* detail) {
if (detail == nullptr) {
return false;
}
detail->Clear();
if (!Has<T>(chassis)) {
return false;
}
return chassis.vehicle_detail().UnpackTo(detail);
}

template <typename T>
class Editor {
public:
explicit Editor(ChassisDetail* chassis) : chassis_(chassis) {
if (chassis_ != nullptr && Has<T>(*chassis_)) {
chassis_->vehicle_detail().UnpackTo(&detail_);
}
}

~Editor() {
if (chassis_ != nullptr) {
chassis_->mutable_vehicle_detail()->PackFrom(detail_);
}
}

T* operator->() { return &detail_; }
T* get() { return &detail_; }

private:
ChassisDetail* chassis_ = nullptr;
T detail_;
};

template <typename T>
Editor<T> Edit(ChassisDetail* chassis) {
return Editor<T>(chassis);
}

} // namespace vehicle_detail
} // namespace canbus
} // namespace apollo
10 changes: 9 additions & 1 deletion modules/canbus/vehicle/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ cc_library(
copts = CANBUS_COPTS,
deps = [
":abstract_vehicle_factory",
"//modules/common/util:factory",
],
)

cc_library(
name = "vehicle_plugins",
deps = [
"//modules/canbus/vehicle/ch:ch_vehicle_factory",
"//modules/canbus/vehicle/devkit:devkit_vehicle_factory",
"//modules/canbus/vehicle/ge3:ge3_vehicle_factory",
Expand All @@ -56,8 +63,8 @@ cc_library(
"//modules/canbus/vehicle/wey:wey_vehicle_factory",
"//modules/canbus/vehicle/yunle:yunle_vehicle_factory",
"//modules/canbus/vehicle/zhongyun:zhongyun_vehicle_factory",
"//modules/common/util:factory",
],
alwayslink = True,
)

cc_test(
Expand All @@ -66,6 +73,7 @@ cc_test(
srcs = ["vehicle_factory_test.cc"],
deps = [
":vehicle_factory",
":vehicle_plugins",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
4 changes: 4 additions & 0 deletions modules/canbus/vehicle/ch/ch_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/ch/ch_controller.h"
#include "modules/canbus/vehicle/ch/ch_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -36,3 +37,6 @@ ChVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::CH,
apollo::canbus::ChVehicleFactory)
4 changes: 4 additions & 0 deletions modules/canbus/vehicle/devkit/devkit_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/devkit/devkit_controller.h"
#include "modules/canbus/vehicle/devkit/devkit_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -37,3 +38,6 @@ DevkitVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::DKIT,
apollo::canbus::DevkitVehicleFactory)
5 changes: 5 additions & 0 deletions modules/canbus/vehicle/ge3/ge3_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*****************************************************************************/

#include "modules/canbus/vehicle/ge3/ge3_vehicle_factory.h"

#include "modules/canbus/vehicle/ge3/ge3_controller.h"
#include "modules/canbus/vehicle/ge3/ge3_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -35,3 +37,6 @@ Ge3VehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::GE3,
apollo::canbus::Ge3VehicleFactory)
4 changes: 4 additions & 0 deletions modules/canbus/vehicle/gem/gem_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/gem/gem_controller.h"
#include "modules/canbus/vehicle/gem/gem_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -37,3 +38,6 @@ GemVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::GEM,
apollo::canbus::GemVehicleFactory)
4 changes: 4 additions & 0 deletions modules/canbus/vehicle/lexus/lexus_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/lexus/lexus_controller.h"
#include "modules/canbus/vehicle/lexus/lexus_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -37,3 +38,6 @@ LexusVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::LEXUS,
apollo::canbus::LexusVehicleFactory)
4 changes: 4 additions & 0 deletions modules/canbus/vehicle/lincoln/lincoln_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/lincoln/lincoln_controller.h"
#include "modules/canbus/vehicle/lincoln/lincoln_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -37,3 +38,6 @@ LincolnVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::LINCOLN_MKZ,
apollo::canbus::LincolnVehicleFactory)
4 changes: 4 additions & 0 deletions modules/canbus/vehicle/mk_mini/mk_mini_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/mk_mini/mk_mini_controller.h"
#include "modules/canbus/vehicle/mk_mini/mk_mini_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -37,3 +38,6 @@ Mk_miniVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::MK_MINI,
apollo::canbus::Mk_miniVehicleFactory)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/neolix_edu/neolix_edu_controller.h"
#include "modules/canbus/vehicle/neolix_edu/neolix_edu_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -38,3 +39,6 @@ Neolix_eduVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::NEOLIX,
apollo::canbus::Neolix_eduVehicleFactory)
4 changes: 4 additions & 0 deletions modules/canbus/vehicle/transit/transit_vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cyber/common/log.h"
#include "modules/canbus/vehicle/transit/transit_controller.h"
#include "modules/canbus/vehicle/transit/transit_message_manager.h"
#include "modules/canbus/vehicle/vehicle_factory_register_macro.h"
#include "modules/common/util/util.h"

namespace apollo {
Expand All @@ -37,3 +38,6 @@ TransitVehicleFactory::CreateMessageManager() {

} // namespace canbus
} // namespace apollo

CANBUS_REGISTER_VEHICLE_FACTORY(apollo::common::TRANSIT,
apollo::canbus::TransitVehicleFactory)
57 changes: 6 additions & 51 deletions modules/canbus/vehicle/vehicle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,18 @@
#include "modules/canbus/vehicle/vehicle_factory.h"

#include "modules/canbus/proto/vehicle_parameter.pb.h"

#include "modules/canbus/vehicle/ch/ch_vehicle_factory.h"
#include "modules/canbus/vehicle/devkit/devkit_vehicle_factory.h"
#include "modules/canbus/vehicle/ge3/ge3_vehicle_factory.h"
#include "modules/canbus/vehicle/gem/gem_vehicle_factory.h"
#include "modules/canbus/vehicle/lexus/lexus_vehicle_factory.h"
#include "modules/canbus/vehicle/lincoln/lincoln_vehicle_factory.h"
#include "modules/canbus/vehicle/mk_mini/mk_mini_vehicle_factory.h"
#include "modules/canbus/vehicle/neolix_edu/neolix_edu_vehicle_factory.h"
#include "modules/canbus/vehicle/transit/transit_vehicle_factory.h"
#include "modules/canbus/vehicle/wey/wey_vehicle_factory.h"
#include "modules/canbus/vehicle/yunle/yunle_vehicle_factory.h"
#include "modules/canbus/vehicle/zhongyun/zhongyun_vehicle_factory.h"
namespace apollo {
namespace canbus {

void VehicleFactory::RegisterVehicleFactory() {
Register(apollo::common::LINCOLN_MKZ, []() -> AbstractVehicleFactory * {
return new LincolnVehicleFactory();
});
Register(apollo::common::GEM, []() -> AbstractVehicleFactory * {
return new GemVehicleFactory();
});
Register(apollo::common::LEXUS, []() -> AbstractVehicleFactory * {
return new LexusVehicleFactory();
});
Register(apollo::common::TRANSIT, []() -> AbstractVehicleFactory * {
return new TransitVehicleFactory();
});
Register(apollo::common::GE3, []() -> AbstractVehicleFactory * {
return new Ge3VehicleFactory();
});
Register(apollo::common::WEY, []() -> AbstractVehicleFactory * {
return new WeyVehicleFactory();
});
Register(apollo::common::ZHONGYUN, []() -> AbstractVehicleFactory * {
return new ZhongyunVehicleFactory();
});
Register(apollo::common::CH,
[]() -> AbstractVehicleFactory * { return new ChVehicleFactory(); });
Register(apollo::common::DKIT, []() -> AbstractVehicleFactory * {
return new DevkitVehicleFactory();
});
Register(apollo::common::NEOLIX, []() -> AbstractVehicleFactory * {
return new Neolix_eduVehicleFactory();
});
Register(apollo::common::MK_MINI, []() -> AbstractVehicleFactory * {
return new Mk_miniVehicleFactory();
});
Register(apollo::common::YUNLE, []() -> AbstractVehicleFactory * {
return new YunleVehicleFactory();
});
VehicleFactoryRegistry& VehicleFactoryRegistry::Instance() {
static VehicleFactoryRegistry registry;
return registry;
}

std::unique_ptr<AbstractVehicleFactory> VehicleFactory::CreateVehicle(
const VehicleParameter &vehicle_parameter) {
auto abstract_factory = CreateObject(vehicle_parameter.brand());
const VehicleParameter& vehicle_parameter) {
auto abstract_factory = VehicleFactoryRegistry::Instance().CreateObject(
vehicle_parameter.brand());
if (!abstract_factory) {
AERROR << "failed to create vehicle factory with "
<< vehicle_parameter.DebugString();
Expand Down
12 changes: 6 additions & 6 deletions modules/canbus/vehicle/vehicle_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ namespace canbus {
* @brief This class is a factory class that will generate different
* vehicle factories based on the vehicle brand.
*/
class VehicleFactory
class VehicleFactoryRegistry
: public common::util::Factory<apollo::common::VehicleBrand,
AbstractVehicleFactory> {
public:
/**
* @brief register supported vehicle factories.
*/
void RegisterVehicleFactory();
static VehicleFactoryRegistry& Instance();
};

class VehicleFactory {
public:
/**
* @brief Creates an AbstractVehicleFactory object based on vehicle_parameter
* @param vehicle_parameter is defined in vehicle_parameter.proto
*/
std::unique_ptr<AbstractVehicleFactory> CreateVehicle(
const VehicleParameter &vehicle_parameter);
const VehicleParameter& vehicle_parameter);
};

} // namespace canbus
Expand Down
Loading
Loading