Skip to content

Commit

Permalink
Sim motor timeouts (#30)
Browse files Browse the repository at this point in the history
* added arm motor timeouts to sim

* motor timeouts

* minor tweaks

* changed motor timeout to ros param

* fix style check
  • Loading branch information
Crypt1cG authored Dec 3, 2024
1 parent c5ca25b commit 383d9ae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/simulator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ simulator:
save_rate: 1.0
save_history: 4096
headless: false
motor_timeout: 100

ref_heading: 90.0 # For the GPS sensor to work

Expand Down
2 changes: 2 additions & 0 deletions simulator/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace mrover {

mIkTargetPub = create_publisher<msg::IK>("arm_ik", 1);

mMotorTimeoutMs = get_parameter("motor_timeout").as_int();

mIsHeadless = get_parameter("headless").as_bool();
mEnablePhysics = mIsHeadless;
{
Expand Down
8 changes: 6 additions & 2 deletions simulator/simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ namespace mrover {
int index{};
boost::container::small_vector<Uniform<ModelUniforms>, 2> visualUniforms;
boost::container::small_vector<Uniform<ModelUniforms>, 2> collisionUniforms;
Clock::time_point lastUpdate = Clock::now();
};

urdf::Model model;
Expand Down Expand Up @@ -263,6 +264,8 @@ namespace mrover {

bool mIsHeadless{};

int64_t mMotorTimeoutMs{};

// Rendering

GlfwInstance mGlfwInstance;
Expand Down Expand Up @@ -362,7 +365,7 @@ namespace mrover {
}

if (auto it = mUrdfs.find("rover"); it != mUrdfs.end()) {
URDF const& rover = it->second;
URDF& rover = it->second;

for (std::size_t i = 0; i < names.size(); ++i) {
std::string const& name = names[i];
Expand All @@ -375,7 +378,8 @@ namespace mrover {
}

std::string const& urdfName = it->second;
URDF::LinkMeta const& linkMeta = rover.linkNameToMeta.at(urdfName);
URDF::LinkMeta& linkMeta = rover.linkNameToMeta.at(urdfName);
linkMeta.lastUpdate = Clock::now();

auto* motor = std::bit_cast<btMultiBodyJointMotor*>(rover.physics->getLink(linkMeta.index).m_userPtr);
assert(motor);
Expand Down
13 changes: 13 additions & 0 deletions simulator/simulator.physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ namespace mrover {
motor->setMaxAppliedImpulse(0.5);
motor->setPositionTarget(0);
}
// check if arm motor commands have expired
// TODO: fix hard-coded names?
for (auto const& name: {"arm_a_link", "arm_b_link", "arm_c_link", "arm_d_link", "arm_e_link"}) {
bool expired = std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - rover.linkNameToMeta.at(name).lastUpdate).count() > mMotorTimeoutMs;
if (expired) {
int linkIndex = rover.linkNameToMeta.at(name).index;
auto* motor = std::bit_cast<btMultiBodyJointMotor*>(rover.physics->getLink(linkIndex).m_userPtr);
assert(motor);
motor->setVelocityTarget(0, 1);
// set p gain to 0 to stop position control
motor->setPositionTarget(0, 0);
}
}
}

float updateDuration = std::clamp(std::chrono::duration_cast<std::chrono::duration<float>>(dt).count(), 0.0f, 0.1f);
Expand Down

0 comments on commit 383d9ae

Please sign in to comment.