Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom JSON constructors for vehicle sub-systems #490

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 26 additions & 9 deletions src/chrono_vehicle/tracked_vehicle/vehicle/TrackedVehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ namespace vehicle {

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
TrackedVehicle::TrackedVehicle(const std::string& filename, ChContactMethod contact_method)
TrackedVehicle::TrackedVehicle(const std::string& filename,
ChContactMethod contact_method,
CustomConstructorsTV custom_constructors)
: ChTrackedVehicle("", contact_method) {
Create(filename);
Create(filename, custom_constructors);
}

TrackedVehicle::TrackedVehicle(ChSystem* system, const std::string& filename) : ChTrackedVehicle("", system) {
Create(filename);
TrackedVehicle::TrackedVehicle(ChSystem* system,
const std::string& filename,
CustomConstructorsTV custom_constructors)
: ChTrackedVehicle("", system) {
Create(filename, custom_constructors);
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
void TrackedVehicle::Create(const std::string& filename) {
void TrackedVehicle::Create(const std::string& filename, CustomConstructorsTV custom_constructors) {
// -------------------------------------------
// Open and parse the input file
// -------------------------------------------
Expand Down Expand Up @@ -68,7 +73,10 @@ void TrackedVehicle::Create(const std::string& filename) {

{
std::string file_name = d["Chassis"]["Input File"].GetString();
m_chassis = ReadChassisJSON(vehicle::GetDataFile(file_name));
m_chassis = ReadChassisJSON(
vehicle::GetDataFile(file_name),
custom_constructors.chassis_constructor
);
if (d["Chassis"].HasMember("Output")) {
m_chassis->SetOutput(d["Chassis"]["Output"].GetBool());
}
Expand All @@ -84,15 +92,21 @@ void TrackedVehicle::Create(const std::string& filename) {

{
std::string file_name = d["Track Assemblies"][0u]["Input File"].GetString();
m_tracks[VehicleSide::LEFT] = ReadTrackAssemblyJSON(vehicle::GetDataFile(file_name));
m_tracks[VehicleSide::LEFT] = ReadTrackAssemblyJSON(
vehicle::GetDataFile(file_name),
custom_constructors.track_assembly_constructor
);
if (d["Track Assemblies"][0u].HasMember("Output")) {
m_tracks[VehicleSide::LEFT]->SetOutput(d["Track Assemblies"][0u]["Output"].GetBool());
}
m_track_offset[LEFT] = d["Track Assemblies"][0u]["Offset"].GetDouble();
}
{
std::string file_name = d["Track Assemblies"][1u]["Input File"].GetString();
m_tracks[VehicleSide::RIGHT] = ReadTrackAssemblyJSON(vehicle::GetDataFile(file_name));
m_tracks[VehicleSide::RIGHT] = ReadTrackAssemblyJSON(
vehicle::GetDataFile(file_name),
custom_constructors.track_assembly_constructor
);
if (d["Track Assemblies"][1u].HasMember("Output")) {
m_tracks[VehicleSide::RIGHT]->SetOutput(d["Track Assemblies"][1u]["Output"].GetBool());
}
Expand All @@ -107,7 +121,10 @@ void TrackedVehicle::Create(const std::string& filename) {

{
std::string file_name = d["Driveline"]["Input File"].GetString();
m_driveline = ReadDrivelineTVJSON(vehicle::GetDataFile(file_name));
m_driveline = ReadDrivelineTVJSON(
vehicle::GetDataFile(file_name),
custom_constructors.driveline_tv_constructor
);
if (d["Driveline"].HasMember("Output")) {
m_driveline->SetOutput(d["Driveline"]["Output"].GetBool());
}
Expand Down
27 changes: 24 additions & 3 deletions src/chrono_vehicle/tracked_vehicle/vehicle/TrackedVehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,40 @@ namespace vehicle {
/// @addtogroup vehicle_tracked
/// @{

/// Constructors for different vehicle subsystems. Used to construct custom
/// instances of the subsystems from a JSON document.
struct CH_VEHICLE_API CustomConstructorsTV {

/// A constructor for creating a vehicle subsystem from a JSON document.
template<typename T>
using CustomConstructor =
std::function<std::shared_ptr<T>(
const std::string&, /// The name of the template to create an instance of
const rapidjson::Document& /// The JSON document
)>;

CustomConstructor<ChChassis> chassis_constructor;
CustomConstructor<ChTrackAssembly> track_assembly_constructor;
CustomConstructor<ChDrivelineTV> driveline_tv_constructor;
};

/// Tracked vehicle model constructed from a JSON specification file.
class CH_VEHICLE_API TrackedVehicle : public ChTrackedVehicle {
public:
TrackedVehicle(const std::string& filename, ChContactMethod contact_method = ChContactMethod::NSC);
TrackedVehicle(const std::string& filename,
ChContactMethod contact_method = ChContactMethod::NSC,
CustomConstructorsTV custom_constructors = {});

TrackedVehicle(ChSystem* system, const std::string& filename);
TrackedVehicle(ChSystem* system,
const std::string& filename,
CustomConstructorsTV custom_constructors = {});

~TrackedVehicle() {}

virtual void Initialize(const ChCoordsys<>& chassisPos, double chassisFwdVel = 0) override;

private:
void Create(const std::string& filename);
void Create(const std::string& filename, CustomConstructorsTV custom_constructors);

private:
double m_track_offset[2]; ///< offsets for the left and right track assemblies
Expand Down
Loading