diff --git a/content/posts/2024-12-17/feature.jpg b/content/posts/2024-12-17/feature.jpg new file mode 100644 index 0000000..c5dc7c5 Binary files /dev/null and b/content/posts/2024-12-17/feature.jpg differ diff --git a/content/posts/2024-12-17/image.png b/content/posts/2024-12-17/image.png new file mode 100644 index 0000000..3fc7a5b Binary files /dev/null and b/content/posts/2024-12-17/image.png differ diff --git a/content/posts/2024-12-17/index.md b/content/posts/2024-12-17/index.md new file mode 100644 index 0000000..2e34b6d --- /dev/null +++ b/content/posts/2024-12-17/index.md @@ -0,0 +1,123 @@ +--- +author: usbharu +draft: true +categories: + - 技術 +date: 2024-12-17T22:30:31+09:00 +tags: + - Rust + - "DCS World" + - embassy + - Raspberry Pi + - Raspberry Pi Pico + - Embedded Rust + - 組み込み + - Tauri + - React +keywords: + - Rust + - "DCS World" + - embassy + - Raspberry Pi + - Raspberry Pi Pico + - Embedded Rust + - 組み込み + - Tauri + - React +title: "DCS Worldをラズピコから操作したい" +relpermalink: posts/2024-12-17/ +url: posts/2024-12-17/ +decription: "DCS Worldをラズピコから操作したい" +--- + +## 何をしたか + +### DCS Worldとは + +DCS World、通称DCSっていう超リアルな戦闘機のシミュレーターがあります。 + +[Digital Combat Simulator | DCS World | Combat Simulator](https://www.digitalcombatsimulator.com/en/) + +### なぜ外部から操作したいか + +このシミュレーター、本物の戦闘機を可能な限り忠実に再現しているのでただ離陸するだけでもクソみたいな数の操作をしないといけません。まぁそう言うのが好きでやってるんですが、ともかく操作が多いのでマウスでポチポチやるのはしんどいわけです。ましてや戦闘中にいちいちマウスを触る余裕はありません。特にVRだと視点が動くとカーソルも動くのでめっちゃ操作しづらいです。というわけで手元に物理的なボタンが欲しくなったので試行錯誤の記録です。なお未完成で、後述するソフトから送られてくるデータの読み取りには成功していますが書き込みはまだできていません。 + +### なぜラズピコなのか + +たまたま手元にあったから + +### 使用技術 + +- DCS-BIOS +- Rust +- Embassy-rp +- Raspberry Pi Pico W +- Tauri + +DCS-BIOSはDCSにデータを送ったりDCSからデータを取得したりすることができるツール群です。Arduinoライブラリとかもあります。 + +Embassyは組み込みRustの非同期ランタイムとかです。それのrp2040用がEmbassy-rp + +今回はWiFiに接続してデータを受け取るのでPico Wでやりますが、最終的に通信できたら何でも良いです。 + +残念ながらラズピコからデータを読んだり書き込んだりする部分は完成してないので、TauriでGUI作ってライブラリが正しく動くことを確認しました。ボタンが多すぎてIOエキスパンダやらなんやらが必要なんですけど基板に収まらなくて発注しないといけない。そんな金は無いので停滞中。 + +## DCS-BIOSについて + +[DCS-Skunkworks/dcs-bios: Data export tool for DCS.](https://github.com/DCS-Skunkworks/dcs-bios) + +もとはGolangで書かれてたみたいですがフォークされてLuaに書き直されたみたいです。LuaはDCSのプラグインの言語なんでわかるんですけどなんでGolangで書かれてたんだろう…。 + +### DCS-BIOSのプロトコル + +日本語での解説は皆無だと思うのでここに書いておきます。DCS-BIOSはメモリの変化の情報をそのまま抜き出してきたような感じのプロトコルで、アドレスとかはありますがアドレスに1対1で対応したデータが送られてくるわけではありません。例えば0x00というアドレスで8バイト分のデータが送られてきたとします。このときデータを見ただけでは0x00からのデータが文字列なのか数値なのかの判断をすることはおろか、データ間の区切りすらわかりません。データを受け取る側は欲しいデータの型と長さとアドレスを知っておく必要があります。あと場合によってはビットマスクの値とビットシフトの値も必要です。 + +で、それがこんな感じ。なんとなく書いたけどいらんなこれ… + +![プロトコルの構造](image.png) + +Data Address以降は何回も繰り返される可能性があります。全ての数値はリトルエンディアンなので読むときは注意する必要があります。 + +データを受け取る側、クライアントは受け取ったデータの内必要な部分を保持しておく必要があります。もちろん全てのデータを保持する必要はないので、65kbもメモリを持っていかれるわけじゃないんですが、いまいちどこが必要なのか探すのが面倒なので私は全部保持してます。PCだとなんとも無いけど組み込みだとちょっと辛い。 + +実装時の注意点として、DCS-BIOSから送られてくるアドレスはあくまでもメモリに変化があった箇所の始点のアドレスなのでそのまま使ってはいけないです。もちろんそのまま使うこともできますが、データが歯抜けになります。(1敗) + +機体のスピードなんかは同時に変化し続けるのでまとめてデータが送られてきます。例えば0x0050から10バイトぐらいが機体のスピードに関するデータが書き込まれている場所だとします。DCS-BIOSは0x0050から10バイトの長さで変更があったよ~と教えてくれるわけです。このとき変化するアドレスは0x0050、0x0051、0x0052...となるわけですが、0x0052のデータが欲しいからと言って0x0052とDCS-BIOSのプロトコルのData Address(今回は0x0050)を直接比較してしまうと当然falseになります。するとこれも当然ながら0x0050以外の箇所はデータがない事になってしまいます。DCS-BIOSはデータを纏めて送ってくるという知識があればすぐに気づく当然のことなんですが、全然気づかなかったのでここに書いておきます。 + +## Embassyについて + +### I2Cについて + +まだ全然分からん感じなので大したことかけないんですが、Embassyを使いながらでもEmbedded HALのライブラリは使えることに気づくのに時間がかかったので、これもここに書いておきます。 + +[Embedded HALのI2Cを使っているところ](https://github.com/usbharu/DCS-F18/blob/10a4b46d4c6c1570f1407369335486df5121739f/Software/firmware/dcs-f18-ddi/src/ddi.rs) + +Embassy-rpのHALもEmbedded HALといい感じに互換性があるみたいです。詳しいことはよくわからないのでそのうちちゃんと読みます。 + + +### 外部入力割り込みについて + +これもさっぱり分からんのですが、どうやらTaskを起動して中でinputの`wait_for_high().await`とかを呼ぶことで実現している…?みたいです。 + +[embassy/examples/rp/src/bin/gpio_async.rs at 8b36a32ed5d834b23e970d5b723dd7df1f1c94a2 · embassy-rs/embassy](https://github.com/embassy-rs/embassy/blob/8b36a32ed5d834b23e970d5b723dd7df1f1c94a2/examples/rp/src/bin/gpio_async.rs) + +よくわからんのでExample + +## Tauriについて + +すごい簡単でいいですねこれ Electronが流行るのもわかる + +Tauriを選んだ理由とかは特に無いんですけど、ラズピコで動くコードがそのままデスクトップでも動くのでバグが入り込む場所が少なくなっていいかなと思って採用しました。 + +なんか書きたいんですけどGitHubがクソ重たくて全然コード見れないので今日はこの辺にしておきます。 + + +## その他 + +ハード側は3Dプリンターの記事でも書いた通りある程度できてきていますが、大量のボタンをまとめてラズピコに送る部分ができてません。 + +![ブレッドボードと3Dプリンター製の筐体](feature.jpg) + +## 次に向けて + +とりあえず基板設計し終えたい。ソフト側もこちらからの書き込みに対応したい。 \ No newline at end of file diff --git a/resources/_gen/images/posts/2023-07-03/feature_hu9675130687420106434.jpg b/resources/_gen/images/posts/2023-07-03/feature_hu9675130687420106434.jpg deleted file mode 100644 index 20a272f..0000000 Binary files a/resources/_gen/images/posts/2023-07-03/feature_hu9675130687420106434.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2023-07-23/feature_hu9156422581551037420.png b/resources/_gen/images/posts/2023-07-23/feature_hu9156422581551037420.png deleted file mode 100644 index 496a494..0000000 Binary files a/resources/_gen/images/posts/2023-07-23/feature_hu9156422581551037420.png and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-10/feature_hu8784029735481068175.jpg b/resources/_gen/images/posts/2024-11-10/feature_hu8784029735481068175.jpg deleted file mode 100644 index ed8ed0c..0000000 Binary files a/resources/_gen/images/posts/2024-11-10/feature_hu8784029735481068175.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-24/feature_hu14669296011999888938.jpg b/resources/_gen/images/posts/2024-11-24/feature_hu14669296011999888938.jpg deleted file mode 100644 index 6c7c61f..0000000 Binary files a/resources/_gen/images/posts/2024-11-24/feature_hu14669296011999888938.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-24/feature_hu14764975846322101606.jpg b/resources/_gen/images/posts/2024-11-24/feature_hu14764975846322101606.jpg deleted file mode 100644 index 32ff5b7..0000000 Binary files a/resources/_gen/images/posts/2024-11-24/feature_hu14764975846322101606.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-24/feature_hu18414511508393131171.jpg b/resources/_gen/images/posts/2024-11-24/feature_hu18414511508393131171.jpg deleted file mode 100644 index 6bff05f..0000000 Binary files a/resources/_gen/images/posts/2024-11-24/feature_hu18414511508393131171.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-24/feature_hu4371344990948604037.jpg b/resources/_gen/images/posts/2024-11-24/feature_hu4371344990948604037.jpg deleted file mode 100644 index e765e8d..0000000 Binary files a/resources/_gen/images/posts/2024-11-24/feature_hu4371344990948604037.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-24/feature_hu4934018083429553072.jpg b/resources/_gen/images/posts/2024-11-24/feature_hu4934018083429553072.jpg deleted file mode 100644 index 258b91d..0000000 Binary files a/resources/_gen/images/posts/2024-11-24/feature_hu4934018083429553072.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-24/feature_hu819268872585750122.jpg b/resources/_gen/images/posts/2024-11-24/feature_hu819268872585750122.jpg deleted file mode 100644 index 03e28f8..0000000 Binary files a/resources/_gen/images/posts/2024-11-24/feature_hu819268872585750122.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-11-24/feature_hu8608746588563938387.jpg b/resources/_gen/images/posts/2024-11-24/feature_hu8608746588563938387.jpg deleted file mode 100644 index e37c32c..0000000 Binary files a/resources/_gen/images/posts/2024-11-24/feature_hu8608746588563938387.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu11041427595094940652.jpg b/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu11041427595094940652.jpg deleted file mode 100644 index b23a6e9..0000000 Binary files a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu11041427595094940652.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu13260678441379930923.jpg b/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu13260678441379930923.jpg deleted file mode 100644 index 6823a9b..0000000 Binary files a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu13260678441379930923.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu14503313738354224210.jpg b/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu14503313738354224210.jpg deleted file mode 100644 index 6b0bcf7..0000000 Binary files a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu14503313738354224210.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu5438437609423427027.jpg b/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu5438437609423427027.jpg deleted file mode 100644 index 86c4a5e..0000000 Binary files a/resources/_gen/images/posts/2024-12-12/PXL_20241216_145258501_hu5438437609423427027.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-12-12/feature_hu4966155463133835216.jpg b/resources/_gen/images/posts/2024-12-12/feature_hu4966155463133835216.jpg deleted file mode 100644 index 94a972b..0000000 Binary files a/resources/_gen/images/posts/2024-12-12/feature_hu4966155463133835216.jpg and /dev/null differ diff --git a/resources/_gen/images/posts/2024-12-17/feature_hu10326612880981716930.jpg b/resources/_gen/images/posts/2024-12-17/feature_hu10326612880981716930.jpg new file mode 100644 index 0000000..98ba33e Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/feature_hu10326612880981716930.jpg differ diff --git a/resources/_gen/images/posts/2024-12-17/feature_hu15439716907959684397.jpg b/resources/_gen/images/posts/2024-12-17/feature_hu15439716907959684397.jpg new file mode 100644 index 0000000..242c65d Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/feature_hu15439716907959684397.jpg differ diff --git a/resources/_gen/images/posts/2024-12-17/feature_hu15555614034521797521.jpg b/resources/_gen/images/posts/2024-12-17/feature_hu15555614034521797521.jpg new file mode 100644 index 0000000..6545fe3 Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/feature_hu15555614034521797521.jpg differ diff --git a/resources/_gen/images/posts/2024-12-17/feature_hu18394740213133642202.jpg b/resources/_gen/images/posts/2024-12-17/feature_hu18394740213133642202.jpg new file mode 100644 index 0000000..7953a15 Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/feature_hu18394740213133642202.jpg differ diff --git a/resources/_gen/images/posts/2024-12-17/feature_hu4941843681775552409.jpg b/resources/_gen/images/posts/2024-12-17/feature_hu4941843681775552409.jpg new file mode 100644 index 0000000..ac14306 Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/feature_hu4941843681775552409.jpg differ diff --git a/resources/_gen/images/posts/2024-12-17/feature_hu5097603391489478236.jpg b/resources/_gen/images/posts/2024-12-17/feature_hu5097603391489478236.jpg new file mode 100644 index 0000000..bbad343 Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/feature_hu5097603391489478236.jpg differ diff --git a/resources/_gen/images/posts/2024-12-17/image_hu10775545817333987943.png b/resources/_gen/images/posts/2024-12-17/image_hu10775545817333987943.png new file mode 100644 index 0000000..3a2b281 Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/image_hu10775545817333987943.png differ diff --git a/resources/_gen/images/posts/2024-12-17/image_hu15918569041969274411.png b/resources/_gen/images/posts/2024-12-17/image_hu15918569041969274411.png new file mode 100644 index 0000000..b4b2cbd Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/image_hu15918569041969274411.png differ diff --git a/resources/_gen/images/posts/2024-12-17/image_hu3361220782271158417.png b/resources/_gen/images/posts/2024-12-17/image_hu3361220782271158417.png new file mode 100644 index 0000000..28e9971 Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/image_hu3361220782271158417.png differ diff --git a/resources/_gen/images/posts/2024-12-17/image_hu3405015886063706131.png b/resources/_gen/images/posts/2024-12-17/image_hu3405015886063706131.png new file mode 100644 index 0000000..86d8b6f Binary files /dev/null and b/resources/_gen/images/posts/2024-12-17/image_hu3405015886063706131.png differ