mirror of https://github.com/fan-ziqi/rl_sar.git
fix: add mutex protection for robot_date to ensure thread safety in multi-threaded environments
This commit is contained in:
parent
6e09eb97de
commit
26166fb485
|
@ -5,6 +5,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
|
@ -123,6 +124,7 @@ public:
|
||||||
|
|
||||||
RobotState<double> robot_state;
|
RobotState<double> robot_state;
|
||||||
RobotCommand<double> robot_command;
|
RobotCommand<double> robot_command;
|
||||||
|
std::mutex robot_state_mutex;
|
||||||
|
|
||||||
// init
|
// init
|
||||||
void InitObservations();
|
void InitObservations();
|
||||||
|
|
|
@ -139,6 +139,8 @@ void RL_Real::SetCommand(const RobotCommand<double> *command)
|
||||||
|
|
||||||
void RL_Real::RobotControl()
|
void RL_Real::RobotControl()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(robot_state_mutex);
|
||||||
|
|
||||||
this->motiontime++;
|
this->motiontime++;
|
||||||
|
|
||||||
this->GetState(&this->robot_state);
|
this->GetState(&this->robot_state);
|
||||||
|
@ -148,6 +150,8 @@ void RL_Real::RobotControl()
|
||||||
|
|
||||||
void RL_Real::RunModel()
|
void RL_Real::RunModel()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(robot_state_mutex);
|
||||||
|
|
||||||
if (this->running_state == STATE_RL_RUNNING)
|
if (this->running_state == STATE_RL_RUNNING)
|
||||||
{
|
{
|
||||||
this->obs.ang_vel = torch::tensor(this->robot_state.imu.gyroscope).unsqueeze(0);
|
this->obs.ang_vel = torch::tensor(this->robot_state.imu.gyroscope).unsqueeze(0);
|
||||||
|
|
|
@ -144,6 +144,8 @@ void RL_Real::SetCommand(const RobotCommand<double> *command)
|
||||||
|
|
||||||
void RL_Real::RobotControl()
|
void RL_Real::RobotControl()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(robot_state_mutex);
|
||||||
|
|
||||||
this->motiontime++;
|
this->motiontime++;
|
||||||
|
|
||||||
this->GetState(&this->robot_state);
|
this->GetState(&this->robot_state);
|
||||||
|
@ -153,6 +155,8 @@ void RL_Real::RobotControl()
|
||||||
|
|
||||||
void RL_Real::RunModel()
|
void RL_Real::RunModel()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(robot_state_mutex);
|
||||||
|
|
||||||
if (this->running_state == STATE_RL_RUNNING)
|
if (this->running_state == STATE_RL_RUNNING)
|
||||||
{
|
{
|
||||||
this->obs.ang_vel = torch::tensor(this->robot_state.imu.gyroscope).unsqueeze(0);
|
this->obs.ang_vel = torch::tensor(this->robot_state.imu.gyroscope).unsqueeze(0);
|
||||||
|
|
|
@ -153,6 +153,8 @@ void RL_Sim::SetCommand(const RobotCommand<double> *command)
|
||||||
|
|
||||||
void RL_Sim::RobotControl()
|
void RL_Sim::RobotControl()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(robot_state_mutex);
|
||||||
|
|
||||||
if (this->control.control_state == STATE_RESET_SIMULATION)
|
if (this->control.control_state == STATE_RESET_SIMULATION)
|
||||||
{
|
{
|
||||||
gazebo_msgs::SetModelState set_model_state;
|
gazebo_msgs::SetModelState set_model_state;
|
||||||
|
@ -216,6 +218,8 @@ void RL_Sim::JointStatesCallback(const sensor_msgs::JointState::ConstPtr &msg)
|
||||||
|
|
||||||
void RL_Sim::RunModel()
|
void RL_Sim::RunModel()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(robot_state_mutex);
|
||||||
|
|
||||||
if (this->running_state == STATE_RL_RUNNING && simulation_running)
|
if (this->running_state == STATE_RL_RUNNING && simulation_running)
|
||||||
{
|
{
|
||||||
this->obs.lin_vel = torch::tensor({{this->vel.linear.x, this->vel.linear.y, this->vel.linear.z}});
|
this->obs.lin_vel = torch::tensor({{this->vel.linear.x, this->vel.linear.y, this->vel.linear.z}});
|
||||||
|
|
Loading…
Reference in New Issue