MyoSuite Tutorial No.1

# はじめに
本記事は以下の人を対象としています．

- 筋骨格シミュレータに興味がある人
- MuJoCoに興味がある人

:::note warn
本記事では[1_Get_Started.ipynb](https://github.com/MyoHub/myosuite/blob/main/docs/source/tutorials/1_Get_Started.ipynb)をGoogle Colabで実行した際の手順と結果に関してまとめる．
:::

# MyoSuiteとは

https://sites.google.com/view/myosuite

MuJoCoを学習モジュールとして組み込んだ筋骨格シミュレータ

:::note info
2024年にICRAにてworkshopを展開
https://sites.google.com/view/myosuite/myosymposium/icra24
:::

# Tutorial Notebook
https://github.com/MyoHub/myosuite/tree/main/docs/source/tutorials

# 実施事項
Tutorialに記載のコードを1つずつ試して, Errorになったところは都度修正版を記載する.

## ライブラリのinstallと環境変数設定
```bash
!pip install -U myosuite
%env MUJOCO_GL=egl 
```
:::note warn
環境変数```MUJOCO_GL=egl```を設定しないと```mujoco.FatalError: gladLoadGL error```が出る.
https://github.com/Farama-Foundation/Gymnasium/issues/501#issuecomment-1730755393
:::

## ライブラリのimportと結果表示の関数
```python
from myosuite.utils import gym # importが上手くいくと"MyoSuite:> Registering Myo Envs"と表示される
import skvideo.io
import numpy as np
import os

from IPython.display import HTML
from base64 import b64encode

def show_video(video_path, video_width = 400):

  video_file = open(video_path, "r+b").read()

  video_url = f"data:video/mp4;base64,{b64encode(video_file).decode()}"
  return HTML(f"""<video autoplay width={video_width} controls><source src="{video_url}"></video>""")

```

:::note alert
- skvideo.ioの動作
- HTML, b64encodeの働きの確認
:::

## modelのimportから結果確認
```python
# modelのimport
env = gym.make('myoElbowPose1D6MRandom-v0')
print('List of cameras available', [env.sim.model.camera(i).name for i in range(env.sim.model.ncam)])
env.reset()
frames = []
for _ in range(100):
    frame = env.sim.renderer.render_offscreen(
                        width=400,
                        height=400,
                        camera_id=0)
    frames.append(frame)
    env.step(env.action_space.sample()) # take a random action
env.close()

os.makedirs('videos', exist_ok=True)
# make a local copy
skvideo.io.vwrite('videos/temp.mp4', np.asarray(frames),outputdict={"-pix_fmt": "yuv420p"})

# show in the notebook
show_video('videos/temp.mp4')
```
:::note alert
- ```env.reset()```の動きの確認
- ```sim.renderer```の動きの確認
:::
出力結果(GIFにしているので連続だが実際は4秒ほど)
![1_Get_Started_default.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2043769/1624a1bf-88f1-5145-db78-e390a236e27d.gif)

https://youtube.com/shorts/rrftnczOMLs?feature=share

:::note warn
<details><summary>いくつか警告が出てくる</summary>

```bash
/usr/local/lib/python3.10/dist-packages/gymnasium/core.py:311: UserWarning: WARN: env.sim to get variables from other wrappers is deprecated and will be removed in v1.0, to get this variable you can do `env.unwrapped.sim` for environment variables or `env.get_wrapper_attr('sim')` that will search the reminding wrappers.
  logger.warn(
/usr/local/lib/python3.10/dist-packages/gymnasium/utils/passive_env_checker.py:135: UserWarning: WARN: The obs returned by the `reset()` method was expecting numpy array dtype to be float32, actual type: float64
  logger.warn(
/usr/local/lib/python3.10/dist-packages/gymnasium/utils/passive_env_checker.py:159: UserWarning: WARN: The obs returned by the `reset()` method is not within the observation space.
  logger.warn(f"{pre} is not within the observation space.")
List of cameras available ['side_view', 'front_view']
/usr/local/lib/python3.10/dist-packages/gymnasium/utils/passive_env_checker.py:135: UserWarning: WARN: The obs returned by the `step()` method was expecting numpy array dtype to be float32, actual type: float64
  logger.warn(
/usr/local/lib/python3.10/dist-packages/gymnasium/utils/passive_env_checker.py:159: UserWarning: WARN: The obs returned by the `step()` method is not within the observation space.
  logger.warn(f"{pre} is not within the observation space.")
/usr/local/lib/python3.10/dist-packages/skvideo/io/ffmpeg.py:466: DeprecationWarning: tostring() is deprecated. Use tobytes() instead.
  self._proc.stdin.write(vid.tostring())
```

</details>
見たところwrapperやnumpyの中の関数について書かれている，ライブラリ側の問題？
:::

## 最終結果の表示
一番最後の姿勢の画像表示

```python
!pip install matplotlib
import matplotlib.pyplot as plt
plt.imshow(frame)
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2043769/615d4b2a-e0f9-104b-c58b-3a6140d9ac63.png)


# 参考資料

https://qiita.com/hokusei/items/7c6b1af048acc7f0d462