amino  1.0-beta2
Lightweight Robot Utility Library
motionplanning.py
Go to the documentation of this file.
1 # Copyright (c) 2019, Colorado School of Mines
2 # All rights reserved.
3 #
4 # Author(s): Matthew A. Schack <mschack@mines.edu>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 #
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # SUCH DAMAGE.
31 
32 
35 """Motion Planning"""
36 
37 
38 
39 import ctypes
40 from amino.scenegraph import RxSgSub
41 from amino.defaults import *
42 
43 LIBAMINOMP = ctypes.CDLL("libamino-planning.so")
44 
45 class RxMp(ctypes.Structure):
46  """Opaque type for struct aa_rx_mp"""
47 
48 class MotionPlan(object):
49  """Context object for motion planning."""
50 
51  __slots__ = ["_ptr"]
52 
53  def __init__(self, ssg):
54  self._ptr_ptr = LIBAMINOMP.aa_rx_mp_create(ssg._ptr)
55 
56 
57  def __del__(self):
58  LIBAMINOMP.aa_rx_mp_destroy(self._ptr_ptr)
59 
60 
61  def motion_plan(self, start, goal, timeout):
62  """Compute a motion plan.
63 
64  Args:
65  start: A list containing the current configuration space state
66  goal: A list containing the goal configuraiton space state
67  timeout: Timout before a motion planning failure is declared
68 
69  Returns:
70  None if no motion plan is found, a list of configurations (the
71  motion plan) otherwise.
72  """
73 
74  c_start = (ctypes.c_double * len(start))(*start)
75  c_goal = (ctypes.c_double * len(goal))(*goal)
76  LIBAMINOMP.aa_rx_mp_set_start(self._ptr_ptr, len(start), c_start)
77  if LIBAMINOMP.aa_rx_mp_set_goal(self._ptr_ptr, len(goal), c_goal) != AA_RX_OK:
78  return None
79 
80  LIBAMINOMP.aa_rx_mp_set_simplify(self._ptr_ptr, ctypes.c_int(1))
81 
82  n_path = ctypes.c_size_t(0)
83 
84  path = ctypes.POINTER(ctypes.c_double)()
85  ret = LIBAMINOMP.aa_rx_mp_plan(self._ptr_ptr, timeout, ctypes.byref(n_path),
86  ctypes.byref(path))
87 
88  if ret != AA_RX_OK:
89  return None
90 
91  py_path = []
92  for i in range(0, n_path.value*len(start), len(start)):
93  cord = []
94  for j in range(0, len(start)):
95  cord.append(path[i+j])
96  py_path.append(cord)
97 
98  return py_path
99 
100 
101 LIBAMINOMP.aa_rx_mp_create.argtypes = [ctypes.POINTER(RxSgSub)]
102 LIBAMINOMP.aa_rx_mp_create.restype = ctypes.POINTER(RxMp)
103 
104 LIBAMINOMP.aa_rx_mp_destroy.argtypes = [ctypes.POINTER(RxMp)]
105 
106 LIBAMINOMP.aa_rx_mp_set_simplify.argtypes = [ctypes.POINTER(RxMp), ctypes.c_int]
107 LIBAMINOMP.aa_rx_mp_set_sbl.argtypes = [ctypes.POINTER(RxMp), ctypes.POINTER(ctypes.c_int)]
108 
109 LIBAMINOMP.aa_rx_mp_set_start.argtypes = [ctypes.POINTER(RxMp), ctypes.c_int,
110  ctypes.POINTER(ctypes.c_double)]
111 
112 LIBAMINOMP.aa_rx_mp_set_goal.argtypes = [ctypes.POINTER(RxMp), ctypes.c_int,
113  ctypes.POINTER(ctypes.c_double)]
114 
115 LIBAMINOMP.aa_rx_mp_plan.argtypes = [ctypes.POINTER(RxMp), ctypes.c_double,
116  ctypes.POINTER(ctypes.c_size_t),
117  ctypes.POINTER(ctypes.POINTER(ctypes.c_double))]
def motion_plan(self, start, goal, timeout)
Compute a motion plan.