1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Radu Serban
13 // =============================================================================
14 //
15 // Tracked vehicle double-pin sprocket model constructed with data from file
16 // (JSON format).
17 //
18 // =============================================================================
19 
20 #include "chrono/assets/ChTriangleMeshShape.h"
21 #include "chrono_vehicle/ChVehicleModelData.h"
22 #include "chrono_vehicle/tracked_vehicle/sprocket/SprocketDoublePin.h"
23 #include "chrono_vehicle/utils/ChUtilsJSON.h"
24 
25 #include "chrono_thirdparty/filesystem/path.h"
26 
27 using namespace rapidjson;
28 
29 namespace chrono {
30 namespace vehicle {
31 
32 // -----------------------------------------------------------------------------
33 // -----------------------------------------------------------------------------
SprocketDoublePin(const std::string & filename)34 SprocketDoublePin::SprocketDoublePin(const std::string& filename) : ChSprocketDoublePin(""), m_has_mesh(false) {
35     Document d; ReadFileJSON(filename, d);
36     if (d.IsNull())
37         return;
38 
39     Create(d);
40 
41     GetLog() << "Loaded JSON: " << filename.c_str() << "\n";
42 }
43 
SprocketDoublePin(const rapidjson::Document & d)44 SprocketDoublePin::SprocketDoublePin(const rapidjson::Document& d) : ChSprocketDoublePin(""), m_has_mesh(false) {
45     Create(d);
46 }
47 
Create(const rapidjson::Document & d)48 void SprocketDoublePin::Create(const rapidjson::Document& d) {
49     // Invoke base class method.
50     ChPart::Create(d);
51 
52     // Read inertia properties
53     m_num_teeth = d["Number Teeth"].GetInt();
54     m_gear_mass = d["Gear Mass"].GetDouble();
55     m_gear_inertia = ReadVectorJSON(d["Gear Inertia"]);
56     m_axle_inertia = d["Axle Inertia"].GetDouble();
57     m_separation = d["Gear Separation"].GetDouble();
58 
59     // Read lateral backlash (for contact against detracking)
60     m_lateral_backlash = d["Lateral Backlash"].GetDouble();
61 
62     // Read profile information
63     assert(d.HasMember("Profile"));
64     m_gear_RT = d["Profile"]["Addenum Radius"].GetDouble();
65     m_gear_R = d["Profile"]["Arc Radius"].GetDouble();
66     m_gear_RA = d["Profile"]["Assembly Radius"].GetDouble();
67     m_gear_C = d["Profile"]["Arc Center Height"].GetDouble();
68     m_gear_W = d["Profile"]["Arc Center Offset"].GetDouble();
69 
70     // Read contact material data
71     assert(d.HasMember("Contact Material"));
72     m_mat_info = ReadMaterialInfoJSON(d["Contact Material"]);
73 
74     // Read sprocket visualization
75     if (d.HasMember("Visualization")) {
76         assert(d["Visualization"].HasMember("Mesh"));
77         m_meshFile = d["Visualization"]["Mesh"].GetString();
78         m_has_mesh = true;
79     }
80 }
81 
CreateContactMaterial(ChContactMethod contact_method)82 void SprocketDoublePin::CreateContactMaterial(ChContactMethod contact_method) {
83     m_material = m_mat_info.CreateMaterial(contact_method);
84 }
85 
86 // -----------------------------------------------------------------------------
87 // -----------------------------------------------------------------------------
AddVisualizationAssets(VisualizationType vis)88 void SprocketDoublePin::AddVisualizationAssets(VisualizationType vis) {
89     if (vis == VisualizationType::MESH && m_has_mesh) {
90         auto trimesh = chrono_types::make_shared<geometry::ChTriangleMeshConnected>();
91         trimesh->LoadWavefrontMesh(vehicle::GetDataFile(m_meshFile), false, false);
92         auto trimesh_shape = chrono_types::make_shared<ChTriangleMeshShape>();
93         trimesh_shape->SetMesh(trimesh);
94         trimesh_shape->SetName(filesystem::path(m_meshFile).stem());
95         trimesh_shape->SetStatic(true);
96         m_gear->AddAsset(trimesh_shape);
97     } else {
98         ChSprocket::AddVisualizationAssets(vis);
99     }
100 }
101 
102 }  // end namespace vehicle
103 }  // end namespace chrono
104