0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

経路生成 (RRT) を実装してみた Part4

0
Last updated at Posted at 2025-06-03

はじめに

私がロボットに関して興味を持っており,ロボットの経路を作成したいと思ったところ,RRT (Rapidly-exploring Random Tree) と呼ばれる経路生成の手法に辿り着いた.
本記事では,経路生成の古典的手法である RRT (Rapidly-exploring Random Tree) をPythonより,実装してみる.
前記事では,干渉物が存在しない3次元空間を探索するRRTを実装した.
前記事の内容を下記に記します.
https://qiita.com/haruhiro1020/items/4482e0ba2be5f1b835a4
本記事では,球の干渉物が存在する3次元空間を探索するRRTを実装する.

本記事で実装すること

・球の干渉物が存在する3次元空間でのRRTによる経路生成
・経路生成のアニメーションを作成

本記事では実装できないこと(将来実装したい内容)

・球以外干渉物が存在する3次元空間でのRRTによる経路生成

動作環境

・macOS Sequoia (バージョン15.5)
・Python3 (3.10.9)
・Numpy (1.23.5) (数値計算用ライブラリ)
・Matplotlib (3.7.0) (アニメーション作成用ライブラリ)

経路生成手法のまとめ

経路生成手法をまとめた記事を展開いたします.RRT以外の手法も記載してますので,参照いただけますと幸いです.
(https://qiita.com/haruhiro1020/items/000b3287fce082ae4283)

RRTに関して

経路生成手法である,前記事でRRTに関しての説明をしたため,本記事では割愛する.

球の干渉物との干渉判定について

球の干渉物との干渉判定の方法を説明する.
干渉の有無は下図の3パターンで判別できる.
(1):干渉していない点
(2):干渉している点(干渉物と接している)
(3):干渉している点
(1)と(2),(3)の判別方法は以下数式で求まる.

\displaylines{
d_i(2点間の距離) = \sqrt{(x_b - x_i)^2 + (y_b - y_i)^2 + (z_b - z_i)^2}\\
(i = 1, 2, 3, ...) \\
f(d_i) = \left\{
\begin{array}{ll}
1(干渉あり) & (d_i \leq r_b + r_i) \\
0(干渉なし) & (d_i \gt r_b + r_i)
\end{array}
\right.
}

rrt_1.drawio.png

2点間の距離が2点の半径の合計以下であれば,干渉したと判定する.
干渉を回避した経路を生成したいため,干渉した点は不採用とする.

球の干渉物との干渉判定

Pythonによる干渉判定の実装は下記の通りです.

interference.py
# 干渉物を定義する

# ライブラリの読み込み
import numpy as np


# 新規追加 ↓
class Interference2D:
    """
    2次元の干渉物クラス(抽象クラス)
    
    プロパティ
        _center(numpy.ndarray): 中心位置(x, y)
        _radius(float): 半径
    
    publicメソッド (全てのクラスから参照可能)
        is_interference_circle(): 円形との干渉判定
        dimention(): _DIMENTION定数のゲッター
    """
    # 定数の定義
    _DIMENTION = 2      # 次元数
    
    def is_interference_circle(self, center, radius):
        """
        円形との干渉判定
        
        パラメータ
            center(numpy.ndarray): 中心位置(x, y)
            radius(float): 半径
        
        戻り値
            is_interference(bool): True / False = 干渉あり / なし
        """
        pass

    @property
    def dimention(self):
        """
        _DIMENTION定数のゲッター
        """
        return self._DIMENTION
# 新規追加 ↑


class Circle(Interference2D):
    """
    円形の干渉物クラス
    
    プロパティ
        _center(numpy.ndarray): 中心位置(x, y)
        _radius(float): 半径
    
    publicメソッド (全てのクラスから参照可能)
        is_interference_circle(): 円形との干渉判定
        center(): _centerプロパティのゲッター
        radius(): _radiusプロパティのゲッター
    """
    # 定数の定義
    
    def __init__(self, center, radius):
        """
        コンストラクタ
        
        パラメータ
            center(numpy.ndarray): 中心位置(x, y)
            radisu(float): 半径
        """
        # プロパティの初期化
        self._center = center
        self._radius = radius
    
    @property
    def center(self):
        """
        _centerプロパティのゲッター
        """
        return self._center
    
    @property
    def radius(self):
        """
        _radiusプロパティのゲッター
        """
        return self._radius
    
    def is_interference_circle(self, center, radius):
        """
        円形との干渉判定
        
        パラメータ
            center(numpy.ndarray): 中心位置(x, y)
            radius(float): 半径
        
        戻り値
            is_interference(bool): True / False = 干渉あり / なし
        """
        is_interference = False
        # 2点間の差分を計算
        difference = self._center - center
        # 2点間の距離(ユークリッド距離)を算出
        distance = np.linalg.norm(difference)
        if distance <= self._radius + radius:
            # 干渉した
            is_interference = True
        
        return is_interference


# 新規追加 ↓
class Interference3D:
    """
    3次元の干渉物クラス(抽象クラス)
    
    プロパティ
        _center(numpy.ndarray): 中心位置(x, y)
        _radius(float): 半径
    
    publicメソッド (全てのクラスから参照可能)
        is_interference_ball(): 球との干渉判定
        dimention(): _DIMENTION定数のゲッター
    """
    # 定数の定義
    _DIMENTION = 3      # 次元数
    
    def is_interference_ball(self, center, radius):
        """
        球との干渉判定
        
        パラメータ
            center(numpy.ndarray): 中心位置(x, y, z)
            radius(float): 半径
        
        戻り値
            is_interference(bool): True / False = 干渉あり / なし
        """
        pass

    @property
    def dimention(self):
        """
        _DIMENTION定数のゲッター
        """
        return self._DIMENTION

class Ball(Interference3D):
    """
    球の干渉物クラス
    
    プロパティ
        _center(numpy.ndarray): 中心位置(x, y, z)
        _radius(float): 半径
    
    publicメソッド (全てのクラスから参照可能)
        is_interference_ball(): 球との干渉判定
    """
    # 定数の定義
    
    def __init__(self, center, radius):
        """
        コンストラクタ
        
        パラメータ
            center(numpy.ndarray): 中心位置(x, y, z)
            radisu(float): 半径
        """
        # プロパティの初期化
        self._center = center
        self._radius = radius
    
    @property
    def center(self):
        """
        _centerプロパティのゲッター
        """
        return self._center
    
    @property
    def radius(self):
        """
        _radiusプロパティのゲッター
        """
        return self._radius
    
    def is_interference_ball(self, center, radius):
        """
        球との干渉判定
        
        パラメータ
            center(numpy.ndarray): 中心位置(x, y, z)
            radius(float): 半径
        
        戻り値
            is_interference(bool): True / False = 干渉あり / なし
        """
        is_interference = False
        # 2点間の差分を計算
        difference = self._center - center
        # 2点間の距離(ユークリッド距離)を算出
        distance = np.linalg.norm(difference)
        if distance <= self._radius + radius:
            # 干渉した
            is_interference = True
        
        return is_interference
# 新規追加 ↑

始点から終点までの経路生成(球の干渉物が存在する3次元空間)

PythonによるRRTの実装は下記の通りです.
前記事と大方同じである.新規追加箇所は 「# 新規追加」と記載した.

rrt.py
# 経路生成手法であるRRT (Rapidly-exploring Random Tree) の実装

# ライブラリの読み込み
import numpy as np
import time


class RRT:
    """
    RRTクラス
    
    プロパティ
        _pathes(numpy.ndarray): 始点から終点までの経路
        _debug_pathes(numpy.ndarray): デバッグ用経路
        _debug_random_pathes(numpy.ndarray): デバッグ用ランダム位置
        _strict_min_pos(numpy.ndarray): 探索の最小範囲
        _strict_max_pos(numpy.ndarray): 探索の最大範囲
        _interference_obj(list): 干渉物
    
    publicメソッド (全てのクラスから参照可能)
        planning(): 経路生成の実装
        pathes(): _pathesプロパティのゲッター
        debug_pathes(): _debug_pathesプロパティのゲッター
        debug_random_pathes(): _debug_random_pathesプロパティのゲッター
        strict_min_pos(): _strict_min_posプロパティのゲッター
        strict_max_pos(): _strict_max_posプロパティのゲッター
        interference_obj(): _interference_objプロパティのゲッター
    
    protectedメソッド (自クラスまたは子クラスが参照可能)
        _reset(): データの初期化
        _set_node(): データにノードを設定する
        _chk_end_pos_dist(): 終点との距離が一定範囲内であるかの確認
        _fin_planning(): 経路生成の終了処理
        _calc_new_pos(): 最短ノードからランダムな値方向へ新しいノード(位置)を作成
        _get_near_node(): ランダムな位置と最短距離であるノードを取得
        _strict_planning_pos(): 探索範囲を制限する
        _get_random_pos(): ランダムな位置を取得
        _set_interference_obj(): 干渉物を設定
        _is_interference_pos(): 干渉判定処理
    """
    # 定数の定義
    _INITIAL_NODE_NEAR = -1     # 初期値の最短距離ノード番号
    _MOVING_VALUE = 0.25        # 1回の移動量
    _STRICT_PLANNING_POS = 0.2  # 探索範囲の制限
    _TIMEOUT_VALUE = 10         # タイムアウト時間 [second]
    _FILE_NAME_PATHES = "pathes.csv"                # _pathesプロパティを保存するファイル名
    _FILE_NAME_DEBUG_PATHES = "debug_pathes.csv"    # _debug_pathesプロパティを保存するファイル名
    _FILE_NAME_DEBUG_RANDOM_PATHES = "debug_random_pathes.csv"  # _debug_random_pathesプロパティを保存するファイル名
    _NODE_RADIUS = 0.05         # ノードの半径 (新規追加)
    _DIMENTION_2D = 2           # 2次元データの経路生成
    _DIMENTION_3D = 3           # 3次元データの経路生成
    
    def __init__(self):
        """
        コンストラクタ
        """
        self._debug_pathes = []
        self._debug_random_pathes = []
        self._pathes = []
        self._interference_obj = []
    
    @property
    def pathes(self):
        """
        _pathesプロパティのゲッター
        """
        return self._pathes
    
    @property
    def debug_pathes(self):
        """
        _debug_pathesプロパティのゲッター
        """
        return self._debug_pathes
    
    @property
    def debug_random_pathes(self):
        """
        _debug_random_pathesプロパティのゲッター
        """
        return self._debug_random_pathes
    
    @property
    def strict_min_pos(self):
        """
        _strict_min_posプロパティのゲッター
        """
        return self._strict_min_pos

    @property
    def strict_max_pos(self):
        """
        _strict_max_posプロパティのゲッター
        """
        return self._strict_max_pos
    
    @property
    def interference_obj(self):
        """
        _interference_objプロパティのゲッター
        """
        return self._interference_obj

    def _set_interference_obj(self, interference_obj, dim):
        """
        干渉物を設定

        パラメータ
            interference_obj(list): 干渉物
            dim(int): 始点/終点の次元数
        
        戻り値
            set_result(bool): True / False = 設定の成功 / 失敗
        """
        set_result = True
        if interference_obj:
            # 干渉物が存在する
            # 干渉物と始点/終点の次元数が一致しているかの確認 (# 変更 ↓)
            for interference in interference_obj:
                print(f"dim = {dim}")
                print(f"interference.dimention = {interference.dimention}")
                if dim != interference.dimention:
                    # 不一致
                    set_result = False
                    break

            if set_result:
                self._interference_obj = interference_obj
            # 変更 ↑
        else:
            self._interference_obj = []

        return set_result

    def _set_node(self, node):
        """
        データにノードを設定する

        パラメータ
            node(numpy.ndarray): ノード位置と最短距離ノード番号 (最終要素)
        """
        if len(self._debug_pathes) == 0:
            self._debug_pathes = node
        else:
            self._debug_pathes = np.append(self._debug_pathes, node, axis=0)
    
    def _set_random_node(self, node):
        """
        データにノードを設定する

        パラメータ
            node(numpy.ndarray): ノード位置と最短距離ノード番号 (最終要素)
        """
        if len(self._debug_random_pathes) == 0:
            self._debug_random_pathes = node
        else:
            self._debug_random_pathes = np.append(self._debug_random_pathes, node, axis=0)
    
    def planning(self, start_pos, end_pos, interference_obj):
        """
        経路生成

        パラメータ
            start_pos(numpy.ndarray): 経路生成の始点
            end_pos(numpy.ndarray): 経路生成の終点
            interference_obj(list): 干渉物
        
        戻り値
            result(bool): True / False = 成功 / 失敗
        """
        # 処理結果
        result = False
        
        # 始点と終点の次元数が一致しているかの確認
        start_pos_dim = np.size(start_pos)
        end_pos_dim   = np.size(end_pos)
        if start_pos_dim != end_pos_dim:
            # 次元数が異なるので異常
            return result

        # 干渉物を設定 (# 変更)
        if not self._set_interference_obj(interference_obj, start_pos_dim):
            # 干渉物と始点/終点の次元数が不一致のため,処理失敗
            return result

        # 始点ノードを設定
        self._set_node(np.append(start_pos, self._INITIAL_NODE_NEAR).reshape(1, -1))
        self._set_random_node(np.append(start_pos, self._INITIAL_NODE_NEAR).reshape(1, -1))
        # 探索する範囲を設定
        self._strict_planning_pos(start_pos, end_pos)
        
        # 経路生成は一定時間内に終了させる
        start_time = time.time()

        # 終点までの経路生成が終わるまでループ
        while True:
            # ランダムな値を取得
            random_pos = self._get_random_pos()
            # ランダムな値と最短ノードを計算
            near_node = self._get_near_node(random_pos)
            # 最短ノードからランダムな値方向へ新しいノード(位置)を作成
            new_pos = self._calc_new_pos(random_pos, near_node)
            # 新しい点と干渉物との干渉判定 (# 新規追加)
            is_interference = self._is_interference_pos(new_pos, start_pos_dim)
            if not is_interference:
                # 干渉なしのため,データを設定する
                # ランダムノードを設定
                self._set_random_node(np.append(random_pos, near_node).reshape(1, -1))
                # ノードを設定
                self._set_node(np.append(new_pos, near_node).reshape(1, -1))
                # 終点との距離が一定範囲内であるかの確認
                if self._chk_end_pos_dist(new_pos, end_pos):
                    # 一定範囲内のため,経路生成の完了
                    result = True
                    break
            
            # 干渉の有無に関わらずにタイムアウトの確認はする
            now_time = time.time()
            if now_time - start_time >= self._TIMEOUT_VALUE:
                # タイムアウト
                break
        
        if result:
            # 経路生成に成功のため,経路生成の終了処理
            self._fin_planning(start_pos, end_pos)

        return result

    def _is_interference_pos(self, pos, dim):
        """
        干渉判定処理

        パラメータ
            pos(numpy.ndarray): 干渉判定したい点
            dim(int): 次元数
        
        戻り値
            is_interference(bool): True / False = 干渉あり / なし
        """
        is_interference = False
        
        for interference in self._interference_obj:
            # 新規追加/変更 ↓
            if dim == self._DIMENTION_2D:
                # RRTで生成した点は円形と仮定して,干渉判定
                if interference.is_interference_circle(pos, self._NODE_RADIUS):
                    # 干渉あり
                    is_interference = True
                    break
            else:
                # RRTで生成した点は球と仮定して,干渉判定
                if interference.is_interference_ball(pos, self._NODE_RADIUS):
                    # 干渉あり
                    is_interference = True
                    break
            # 新規追加/変更 ↑

        return is_interference

    def _chk_end_pos_dist(self, pos, end_pos):
        """
        終点との距離が一定範囲内であるかの確認

        パラメータ
            pos(numpy.ndarray): ノード位置
            end_pos(numpy.ndarray): 経路生成の終点
        
        戻り値
            is_near(bool): True / False = 一定範囲内である / でない
        """
        is_near = False
        # 距離を計算
        dist = np.linalg.norm(end_pos - pos)
        # 一定範囲内であるかの確認
        if dist <= self._MOVING_VALUE:
            is_near = True
        
        return is_near

    def _fin_planning(self, start_pos, end_pos):
        """
        経路生成の終了処理

        パラメータ
            start_pos(list): 経路生成の始点
            end_pos(list): 経路生成の終点
        """
        # 始点から終点までの経路に関係するノードを選択
        revers_path = end_pos
        near_node = -1
        while True:
            # 終点から始点方向へノードを取得
            node = self._debug_pathes[near_node, :]
            pos = node[:-1].reshape(1, -1)
            # 浮動小数型になっているので,整数型に型変換
            near_node = int(node[-1])
            revers_path = np.append(revers_path, pos, axis=0)
            if near_node == self._INITIAL_NODE_NEAR:
                # 始点ノードまで取得できたため,処理終了
                break
        
        # 経路が終点からの順番になっているため,始点から終点とする
        self._pathes = revers_path[::-1]
        # デバッグ用の経路に終点を追加
        self._set_random_node(np.append(end_pos, self._debug_pathes.shape[0] - 1).reshape(1, -1))
        self._set_node(np.append(end_pos, self._debug_pathes.shape[0] - 1).reshape(1, -1))

    def _calc_new_pos(self, random_pos, near_node):
        """
        最短ノードからランダムな値方向へ新しいノード(位置)を作成

        パラメータ
            random_pos(numpy.ndarray): ランダムな位置
            near_node(numpy.ndarray): 最短ノード
        
        戻り値
            new_pos(numpy.ndarray): 新しいノード
        """
        # 方向を計算
        near_pos = self._debug_pathes[near_node, :-1]
        direction = random_pos - near_pos
        # 方向の大きさを1にする
        norm_direction = direction / np.linalg.norm(direction)
        # 新しいノードを作成
        new_pos = near_pos + norm_direction * self._MOVING_VALUE

        return new_pos

    def _get_near_node(self, random_pos):
        """
        ランダムな位置との最短距離ノードを取得

        パラメータ
            random_pos(numpy.ndarray): ランダムな位置
        
        戻り値
            min_dist_node(int): 最短距離ノード
        """
        # 最後以外に位置が保存されている
        node_pos = self._debug_pathes[:, :-1]
        # 差分を計算
        difference = node_pos - random_pos
        # 差分から距離を計算
        distance = np.linalg.norm(difference, axis=1)
        # 最小距離のノードを取得
        min_dist_node = np.argmin(distance)

        return min_dist_node

    def _strict_planning_pos(self, start_pos, end_pos):
        """
        探索範囲を制限する

        パラメータ
            start_pos(numpy.ndarray): 始点
            end_pos(numpy.ndarray): 終点
        """
        all_pos = np.append(start_pos, end_pos, axis=0)
        # 各列の最大/最小値を取得
        min_pos = np.min(all_pos, axis=0)
        max_pos = np.max(all_pos, axis=0)
        self._strict_min_pos = min_pos - self._STRICT_PLANNING_POS
        self._strict_max_pos = max_pos + self._STRICT_PLANNING_POS

    def _get_random_pos(self):
        """
        ランダムな位置を取得

        戻り値
            random_pos(numpy.ndarray): ランダムな位置
        """
        random_pos = np.random.uniform(self._strict_min_pos, self._strict_max_pos)

        return random_pos

    def save(self):
        """
        生成した経路をファイル保存
        """
        np.savetxt(self._FILE_NAME_PATHES, self._pathes)
        np.savetxt(self._FILE_NAME_DEBUG_PATHES, self._debug_pathes)
        np.savetxt(self._FILE_NAME_DEBUG_RANDOM_PATHES, self._debug_random_pathes)

経路生成のアニメーション

上記でRRTの実装をしたが,想定通りに動いているかの確認が必要なため,アニメーションを作成して確認する.
PythonによるRRTのアニメーション実装は下記の通りです.
前記事と大方同じである.新規追加箇所は 「# 新規追加」と記載した.

rrt_animation.py
# RRTで作成した経路のアニメーションを作成する

# ライブラリの読み込み
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as ani
import matplotlib.patches as patches


class RRTAnimation:
    """
    RRTのアニメーション作成
    
    プロパティ
        _min_pos(numpy.ndarray): RRTの最小探索範囲
        _max_pos(numpy.ndarray): RRTの最大探索範囲
        _figure: 描画枠
        _axis: 描画内容
    
    publicメソッド (全てのクラスから参照可能)
        plot_2DAnimation(): 2D(2次元)アニメーション作成
        plot_3DAnimation(): 3D(3次元)アニメーション作成
    
    protectedメソッド (自クラスまたは子クラスが参照可能)
        _reset2D(): 2次元データのリセット
        _reset3D(): 3次元データのリセット
        _update_3Ddata(): 3次元各データの更新
    """
    # 定数の定義
    _ONE_CUT_NUM = 6        # 1回
    _ANIMATION_NAME = "rrt_animation.gif"
    
    def __init__(self, min_pos, max_pos):
        """
        コンストラクタ
        """
        self._min_pos = min_pos
        self._max_pos = max_pos
        
    def _reset2D(self):
        """
        2次元データのリセット
        """
        self._figure = plt.Figure()
        self._axis = self._figure.add_subplot(111)
        
        # X/Y軸に文字を記載
        self._axis.set_xlabel("X")
        self._axis.set_ylabel("Y")
        # プロットするX/Yの範囲を設定
        self._axis.set_xlim(self._min_pos[0], self._max_pos[0])
        self._axis.set_ylim(self._min_pos[1], self._max_pos[1])
        self._axis.grid()
        self._axis.set_aspect("equal")
        
    def plot_2DAnimation(self, pathes, debug_pathes, debug_random_pathes):
        """
        2D(2次元)アニメーション作成
        
        パラメータ
            pathes(numpy.ndarray): 経路
            debug_pathes(numpy.ndarray): デバッグ用経路
            debug_random_pathes(numpy.ndarray): デバッグ用ランダム経路
        
        戻り値
            plot_result(bbol): True / False = 成功 / 失敗
        """
        plot_result = False

        # データをリセットする
        self._reset2D()
        
        # 引数の確認
        if pathes.size == 0 or debug_pathes.size == 0 or debug_random_pathes.size == 0:
            return plot_result
        
        # デバッグ用経路とデバッグ用ランダム経路の行数が一致していることを確認
        if debug_pathes.shape[0] != debug_random_pathes.shape[0]:
            # 異常なデータ
            return plot_result

        # ノード位置を取得
        debug_pathes_pos = debug_pathes[:, :-1]
        debug_pathes_near_node = debug_pathes[:, -1]
        imgs = []
        
        # アニメーション
        for i in range(debug_pathes.shape[0]):
            for j in range(self._ONE_CUT_NUM * 2):
                # デバッグ用経路の点数分ループ
                path_images = []
                # ランダムな経路をプロット
                image = self._axis.plot(debug_random_pathes[i, 0], debug_random_pathes[i, 1], color="red", marker="*")
                path_images.extend(image)
                # ノードをプロット
                image = self._axis.scatter(debug_pathes_pos[:i, 0], debug_pathes_pos[:i, 1], color="black", marker="*")
                path_images.extend([image])
                # 始点をプロット
                image = self._axis.scatter(debug_pathes_pos[ 0, 0], debug_pathes_pos[ 0, 1], color="cyan", marker="*", s=24)
                path_images.extend([image])
                # 終点をプロット
                image = self._axis.scatter(debug_pathes_pos[-1, 0], debug_pathes_pos[-1, 1], color="lime", marker="*", s=24)
                path_images.extend([image])
                # 現ノードをプロット
                image = self._axis.scatter(debug_pathes_pos[ i, 0], debug_pathes_pos[ i, 1], color="blue", s=24)
                path_images.extend([image])
                # 最短ノードもプロット
                if i != 0:
                    # 始点には最短ノードが存在しないから,始点以外でプロットする
                    near_node = int(debug_pathes_near_node[i])
                    plot_pos = np.append(debug_pathes_pos[i].reshape(1, -1), debug_pathes_pos[near_node].reshape(1, -1), axis=0)
                    image = self._axis.plot(plot_pos[:, 0], plot_pos[:, 1], color="red")
                    path_images.extend(image)
                    image = self._axis.scatter(plot_pos[1, 0], plot_pos[1, 1], color="green", marker="*", s=24)
                    path_images.extend([image])
                imgs.append(path_images)
        
        # 最終的なパスをプロット
        for _ in range(self._ONE_CUT_NUM * 4):
            path_images = []
            image = self._axis.plot(pathes[:, 0], pathes[:, 1], color="green")
            path_images.extend(image)
            image = self._axis.scatter(debug_pathes[:, 0], debug_pathes[:, 1], color="gray")
            path_images.extend([image])
            imgs.append(path_images)

        # アニメーション
        animation = ani.ArtistAnimation(self._figure, imgs)
        animation.save(self._ANIMATION_NAME, writer='imagemagick')
        plt.show()
        
        plot_result = True
        
        return plot_result
    
    def _reset3D(self):
        """
        3次元データのリセット
        """
        self._figure = plt.figure()
        self._axis = self._figure.add_subplot(111, projection="3d")
        
        # 0 ~ 2piまでの範囲とする
        theta_1_0 = np.linspace(0, np.pi * 2, 100) 
        theta_2_0 = np.linspace(0, np.pi * 2, 100)
        theta_1, theta_2 = np.meshgrid(theta_1_0, theta_2_0)
        
        # x, y, zの曲座標表示 (中心点が原点である半径1の球)
        self._x = np.cos(theta_2) * np.sin(theta_1)
        self._y = np.sin(theta_2) * np.sin(theta_1)
        self._z = np.cos(theta_1)
        
    def _set_3DAxis(self):
        """
        3次元データのラベルや範囲を設定
        """
        # X/Y/Z軸に文字を記載
        self._axis.set_xlabel("X")
        self._axis.set_ylabel("Y")
        self._axis.set_zlabel("Z")
        # プロットするX/Y/Zの範囲を設定
        self._axis.set_xlim(self._min_pos[0], self._max_pos[0])
        self._axis.set_ylim(self._min_pos[1], self._max_pos[1])
        self._axis.set_zlim(self._min_pos[2], self._max_pos[2])
        self._axis.grid()
        self._axis.set_aspect("equal")
        
    def _update_3Ddata(self, i, pathes, debug_pathes, debug_random_pathes, interference_obj):
        """
        3D(3次元)各データの更新

        パラメータ
            i(int): フレーム番号
            pathes(numpy.ndarray): 経路
            debug_pathes(numpy.ndarray): デバッグ用経路
            debug_random_pathes(numpy.ndarray): デバッグ用ランダム経路
            interference_obj(list): 干渉物
        """
        # 以前のプロットをクリアする
        self._axis.clear()
        self._set_3DAxis()

        # ノード位置を取得
        debug_pathes_pos = debug_pathes[:, :-1]
        debug_pathes_near_node = debug_pathes[:, -1]
        
        # 干渉物のプロット
        self._plot_3Dinterference_obj(interference_obj)
        
        if i < debug_pathes.shape[0]:
            # 各ノードをプロットする
            # ランダムな経路をプロット
            self._axis.plot(debug_random_pathes[i, 0], debug_random_pathes[i, 1], debug_random_pathes[i, 2], color="red", marker="*")
            # ノードをプロット
            self._axis.scatter(debug_pathes_pos[:i, 0], debug_pathes_pos[:i, 1], debug_pathes_pos[:i, 2], color="black", marker="*")
            # 始点をプロット
            self._axis.scatter(debug_pathes_pos[ 0, 0], debug_pathes_pos[ 0, 1], debug_pathes_pos[ 0, 2], color="cyan", marker="*", s=24)
            # 終点をプロット
            self._axis.scatter(debug_pathes_pos[-1, 0], debug_pathes_pos[-1, 1], debug_pathes_pos[-1, 2], color="lime", marker="*", s=24)
            # 現ノードをプロット
            self._axis.scatter(debug_pathes_pos[ i, 0], debug_pathes_pos[ i, 1], debug_pathes_pos[ i, 2], color="blue", s=24)
            # 最短ノードもプロット
            if i != 0:
                # 始点には最短ノードが存在しないから,始点以外でプロットする
                near_node = int(debug_pathes_near_node[i])
                plot_pos = np.append(debug_pathes_pos[i].reshape(1, -1), debug_pathes_pos[near_node].reshape(1, -1), axis=0)
                self._axis.plot(plot_pos[:, 0], plot_pos[:, 1], plot_pos[:, 2], color="red")
                self._axis.scatter(plot_pos[1, 0], plot_pos[1, 1], plot_pos[1, 2], color="green", marker="*", s=24)
        else:
            # 生成した経路をプロットする
            self._axis.plot(pathes[:, 0], pathes[:, 1], pathes[:, 2], color="green")
            self._axis.scatter(debug_pathes[:, 0], debug_pathes[:, 1], debug_pathes[:, 2], color="gray")
    
    # 新規追加 ↓
    def _plot_3Dinterference_obj(self, interference_obj):
        """
        球の干渉物のプロット
        
        パラメータ
            interference_obj(list): 球の干渉物
        
        戻り値
            interference_images(list): 干渉物データ
        """
        for interference in interference_obj:
            # 球データをプロット
            center = interference.center
            self._axis.plot_wireframe(self._x * interference.radius + center[0], self._y * interference.radius + center[1], self._z * interference.radius + center[2], color="gray", alpha=0.1)
    # 新規追加 ↑
    
    def plot_3DAnimation(self, pathes, debug_pathes, debug_random_pathes, interference_obj):
        """
        3D(3次元)アニメーション作成
        
        パラメータ
            pathes(numpy.ndarray): 経路
            debug_pathes(numpy.ndarray): デバッグ用経路
            debug_random_pathes(numpy.ndarray): デバッグ用ランダム経路
        
        戻り値
            plot_result(bbol): True / False = 成功 / 失敗
        """
        plot_result = False

        # データをリセットする
        self._reset3D()
        
        # 引数の確認
        if pathes.size == 0 or debug_pathes.size == 0 or debug_random_pathes.size == 0:
            return plot_result
        
        # デバッグ用経路とデバッグ用ランダム経路の行数が一致していることを確認
        if debug_pathes.shape[0] != debug_random_pathes.shape[0]:
            # 異常なデータ
            return plot_result

        # アニメーションのフレーム数
        n_frame = debug_pathes.shape[0] + self._ONE_CUT_NUM * 4
        animation = ani.FuncAnimation(self._figure, self._update_3Ddata, fargs=(pathes, debug_pathes, debug_random_pathes, interference_obj), interval=500, frames=n_frame)

        # アニメーション
        animation.save(self._ANIMATION_NAME, writer="imagemagick")
        plt.show()
        
        plot_result = True
        
        return plot_result

RRTの経路生成およびアニメーションを実施するメイン処理は下記の通りである.

main.py
# ライブラリの読み込み
import numpy as np
import matplotlib.pyplot as plt

# 自作モジュールの読み込み
from rrt import RRT
from rrt_animation import RRTAnimation
from interference import Circle, Ball


def path_planning():
    """
    経路生成
    """
    # 始点と終点を作成
    start_pos = np.array([0, 0, 0]).reshape(1, -1)
    end_pos   = np.array([2, 2, 2]).reshape(1, -1)
    
    # 干渉物の中心位置の最小値/最大値を定義
    interference_min_pos = np.array([0.5, 0.5, 0.5])
    interference_max_pos = np.array([1.5, 1.5, 1.5])
    # 干渉物の半径を定義
    interference_radius = 0.3
    # 干渉物の数を定義
    interference_num = 4
    # 干渉物を作成
    interference_obj = [Ball(np.random.uniform(interference_min_pos, interference_max_pos), interference_radius) for _ in range(interference_num)]

    # RRTクラスのインスタンスを作成
    rrt = RRT()
    # 始点から終点までの経路を生成
    planning_result = rrt.planning(start_pos, end_pos, interference_obj)
    # 生成した経路をファイルに保存
    rrt.save()
    
    # RRTアニメーションのインスタンスを作成
    rrtAni = RRTAnimation(rrt.strict_min_pos, rrt.strict_max_pos)
    # アニメーションを描画
    result = rrtAni.plot_3DAnimation(rrt.pathes, rrt.debug_pathes, rrt.debug_random_pathes, interference_obj)

if __name__ == "__main__":
    # 本ファイルがメインで呼ばれた時の処理
    path_planning()

main.pyを実行すると,"rrt_animation.gif"が作成される.
RRTによる経路生成は下図のようになった.
アニメーションの見方は以下の通りである.
・水色の星形(左下)データ:経路生成の始点
・緑色の星形(右上)データ:経路生成の終点
・黒色の星形データ:RRTで生成された経由点
・青色の球データ:新規データ
・赤色の星形データ:乱数データ
・赤色の線:乱数データと最短距離のデータを結んだ線
・緑色の線:最終的な始点から終点までの経路
・灰色の球:干渉物

rrt_animation.gif

上アニメーションより,干渉物を回避した経路が生成できていることが確認できた.
今回は球の干渉物が存在する環境において経路生成をしたが,球以外の干渉物でも回避できる経路生成を実装したい.

おわりに

本記事では,Pythonを使用して,下記内容を実装しました.
・3次元空間で,球の干渉物を回避するRRTによる経路生成

次記事では,下記内容を実装していきます.
・円形以外の干渉物が存在する,2次元空間でのRRTによる経路生成
・始点と終点の両方から,経路生成を実施するRRT-Connectによる経路生成
(https://qiita.com/haruhiro1020/items/2b3985495b864a4a7e16)

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?