cmvr-es/cmvr-es/test/mujoco_manual_ui_test.cpp

243 lines
7.9 KiB
C++

#include <atomic>
#include <chrono>
#include <cstdlib>
#include <cmath>
#include <limits>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <gtest/gtest.h>
#include "common/base/logging/logger.h"
#include "devices/arm/robot_arm.h"
#include "devices/camera/abstract_camera.h"
#include "runtime/include/cmvr_runtime.h"
#include "simulate/mujoco/mujoco_world/include/mujoco_world.h"
namespace {
std::string manualConfigPath()
{
if (const char* env = std::getenv("CMVR_MANUAL_UI_CONFIG_PATH")) {
return env;
}
return CMVR_MANUAL_UI_CONFIG_PATH;
}
std::string manualArmId()
{
if (const char* env = std::getenv("CMVR_MANUAL_ARM_ID")) {
return env;
}
return "mujoco_right_arm";
}
std::string manualRealArmId()
{
if (const char* env = std::getenv("CMVR_MANUAL_REAL_ARM_ID")) {
return env;
}
return "right_arm";
}
bool shouldRunRealArmTest()
{
const char* env = std::getenv("CMVR_RUN_REAL_ARM_TEST");
return env != nullptr && std::string(env) == "1";
}
struct ManualUiRunResult {
bool ok = true;
std::string error;
};
ManualUiRunResult runManualUiTest(
const std::shared_ptr<cmvr::device::RobotArm>& arm,
const std::string& arm_id,
const std::atomic_bool& stop_requested,
const std::shared_ptr<cmvr::simulate::MujocoWorld>& mujoco_world = nullptr)
{
if (stop_requested.load()) {
return {};
}
std::vector<double> q_target = {0, 1, 1.6, 1.6, -2.5, 0.12, 0.12};
cmvr::device::MotionOptions joint_options;
joint_options.velocity = 1.0;
joint_options.acceleration = 5.0;
joint_options.joint_velocity_limits.assign(7, 2.5);
CMVR_LOG(INFO) << "[MujocoManualUiTest] moveJ " << arm_id;
auto result = arm->moveJ(cmvr::device::JointPositionCommand{q_target}, joint_options);
if (!result.ok()) {
return {false, result.message};
}
if (stop_requested.load()) {
return {};
}
std::this_thread::sleep_for(std::chrono::seconds(1));
auto move_l_target = arm->getTcpPose(cmvr::device::FrameType::Base);
// move_l_target.x += -0.1;
move_l_target.rz += -0.2;
cmvr::device::MotionOptions cartesian_options;
cartesian_options.velocity = 0.1;
cartesian_options.acceleration = 5.0;
cartesian_options.jerk = 10.0;
CMVR_LOG(INFO) << "[MujocoManualUiTest] moveL +X";
result = arm->moveL(move_l_target, cartesian_options, cmvr::device::FrameType::Base);
if (!result.ok()) {
return {false, result.message};
}
if (stop_requested.load()) {
return {};
}
// std::this_thread::sleep_for(std::chrono::seconds(1));
//
// cmvr::device::CartesianVelocity speed_l_velocity;
// speed_l_velocity.vz = 0.19;
// // speed_l_velocity.wy = 0.19;
//
// CMVR_LOG(INFO) << "[MujocoManualUiTest] speedL +X";
// result = arm->speedL(speed_l_velocity, 100.0, 1.0, cmvr::device::FrameType::Base);
// if (!result.ok()) {
// return {false, result.message};
// }
// auto last_pose = arm->getTcpPose(cmvr::device::FrameType::Base);
// auto last_time = std::chrono::steady_clock::now();
// auto read_sim_time = [&]() {
// if (!mujoco_world) {
// return std::numeric_limits<double>::quiet_NaN();
// }
// std::lock_guard<std::mutex> lock(mujoco_world->mutex());
// const auto* data = mujoco_world->data();
// return data ? data->time : std::numeric_limits<double>::quiet_NaN();
// };
// double last_sim_time = read_sim_time();
// const auto speed_l_end_time = last_time + std::chrono::seconds(2);
// while (!stop_requested.load() && std::chrono::steady_clock::now() < speed_l_end_time) {
// std::this_thread::sleep_for(std::chrono::milliseconds(50));
//
// const auto now = std::chrono::steady_clock::now();
// const auto pose = arm->getTcpPose(cmvr::device::FrameType::Base);
// const auto command_twist = arm->getSpeedLCommandTwistBase();
// const double command_norm =
// std::sqrt(command_twist.vx * command_twist.vx +
// command_twist.vy * command_twist.vy +
// command_twist.vz * command_twist.vz);
// const double dt = std::chrono::duration<double>(now - last_time).count();
// const double sim_time = read_sim_time();
// const double sim_dt =
// std::isfinite(sim_time) && std::isfinite(last_sim_time)
// ? sim_time - last_sim_time
// : std::numeric_limits<double>::quiet_NaN();
// const double sim_realtime_ratio =
// dt > 1e-6 && std::isfinite(sim_dt)
// ? sim_dt / dt
// : std::numeric_limits<double>::quiet_NaN();
// if (dt > 1e-6) {
// const double vx = (pose.x - last_pose.x) / dt;
// const double vy = (pose.y - last_pose.y) / dt;
// const double vz = (pose.z - last_pose.z) / dt;
// const double v_norm = std::sqrt(vx * vx + vy * vy + vz * vz);
// CMVR_LOG(INFO) << "[MujocoManualUiTest] tcp velocity base: vx="
// << vx << ", vy=" << vy << ", vz=" << vz
// << ", norm=" << v_norm
// << ", command_vx=" << command_twist.vx
// << ", command_vy=" << command_twist.vy
// << ", command_vz=" << command_twist.vz
// << ", command_norm=" << command_norm
// << ", wall_dt=" << dt;
// if (mujoco_world) {
// CMVR_LOG(INFO) << "[MujocoManualUiTest] sim timing: sim_dt="
// << sim_dt
// << ", sim_realtime_ratio=" << sim_realtime_ratio;
// }
// }
//
// last_pose = pose;
// last_time = now;
// last_sim_time = sim_time;
// }
// (void)arm->stopL(10);
// std::this_thread::sleep_for(std::chrono::seconds(1000));
return {};
}
} // namespace
TEST(MujocoManualUiTest, RunMujocoArm)
{
cmvr::Runtime runtime;
ASSERT_TRUE(runtime.init(manualConfigPath()));
ASSERT_TRUE(runtime.startTasks());
auto& devices = runtime.deviceManager();
const std::string arm_id = manualArmId();
auto arm = devices.getDevice<cmvr::device::RobotArm>(arm_id);
auto mujoco_camera = devices.getDevice<cmvr::device::AbstractCamera>("mujoco_hand_cam");
if (!mujoco_camera) {
mujoco_camera = devices.getDevice<cmvr::device::AbstractCamera>("xiaozhao_mujoco_hand_cam");
}
auto viewer = runtime.mujocoViewer("mujoco_viewer");
auto mujoco_world = cmvr::simulate::MujocoWorldDevice::worldFor("mujoco_world");
ASSERT_TRUE(arm);
ASSERT_TRUE(viewer);
std::atomic_bool stop_requested{false};
ManualUiRunResult worker_result;
std::thread worker([&]() {
std::this_thread::sleep_for(std::chrono::seconds(1));
worker_result = runManualUiTest(arm, arm_id, stop_requested, mujoco_world);
if (mujoco_camera && !stop_requested.load()) {
mujoco_camera->start();
}
});
viewer->runOnMainThread();
stop_requested.store(true);
if (worker.joinable()) {
worker.join();
}
EXPECT_TRUE(worker_result.ok) << worker_result.error;
runtime.shutdown();
}
TEST(MujocoManualUiTest, RunRealArm)
{
// if (!shouldRunRealArmTest()) {
// GTEST_SKIP() << "Set CMVR_RUN_REAL_ARM_TEST=1 to run the real arm manual UI test.";
// }
cmvr::Runtime runtime;
ASSERT_TRUE(runtime.init(manualConfigPath()));
ASSERT_TRUE(runtime.startTasks());
const std::string arm_id = manualRealArmId();
auto arm = runtime.deviceManager().getDevice<cmvr::device::RobotArm>(arm_id);
ASSERT_TRUE(arm);
std::atomic_bool stop_requested{false};
const ManualUiRunResult result = runManualUiTest(arm, arm_id, stop_requested);
EXPECT_TRUE(result.ok) << result.error;
runtime.shutdown();
}