fix: stop ctrl write from fighting MuJoCo viewer when no controller is connected#121
Open
SHABRAWii wants to merge 3 commits into
Open
fix: stop ctrl write from fighting MuJoCo viewer when no controller is connected#121SHABRAWii wants to merge 3 commits into
SHABRAWii wants to merge 3 commits into
Conversation
Two race conditions crash the simulator on Reload: 1. PhysicsLoop calls mj_deleteData/mj_deleteModel while the bridge threads are still reading from those pointers. Fix: add two std::atomic<bool> globals (g_bridge_pause_request / g_bridge_paused) as a pause/resume handshake. PhysicsLoop waits for the bridge to stop before freeing memory. UnitreeSdk2BridgeThread destroys and recreates the bridge on each reload instead of holding stale pointers forever. 2. RecurrentThread::~RecurrentThread() destroys mFunc (the run() lambda) before sending pthread_cancel, and never sets mQuit. If the thread wakes between those two steps it calls the destroyed std::function and hits std::terminate(). Fix: replace RecurrentThread with a std::thread owned by RobotBridge, controlled by a std::atomic<bool> stop_flag_. pre_destroy() sets the flag and joins the thread — guaranteed clean exit before any destructor runs.
…mbers The private section still declared the old `thread_` after the method bodies were updated to use `stop_flag_` and `run_thread_`, leaving the class in a non-compiling state.
Without this, run() unconditionally writes zeros to mj_data_->ctrl[] 1000 times/second even before any controller connects, constantly resetting joints and fighting the MuJoCo viewer control tab.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
RobotBridge::run(),mj_data_->ctrl[]was written unconditionallyevery 1ms regardless of whether a LowCmd message had ever been received.
This meant the bridge was resetting all joints to zero 1000 times/second,
constantly fighting the MuJoCo viewer's control tab sliders.
This is unlike the Python implementation where
LowCmdHandleris a DDScallback — it only writes
ctrlwhen a message actually arrives.Fix
Guard the
ctrlwrite with!lowcmd->isTimeout(), which returnstruewhen no message has been received within the timeout window (default 1s).
Joints are now only driven when a real command is present, matching the
Python behavior.
Testing
are no longer overridden