Download code/case1/runner/src/art_lockstep.cpp from harryzhang1018/TestRandomization: direct link, hf CLI and curl.
- Browser
- Download file 12.1 kB
-
https://huggingface.co/datasets/harryzhang1018/TestRandomization/resolve/main/code/case1/runner/src/art_lockstep.cpp
- Command line
-
hf download hf://datasets/harryzhang1018/TestRandomization/code/case1/runner/src/art_lockstep.cpp
-
curl -L -o art_lockstep.cpp https://huggingface.co/datasets/harryzhang1018/TestRandomization/resolve/main/code/case1/runner/src/art_lockstep.cpp
12.1 kB
| // ============================================================================= | |
| // 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. | |
| // ============================================================================= | |
| 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; | |
| } | |