Skip to content

Commit e7da5e6

Browse files
committed
subscriber fix
1 parent 1a92e74 commit e7da5e6

7 files changed

Lines changed: 57 additions & 35 deletions

File tree

src/bitbots_motion/bitbots_rl_motion/handlers/ball_handler.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ def __init__(self, config):
99
def ball_pos_callback(self, msg):
1010
self.ball_pos = msg
1111

12+
def has_data(self):
13+
return (self.ball_pos != None)
1214

1315
def get_ball_pos(self):
14-
try:
15-
return self.ball_pos
16-
except:
17-
return None
16+
return self.ball_pos

src/bitbots_motion/bitbots_rl_motion/handlers/command_handler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ def __init__(self, config):
99
self._cmd_vel = None
1010

1111
def get_command(self):
12-
try:
13-
command = np.array([self._cmd_vel.linear.x, self._cmd_vel.linear.y, self._cmd_vel.angular.z], dtype=np.float32)
14-
return command
15-
except:
16-
return None
12+
command = np.array([self._cmd_vel.linear.x, self._cmd_vel.linear.y, self._cmd_vel.angular.z], dtype=np.float32)
13+
return command
14+
15+
def has_data(self):
16+
return (self._cmd_vel != None)
1717

1818
def cmd_vel_callback(self, msg):
1919
self._cmd_vel = msg

src/bitbots_motion/bitbots_rl_motion/handlers/gravity_handler.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,26 @@ def __init__(self, config):
99
super().__init__(config)
1010

1111
self._imu_data = None
12-
self._gravity = None
1312

1413
# Callables
1514
def imu_callback(self, msg):
1615
self._imu_data = msg
1716

17+
def has_data(self):
18+
return (self._imu_data != None)
1819

1920
def get_gravity(self):
20-
try:
21-
gravity = (
22-
quat2mat(
23-
[
24-
self._imu_data.orientation.w,
25-
self._imu_data.orientation.x,
26-
self._imu_data.orientation.y,
27-
self._imu_data.orientation.z,
28-
]
29-
)
30-
@ euler2mat(0, -0.0, 0)
31-
).T @ np.array([0, 0, -1], dtype=np.float32)
32-
return gravity
33-
except:
34-
return None
21+
gravity = (
22+
quat2mat(
23+
[
24+
self._imu_data.orientation.w,
25+
self._imu_data.orientation.x,
26+
self._imu_data.orientation.y,
27+
self._imu_data.orientation.z,
28+
]
29+
)
30+
@ euler2mat(0, -0.0, 0)
31+
).T @ np.array([0, 0, -1], dtype=np.float32)
32+
return gravity
33+
3534

src/bitbots_motion/bitbots_rl_motion/handlers/gyro_handler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ def __init__(self, config):
1010
# Callables
1111
def imu_callback(self, msg):
1212
self._imu_data = msg
13-
13+
14+
def has_data(self):
15+
return (self._imu_data != None)
1416

1517
def get_gyro(self):
1618
try:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class Handler:
22
def __init__(self, config):
33
self._config = config
4+
5+
def has_data(self):
6+
pass

src/bitbots_motion/bitbots_rl_motion/handlers/joint_handler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def __init__(self, config):
1717
def joint_state_callback(self, msg):
1818
self._joint_state = msg
1919

20+
def has_data(self):
21+
return (self._joint_state != None)
22+
2023
def get_angle_data(self):
2124
try:
2225
joint_angles = (

src/bitbots_motion/bitbots_rl_motion/nodes/rl_node.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from rclpy.qos import QoSProfile
2929
from rclpy.subscription import Subscription
3030

31+
from handlers.handler import Handler
32+
3133

3234
class RLNode(Node):
3335
"""Node to control the wolfgang humanoid."""
@@ -56,6 +58,14 @@ def _load_config(self, path: str):
5658

5759
# TODO: fix
5860
def _timer_callback(self):
61+
if not self._config:
62+
raise ConfigError("Configuration is missing!")
63+
64+
# Prüfen ob alle Subscriber schon mindestens eine Nachricht hatten
65+
if not self._all_sensors_ready():
66+
self.get_logger().warning("Waiting for all sensors to be available", throttle_duration_sec=1.0)
67+
return
68+
5969
if self._config:
6070
for subscription in self._subs:
6171
if subscription is None:
@@ -89,6 +99,13 @@ def _timer_callback(self):
8999
self._phase.set_phase(np.fmod(phase_tp1 + np.pi, 2 * np.pi) - np.pi)
90100
else:
91101
raise ConfigError("Configuration is missing! Try to run self.config() in init.")
102+
103+
def _all_sensors_ready(self):
104+
for handler in self._handlers:
105+
if not handler.has_data():
106+
return False
107+
108+
return True
92109

93110
def load_model(self, model):
94111
path_to_model = os.path.join(get_package_share_directory("bitbots_rl_motion"), "models", model)
@@ -107,20 +124,19 @@ def load_model(self, model):
107124
for out in self._onnx_model.graph.output:
108125
self._onnx_output_name.append(out)
109126

110-
self._timer = self.create_timer(self._config["phase"]["control_dt"], self._timer_callback)
111-
112127
self._subs = []
128+
self._handlers = []
113129

114-
for (key, value) in self.__dict__.values():
115-
if type(value) is Subscription:
116-
self._subs.append(key)
130+
for key, value in self.__dict__.items():
131+
if isinstance(value, Subscription):
132+
self._subs.append(value)
133+
if isinstance(value, Handler):
134+
self._handlers.append(value)
135+
136+
self._timer = self.create_timer(self._config["phase"]["control_dt"], self._timer_callback)
117137

118138
self.load_phase()
119139

120-
def obs(self):
121-
# Should be defined in subclass
122-
pass
123-
124140
def publisher(self):
125141
# Should be defined in subclass
126142
pass

0 commit comments

Comments
 (0)