バックナンバーはこちら
https://www.simulationroom999.com/blog/model-based-of-minimum-backnumber/
はじめに
オープンソースドライビングシミュレータであるCARLAの話。
今回はPythonAPIについて。
登場人物
博識フクロウのフクさん
![](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
イラストACにて公開の「kino_k」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=iKciwKA9&area=1
エンジニア歴8年の太郎くん
![](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/技術者01アップ.png)
イラストACにて公開の「しのみ」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=uCKphAW2&area=1
PythonAPI
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
さて、前回説明したCARLAだけど、
PythonAPIというものを持っている。
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」13アップ.png)
PythonAPI?
言葉だけで判断するとPython用のプログラムインターフェースを持っているってことかな?
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/まるフクロウ.png)
その通り。
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」10アップ.png)
だと、するとダメじゃん!
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/はてなフクロウ.png)
なんで?
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」09アップ.png)
以前作ったPIDはC言語で作られてるだよ?
PythonAPIあっても呼び出せないじゃん!
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/技術者02アップ.png)
あ、もしかしてSimulinkモデルからPythonコードを自動生成できるとか?
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/考え中フクロウ.png)
今のところPythonコードの自動生成は聞いた事ないな。
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」11アップ.png)
じゃ、ダメじゃん!
全体構成
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
ただ、PythonからC言語から作成したライブラリを呼び出すことはできる。
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」13アップ.png)
ん?それだと何が解決するの?
イマイチ頭の中で整理が出来てない。
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
絵で描くとこんな感じだよ。
![CARLA、Python、PID制御、Cコード、PID制御DLL、キー入力(アクセス、ブレーキ)、車両情報(車速)、車両制御(アクセル、ブレーキ)、Python API](https://www.simulationroom999.com/blog/wp-content/uploads/2020/07/02_全体構成-1024x585.png)
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
イメージ的には以前やったSILSの構成に近いね。
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/技術者03アップ.png)
なるほど。
アルゴリズムの一部であるPID制御をDLLに実現して、
全体をPythonで制御する感じか。
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/お休みフクロウ.png)
というわけでPythonからのDLL呼び出しが分かれば、
一応一連の流れは作れそうってことになる。
PythonからのDLL呼び出し
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」13アップ.png)
で、PythonからのDLL呼び出しってどうするの?
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
まぁこれはググった方が早い気がするが、
流れとしてはこう。
C言語側(PID.dll)
extern "C" __declspec(dllexport) double PID_Controller(double target, double ap )
{
/* PID制御の処理 */
}
Python側の呼び出し
from ctypes import *
dll = cdll.LoadLibrary("PID.dll")
dll.PID_Controller(50, ap )
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/技術者02アップ.png)
これだけ見ると簡単そう。
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/お休みフクロウ.png)
まぁ実際はどのタイミングでどんな周期って問題は出てくると思うけど、
それはその時に悩めばいいかな。
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」10アップ.png)
ちょっと、不安になるようなこと言わないでよ。
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
まぁ失敗覚悟でやってみるってのも重要だよ。
単に答えを知るよりも理解を進むと思うし。
あと、もしかしたらあの伏線が回収できるかもしれない?
![太郎くん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/「技術者a」20アップ.png)
(伏線?なんかあったような気がするが、なんだっけか?)
まとめ
![フクさん](https://www.simulationroom999.com/blog/wp-content/uploads/2020/05/指差しフクロウ.png)
まとめだよ。
- CARLAはPythonAPIを使ってPythonから制御できる。
- PythonはDLLを呼び出すことができる。
- よって、C言語書かれたPID制御をPythonから利用する場合はDLLにした方が良い。
バックナンバーはこちら
コメント