Skip to content

Commit 3c2afeb

Browse files
committed
creating a framework (not working)
1 parent 5935ca6 commit 3c2afeb

14 files changed

Lines changed: 392 additions & 6 deletions

File tree

src/bitbots_behavior/bitbots_blackboard/bitbots_blackboard/capsules/world_model_capsule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self, node, blackboard):
5959

6060
# Ball state
6161
self._ball: PointStamped = PointStamped(
62-
header=Header(stamp=Time(clock_type=ClockType.ROS_TIME).to_msg(), frame_id=self.map_frame)
62+
header=Header(stamp=Time(clock_type=ClockType.ROS_TIME), frame_id=self.map_frame)
6363
) # The ball in the map frame (default to the center of the field if ball is not seen yet)
6464
self._ball_covariance: np.ndarray = np.zeros((2, 2)) # Covariance of the ball
6565

src/bitbots_behavior/bitbots_body_behavior/bitbots_body_behavior/behavior_dsd/actions/kick_ball.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ def perform(self, reevaluate=False):
100100

101101
# only check 2 directions, left and right
102102
kick_direction = self.blackboard.costmap.get_best_kick_direction(
103-
-self.penalty_kick_angle, self.penalty_kick_angle, 2, self.kick_length, self.angular_range
103+
-self.penalty_kick_angle,
104+
self.penalget_ball_position_uvty_kick_angle,
105+
2,
106+
self.kick_length,
107+
self.angular_range,
104108
)
105109
else:
106110
ball_u, ball_v = self.blackboard.world_model.get_ball_position_uv()
@@ -141,6 +145,7 @@ def perform(self, reevaluate=False):
141145
goal_pose.header.stamp = self.blackboard.node.get_clock().now().to_msg()
142146
goal_pose.header.frame_id = self.blackboard.world_model.base_footprint_frame
143147

148+
# TODO: Extract
144149
ball_u, ball_v = self.blackboard.world_model.get_ball_position_uv()
145150
goal_pose.pose.position.x = ball_u
146151
goal_pose.pose.position.y = ball_v

src/bitbots_misc/bitbots_bringup/launch/motion.launch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<arg name="sim" value="$(var sim)" />
4040
</include>
4141

42-
<!-- launch the walking and kicking -->
42+
<!-- launch the walking -->
4343
<group if="$(var walking)">
4444
<node pkg="bitbots_rl_walk" exec="walk" output="screen">
4545
<param name="use_sim_time" value="$(var sim)"/>

src/bitbots_rl_walk/bitbots_rl_walk/kick.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(self):
104104
# Load the ONNX model
105105
self._onnx_session = rt.InferenceSession(ONNX_MODEL, providers=["CPUExecutionProvider"])
106106

107-
self._joint_command_pub = self.create_publisher(JointCommand, "DynamixelController/command", 10)
107+
self._joint_command_pub = self.create_publisher(JointCommand, "kick_motor_goals", 10)
108108
self._imu_sub = self.create_subscription(Imu, "imu/data", self._imu_callback, 10)
109109
self._joint_state_sub = self.create_subscription(JointState, "joint_states", self._joint_state_callback, 10)
110110
self._cmd_vel_sub = self.create_subscription(Twist, "cmd_vel", self._cmd_vel_callback, 10)
@@ -122,7 +122,7 @@ def __init__(self):
122122

123123
joint_command.positions = WALKREADY_STATE
124124
self._joint_command_pub.publish(joint_command)
125-
time.sleep(20)
125+
time.sleep(1)
126126

127127
def _joint_state_callback(self, msg: JointState):
128128
self._joint_state = msg
@@ -166,6 +166,8 @@ def _timer_callback(self):
166166
self.get_logger().warning("Waiting for goal pose data", throttle_duration_sec=1.0)
167167

168168
return
169+
else:
170+
self.get_logger().warning("RL Kick has all data!")
169171

170172
# TODO consider IMU mounting offset
171173

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
3+
from ament_index_python import get_package_share_directory
4+
from handler.gyro_handler import GyroHandler
5+
6+
from src.rl_node import RLNode
7+
8+
walk_policy_path = os.path.join(get_package_share_directory("bitbots_rl_walk"), "models", "wolfgang_walk_ppo.onnx")
9+
kick_policy_path = os.path.join(get_package_share_directory("bitbots_rl_walk"), "models", "wolfgang_kick_ppo.onnx")
10+
11+
# Attention: Handler structure should match obs structure!
12+
walk_policy_obs_stack = [
13+
GyroHandler(),
14+
# GravityHandler(),
15+
]
16+
17+
walk_policy_publishers = []
18+
19+
20+
walk_node = RLNode(walk_policy_path)
21+
kick_node = RLNode(kick_policy_path)

