Skip to content

Commit

Permalink
feat: possibly done! Decrease simulator refresh rate
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed Jan 30, 2024
1 parent 2eb6243 commit a758b4e
Show file tree
Hide file tree
Showing 5 changed files with 428 additions and 381 deletions.
7 changes: 5 additions & 2 deletions Dockerfile-gym-ros
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ RUN echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc && \
mv f1tenth_racetracks /sim_ws/src/car_node/car_node/map/

COPY model-gym-ros-env/car_node/setup.py /sim_ws/src/car_node/setup.py
COPY model-gym-ros-env/car_node/car_node/main.py model-gym-ros-env/car_node/car_node/wrapper.py model-gym-ros-env/car_node/car_node/map_utility.py model-gym-ros-env/car_node/car_node/setup.py /sim_ws/src/car_node/car_node/
COPY model-gym-ros-env/car_node/car_node/main.py model-gym-ros-env/car_node/car_node/setup.py /sim_ws/src/car_node/car_node/

COPY model-gym-ros-env/f1tenth_gym_ros/config/sim.yaml /sim_ws/src/f1tenth_gym_ros/config/sim.yaml
COPY model-gym-ros-env/f1tenth_gym_ros/f1tenth_gym_ros/gym_bridge.py /sim_ws/src/f1tenth_gym_ros/f1tenth_gym_ros/gym_bridge.py

COPY train_test /sim_ws/src/car_node/car_node/train_test
COPY models/* /sim_ws/src/car_node/car_node/models/
COPY model-gym-ros-env/f1tenth_gym_ros/config/sim.yaml /sim_ws/src/f1tenth_gym_ros/config/sim.yaml

RUN pip install -e /sim_ws/src/car_node/car_node && \
source /opt/ros/foxy/setup.bash && \
Expand Down
37 changes: 12 additions & 25 deletions model-gym-ros-env/car_node/car_node/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@
from sensor_msgs.msg import LaserScan
from ackermann_msgs.msg import AckermannDriveStamped


import gym
from stable_baselines3 import PPO
from car_node.wrapper import F110_Wrapped
from f110_gym.envs.base_classes import Integrator
from car_node.map_utility import get_map, get_formatted_map, map_ext
from car_node.wrapper import convert_range

timestep = 0.01
tensorboard_path = './train_test/'
map = "BrandsHatch"
path = get_map(map)
map_path = get_formatted_map(path)
device = 'cpu'

STEER_SCALE = 0.5
SPEED_SCALE = 0.7
STEERING_RANGE_MIN = -0.4189
STEERING_RANGE_MAX = 0.4189
SPEED_RANGE_MIN = -5
SPEED_RANGE_MAX = 20
MODEL_INTERVAL = [-1, 1]

class PPOModelEvaluator(Node):

Expand All @@ -35,31 +28,25 @@ def __init__(self):
lidarscan_topic = '/scan'
drive_topic = '/drive'

# Model & Env
self.eval_env = gym.make('f110_gym:f110-v0', map=map_path, map_ext=map_ext, num_agents=1, timestep=timestep, integrator=Integrator.RK4)
self.eval_env = F110_Wrapped(self.eval_env, random_map=False)
self.eval_env.set_map_path(path)
self.model = PPO.load("/sim_ws/src/car_node/car_node/train_test/best_global_model", self.eval_env, device=device)
self.vec_env = self.model.get_env()
self.model = PPO.load("/sim_ws/src/car_node/car_node/models/YasMarina_optimize_model.zip", device=device)

# ROS
self.lidar_sub = self.create_subscription(LaserScan, lidarscan_topic, self.lidar_callback, 10)
self.drive_pub = self.create_publisher(AckermannDriveStamped, drive_topic, 10)

def lidar_callback(self, data):
d = np.array(data.ranges, dtype=np.float64)
d = convert_range(d, [data.angle_min, data.angle_max], [-1, 1])

action, _states = self.model.predict(d, deterministic=True)
action = self.eval_env.un_normalise_actions(action)
d = convert_range(d, [data.range_min, data.range_max], MODEL_INTERVAL)

self.get_logger().info(f'{action[0], action[1]}')
action = self.model.predict(d, deterministic=True)[0]
steer = convert_range(action[0], MODEL_INTERVAL, [STEERING_RANGE_MIN, STEERING_RANGE_MAX])
speed = convert_range(action[1], MODEL_INTERVAL, [SPEED_RANGE_MIN, SPEED_RANGE_MAX])

drive_msg = AckermannDriveStamped()
drive_msg.header.stamp = self.get_clock().now().to_msg()
drive_msg.header.frame_id = "laser"
drive_msg.drive.steering_angle = action[0] * STEER_SCALE
drive_msg.drive.speed = action[1] * SPEED_SCALE
drive_msg.drive.steering_angle = steer
drive_msg.drive.speed = speed
self.drive_pub.publish(drive_msg)

def main(args=None):
Expand Down
101 changes: 0 additions & 101 deletions model-gym-ros-env/car_node/car_node/map_utility.py

This file was deleted.

Loading

0 comments on commit a758b4e

Please sign in to comment.