Skip to content

Instantly share code, notes, and snippets.

@Manojbhat09
Last active September 24, 2020 06:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manojbhat09/713f465f1c1a9b7932e73c1b542fa120 to your computer and use it in GitHub Desktop.
Save Manojbhat09/713f465f1c1a9b7932e73c1b542fa120 to your computer and use it in GitHub Desktop.

Credits: Zuxin Liu https://github.com/liuzuxin

Ubuntu 18.04 Environment Configuration (from beginning to giving up)

1. Install Ubuntu

  • make sure that the /swap partition is twice larger than the computer memory.
  • 25GB is enough for root /.
  • The /boot partition should be use as 'EFI system partition'. 1GB is enough.
  • The / partition should be of 'primary' type. Others should be 'logical'.

2. Follow ROS installation instruction

3. Install Nvidia GPU driver

4. Install CUDA

  • From CUDA official website to download CUDA-*-10.run file and install

  • Add those command to your ~/.bashrc and comment other CUDA command:

export CUDA_HOME=/usr/local/cuda  # change this to your cuda root folder
    
export CUDA_INC_PATH=${CUDA_HOME}/include    
export CUDA_LIB_PATH=${CUDA_HOME}/lib64    
    
export CUDA_INSTALL_PATH=${CUDA_HOME}    
    
export PATH=${CUDA_HOME}/bin:$PATH    
export PATH=${CUDA_HOME}/computeprof/bin:$PATH    
    
export LD_LIBRARY_PATH=${CUDA_HOME}/computeprof/bin:$LD_LIBRARY_PATH    
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64:$LD_LIBRARY_PATH    
export LD_LIBRARY_PATH=${CUDA_HOME}/extras/CUPTI/lib64:$LD_LIBRARY_PATH    
export MANPATH=${CUDA_HOME}/man:$MANPATH    
    
export OPENCL_HOME=${CUDA_HOME}    
export OPENCL_INC_PATH=${OPENCL_HOME}/include    
export OPENCL_LIB_PATH=${OPENCL_HOME}/lib64    
export LD_LIBRARY_PATH=${OPENCL_LIB_PATH}:$LD_LIBRARY_PATH

5. Install Anaconda3

  • After installation, do not add anything to ~/.bashrc because it will influence ROS!

  • Add the following command to your ~/.bashrc

alias condaenv="export PATH="/home/safeai/anaconda3/bin:$PATH""

When you want to use conda environment in the terminal, you only need to execute condaenv command for the first time, and then in this terminal, all the python path will be linked to your anconda environment.

  • You may want to setup a python 3.6 conda environment which can be used in ROS, using following command:
conda create -n py36 python=3.6

Then add alias py36="source activate py36" to your ~/.bashrc file. When you want to use py36 environment in the terminal, you only need to execute condaenv command for the first time, and then run py36 in this terminal, now you can use this python environment!

6. How to use ROS in anaconda py36 environment

sudo apt-get install python3-pip python3-yaml
pip install rospkg catkin_pkg
  • Now you can test in your terminal:
python
import rospy

If no error message, then it is OK.

* Use cv_bridge in py36 environment

  • If you directly use cv_bridge in python3, you may have following issue:
File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/core.py", line 91, in encoding_to_cvtype2
    from cv_bridge.boost.cv_bridge_boost import getCvType
ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)

This is because the cv_bridge module is compiled via python2.7.

  • To solve this, let's install opencv in anaconda first and some tools as well.
conda install -c conda-forge opencv
sudo apt-get install python-catkin-tools python3-dev python3-numpy
  • Then create a new workspace for ros packages which you want to compile by python3.6
cd ~/ && mkdir py3_ws && cd py3_ws
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
mkdir src
catkin config --install
cd src
git clone -b melodic https://github.com/ros-perception/vision_opencv.git
cd ..
catkin build cv_bridge

