File size: 12,062 Bytes
0c3dd1d | 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | // =============================================================================
// art_lockstep.cpp -- lock-step ARTcar physics server for the Case 1 TR campaign
//
// New code written in 2026 (not an original 2024 artifact).
//
// Vehicle/terrain setup is copied line-for-line from the released
// demo_ROS_dART.cpp (sha256 389a8f69...54ec8, image uwsbel/ros:iros2024,
// Chrono fork commit 9d26ff7). The ROS manager, ChSensorManager (GPU camera
// and lidar), Irrlicht-free visual assets and soft-real-time pacing are
// omitted; see README_case1.md (section "Runner") for why they do not change
// the dynamics and for the bitwise test.
//
// Differences that ARE configurable per process (one process == one episode):
// --x --y --yaw chassis reference-frame initial pose (z = 0.25 as in
// the original, start at rest, Q_from_AngZ(yaw))
// --terrain-json PATH RigidTerrain JSON (default: the original
// <vehicle data>/terrain/RigidPlane.json)
// --waypoint-boxes CSV optionally add the original fixed, non-colliding
// visual waypoint boxes (for the equivalence test)
// --replicate-vis 0|1 optionally call the original visualization setters
// --data-path DIR Chrono::Vehicle data dir (default
// /sbel/Desktop/chrono/build/data/vehicle/, i.e. what
// the original binary resolves from build/bin with
// the default relative path "../data/vehicle/")
//
// Message format (line based, stdin -> stdout; all doubles printed with %.17g so
// that they round-trip exactly):
// S <n> run n iterations of the original loop body
// -> state line
// U <steer> <thr> <brk> driver->SetSteering/SetThrottle/SetBraking
// (ChDriver clamps to [-1,1],[0,1],[0,1])
// -> "OK <steer> <thr> <brk>" (stored values)
// G -> state line
// Q exit(0)
// State line: "X" followed by the fields listed in kStateFields below.
// =============================================================================
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
#include "chrono/core/ChTypes.h"
#include "chrono/physics/ChSystemNSC.h"
#include "chrono/assets/ChVisualMaterial.h"
#include "chrono/assets/ChVisualShape.h"
#include "chrono/physics/ChBodyEasy.h"
#include "chrono/physics/ChBody.h"
#include "chrono_vehicle/ChConfigVehicle.h"
#include "chrono_vehicle/ChVehicleModelData.h"
#include "chrono_vehicle/terrain/RigidTerrain.h"
#include "chrono_vehicle/ChDriver.h"
#include "chrono_models/vehicle/artcar/ARTcar.h"
using namespace chrono;
using namespace chrono::vehicle;
using namespace chrono::vehicle::artcar;
// ---- settings copied verbatim from demo_ROS_dART.cpp ------------------------
static VisualizationType chassis_vis_type = VisualizationType::PRIMITIVES;
static VisualizationType suspension_vis_type = VisualizationType::PRIMITIVES;
static VisualizationType steering_vis_type = VisualizationType::PRIMITIVES;
static VisualizationType wheel_vis_type = VisualizationType::NONE;
static ChContactMethod contact_method = ChContactMethod::NSC;
static CollisionType chassis_collision_type = CollisionType::NONE;
static TireModelType tire_model = TireModelType::TMEASY;
static std::string rigidterrain_file("terrain/RigidPlane.json");
static double step_size = 1e-3;
// -----------------------------------------------------------------------------
static const char* kStateFields =
"step time cog_x cog_y cog_z q_e0 q_e1 q_e2 q_e3 vel_x vel_y vel_z wloc_x wloc_y wloc_z "
"ref_x ref_y ref_z applied_steer applied_thr applied_brake drv_steer drv_thr drv_brake "
"veh_speed finite step_wall_s";
static void die(const std::string& msg) {
std::fprintf(stderr, "ART_LOCKSTEP_FATAL %s\n", msg.c_str());
std::fflush(stderr);
std::exit(3);
}
int main(int argc, char* argv[]) {
auto t_start = std::chrono::steady_clock::now();
double x = 0.0, y = 0.0, theta = 0.0;
std::string data_path = "/sbel/Desktop/chrono/build/data/vehicle/";
std::string terrain_json; // empty -> GetDataFile(rigidterrain_file)
std::string waypoint_boxes_csv;
int replicate_vis = 0;
for (int i = 1; i < argc; i++) {
std::string a = argv[i];
auto need = [&](const char* name) -> std::string {
if (i + 1 >= argc)
die(std::string("missing value for ") + name);
return std::string(argv[++i]);
};
if (a == "--x")
x = std::strtod(need("--x").c_str(), nullptr);
else if (a == "--y")
y = std::strtod(need("--y").c_str(), nullptr);
else if (a == "--yaw")
theta = std::strtod(need("--yaw").c_str(), nullptr);
else if (a == "--terrain-json")
terrain_json = need("--terrain-json");
else if (a == "--waypoint-boxes")
waypoint_boxes_csv = need("--waypoint-boxes");
else if (a == "--replicate-vis")
replicate_vis = std::atoi(need("--replicate-vis").c_str());
else if (a == "--data-path")
data_path = need("--data-path");
else if (a == "--fields") {
std::printf("%s\n", kStateFields);
return 0;
} else
die("unknown argument " + a);
}
vehicle::SetDataPath(data_path);
if (terrain_json.empty())
terrain_json = vehicle::GetDataFile(rigidterrain_file);
// ---- vehicle: identical call sequence to demo_ROS_dART.cpp lines 117-131 ----
ChVector<> initLoc(x, y, 0.25);
ChQuaternion<> initRot = Q_from_AngZ(theta);
ARTcar vehicle;
vehicle.SetContactMethod(contact_method);
vehicle.SetChassisCollisionType(chassis_collision_type);
vehicle.SetChassisFixed(false);
vehicle.SetInitPosition(ChCoordsys<>(initLoc, initRot));
vehicle.SetTireType(tire_model);
vehicle.SetTireStepSize(step_size);
vehicle.SetMaxMotorVoltageRatio(0.09f);
vehicle.SetStallTorque(0.3f);
vehicle.SetTireRollingResistance(0.05f);
vehicle.Initialize();
if (replicate_vis) { // demo_ROS_dART.cpp lines 133-139 (visual assets only)
VisualizationType tire_vis_type = VisualizationType::MESH;
vehicle.SetChassisVisualizationType(chassis_vis_type);
vehicle.SetSuspensionVisualizationType(suspension_vis_type);
vehicle.SetSteeringVisualizationType(steering_vis_type);
vehicle.SetWheelVisualizationType(wheel_vis_type);
vehicle.SetTireVisualizationType(tire_vis_type);
}
auto system = vehicle.GetSystem();
if (!waypoint_boxes_csv.empty()) { // demo_ROS_dART.cpp lines 157-196
std::vector<std::tuple<double, double, double, double>> positions;
std::ifstream file(waypoint_boxes_csv);
if (!file.is_open())
die("cannot open waypoint csv " + waypoint_boxes_csv);
std::string line;
while (std::getline(file, line)) {
std::stringstream ss(line);
double px, py, pz, pw;
char delimiter;
if (ss >> px >> delimiter >> py >> delimiter >> pz >> delimiter >> pw)
positions.emplace_back(px, py, pz, pw);
else
die("invalid csv line " + line);
}
auto vis_mat_path = chrono_types::make_shared<chrono::ChVisualMaterial>();
vis_mat_path->SetDiffuseColor(chrono::ChColor(0.0f, 1.0f, 0.0f));
for (const auto& pos : positions) {
auto box_body = chrono_types::make_shared<chrono::ChBodyEasyBox>(0.1, 0.1, 0.0001, 1000, true, false);
box_body->SetPos(chrono::ChVector<>(std::get<0>(pos), std::get<1>(pos), 0));
box_body->SetBodyFixed(true);
auto shape = box_body->GetVisualModel()->GetShapes()[0].first;
if (shape->GetNumMaterials() == 0)
shape->AddMaterial(vis_mat_path);
else
shape->GetMaterials()[0] = vis_mat_path;
system->Add(box_body);
}
}
// ---- terrain and driver: demo_ROS_dART.cpp lines 226-230 ----
RigidTerrain terrain(system, terrain_json);
terrain.Initialize();
auto driver = std::make_shared<ChDriver>(vehicle.GetVehicle());
auto chassis = vehicle.GetChassisBody();
DriverInputs last_applied = driver->GetInputs();
long long step_count = 0;
double step_wall_total = 0.0;
double init_s = std::chrono::duration<double>(std::chrono::steady_clock::now() - t_start).count();
std::printf("READY init_s=%.9f terrain_json=%s data_path=%s chrono_version=%s fields=%s\n", init_s,
terrain_json.c_str(), data_path.c_str(), CHRONO_VERSION, kStateFields);
std::fflush(stdout);
auto print_state = [&]() {
const ChVector<>& p = chassis->GetPos(); // COG frame (ChBodyAuxRef) == ChROSBodyHandler pose
const ChQuaternion<>& q = chassis->GetRot(); // COG frame rotation (e0=w, e1..e3)
const ChVector<>& v = chassis->GetPos_dt(); // COG velocity, absolute frame == ChROSBodyHandler twist
ChVector<> w = chassis->GetWvel_loc(); // local angular velocity == ChROSBodyHandler twist.angular
ChVector<> r = chassis->GetFrame_REF_to_abs().GetPos();
DriverInputs d = driver->GetInputs();
double spd = vehicle.GetVehicle().GetSpeed();
double vals[] = {p.x(), p.y(), p.z(), q.e0(), q.e1(), q.e2(), q.e3(), v.x(), v.y(), v.z(), w.x(), w.y(),
w.z(), r.x(), r.y(), r.z()};
bool finite = std::isfinite(spd);
for (double val : vals)
finite = finite && std::isfinite(val);
std::printf("X %lld %.17g", step_count, system->GetChTime());
for (double val : vals)
std::printf(" %.17g", val);
std::printf(" %.17g %.17g %.17g %.17g %.17g %.17g %.17g %d %.9f\n", last_applied.m_steering,
last_applied.m_throttle, last_applied.m_braking, d.m_steering, d.m_throttle, d.m_braking, spd,
finite ? 1 : 0, step_wall_total);
std::fflush(stdout);
};
print_state();
char buf[512];
while (std::fgets(buf, sizeof(buf), stdin)) {
if (buf[0] == 'S') {
long long n = std::atoll(buf + 1);
auto t0 = std::chrono::steady_clock::now();
for (long long k = 0; k < n; k++) {
// ---- loop body: demo_ROS_dART.cpp lines 332-342 (sensor/ROS/realtime omitted) ----
DriverInputs driver_inputs = driver->GetInputs();
double time = vehicle.GetSystem()->GetChTime();
driver->Synchronize(time);
vehicle.Synchronize(time, driver_inputs, terrain);
terrain.Synchronize(time);
driver->Advance(step_size);
vehicle.Advance(step_size);
terrain.Advance(step_size);
last_applied = driver_inputs;
step_count++;
}
step_wall_total += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
print_state();
} else if (buf[0] == 'U') {
double s = 0, t = 0, b = 0;
if (std::sscanf(buf + 1, "%lf %lf %lf", &s, &t, &b) != 3)
die(std::string("bad U command: ") + buf);
driver->SetSteering(s);
driver->SetThrottle(t);
driver->SetBraking(b);
DriverInputs d = driver->GetInputs();
std::printf("OK %.17g %.17g %.17g\n", d.m_steering, d.m_throttle, d.m_braking);
std::fflush(stdout);
} else if (buf[0] == 'G') {
print_state();
} else if (buf[0] == 'Q') {
break;
} else {
die(std::string("unknown command: ") + buf);
}
}
return 0;
}
|