|
| 1 | +# Copyright 2025 SB Intuitions Corp. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | +from typing import Literal |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +import tqdm |
| 20 | +import tyro |
| 21 | +from plainmp.ik import solve_ik_srinv |
| 22 | +from plainmp.robot_spec import OpenArmV10RarmSpec |
| 23 | +from skrobot.coordinates import Coordinates |
| 24 | +from threadpoolctl import threadpool_limits |
| 25 | + |
| 26 | + |
| 27 | +class PlainmpIKSolver: |
| 28 | + def __init__(self): |
| 29 | + self.spec = OpenArmV10RarmSpec() |
| 30 | + |
| 31 | + def solve(self, tcp_coords_ros: Coordinates, n_global_iter: int) -> np.ndarray | None: |
| 32 | + cst = self.spec.create_tcp_pose_const(tcp_coords_ros) |
| 33 | + lb, ub = self.spec.angle_bounds() |
| 34 | + ret = solve_ik_srinv(cst, lb, ub, q_seed=None, max_trial=n_global_iter) |
| 35 | + if ret.success: |
| 36 | + return ret.q |
| 37 | + return None |
| 38 | + |
| 39 | + |
| 40 | +def benchmark( |
| 41 | + z: float, |
| 42 | + direction: Literal["forward1", "forward2", "down1", "down2"] = "forward1", |
| 43 | +): |
| 44 | + if direction == "forward1": |
| 45 | + co_base = Coordinates() |
| 46 | + elif direction == "forward2": |
| 47 | + co_base = Coordinates() |
| 48 | + co_base.rotate(-np.pi * 0.5, "x") |
| 49 | + elif direction == "down1": |
| 50 | + co_base = Coordinates() |
| 51 | + co_base.rotate(np.pi * 0.5, "y") |
| 52 | + elif direction == "down2": |
| 53 | + co_base = Coordinates() |
| 54 | + co_base.rotate(np.pi * 0.5, "y") |
| 55 | + co_base.rotate(-np.pi * 0.5, "x") |
| 56 | + else: |
| 57 | + assert False |
| 58 | + |
| 59 | + result_dir = Path(__file__).parent / "result" |
| 60 | + result_dir.mkdir(exist_ok=True) |
| 61 | + |
| 62 | + xlin = np.linspace(0.0, 0.8, 20) |
| 63 | + ylin = np.linspace(-0.5, 0.5, 20) |
| 64 | + X, Y = np.meshgrid(xlin, ylin) |
| 65 | + points2d = np.stack([X.ravel(), Y.ravel()], axis=1) |
| 66 | + solver = PlainmpIKSolver() |
| 67 | + |
| 68 | + with threadpool_limits(limits=1, user_api="blas"): |
| 69 | + qs_reachable = [] |
| 70 | + labels = [] |
| 71 | + for point2d in tqdm.tqdm(points2d): |
| 72 | + point3d = np.hstack([point2d, z]) |
| 73 | + co = co_base.copy_worldcoords() |
| 74 | + co.translate(point3d, wrt="world") |
| 75 | + ret = solver.solve(co, 30) |
| 76 | + success = ret is not None |
| 77 | + labels.append(success) |
| 78 | + if success: |
| 79 | + qs_reachable.append(ret) |
| 80 | + is_reachables = np.array(labels) |
| 81 | + qs_reachable = np.array(qs_reachable) |
| 82 | + pts_reachable = points2d[is_reachables] |
| 83 | + |
| 84 | + d = {"z": z, "pts": pts_reachable, "qs": qs_reachable} |
| 85 | + with (result_dir / f"{direction}-z-{round(z, 2)}.npz").open(mode="wb") as f: |
| 86 | + np.savez(f, d) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + tyro.cli(benchmark) |
0 commit comments