Translate

2019年1月21日月曜日

Microsoft製のドローン・自動車シミュレータ AirSim を試す

Donkey CarにはDonkeyシミュレータというシミュレータソフトが付いているが、
Donkey Car以外では使用できない。
またDonkeyシミュレータは、コースが制限されており、ここで学習したモデルを実際の道路で走らせることはほぼできない。

最近すべてのメーカが自動運転車の実現を目指している
(もしくはそれに類する機能(自動ブレーキなど)の実現を目指している)
のは、素人でも推測可能だけど、
それならシミュレータソフトもいくつか出てきてもいいはずなんじゃないか
と思っていた。

だって、自動運転を認可するには
複数メーカの車が開発したモデルの性能評価を国はしなきゃいけないわけで
そうなると国ごとに標準シミュレータは開発しておかないとだめなんじゃないか、
と簡単に想像できるし...


で、どこかにはあるんじゃないかとおもいながらSlackを眺めていたら

でてきたよ

[GitHub] Microsoft/AirSim
https://github.com/Microsoft/AirSim



どうも自動車だけでなくドローンにも対応しているらしい..

バイナリのダウンロードはここからできるのだけど、
Windows版のバイナリは以下のシーンごとにダウンロードできるようになっている。

  • 市街地(動く車や歩行者がいる広い環境)
  • 住宅近隣(小都市近所ブロック)
  • 山の風景
  • アフリカ(起伏のある地形やアニメーションの動物)
  • 張家界(中国の張家界山)
  • 倉庫
  • 単純な迷路
  • 海岸線(マウイのハナ道路に似た短い区間)

ためしに住宅近隣シーンである AirSimNH.zip を落とす。
それぞれギガ単位(以上)のサイズがあるので、全部落とそうとすると相当時間がかかる。

unzipすると、AirSimNH\run.batが登場するのでこれを実行すると、ダイアログ画面が出てきて、はいを選択すると自動車、いいえを選択するとドローンのシミュレータ画面がたちあがる。
ドローンの方は、運転の仕方がいまいちよくわからないので自動車だけを試してみることにした。

自動車は上下左右のボタンで手動運転可能だ。



表示の変更は(Fn+)F1を押すとヘルプが出てくるので、こちらを読めばわかるとおもう。

とりあえず変なことになったらBackspaceを押せば最初の状態に戻るので、上下左右とBSだけ覚えておけばいい。

"シミュレータ"なので、キーボードからの手動運転だけでなくAPI経由でも可能だ。
上記GitHubにあるサンプルを動かしてみた。

自動車を選択してシミュレータの画面が出たら、以下のサンプルコードをpython3で実行する。

なお実行するには以下のパッケージを予めインストールする必要がある。


pip install msgpack-rpc-python
pip install airsim



import airsim
import time

client = airsim.CarClient()
client.confirmConnection()
client.enableApiControl(True)

car_controls = airsim.CarControls()

while True:
    car_state = client.getCarState()
    print("Speed %d, Gear %d" % (car_state.speed, car_state.gear))

    car_controls.throttle = 1
    car_controls.steering = 1
    client.setCarControls(car_controls)

    time.sleep(1)

    responses = client.simGetImages([
        airsim.ImageRequest(0, airsim.ImageType.DepthVis),
        airsim.ImageRequest(1, airsim.ImageType.DepthPlanner, True)
    ])
    print('Retrieved images: %d', len(responses))

    for response in responses:
        if response.pixels_as_float:
            print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
            airsim.write_pfm('py1.pfm', airsim.get_pfm_array(response))
        else:
            print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
            airsim.write_file('py1.png', response.image_data_uint8)

airsimのAPIを知らなくても、
眺めるだけで
1秒毎にスロットル、ステアリングに1を与え、
レスポンスをPNGファイル(白黒カメラ画像)とPFMファイル(同配列のシリアライズデータ)をカレントに出力するというサンプルだということがわかるとおもう。
#シミュレータ画面ではくるくる回って作にあたってしまう

複数の車を登場させることも可能だ。
Windowsの場合は一度シミュレータを動かすと、
C:\Users\<ユーザ名>\Document\AirSim フォルダ直下にsettings.jsonファイルができているので、これを以下のように変更して、サンプルコードを動かせばいい。
(こちらのコードはC:\tempを先に作っておかないと動作しない)


{
 "SettingsVersion": 1.2,
 "SimMode": "Car",
 
 "Vehicles": {
  "Car1": {
    "VehicleType": "PhysXCar",
    "X": 4, "Y": 0, "Z": -2
  },
  "Car2": {
    "VehicleType": "PhysXCar",
    "X": -4, "Y": 0, "Z": -2
  }
    }
}

# -*- coding: utf-8 -*-
import airsim