src/bitbots_rl_walk/bitbots_rl_walk/walk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def __init__(self):
175175
# Load the ONNX model
176176
self._onnx_session = rt.InferenceSession(ONNX_MODEL, providers=["CPUExecutionProvider"])
177177

178-
self._joint_command_pub = self.create_publisher(JointCommand, "DynamixelController/command", 10)
178+
self._joint_command_pub = self.create_publisher(JointCommand, "walking_motor_goals", 10)
179179
self._imu_sub = self.create_subscription(Imu, "imu/data", self._imu_callback, 10)
180180
self._joint_state_sub = self.create_subscription(JointState, "joint_states", self._joint_state_callback, 10)
181181
self._cmd_vel_sub = self.create_subscription(Twist, "cmd_vel", self._cmd_vel_callback, 10)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from geometry_msgs.msg import Twist
2+
from handler.command_handler import CommandHandler
3+
from handler.gravity_handler import GravityHandler
4+
from handler.gyro_handler import GyroHandler
5+
from handler.joints_handler import JointsHandler
6+
from sensor_msgs.msg import Imu, JointState
7+
8+
from bitbots_msgs.msg import JointCommand
9+
from src.rl_node import RLNode
10+
11+
12+
class WalkNode(RLNode):
13+
def __init__(self, walk_policy_path):
14+
super().__init__(walk_policy_path)
15+
16+
# publishers
17+
self._joint_command_pub = self.create_publisher(JointCommand, "walking_motor_goals", 10)
18+
19+
# suscribers
20+
self._imu_sub = self.create_subscription(Imu, "imu/data", self._imu_callback, 10)
21+
self._joint_state_sub = self.create_subscription(JointState, "joint_states", self._joint_state_callback, 10)
22+
self._cmd_vel_sub = self.create_subscription(Twist, "cmd_vel", self._cmd_vel_callback, 10)
23+
24+
# handlers
25+
self._gyro_handler = GyroHandler()
26+
self._gravity_handler = GravityHandler()
27+
self._joints_handler = JointsHandler()
28+
self._command_handler = CommandHandler()
29+
30+
def _imu_callback(self, msg):
31+
self._gyro_handler.imu_callback(msg)
32+
self._gravity_handler.imu_callback(msg)
33+
34+
def _joint_state_callback(self, msg):
35+
self._joints_handler.joint_state_callback(msg)
36+
37+
def _cmd_vel_callback(self, msg):
38+
self._command_handler.cmd_vel_callback(msg)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
3+
4+
class CommandHandler:
5+
def __init__(self):
6+
self._cmd_vel = None
7+
8+
def get_data(self):
9+
command = np.array([self._cmd_vel.linear.x, self._cmd_vel.linear.y, self._cmd_vel.angular.z], dtype=np.float32)
10+
return command
11+
12+
def cmd_vel_callback(self, msg):
13+
self._cmd_vel = msg
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
from transforms3d.euler import euler2mat
3+
from transforms3d.quaternions import quat2mat
4+
5+
6+
class GravityHandler:
7+
def __init__(self):
8+
self._imu_data = None
9+
self._gravity = None
10+
11+
# Callables
12+
def imu_callback(self, msg):
13+
self._imu_data = msg
14+
15+
def get_data(self):
16+
gravity = (
17+
quat2mat(
18+
[
19+
self._imu_data.orientation.w,
20+
self._imu_data.orientation.x,
21+
self._imu_data.orientation.y,
22+
self._imu_data.orientation.z,
23+
]
24+
)
25+
@ euler2mat(0, -0.0, 0)
26+
).T @ np.array([0, 0, -1], dtype=np.float32)
27+
28+
return gravity
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
from sensor_msgs.msg import Imu
3+
4+
5+
# TODO: Adapt to software
6+
class GyroHandler:
7+
def __init__(self, subscribers):
8+
self._gyro = {"subscriber": {"Imu": [Imu, "imu/data", self.imu_callback, 10]}, "name": "gyro"}
9+
10+
# TODO:
11+
12+
# Callables
13+
def imu_callback(self, msg):
14+
self._imu_data = msg
15+
16+
def set(self, key, value):
17+
self._gyro[key] = value
18+
19+
def get_data(self):
20+
gyro = np.array(
21+
[
22+
self._imu_data.angular_velocity.x,
23+
self._imu_data.angular_velocity.y,
24+
self._imu_data.angular_velocity.z,
25+
],
26+
dtype=np.float32,
27+
)
28+
29+
return gyro
30+
31+
def get_confg(self):
32+
return self._gyro

0 commit comments

Comments
 (0)