23
16

More than 3 years have passed since last update.

ROS for Windows インストールから動作確認まで

Last updated at Posted at 2020-08-10

はじめに

この記事では、ROS for Windowsのインストール方法と、動作確認としてチュートリアルのシンプルなPublisherとSubscriberを実行してみるをやりたいと思います。ROS for Windowsをインストールした時の備忘録的な記事です。
注意点ですが、この記事ではROSをWSL上にインストールするのではなく、Windowsに直接インストールします。WSL上にインストール記事はたくさんあったのですが、Windowsに直接インストールする記事は少なく需要があるかわかりませんが、書いていこうと思います。

※これから紹介するインストール方法は2020/8/10時点のものです。インストールする際は事前に公式のインストール方法を必ず確認してください。

※ROS for Windowsは現時点でROSのすべての機能が使えるわけではありません

インストール

公式通りにインストールしていきます。ここでインストールするのはROS noeticとします。
http://wiki.ros.org/Installation/Windows

Visual Studio 2019のインストール

すでにインストールしてある方はスキップします。ダウンロードはこちらから。

インストール時は以下を含める必要があります。

  • C++によるデスクトップ開発
  • 英語の言語パック

インストールが完了したらPCを再起動します。

パッケージマネージャ(Chocolatey)のインストール

Visual Studio Command Promptを管理者権限で起動して以下をコピペします。

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

ここで注意点ですが、ウイルス対策ソフトのリアルタイムスキャンによってコマンドが実行できない場合があります。その場合は、一時的に無効にするか、C:\optをリアルタイムスキャンの対象から除外してください。

次にVisual Studio Command Promptを管理者権限で再起動してgitをインストールします。すでにインストール済みであればスキップします。

choco upgrade git -y

インストール確認

git --version

ROSのインストール

ROS1をインストールするには以下のコマンドを実行します。

mkdir c:\opt\chocolatey
set ChocolateyInstall=c:\opt\chocolatey
choco source add -n=ros-win -s="https://aka.ms/ros/public" --priority=1
choco upgrade ros-noetic-desktop_full -y --execution-timeout=0

上記でエラーが出た場合は以下。(私の環境ではエラーが出たので以下のコマンドでインストールしました)

mkdir c:\opt\chocolatey
set ChocolateyInstall=c:\opt\chocolatey
choco source add -n=ros-win -s="https://aka.ms/ros/public" --priority=1
choco upgrade ros-noetic-desktop_full -y --execution-timeout=0 --pre

ROS2の場合は以下です。(こちらは試していません)

mkdir c:\opt\chocolatey
set ChocolateyInstall=c:\opt\chocolatey
choco source add -n=ros-win -s="https://aka.ms/ros/public" --priority=1
choco upgrade ros-eloquent-desktop -y --execution-timeout=0 --pre

ROSコマンドウィンドウのショートカット作成

ROSコマンドを使うには、コマンドプロンプトを起動するたびに毎回セットアップスクリプトを実行する必要があるため、以下の手順でショートカットを作ります。

  1. エクスプローラの任意の場所(もしくはデスクトップ)で右クリック
  2. 新規作成>ショートカット
  3. 項目の場所に以下をコピペ(Visual Studio 2019 コミュニティ版の場合)

    C:\Windows\System32\cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat" -arch=amd64 -host_arch=amd64&& set ChocolateyInstall=c:\opt\chocolatey&& c:\opt\ros\noetic\x64\setup.bat
    
  4. 名前を「ROS」に設定(任意)

  5. ショートカットを右クリックしてプロパティ>詳細設定

  6. 「管理者として実行」にチェックを入れる

動作確認

基本的にはチュートリアル通りにやります。当然ながらチュートリアルはLinuxコマンドなので必要なところは適宜Windowsのコマンドに置き換えます。
以下、ワークスペースはC:\path\to\catkin_wsに作ることとします。

ワークスペース作成

mkdir -p C:\path\to\catkin_ws\src
cd C:\path\to\catkin_ws\src
catkin_init_workspace
cd C:\path\to\catkin_ws
catkin_make
C:\path\to\catkin_ws\devel\setup.bat

パッケージ作成

チュートリアルと同じくbeginner_tutorialsパッケージを作ります。

cd C:\path\to\catkin_ws\src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
cd C:\path\to\catkin_ws
catkin_make

チュートリアルと同じように以下のコマンドでパッケージ依存の確認ができます。

C:\path\to\catkin_ws\devel\setup.bat
rospack depends1 beginner_tutorials

talker.pyとlistner.pyの準備

beginner_tutorialsのディレクトリに移動します。

cd C:\path\to\catkin_ws\src\beginner_tutorials

roscdを使うとエラーが出ました。原因は不明です。(誰か教えてください)

>roscd beginner_tutorials
Traceback (most recent call last):
  File "C:\opt\ros\noetic\x64\bin\\rosfindpath.py", line 82, in <module>
    sys.exit(findpathmain(sys.argv[1:]))
  File "C:\opt\ros\noetic\x64\bin\\rosfindpath.py", line 74, in findpathmain
    rosdir = os.path.normpath(os.path.sep.join([package_dir, reldir]))
TypeError: sequence item 0: expected str instance, bytes found
( の使い方が誤っています。

次にpythonコードを保存するディレクトリを作ります。

mkdir scripts
cd scripts

talker.pylistener.pyをダウンロードする。ここではwgetの代わりにwindowsに標準でインストールされているbitsadminを使用します。

bitsadmin.exe /TRANSFER getpythoncode https://raw.github.com/ros/ros_tutorials/indigo-devel/rospy_tutorials/001_talker_listener/talker.py C:\path\to\catkin_ws\src\beginner_tutorials\scripts\talker.py
bitsadmin.exe /TRANSFER getpythoncode https://raw.github.com/ros/ros_tutorials/indigo-devel/rospy_tutorials/001_talker_listener/listener.py C:\path\to\catkin_ws\src\beginner_tutorials\scripts\listener.py

C:\path\to\catkin_ws\src\beginner_tutorials\CMakeLists.txtを編集する。catkin_install_pythonを見つけてアンコメントして以下のようにする。

catkin_install_python(PROGRAMS
  scripts/talker.py
  scripts/listener.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

ビルドする。

cd C:\path\to\catkin_ws
catkin_make

コマンドプロンプトをショートカットから3つ開いてそれぞれで以下を実行する。

roscore
C:\path\to\catkin_ws\devel\setup.bat
rosrun beginner_tutorials talker.py
C:\path\to\catkin_ws\devel\setup.bat
rosrun beginner_tutorials listener.py

そうするとLinux版と同じ動作が確認できると思います。簡単にではありますが以上で動作確認終了です。

アップデートしたいとき

ショートカットからコマンドウィンドウを開いて以下のコマンドでROSをアップデートできます。

set ChocolateyInstall=c:\opt\chocolatey
choco upgrade all -y --execution-timeout=0

アンインストールしたいとき

ROSが起動していないか確認して、以下のコマンドでアンインストールできます。

rmdir /s /q c:\opt

困ったところ

roscdが使えない

なくてもROS自体は使えますが、やはりほしいですね。誰か解決策を知っている方がいたら教えていただきたいです。
すべてのROSコマンドを試しているわけではないので他にも使えないコマンドがあるかも。

devel/setup.batをコマンドウィンドウを開くたびに実行する必要がある

これはショートカット作成時の「項目の場所」に以下を追加すればいいのですが、私の環境では文字数制限で入りませんでした。

&& C:\path\to\catkin_ws\devel\setup.bat

おまけ(亀)

以下のコマンドでおなじみの亀がWindowsでも見れます。

rosrun turtlesim turtlesim_node

image.png

23
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
16