Now you can use cv_bridge in python3! (But you need to source this workspace first)

  • How to verify? create a test.py file and paste the following code into it:
#!/usr/bin/env python
from __future__ import print_function

import roslib
import sys
import rospy

import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError


class image_converter:

  def __init__(self):
    self.image_pub = rospy.Publisher("/image_topic2",Image)

    self.bridge = CvBridge()
    self.image_sub = rospy.Subscriber("/wide_stereo/left/image_raw_throttle",Image,self.callback)

  def callback(self,data):
    try:
      cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      print(e)

    (rows,cols,channels) = cv_image.shape
    if cols > 60 and rows > 60 :
      cv2.circle(cv_image, (150,50), 20, 255)

    cv2.imshow("Image window", cv_image)
    cv2.waitKey(3)

    try:
      self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))
    except CvBridgeError as e:
      print(e)

def main(args):
  ic = image_converter()
  rospy.init_node('image_converter', anonymous=True)
  try:
    rospy.spin()
  except KeyboardInterrupt:
    print("Shutting down")
  cv2.destroyAllWindows()

if __name__ == '__main__':
    main(sys.argv)

Then download any test dataset from https://projects.csail.mit.edu/stata/downloads.php , for example I downloaded the 2011-04-06-07-04-17.bag rosbag, so I just start a roscore in a terminal, go to the rosbag folder and run

rosbag play 2011-04-06-07-04-17.bag

Then start a new terminal:

condaenv
py36
source ~/py3_ws/devel/setup.bash

Then execute the test.py file by python test.py Hopefully you can see the result ! ROS can communicate with python3 node via cv_bridge! You can use any deep learning model now!

  • For any ros packages you want to use in python3, you can try such kind of method. Be sure that don't create too much ros workspace because they may influence each other. My suggestion is do not create more than 3 ros workspace in one computer!

7. Use Pytorch and cudnn in Anaconda

  • Just conda install pytorch

8. Tricks

Inputrc configuration

Make your terminal tab completion not sensitive to the case and the command search more efficient:

sudo vim /etc/inputrc

add

set completion-ignore-case On
"\ep": history-search-backward
"\e[A": history-search-backward
"\e[B": history-search-forward

CANdriver for polysync

Do not install Kvaser driver.

sudo apt-get can-utils
sudo modprobe can
sudo modprobe can_raw
sudo modprobe kvaser_usb
sudo ip link set can0 type can bitrate 500000
sudo ip link set up can0

If can’t find can0, maybe Kvaser's CANlib is installed. When those are installed, they blacklist socketcan which will cause problems. Then you need to uninstall Kvaser driver first.

Use multiple gcc version on Ubuntu 18.04

For CUDA 9 on Ubuntu 18.04, it requires gcc version 6 or below. reference: https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/ Use sudo update-alternatives --config gcc to change the gcc version.

Install cudnn from .tar

  1. Navigate to your directory containing the cuDNN Tar file.

  2. Unzip the cuDNN package. $ tar -xzvf cudnn-9.0-linux-x64-v7.tgz

  3. Copy the following files into the CUDA Toolkit directory.

sudo cp cuda/include/cudnn.h /usr/local/cuda/include 
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

Install python-pcl with pcl-1.8 in anconda

conda install pcl python-pcl -c artemuk -c conda-forge

tmux configuration

# Send prefix
set-option -g prefix C-a
unbind-key C-a
bind-key C-a send-prefix

# Use Alt-arrow keys to switch panes
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

# Shift arrow to switch windows
bind -n S-Left previous-window
bind -n S-Right next-window

# Mouse mode
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on

# Set easier window split keys
bind-key v split-window -h
bind-key h split-window -v

# Easy config reload
bind-key r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"

https://www.jianshu.com/p/fd3bbdba9dc9

Ubuntu 1804 -> 2004

https://ubuntu.com/blog/how-to-upgrade-from-ubuntu-18-04-lts-to-20-04-lts-today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment