-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (115 loc) · 5.39 KB
/
main.cpp
File metadata and controls
131 lines (115 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// main.cpp
#include "include/WheelSimulator.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include "include/json.hpp"
using json = nlohmann::json;
int main(int argc, char* argv[]) {
// Process input data
if (argc != 4) {
std::cerr << "Usage: ./WheelSimulator <input_json_folder_path> <batch_name> <slip>" << std::endl;
return EXIT_FAILURE;
}
std::filesystem::path input_files(argv[1]);
std::ifstream file((input_files / "job_parameters.json"));
if (!file) {
std::cerr << "Could not open " << argv[1] << "\n";
return 1;
}
json job_json;
file >> job_json;
// Import slip and batch directory from CLI arguments
double slip = std::atof(argv[3]);
double sim_endtime = job_json.value("sim_endtime", 0.1);
std::string batch_dir = argv[2];
float rotational_velocity = job_json.value("rotational_velocity", 0.2);
float step_size = job_json.value("step_size", 1e-6);
std::filesystem::path data_drivepath = job_json.value("data_drivepath", "/ocean/projects/mch240013p/matthies/");
float angle_deg = job_json.value("wheel_angle", 0.0);
float offset_x = job_json["offset"]["x"];
float offset_y = job_json["offset"]["y"];
float offset_z = job_json["offset"]["z"];
float settling_time = job_json["settling_time"];
if(!job_json.contains("wheel_folder_path")){
std::cerr << "Error: 'wheel_folder_path' is missing from the job_json\n";
return 1;
}
std::filesystem::path wheel_directory = job_json["wheel_folder_path"];
std::filesystem::path wheel_filepath = wheel_directory / "moonranger_wheel.obj";
if(!job_json.contains("terrain_filepath")){
std::cerr << "Error: 'terrain_filepath' is missing from the job_json\n";
return 1;
}
std::filesystem::path terrain_directory = job_json["terrain_filepath"];
std::filesystem::path terrain_filepath = terrain_directory / "GRC_3e5_Reduced_Footprint.csv";
std::filesystem::path wheel_json_path = input_files / "wheel_parameters.json";
std::ifstream file2(wheel_json_path);
if (!file2) {
std::cerr << "Could not open " << wheel_json_path << "\n";
return 1;
}
json wheel_json;
file2 >> wheel_json;
// read wheel json parameters
float width = wheel_json["width"];
float rim_radius = wheel_json["rim_radius"]; //rim_radius is effective radius
float outer_radius = wheel_json["outer_radius"];
float mass = wheel_json.value("mass", 0.238);
float total_mass = wheel_json.value("total_mass", 4.5);
std::unordered_map<std::string, float> material_properties_wheel;
material_properties_wheel = wheel_json["material_properties"].get<std::unordered_map<std::string, float>>();
std::filesystem::path terrain_json_path = input_files / "terrain_parameters.json";
std::ifstream file3(terrain_json_path);
if (!file3) {
std::cerr << "Could not open " << terrain_json_path << "\n";
return 1;
}
json terrain_json;
file3 >> terrain_json;
//read terrain json parameters
double world_size_x = terrain_json["world_size"]["x"];
double world_size_y = terrain_json["world_size"]["y"];
double world_size_z = terrain_json["world_size"]["z"];
float world_bottom = terrain_json.value("world_bottom", -0.5);
float terrain_density = terrain_json.value("terrain_density", 2.6e3);
float volume1 = terrain_json.value("volume1", 4.2520508);
float volume2 = terrain_json.value("volume2", 2.1670011);
std::vector<float> scales = terrain_json["scales"].get<std::vector<float>>();
float scale_factor = terrain_json.value("scale_factor", 10);
float3 MOI1;
if(terrain_json.contains("MOI1") && terrain_json["MOI1"].is_array() && terrain_json["MOI1"].size() == 3){
MOI1.x = terrain_json["MOI1"][0].get<float>();
MOI1.y = terrain_json["MOI1"][1].get<float>();
MOI1.z = terrain_json["MOI1"][2].get<float>();
}
else{
MOI1 = make_float3(1.6850426f, 1.6375114f, 2.1187753f);
}
float3 MOI2;
if(terrain_json.contains("MOI2") && terrain_json["MOI2"].is_array() && terrain_json["MOI2"].size() == 3){
MOI2.x = terrain_json["MOI2"][0].get<float>();
MOI2.y = terrain_json["MOI2"][1].get<float>();
MOI2.z = terrain_json["MOI2"][2].get<float>();
}
else{
MOI2 = make_float3(0.57402126f, 0.60616378f, 0.92890173f);
}
std::unordered_map<std::string, float> material_properties;
material_properties = terrain_json["material_properties"].get<std::unordered_map<std::string, float>>();
Wheel wheel(outer_radius, rim_radius, width, mass, wheel_filepath, total_mass, material_properties_wheel);
Terrain terrain(terrain_filepath, world_size_x, world_size_y, world_size_z, world_bottom,
terrain_density, volume1, volume2, scales, scale_factor, MOI1, MOI2, material_properties);
SimParams simparams(slip, sim_endtime, batch_dir, data_drivepath,
rotational_velocity, step_size, angle_deg, offset_x, offset_y, offset_z, settling_time);
try {
WheelSimulator simulator(wheel, terrain, simparams, job_json);
simulator.PrepareSimulation();
simulator.RunSimulation();
} catch (const std::exception& e) {
std::cerr << "Simulation failed: " << e.what() << std::endl;
return EXIT_FAILURE;
}
std::cout << "Simulation completed successfully." << std::endl;
return EXIT_SUCCESS;
}