import time
import os
import numpy as np


# connect to the AirSim simulator 
client = airsim.CarClient()
client.confirmConnection()
client.enableApiControl(True, "Car1")
client.enableApiControl(True, "Car2")

car_controls1 = airsim.CarControls()
car_controls2 = airsim.CarControls()


for idx in range(3):
    # get state of the car
    car_state1 = client.getCarState("Car1")
    print("Car1: Speed %d, Gear %d" % (car_state1.speed, car_state1.gear))
    car_state2 = client.getCarState("Car2")
    print("Car1: Speed %d, Gear %d" % (car_state2.speed, car_state2.gear))

    # go forward
    car_controls1.throttle = 0.5
    car_controls1.steering = 0.5
    client.setCarControls(car_controls1, "Car1")
    print("Car1: Go Forward")

    car_controls2.throttle = 0.5
    car_controls2.steering = -0.5
    client.setCarControls(car_controls2, "Car2")
    print("Car2: Go Forward")
    time.sleep(3)   # let car drive a bit


    # go reverse
    car_controls1.throttle = -0.5
    car_controls1.is_manual_gear = True
    car_controls1.manual_gear = -1
    car_controls1.steering = -0.5
    client.setCarControls(car_controls1, "Car1")
    print("Car1: Go reverse, steer right")
    car_controls1.is_manual_gear = False; # change back gear to auto
    car_controls1.manual_gear = 0  

    car_controls2.throttle = -0.5
    car_controls2.is_manual_gear = True
    car_controls2.manual_gear = -1
    car_controls2.steering = 0.5
    client.setCarControls(car_controls2, "Car2")
    print("Car2: Go reverse, steer right")
    car_controls2.is_manual_gear = False; # change back gear to auto
    car_controls2.manual_gear = 0  
    time.sleep(3)   # let car drive a bit


    # apply breaks
    car_controls1.brake = 1
    client.setCarControls(car_controls1, "Car1")
    print("Car1: Apply break")
    car_controls1.brake = 0 #remove break

    car_controls2.brake = 1
    client.setCarControls(car_controls2, "Car2")
    print("Car2: Apply break")
    car_controls2.brake = 0 #remove break
    time.sleep(3)   # let car drive a bit
    
    # get camera images from the car
    responses1 = client.simGetImages([
        airsim.ImageRequest("0", airsim.ImageType.DepthVis),  #depth visualization image
        airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car1")  #scene vision image in uncompressed RGBA array
    print('Car1: Retrieved images: %d' % (len(responses1)))
    responses2 = client.simGetImages([
        airsim.ImageRequest("0", airsim.ImageType.Segmentation),  #depth visualization image
        airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)], "Car2")  #scene vision image in uncompressed RGBA array
    print('Car2: Retrieved images: %d' % (len(responses2)))

    for response in responses1 + responses2:
        filename = 'c:/temp/car_multi_py' + str(idx)

        if response.pixels_as_float:
            print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
            airsim.write_pfm(os.path.normpath(filename + '.pfm'), airsim.get_pfm_array(response))
        elif response.compress: #png format
            print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
            airsim.write_file(os.path.normpath(filename + '.png'), response.image_data_uint8)
        else: #uncompressed array
            print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))
            img1d = np.fromstring(response.image_data_uint8, dtype=np.uint8) #get numpy array
            img_rgba = img1d.reshape(response.height, response.width, 4) #reshape array to 4 channel image array H X W X 4
            img_rgba = np.flipud(img_rgba) #original image is flipped vertically
            img_rgba[:,:,1:2] = 100 #just for fun add little bit of green in all pixels
            airsim.write_png(os.path.normpath(filename + '.greener.png'), img_rgba) #write to png 


#restore to original state
client.reset()

client.enableApiControl(False)

こっちはすぐにブレーキを掛けているものだから、シミュレータ画面上ではピクリとも動かないのでとてもつまらないが..



あとはAPIをVehicleフレームワーク上で動作するようにPartクラスをつくるだけで、Donkey Carの学習モデルをこのシミュレータで試すこともできるようになる。


なお、気象APIで雨を降らせることのできる機能はpypi上のパッケージバージョンでは未対応なので、リポジトリ最新をダウンロードして手動インストール(pip install -e .)を実行しないとだめらしい(執筆時点:2019年1月21日)。

0 件のコメント:

既存アプリケーションをK8s上でコンテナ化して動かす場合の設計注意事項メモ

既存アプリをK8sなどのコンテナにして動かすには、どこを注意すればいいか..ちょっと調べたときの注意事項をメモにした。   1. The Twelve Factors (日本語訳からの転記) コードベース   バージョン管理されている1つのコードベースと複数のデプロイ 依存関係 ...