Skip to content

Commit

Permalink
add lab4
Browse files Browse the repository at this point in the history
  • Loading branch information
zzx9636 committed Apr 18, 2023
1 parent a871b2b commit aaef24f
Show file tree
Hide file tree
Showing 20 changed files with 1,421 additions and 1 deletion.
123 changes: 123 additions & 0 deletions ROS_Core/src/Labs/Lab4/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
cmake_minimum_required(VERSION 3.0.2)
project(racecar_learning)


## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
dynamic_reconfigure
rospy
std_msgs
nav_msgs
racecar_msgs
)

# Uncomment this if the package has a setup.py. This macro ensures
# modules and global scripts declared therein get installed
# See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )

## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )

## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )

## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs # Or other packages containing msgs
# )

################################################
## Declare ROS dynamic reconfigure parameters ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder
generate_dynamic_reconfigure_options(
cfg/controller.cfg
)


###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
CATKIN_DEPENDS
rospy
std_msgs
racecar_msgs
nav_msgs
dynamic_reconfigure
)

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
install(PROGRAMS
scripts/imitation_learning_node.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

1 change: 0 additions & 1 deletion ROS_Core/src/Labs/Lab4/README.md

This file was deleted.

Binary file added ROS_Core/src/Labs/Lab4/asset/loop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ROS_Core/src/Labs/Lab4/asset/rqt_truck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions ROS_Core/src/Labs/Lab4/cfg/controller.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python
PACKAGE = "racecar_learning"

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

gen.add("latency", double_t, 0, "Latency compensation for control", 0.2, -1, 1)
gen.add("ref_speed", double_t, 0, "Reference speed for control", 1, 0, 1.5)

exit(gen.generate("racecar_learning", "racecar_learning", "controller"))
Binary file added ROS_Core/src/Labs/Lab4/data/data_example.pkl
Binary file not shown.
21 changes: 21 additions & 0 deletions ROS_Core/src/Labs/Lab4/launch/lab4.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<launch>
<arg name="output" default="screen" doc="display output to screen or log file"/>
<arg name="odom_topic" default="/SLAM/Pose" doc="ROS topic for the pose"/>
<arg name="control_topic" default="/Control" doc="ROS topic for control input"/>
<arg name="path_topic" default="/Routing/Path" doc="ROS topic for reference path"/>
<arg name='model_path' default="" doc='path to the model file'/>
<arg name='lr' default="0.01" doc='learning rate'/>


<node pkg="racecar_learning" type="learning_node.py" name="imitation_learning" output="$(arg output)">
<param name="odom_topic" value="$(arg odom_topic)" />
<param name="control_topic" value="$(arg control_topic)" />
<param name="path_topic" value="$(arg path_topic)" />
<param name="package_path" value="$(find racecar_learning)" />
<param name="simulation" value="False" />
<param name="PWM_model" value="$(find racecar_learning)/models/pwm.sav" />
<param name="model_path" value="$(arg model_path)" />
<param name="lr" value="$(arg lr)" />
</node>

</launch>
Binary file added ROS_Core/src/Labs/Lab4/models/model_example.pt
Binary file not shown.
Binary file added ROS_Core/src/Labs/Lab4/models/pwm.sav
Binary file not shown.
20 changes: 20 additions & 0 deletions ROS_Core/src/Labs/Lab4/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<package format="2">
<name>racecar_learning</name>
<version>0.1.0</version>
<description>The traj_tracking_ros package</description>

<maintainer email="[email protected]">Zixu Zhang</maintainer>
<license>BSD</license>

<author >Zixu Zhang</author>


<buildtool_depend>catkin</buildtool_depend>

<depend>rospy</depend>
<depend>std_msgs</depend>
<depend>racecar_msgs</depend>
<depend>dynamic_reconfigure</depend>
<depend>nav_msgs</depend>
</package>
73 changes: 73 additions & 0 deletions ROS_Core/src/Labs/Lab4/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Lab 4: Behavior Cloning
In this lab, we will use a basic behavior cloning algorithm to train a model to drive the truck.


**Make sure that your repo is up-to-dated**
# Setup Environment
## 1. Open a new terminal and SSH into your robot
```
ssh nvidia@<IP OF YOUR ROBOT>
```
## 2. Activate *ros_base* environment on your robot
```
conda activate ros_base
```
## 3. Install Pytorch
```
pip3 install torch
```

# Launch Learning Node
## Open **three** new terminals and let's call them *T1*, *T2*, and *T3*
### 1. In *T1*, SSH into your robot and launch SLAM
```
ssh nvidia@<IP OF YOUR ROBOT>
cd ~/StartUp
./start_ros.sh <IP OF YOUR ROBOT>
```
### 2. In *T2*, navigate to the repo on your PC
```
cd <REPO ON YOUR PC>/ROS_Core
conda activate ros_base
catkin_make
source devel/setup.bash
source network_ros_client.sh <IP OF YOUR ROBOT> <IP OF YOUR PC>
roslaunch racecar_interface visualization.launch enable_routing:=false
```
### 3. Start the SLAM from RQT
![](./asset/rqt_truck.png)
### 4. In *T3*, SSH into your robot and start learning node
```
ssh nvidia@<IP OF YOUR ROBOT>
cd <REPO ON YOUR ROBOT>/ROS_Core
conda activate ros_base
catkin_make
source devel/setup.bash
source network_ros_host.sh <IP OF YOUR ROBOT>
roslaunch racecar_learning lab4.launch
```

# Start Training Online
## 1. In the RQT, call the service *"learning/start_learn"* from RQT to start training.
## 2. In the RVIZ, use the *"2D Nav Goal"* to set a reference path for the robot
A loop will be generated automatically as your robot's reference path, use your controller to drive the robot along the path.
![](./asset/loop.png)
## 3. In *T3*, you will see the loss be printed out.
You can drive your robot along the reference path for a few loops, then simply let the robot stop on the track and wait the loss to converge.
## 4. Once the loss converges, call the service *"learning/eval"* from RQT to pause the training and evalute the model.
Hit the down button on your controller to start the evaluation. The robot will drive along the reference path.
## 5. If the robot drives well, call the service *"learning/save_model"* from RQT to save the model and call the service *"learning/save_data"* to stop the training.
Your model will be saved in folder ["ROS_Core/Labs/Lab4/models"](./models) on your robot, and the training data will be saved in folder ["ROS_Core/Labs/Lab4/data"](./data) on your robot.
## 6. If you do not like your model, call the service *"learning/start_learn"* again from RQT to resume training again.

# Traning Offline
With data collected from the previous step, we can train the model offline using provided [iPython notebook](./scripts/offline_train.ipynb). You can train this on your own PC, which should be significantly faster than the computer on the robot.

# Test the Model
You can evaluate the model trained offline by using additional parameter during the launch of the learning node. In *T3*, relaunch the node using
```
roslaunch racecar_learning lab4.launch model_path:=<PATH TO YOUR MODEL>
```

# Task: Train your own behavior cloning policy, test it on the robot and show it to your AIs

Loading

0 comments on commit aaef24f

Please sign in to comment.