Skip to content

Commit

Permalink
Merge branch 'simple_learn'
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed Jan 19, 2024
2 parents 4cbfbaf + d42d40c commit 7fa0178
Show file tree
Hide file tree
Showing 23 changed files with 1,374 additions and 250 deletions.
7 changes: 7 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
version = 1

exclude_patterns = [
"examples/**",
"gym/f110_gym/**",
"gym_env/share/**",
"docs/**"
]

[[analyzers]]
name = "python"

Expand Down
37 changes: 25 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@ name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

build-gym:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9"]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

python-version: '3.8'
- name: Install
run: |
python -m pip install --upgrade pip
pip install -e .
build-ros:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install
run: |
python -m pip install --upgrade pip
pip install -e ./model-gym-ros-env/car_node/car_node
build-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Build Docker image
run: |
docker build .
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ runs/
PPO_*/


train_test/PPO_*/


### VirtualEnv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
Expand Down
102 changes: 102 additions & 0 deletions Dockerfile-gym-ros
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# MIT License

# Copyright (c) 2020 Hongrui Zheng

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

FROM ros:foxy

SHELL ["/bin/bash", "-c"]

# dependencies
RUN apt-get update --fix-missing && \
apt-get install -y git \
nano \
python3-pip \
libeigen3-dev \
tmux \
ros-foxy-rviz2
RUN apt-get -y dist-upgrade
RUN pip3 install transforms3d

# f1tenth gym
RUN git clone https://github.com/f1tenth/f1tenth_gym
RUN cd f1tenth_gym && \
pip3 install -e .

# ros2 gym bridge
# RUN mkdir -p sim_ws/src/f1tenth_gym_ros
# COPY ./f1tenth_gym_ros /sim_ws/src/f1tenth_gym_ros
RUN git clone https://github.com/f1tenth/f1tenth_gym_ros.git /sim_ws/src/f1tenth_gym_ros && \
cd /sim_ws/src/f1tenth_gym_ros && \
git checkout 910789ad9029839abda0d7b6d66f46945fe5cef0
RUN source /opt/ros/foxy/setup.bash && \
cd sim_ws/ && \
apt-get update --fix-missing && \
rosdep install -i --from-path src --rosdistro foxy -y && \
colcon build

# our dependencies
RUN echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc && \
echo "source /sim_ws/install/setup.bash" >> ~/.bashrc && \
pip install --upgrade pip && \
# ROS2 packages
source /opt/ros/foxy/setup.bash && \
ros2 pkg create --destination-directory /sim_ws/src car_node --build-type ament_python --dependencies rclpy && \
# f1tenth gym env branch v1.0.0
rm -rf /sim_ws/src/car_node/car_node && \
git clone --depth=1 https://github.com/f1tenth/f1tenth_gym.git /sim_ws/src/car_node/car_node && \
cd /sim_ws/src/car_node/car_node && \
git checkout cd56335eda43ff4e401331c461877227474a3ed4 && \
touch /sim_ws/src/car_node/car_node/__init__.py && \
# Put all maps maps in f1tenth gym ros
cd / && \
git clone --single-branch --branch v1.0.0 https://github.com/f1tenth/f1tenth_racetracks.git && \
cp f1tenth_racetracks/*/*.{png,yaml} /sim_ws/src/f1tenth_gym_ros/maps/ && \
# Put all maps in car_node
mkdir -p /sim_ws/src/car_node/car_node/map/ && \
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 train_test /sim_ws/src/car_node/car_node/train_test

RUN pip install -e /sim_ws/src/car_node/car_node && \
# pip install stable_baselines3 && \
# pip install 'shimmy>=0.2.1' && \
source /opt/ros/foxy/setup.bash && \
cd sim_ws/ && \
colcon build

# RUN pip install setuptools==65.7.0 && \
# pip install -e /sim_ws/src/car_node/car_node && \
# pip install stable_baselines3 && \
# pip install 'shimmy>=0.2.1' && \
# cd sim_ws/ && \
# colcon build

# pip install gym[atari,accept-rom-license]==0.19.0 && \
# pip install 'stable-baselines3[extra]' && \
# pip install -e /sim_ws/src/car_node/car_node && \
# source /opt/ros/foxy/setup.bash && \
# cd sim_ws/ && \
# colcon build

WORKDIR '/sim_ws'
ENTRYPOINT ["/bin/bash"]
46 changes: 46 additions & 0 deletions docker-compose-ros-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# MIT License

# Copyright (c) 2020 Hongrui Zheng

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

version: '3'
services:
sim:
image: manuandru/f1tenth-gym-ros-model-env:latest
# volumes:
# - .:/sim_ws/src/f1tenth_gym_ros
# - ../src:/sim_ws/src/
environment:
- DISPLAY=novnc:0.0
networks:
- x11
stdin_open: true
tty: true
novnc:
image: theasp/novnc:latest
environment:
- DISPLAY_WIDTH=1728
- DISPLAY_HEIGHT=972
ports:
- "8080:8080"
networks:
- x11
networks:
x11:
1 change: 0 additions & 1 deletion f1tenth_gym-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ cd f1tenth_gym
conda create -n f1tenth_gym python=3.8
conda activate f1tenth_gym
pip install -e .
pip install -r requirements.txt
```

If there are any error during installation([source](https://github.com/freqtrade/freqtrade/issues/8376#issuecomment-1519257211)):
Expand Down
73 changes: 73 additions & 0 deletions model-gym-ros-env/car_node/car_node/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python
# from __future__ import print_function
import numpy as np
import rclpy
from rclpy.node import Node

#ROS Imports
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

class PPOModelEvaluator(Node):

def __init__(self):
super().__init__('wall_follow_node')

#Topics & Subs, Pubs
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()

# 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)

self.get_logger().info(f'{action[0], action[1]}')

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
self.drive_pub.publish(drive_msg)

def main(args=None):
rclpy.init(args=args)
node = PPOModelEvaluator()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()

if __name__=='__main__':
main()
Loading

0 comments on commit 7fa0178

Please sign in to comment.