Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set simulation state from Python API #291

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mjpc/grpc/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ message GetStateResponse {

message SetStateRequest {
State state = 1;
bool set_simulation = 2;
}
message SetStateResponse {}

Expand Down
11 changes: 11 additions & 0 deletions mjpc/grpc/agent_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ grpc::Status AgentService::SetState(grpc::ServerContext* context,
task->Transition(model, data_);
agent_.SetState(data_);

// Set simulation state
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be done in ui_agent_service too.

if (request->set_simulation()) {
Agent::StepJob job = [&agent_data = data_](
Agent* agent, const mjModel* model, mjData* data) {
mju_copy(data->qpos, agent_data->qpos, model->nq);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is missing some states. Copy the rest of them: time, act, mocap_pos, mocap_quat, userdata.

mju_copy(data->qvel, agent_data->qvel, model->nv);
data->time = agent_data->time;
};
agent_.RunBeforeStep(std::move(job));
}

return grpc::Status::OK;
}

Expand Down
6 changes: 5 additions & 1 deletion python/mujoco_mpc/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def set_state(
mocap_pos: Optional[npt.ArrayLike] = None,
mocap_quat: Optional[npt.ArrayLike] = None,
userdata: Optional[npt.ArrayLike] = None,
set_simulation: Optional[bool] = False,
):
"""Set `Agent`'s MuJoCo `data` state.

Expand All @@ -200,6 +201,7 @@ def set_state(
mocap_pos: `data.mocap_pos`.
mocap_quat: `data.mocap_quat`.
userdata: `data.userdata`.
set_simulation: bool, set the simulation state.
"""
# if mocap_pos is an ndarray rather than a list, flatten it
if hasattr(mocap_pos, "flatten"):
Expand All @@ -217,7 +219,9 @@ def set_state(
userdata=userdata if userdata is not None else [],
)

set_state_request = agent_pb2.SetStateRequest(state=state)
set_state_request = agent_pb2.SetStateRequest(
state=state, set_simulation=set_simulation
)
self.stub.SetState(set_state_request)

def get_state(self) -> agent_pb2.State:
Expand Down
Loading