@@ -44,6 +44,8 @@ using ::agent::GetActionRequest;
4444using ::agent::GetActionResponse;
4545using ::agent::GetAllModesRequest;
4646using ::agent::GetAllModesResponse;
47+ using ::agent::GetResidualsRequest;
48+ using ::agent::GetResidualsResponse;
4749using ::agent::GetCostValuesAndWeightsRequest;
4850using ::agent::GetCostValuesAndWeightsResponse;
4951using ::agent::GetModeRequest;
@@ -58,6 +60,7 @@ using ::agent::SetCostWeightsRequest;
5860using ::agent::SetModeRequest;
5961using ::agent::SetStateRequest;
6062using ::agent::SetTaskParametersRequest;
63+ using ::agent::Residual;
6164using ::agent::ValueAndWeight;
6265
6366grpc::Status GetState (const mjModel* model, const mjData* data,
@@ -226,6 +229,34 @@ grpc::Status GetAction(const GetActionRequest* request,
226229 return grpc::Status::OK;
227230}
228231
232+ grpc::Status GetResiduals (
233+ const GetResidualsRequest* request, const mjpc::Agent* agent,
234+ const mjModel* model, mjData* data,
235+ GetResidualsResponse* response) {
236+ const mjModel* agent_model = agent->GetModel ();
237+ const mjpc::Task* task = agent->ActiveTask ();
238+ std::vector<double > residuals (task->num_residual , 0 ); // scratch space
239+ task->Residual (model, data, residuals.data ());
240+ std::vector<int > dim_norm_residual = task->dim_norm_residual ;
241+
242+ int residual_shift = 0 ;
243+ for (int i = 0 ; i < task->num_term ; i++) {
244+ CHECK_EQ (agent_model->sensor_type [i], mjSENS_USER);
245+ std::string_view sensor_name (agent_model->names +
246+ agent_model->name_sensoradr [i]);
247+
248+ std::vector<double > sensor_residual_values (
249+ residuals.begin () + residual_shift,
250+ residuals.begin () + residual_shift + dim_norm_residual[i]);
251+ Residual sensor_residual;
252+ sensor_residual.mutable_values ()->Assign (sensor_residual_values.begin (),
253+ sensor_residual_values.end ());
254+ (*response->mutable_values ())[sensor_name] = sensor_residual;
255+ residual_shift += dim_norm_residual[i];
256+ }
257+ return grpc::Status::OK;
258+ }
259+
229260grpc::Status GetCostValuesAndWeights (
230261 const GetCostValuesAndWeightsRequest* request, const mjpc::Agent* agent,
231262 const mjModel* model, mjData* data,
0 commit comments