├── DualMultWS.jl
├── LICENSE
├── ParkingConstraints.jl
├── ParkingSignedDist.jl
├── README.md
├── a_star.jl
├── collision_check.jl
├── hybrid_a_star.jl
├── images
├── TrajParallelHOBCA.gif
└── TrajReverseHOBCA.gif
├── main.jl
├── obstHrep.jl
├── plotTraj.jl
├── reeds_shepp.jl
├── setup.jl
└── veloSmooth.jl
/DualMultWS.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # DualMultWS.jl: computes warm starting points for dual multipliers lambda and mu
28 | ###############
29 | #
30 | function DualMultWS(N,nOb,vOb, A, b,rx,ry,ryaw)
31 |
32 | x = zeros(3,N+1)
33 | x[1,:] = rx
34 | x[2,:] = ry
35 | x[3,:] = ryaw
36 |
37 | m = Model(solver=IpoptSolver(hessian_approximation="exact",mumps_pivtol=1e-5,
38 | max_iter=100,tol=1e-5, print_level=3, suppress_all_output="yes"))
39 |
40 | W_ev = ego[2]+ego[4]
41 | L_ev = ego[1]+ego[3]
42 |
43 | g = [L_ev/2,W_ev/2,L_ev/2,W_ev/2]
44 |
45 | # ofset from X-Y to the center of the ego set
46 | offset = (ego[1]+ego[3])/2 - ego[3]
47 |
48 |
49 | @variable(m, l[1:sum(vOb),1:(N+1)]) # dual multiplier associated with obstacleShape
50 | @variable(m, n[1:nOb*4,1:(N+1)]) # dual multiplier associated with carShape
51 | @variable(m, d[1:nOb,1:(N+1)])
52 |
53 | @NLobjective(m, Max,sum(sum(d[i,k] for k=1:N+1) for i=1:nOb ))
54 |
55 | @constraint(m, l .>= 0 )
56 | @constraint(m, n .>= 0)
57 |
58 | for i in 1:N+1 # iterate over time steps
59 | for j = 1 : nOb # iterate over obstacles
60 | Aj = A[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract obstacle matrix associated with j-th obstacle
61 | lj = l[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract lambda dual variables associate j-th obstacle
62 | nj = n[(j-1)*4+1:j*4 ,:] # extract mu dual variables associated with j-th obstacle
63 | bj = b[sum(vOb[1:j-1])+1 : sum(vOb[1:j])] # extract obstacle matrix associated with j-th obstacle
64 |
65 | # norm(A'*lambda) <= 1
66 | @constraint(m, (sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2 <= 1 )
67 |
68 | # G'*mu + R'*A*lambda = 0
69 | @constraint(m, (nj[1,i] - nj[3,i]) + cos(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + sin(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 )
70 | @constraint(m, (nj[2,i] - nj[4,i]) - sin(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + cos(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 )
71 |
72 | # -g'*mu + (A*t - b)*lambda > 0
73 | @constraint(m, d[j,i] == -sum(g[k]*nj[k,i] for k = 1:4) + (x[1,i]+cos(x[3,i])*offset)*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j])
74 | + (x[2,i]+sin(x[3,i])*offset)*sum(Aj[k,2]*lj[k,i] for k=1:vOb[j]) - sum(bj[k]*lj[k,i] for k=1:vOb[j]))
75 | end
76 | end
77 | tic()
78 | solve(m; suppress_warnings=true)
79 | time = toq();
80 | # print("Auxillery problem time (to warm start dual variables) = ",time," s \n")
81 |
82 |
83 | lp = getvalue(l)'
84 | np = getvalue(n)'
85 |
86 | return lp,np
87 |
88 | end
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/ParkingConstraints.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # checks if the path provided by IPOPT is feasible
28 | # useful when Restoration Failure is reported by IPOPT
29 | ###############
30 |
31 |
32 | function ParkingConstraints(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,x,u,l,n,timeScale,fixTime,sd)
33 |
34 |
35 | # desired safety distance
36 | dmin = 0.05 # anything bigger than 0, e.g. 0.05
37 |
38 | c0 = zeros(5,1)
39 | c1 = zeros(4,1)
40 | c2 = zeros(4,1)
41 | c3 = zeros(4,N)
42 | c4 = zeros(1,1)
43 | c5 = zeros(1,1)
44 | c6 = zeros(4,N+1)
45 |
46 | e = zeros(7,1)
47 |
48 | c0[1] = maximum(abs(u[1,:]))-0.6
49 | c0[2] = maximum(abs(u[2,:]))-0.4
50 |
51 |
52 | c0[3] = maximum(abs(timeScale-1))-0.2
53 |
54 | c0[4] = -minimum(l)
55 | c0[5] = -minimum(n)
56 |
57 | print(c0,"\n")
58 |
59 |
60 | #starting point
61 | c1[1] = abs(x[1,1] - x0[1])
62 | c1[2] = abs(x[2,1] - x0[2])
63 | c1[3] = abs(x[3,1] - x0[3])
64 | c1[4] = abs(x[4,1] - x0[4])
65 |
66 | #end point
67 | c2[1] = abs(x[1,N+1] - xF[1])
68 | c2[2] = abs(x[2,N+1] - xF[2])
69 | c2[3] = abs(x[3,N+1] - xF[3])
70 | c2[4] = abs(x[4,N+1] - xF[4])
71 |
72 | ##############################
73 | # dynamics of the car
74 | ##############################
75 | # - unicycle dynamic with euler forward
76 | # - sampling time scaling, is identical over the horizon
77 |
78 | for i in 1:N
79 |
80 | if fixTime == 1
81 | c3[1,i] = x[1,i+1] - (x[1,i] + Ts*(x[4,i] + Ts/2*u[2,i])*cos((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L)))
82 | c3[2,i] = x[2,i+1] - (x[2,i] + Ts*(x[4,i] + Ts/2*u[2,i])*sin((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L)))
83 | c3[3,i] = x[3,i+1] - (x[3,i] + Ts*(x[4,i] + Ts/2*u[2,i])*tan(u[1,i])/L)
84 | c3[4,i] = x[4,i+1] - (x[4,i] + Ts*u[2,i])
85 | else
86 | c3[1,i] = x[1,i+1] - (x[1,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*cos((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L)))
87 | c3[1,i] = x[2,i+1] - (x[2,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*sin((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L)))
88 | c3[1,i] = x[3,i+1] - (x[3,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*tan(u[1,i])/L)
89 | c3[1,i] = x[4,i+1] - (x[4,i] + timeScale[i]*Ts*u[2,i])
90 |
91 | end
92 |
93 | end
94 |
95 | u0 = [0,0]
96 |
97 |
98 | if fixTime == 1
99 |
100 | # print(diff([0. u[1,:]']'),"\n")
101 | # print(abs(diff([0. u[1,:]']'))/Ts,"\n")
102 | c5 = maximum(abs(diff([0. u[1,:]']'))/Ts) - 0.6
103 | c4 = 0
104 | else
105 | c4 = maximum(abs(diff(timeScale)))
106 | c5 = maximum(abs(diff([0 u[1,:]']'))/(timeScale[1]*Ts)) - 0.6
107 | end
108 |
109 |
110 | ##############################
111 | # obstacle avoidance constraints
112 | ##############################
113 |
114 | # width and length of ego set
115 | W_ev = ego[2]+ego[4]
116 | L_ev = ego[1]+ego[3]
117 |
118 | g = [L_ev/2,W_ev/2,L_ev/2,W_ev/2]
119 |
120 | # ofset from X-Y to the center of the ego set
121 | offset = (ego[1]+ego[3])/2 - ego[3]
122 |
123 |
124 | for i in 1:N+1 # iterate over time steps
125 | for j = 1 : nOb # iterate over obstacles
126 | Aj = A[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract obstacle matrix associated with j-th obstacle
127 | lj = l[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract lambda dual variables associate j-th obstacle
128 | nj = n[(j-1)*4+1:j*4 ,:] # extract mu dual variables associated with j-th obstacle
129 | bj = b[sum(vOb[1:j-1])+1 : sum(vOb[1:j])] # extract obstacle matrix associated with j-th obstacle
130 |
131 | # norm(A'*lambda) <= 1
132 | if sd == 1
133 | c6[1,i] = abs((sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2) - 1
134 | else
135 | c6[1,i] = (sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2 - 1
136 | end
137 |
138 | # G'*mu + R'*A*lambda = 0
139 | c6[2,i] = abs((nj[1,i] - nj[3,i]) + cos(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + sin(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]))
140 | c6[3,i] = abs((nj[2,i] - nj[4,i]) - sin(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + cos(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]))
141 |
142 | # -g'*mu + (A*t - b)*lambda > 0
143 | c6[4,i] = -(-sum(g[k]*nj[k,i] for k = 1:4) + (x[1,i]+cos(x[3,i])*offset)*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j])
144 | + (x[2,i]+sin(x[3,i])*offset)*sum(Aj[k,2]*lj[k,i] for k=1:vOb[j]) - sum(bj[k]*lj[k,i] for k=1:vOb[j])) + dmin
145 | end
146 |
147 | end
148 |
149 | e[1] = maximum(c0)<= 5e-5
150 | e[2] = maximum(c1)<= 5e-5
151 | e[3] = maximum(c2)<= 5e-5
152 | e[4] = maximum(abs(c3))<= 5e-5
153 | e[5] = c4 <= 5e-5
154 | e[6] = c5 <= 5e-5
155 | e[7] = maximum(c6)<= 5e-5
156 |
157 | # print(e,"\n")
158 |
159 | if sum(e) == 7
160 | return 1
161 | else
162 | return 0
163 | end
164 |
165 | end
166 |
--------------------------------------------------------------------------------
/ParkingSignedDist.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # computes collision-free trajectory by appropriately reformulating the distance function
28 | ###############
29 |
30 |
31 | function ParkingSignedDist(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,fixTime,xWS,uWS)
32 |
33 |
34 | # desired safety distance
35 | dmin = 0.05 # anything bigger than 0, e.g. 0.05
36 |
37 | ##############################
38 | # Define JuMP file
39 | ##############################
40 | # Define IPOPT as solver and well as solver settings
41 | ##############################
42 | # seems to work best
43 | m = Model(solver=IpoptSolver(hessian_approximation="exact",mumps_pivtol=1e-6,alpha_for_y="min",recalc_y="yes",
44 | mumps_mem_percent=6000,max_iter=200,tol=1e-5, print_level=0,
45 | min_hessian_perturbation=1e-12,jacobian_regularization_value=1e-7))#,nlp_scaling_method="none"
46 |
47 | ##############################
48 | # defining optimization variables
49 | ##############################
50 | #state
51 | @variable(m, x[1:4,1:(N+1)])
52 | #scaling on sampling time
53 | if fixTime == 0
54 | @variable(m, timeScale[1:N+1])
55 | end
56 | # timeScale = ones(1,N+1)
57 | #control
58 | @variable(m, u[1:2,1:(N)])
59 | # lagrange multipliers for dual dist function
60 | @variable(m, l[1:sum(vOb),1:(N+1)]) # dual multiplier associated with obstacleShape
61 | @variable(m, n[1:nOb*4,1:(N+1)]) # dual multiplier associated with carShape
62 |
63 |
64 | # regularization parameter to improve numerical stability
65 | reg = 1e-7;
66 | ##############################
67 | # cost function
68 | ##############################
69 | # (min control inputs)+
70 | # (min input rate)
71 | # (min time)+
72 | # (regularization wrt HA* traj)
73 | ##############################
74 |
75 | u0 = [0,0]
76 | #fix time objective
77 | if fixTime == 1
78 | @NLobjective(m, Min,sum(0.01*u[1,i]^2 + 0.5*u[2,i]^2 for i = 1:N) +
79 | sum(0.1*((u[1,i+1]-u[1,i])/Ts)^2 + 0.1*((u[2,i+1]-u[2,i])/Ts)^2 for i = 1:N-1)+
80 | (0.1*((u[1,1]-u0[1])/(Ts))^2 + 0.1*((u[2,1]-u0[2])/(Ts))^2) +
81 | sum(0.001*(x[1,i]-xWS[i,1])^2 + 0.001*(x[2,i]-xWS[i,2])^2 + 0.0001*(x[3,i]-xWS[i,3])^2 for i=1:N+1))
82 | else
83 | #varo time objective
84 | @NLobjective(m, Min,sum(0.01*u[1,i]^2 + 0.1*u[2,i]^2 for i = 1:N) +
85 | sum(0.1*((u[1,i+1]-u[1,i])/(timeScale[i]*Ts))^2 + 0.1*((u[2,i+1]-u[2,i])/(timeScale[i]*Ts))^2 for i = 1:N-1) +
86 | (0.1*((u[1,1]-u0[1]) /(timeScale[1]*Ts))^2 + 0.1*((u[2,1]-u0[2]) /(timeScale[1]*Ts))^2) +
87 | sum(0.5*timeScale[i] + 1*timeScale[i]^2 for i = 1:N+1)+
88 | sum(0.001*(x[1,i]-xWS[i,1])^2 + 0.001*(x[2,i]-xWS[i,2])^2 + 0.0001*(x[3,i]-xWS[i,3])^2 for i=1:N+1))
89 | end
90 |
91 | ##############################
92 | # bounds on states, inputs,
93 | # and dual multipliers.
94 | ##############################
95 | #input constraints
96 | @constraint(m, [i=1:N], -0.6 <= u[1,i] <= 0.6)
97 | @constraint(m, [i=1:N], -0.4 <= u[2,i] <= 0.4)
98 |
99 | #state constraints
100 | @constraint(m, [i=1:N+1], XYbounds[1] <= x[1,i] <= XYbounds[2])
101 | @constraint(m, [i=1:N+1], XYbounds[3] <= x[2,i] <= XYbounds[4])
102 |
103 | @constraint(m, [i=1:N+1], -1 <= x[4,i] <= 2)
104 |
105 | # bounds on time scaling
106 | if fixTime == 0
107 | @constraint(m, 0.8 .<= timeScale .<= 1.2)
108 | end
109 |
110 |
111 | # positivity constraints on dual multipliers
112 | @constraint(m, l .>= 0)
113 | @constraint(m, n .>= 0)
114 |
115 | ##############################
116 | # start and finish point
117 | ##############################
118 |
119 | #starting point
120 | @constraint(m, x[1,1] == x0[1])
121 | @constraint(m, x[2,1] == x0[2])
122 | @constraint(m, x[3,1] == x0[3])
123 | @constraint(m, x[4,1] == x0[4])
124 |
125 | #end point
126 | @constraint(m, x[1,N+1] == xF[1])
127 | @constraint(m, x[2,N+1] == xF[2])
128 | @constraint(m, x[3,N+1] == xF[3])
129 | @constraint(m, x[4,N+1] == xF[4])
130 |
131 | ##############################
132 | # dynamics of the car
133 | ##############################
134 | # - unicycle dynamic with euler forward
135 | # - sampling time scaling, is identical over the horizon
136 |
137 | for i in 1:N
138 |
139 | if fixTime == 1 # sampling time is fixed
140 | @NLconstraint(m, x[1,i+1] == x[1,i] + Ts*(x[4,i] + Ts/2*u[2,i])*cos((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L)))
141 | @NLconstraint(m, x[2,i+1] == x[2,i] + Ts*(x[4,i] + Ts/2*u[2,i])*sin((x[3,i] + Ts/2*x[4,i]*tan(u[1,i])/L)))
142 | @NLconstraint(m, x[3,i+1] == x[3,i] + Ts*(x[4,i] + Ts/2*u[2,i])*tan(u[1,i])/L)
143 | @NLconstraint(m, x[4,i+1] == x[4,i] + Ts*u[2,i])
144 | else # sampling time is variable
145 | @NLconstraint(m, x[1,i+1] == x[1,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*cos((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L)))
146 | @NLconstraint(m, x[2,i+1] == x[2,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*sin((x[3,i] + timeScale[i]*Ts/2*x[4,i]*tan(u[1,i])/L)))
147 | @NLconstraint(m, x[3,i+1] == x[3,i] + timeScale[i]*Ts*(x[4,i] + timeScale[i]*Ts/2*u[2,i])*tan(u[1,i])/L)
148 | @NLconstraint(m, x[4,i+1] == x[4,i] + timeScale[i]*Ts*u[2,i])
149 | end
150 |
151 | if fixTime == 0
152 | @constraint(m, timeScale[i] == timeScale[i+1])
153 | end
154 |
155 | end
156 |
157 | u0 = [0,0]
158 | if fixTime == 1
159 | for i in 1:N
160 | if i==1
161 | @constraint(m,-0.6<=(u0[1]-u[1,i])/Ts <= 0.6)
162 | else
163 | @constraint(m,-0.6<=(u[1,i-1]-u[1,i])/Ts <= 0.6)
164 | end
165 | end
166 | else
167 | for i in 1:N
168 | if i==1
169 | @NLconstraint(m,-0.6<=(u0[1]-u[1,i])/(timeScale[i]*Ts) <= 0.6)
170 | else
171 | @NLconstraint(m,-0.6<=(u[1,i-1]-u[1,i])/(timeScale[i]*Ts) <= 0.6)
172 | end
173 | end
174 | end
175 |
176 |
177 | ##############################
178 | # obstacle avoidance constraints
179 | ##############################
180 |
181 | # width and length of ego set
182 | W_ev = ego[2]+ego[4]
183 | L_ev = ego[1]+ego[3]
184 |
185 | g = [L_ev/2,W_ev/2,L_ev/2,W_ev/2]
186 |
187 | # ofset from X-Y to the center of the ego set
188 | offset = (ego[1]+ego[3])/2 - ego[3]
189 |
190 |
191 | for i in 1:N+1 # iterate over time steps
192 | for j = 1 : nOb # iterate over obstacles
193 | Aj = A[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract obstacle matrix associated with j-th obstacle
194 | lj = l[sum(vOb[1:j-1])+1 : sum(vOb[1:j]) ,:] # extract lambda dual variables associate j-th obstacle
195 | nj = n[(j-1)*4+1:j*4 ,:] # extract mu dual variables associated with j-th obstacle
196 | bj = b[sum(vOb[1:j-1])+1 : sum(vOb[1:j])] # extract obstacle matrix associated with j-th obstacle
197 |
198 | # norm(A'*lambda) = 1
199 | @NLconstraint(m, (sum(Aj[k,1]*lj[k,i] for k = 1 : vOb[j]))^2 + (sum(Aj[k,2]*lj[k,i] for k = 1 : vOb[j]))^2 == 1 )
200 |
201 | # G'*mu + R'*A*lambda = 0
202 | @NLconstraint(m, (nj[1,i] - nj[3,i]) + cos(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + sin(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 )
203 | @NLconstraint(m, (nj[2,i] - nj[4,i]) - sin(x[3,i])*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j]) + cos(x[3,i])*sum(Aj[k,2]lj[k,i] for k = 1:vOb[j]) == 0 )
204 |
205 | # -g'*mu + (A*t - b)*lambda > 0
206 | @NLconstraint(m, (-sum(g[k]*nj[k,i] for k = 1:4) + (x[1,i]+cos(x[3,i])*offset)*sum(Aj[k,1]*lj[k,i] for k = 1:vOb[j])
207 | + (x[2,i]+sin(x[3,i])*offset)*sum(Aj[k,2]*lj[k,i] for k=1:vOb[j]) - sum(bj[k]*lj[k,i] for k=1:vOb[j])) >= dmin )
208 | end
209 | end
210 |
211 | ##############################
212 | # set initial guesses
213 | ##############################
214 |
215 | if fixTime == 0
216 | setvalue(timeScale,1*ones(N+1,1))
217 | end
218 | setvalue(x,xWS')
219 | setvalue(u,uWS[1:N,:]')
220 |
221 | lWS,nWS = DualMultWS(N,nOb,vOb, A, b,xWS[:,1],xWS[:,2],xWS[:,3])
222 |
223 | setvalue(l,lWS')
224 | setvalue(n,nWS')
225 |
226 | ##############################
227 | # solve problem
228 | ##############################
229 | # ipopt has sometimes problems in the restoration phase,
230 | # it turns out that restarting ipopt with the previous solution
231 | # as an initial guess works well to achieve a high success rate.
232 | #
233 | # if restoration failure is reported by IPOPT, solution should be checked manually as it can still be feasible
234 | ##############################
235 |
236 | # at most three attempts considered
237 | time1 = 0
238 | time2 = 0
239 |
240 | exitflag = 0
241 |
242 | tic()
243 | status = solve(m; suppress_warnings=true)
244 | time1 = toq();
245 |
246 | if status == :Optimal
247 | exitflag = 1
248 | elseif status ==:Error || status ==:UserLimit
249 |
250 | Feasible = 0
251 | if Feasible == 0
252 | tic()
253 | status = solve(m; suppress_warnings=true)
254 | time2 = toq();
255 |
256 | if status == :Optimal
257 | exitflag = 1
258 | elseif status ==:Error || status ==:UserLimit
259 | xp = getvalue(x)
260 | up = getvalue(u)
261 | if fixTime == 1
262 | timeScalep = ones(1,N+1)
263 | else
264 | timeScalep = getvalue(timeScale)
265 | end
266 | lp = getvalue(l)
267 | np = getvalue(n)
268 | Feasible = 0
269 | Feasible = ParkingConstraints(x0,xF,N,Ts,L,ego,XYbounds,nOb,vOb, A, b,xp,up,lp,np,timeScalep,fixTime,1)
270 |
271 | if Feasible == 1
272 | exitflag = 1
273 | else
274 | exitflag = 0
275 | end
276 | end
277 | else
278 | exitflag = 1
279 | end
280 | else
281 | exitflag = 0
282 | end
283 |
284 | ##############################
285 | # return values
286 | ##############################
287 |
288 | # computation times is the sum of all trials
289 | time = time1+time2
290 | # print(" elapsed time: ")
291 | # print(time)
292 | # println(" seconds")
293 |
294 | xp = getvalue(x)
295 | up = getvalue(u)
296 | if fixTime == 1
297 | timeScalep = ones(1,N+1)
298 | else
299 | timeScalep = getvalue(timeScale)
300 | end
301 |
302 | lp = getvalue(l)
303 | np = getvalue(n)
304 |
305 | return xp, up, timeScalep, exitflag, time, lp, np
306 |
307 | end
308 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # H-OBCA
2 | Hierarchical Optimization-Based Collision Avoidance - An algorithm for generating dynamically feasible parking trajectories
3 |
4 | Paper describing the theory can be found [here](http://arxiv.org/abs/1711.03449).
5 |
6 | ## Short Description
7 | H-OBCA is an optimization-based approach for autonomous parking. It builds on [OBCA](https://github.com/XiaojingGeorgeZhang/OBCA), which is a recent method for generating obstacle-free trajectories using optimal control.
8 |
9 | H-OBCA is able to generate high-quality *kino-dynamically feasible obstacle-free* trajectories. These trajectories are smooth, and can be accurately tracked by simple low-level path following controllers. A [Julia](https://julialang.org/)-based implementation is provided.
10 |
11 |
12 | ## Examples
13 |
14 | ### H-OBCA for Reverse Parking
15 |
16 |
17 | ### H-OBCA for Parallel Parking
18 |
19 |
20 |
21 | ## How to run the Parking code:
22 |
23 | ### First steps
24 |
25 | 1. Change to the directory
26 |
27 | 2. Install Julia from https://julialang.org/downloads/ (code tested on version 0.5.1 and 0.6.0)
28 |
29 | 3. Open Julia in terminal
30 |
31 | 4. Install Julia package JuMP using Pkg.add("JuMP")
32 |
33 | 5. Install Julia package Ipopt using Pkg.add("Ipopt")
34 |
35 | 6. Install Julia package PyPlot using Pkg.add("PyPlot")
36 |
37 | 7. Install Julia package NearestNeighbors using Pkg.add("NearestNeighbors")
38 |
39 | 8. Install Julia package ControlSystems using Pkg.add("ControlSystems")
40 |
41 |
42 | ### Running the parking example
43 |
44 | 1. Start Julia in terminal
45 |
46 | 2. Type in terminal: include("setup.jl")
47 |
48 | 3. Type in terminal: include("main.jl")
49 |
50 |
51 | ### modifying the code
52 |
53 | 1. To play with start points, change x0 in main.jl and run
54 | the code by: include("main.jl")
55 |
56 | 2. If you change anything in one of the collision avoidance
57 | problems, you need to activate the changes by running:
58 | include("setup.jl")
59 |
60 | 3. If you change the size of the car in main.jl, the change
61 | also need to be made in collision_check.jl
62 |
63 | ### Note
64 | 1. this code has been tested on Julia 0.5.1 and 0.6.0, and might not run on any other Julia versions
65 |
66 | 2. For best results, run code in Julia terminal
67 |
--------------------------------------------------------------------------------
/a_star.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # Grid based A* shorest path planning
28 | ###############
29 |
30 |
31 | module a_star
32 |
33 | using PyPlot
34 | using NearestNeighbors
35 | using DataStructures
36 |
37 | const VEHICLE_RADIUS = 5.0 #[m]
38 | const GRID_RESOLUTION = 1.0 #[m]
39 |
40 |
41 | type Node
42 | x::Int64 #x index
43 | y::Int64 #y index
44 | cost::Float64 # cost
45 | pind::Int64 # parent index
46 | end
47 |
48 |
49 | function calc_dist_policy(gx::Float64, gy::Float64,
50 | ox::Array{Float64}, oy::Array{Float64},
51 | reso::Float64, vr::Float64)
52 | """
53 | gx: goal x position [m]
54 | gx: goal x position [m]
55 | ox: x position list of Obstacles [m]
56 | oy: y position list of Obstacles [m]
57 | reso: grid resolution [m]
58 | vr: vehicle radius[m]
59 | """
60 |
61 | ngoal = Node(round(Int64, gx/reso),round(Int64, gy/reso),0.0, -1)
62 |
63 | ox = [iox/reso for iox in ox]
64 | oy = [ioy/reso for ioy in oy]
65 |
66 | obmap, minx, miny, maxx, maxy, xw, yw = calc_obstacle_map(ox, oy, reso, vr)
67 |
68 | #open, closed set
69 | open, closed = Dict{Int64, Node}(), Dict{Int64, Node}()
70 | open[calc_index(ngoal, xw, minx, miny)] = ngoal
71 |
72 | motion = get_motion_model()
73 | nmotion = length(motion[:,1])
74 | pq = PriorityQueue()
75 | enqueue!(pq, calc_index(ngoal, xw, minx, miny), ngoal.cost)
76 |
77 | while true
78 | if length(open) == 0
79 | # println("Finish Search")
80 | break
81 | end
82 |
83 | c_id = dequeue!(pq)
84 | current = open[c_id]
85 |
86 | delete!(open, c_id)
87 | closed[c_id] = current
88 |
89 | for i in 1:nmotion # expand search grid based on motion model
90 | node = Node(current.x+motion[i,1], current.y+motion[i,2], current.cost+motion[i,3], c_id)
91 |
92 | if !verify_node(node, minx, miny, xw, yw, obmap)
93 | continue
94 | end
95 |
96 | node_ind = calc_index(node, xw, minx, miny)
97 |
98 | # If it is already in the closed set, skip it
99 | if haskey(closed,node_ind) continue end
100 |
101 | if haskey(open, node_ind)
102 | if open[node_ind].cost > node.cost
103 | # If so, update the node to have a new parent
104 | open[node_ind].cost = node.cost
105 | open[node_ind].pind = c_id
106 | end
107 | else # add to open set
108 | open[node_ind] = node
109 | enqueue!(pq, calc_index(node, xw, minx, miny), node.cost)
110 | end
111 | end
112 | end
113 |
114 | pmap = calc_policy_map(closed, xw, yw, minx, miny)
115 |
116 | return pmap
117 | end
118 |
119 |
120 | function calc_policy_map(closed, xw, yw, minx, miny)
121 |
122 | pmap = fill(Inf, (xw,yw))
123 |
124 | for n in values(closed)
125 | pmap[n.x-minx, n.y-miny] = n.cost
126 | end
127 | # println(pmap)
128 |
129 | return pmap
130 | end
131 |
132 |
133 | function calc_astar_path(sx::Float64, sy::Float64, gx::Float64, gy::Float64,
134 | ox::Array{Float64}, oy::Array{Float64}, reso::Float64, vr::Float64)
135 | """
136 | sx: start x position [m]
137 | sy: start y position [m]
138 | gx: goal x position [m]
139 | gx: goal x position [m]
140 | ox: x position list of Obstacles [m]
141 | oy: y position list of Obstacles [m]
142 | reso: grid resolution [m]
143 | """
144 |
145 | nstart = Node(round(Int64,sx/reso),round(Int64, sy/reso),0.0, -1)
146 | ngoal = Node(round(Int64, gx/reso),round(Int64, gy/reso),0.0, -1)
147 |
148 | ox = [iox/reso for iox in ox]
149 | oy = [ioy/reso for ioy in oy]
150 |
151 | obmap, minx, miny, maxx, maxy, xw, yw = calc_obstacle_map(ox, oy, reso, vr)
152 |
153 | #open, closed set
154 | open, closed = Dict{Int64, Node}(), Dict{Int64, Node}()
155 | open[calc_index(nstart, xw, minx, miny)] = nstart
156 |
157 | motion = get_motion_model()
158 | nmotion = length(motion[:,1])
159 | pq = PriorityQueue()
160 | enqueue!(pq, calc_index(nstart, xw, minx, miny), calc_cost(nstart, ngoal))
161 |
162 | while true
163 | if length(open) == 0;println("Error: No open set");break;end
164 |
165 | c_id = dequeue!(pq)
166 | current = open[c_id]
167 |
168 | if current.x == ngoal.x && current.y == ngoal.y # check goal
169 | println("Goal!!")
170 | closed[c_id] = current
171 | break
172 | end
173 |
174 | delete!(open, c_id)
175 | closed[c_id] = current
176 |
177 | for i in 1:nmotion # expand search grid based on motion model
178 | node = Node(current.x+motion[i,1], current.y+motion[i,2], current.cost+motion[i,3], c_id)
179 |
180 | if !verify_node(node, minx, miny, xw, yw, obmap)
181 | continue
182 | end
183 |
184 | node_ind = calc_index(node, xw, minx, miny)
185 |
186 | # If it is already in the closed set, skip it
187 | if haskey(closed,node_ind) continue end
188 |
189 | if haskey(open, node_ind)
190 | if open[node_ind].cost > node.cost
191 | # If so, update the node to have a new parent
192 | open[node_ind].cost = node.cost
193 | open[node_ind].pind = c_id
194 | end
195 | else # add to open set
196 | open[node_ind] = node
197 | enqueue!(pq, calc_index(node, xw, minx, miny), calc_cost(node, ngoal))
198 | end
199 | end
200 | end
201 |
202 | rx, ry = get_final_path(closed, ngoal, nstart, xw, minx, miny, reso)
203 |
204 | return rx, ry
205 | end
206 |
207 |
208 | function verify_node(node::Node, minx::Int64, miny::Int64, xw::Int64, yw::Int64, obmap::Array{Bool,2})
209 |
210 | if (node.x - minx) >= xw
211 | return false
212 | elseif (node.x - minx) <= 0
213 | return false
214 | end
215 | if (node.y - miny) >= yw
216 | return false
217 | elseif (node.y - miny) <= 0
218 | return false
219 | end
220 |
221 | #collision check
222 | if obmap[node.x-minx, node.y-miny]
223 | return false
224 | end
225 |
226 | return true
227 | end
228 |
229 |
230 | function calc_cost(n::Node, ngoal::Node)
231 | return (n.cost + h(n.x - ngoal.x, n.y - ngoal.y))
232 | end
233 |
234 |
235 | function get_motion_model()
236 | # dx, dy, cost
237 | motion=[1 0 1;
238 | 0 1 1;
239 | -1 0 1;
240 | 0 -1 1;
241 | -1 -1 sqrt(2);
242 | -1 1 sqrt(2);
243 | 1 -1 sqrt(2);
244 | 1 1 sqrt(2);]
245 |
246 | return motion
247 | end
248 |
249 |
250 | function calc_index(node::Node, xwidth::Int64, xmin::Int64, ymin::Int64)
251 | return (node.y - ymin)*xwidth + (node.x - xmin)
252 | end
253 |
254 |
255 | function calc_obstacle_map(ox::Array{Float64}, oy::Array{Float64}, reso::Float64, vr::Float64)
256 |
257 | minx = round(Int64, minimum(ox))
258 | miny = round(Int64, minimum(oy))
259 | maxx = round(Int64, maximum(ox))
260 | maxy = round(Int64, maximum(oy))
261 | # println("minx:", minx)
262 | # println("miny:", miny)
263 | # println("maxx:", maxx)
264 | # println("maxy:", maxy)
265 |
266 | xwidth = round(Int64, maxx - minx)
267 | ywidth = round(Int64, maxy - miny)
268 | # println("xwidth:", xwidth)
269 | # println("ywidth:", ywidth)
270 |
271 | obmap = fill(false, (xwidth,ywidth))
272 |
273 | kdtree = KDTree(hcat(ox, oy)')
274 | for ix in 1:xwidth
275 | x = ix + minx
276 | for iy in 1:ywidth
277 | y = iy + miny
278 | idxs, onedist = knn(kdtree, [x, y] , 1)
279 | if onedist[1] <= vr/reso
280 | obmap[ix,iy] = true
281 | end
282 | end
283 | end
284 |
285 | # println(" calc_obstacle_map done")
286 |
287 | return obmap, minx, miny, maxx, maxy, xwidth, ywidth
288 | end
289 |
290 |
291 | function get_final_path(closed::Dict{Int64, Node},
292 | ngoal::Node,
293 | nstart::Node,
294 | xw::Int64,
295 | minx::Int64,
296 | miny::Int64,
297 | reso::Float64)
298 |
299 | rx, ry = [ngoal.x],[ngoal.y]
300 | nid = calc_index(ngoal, xw, minx, miny)
301 | while true
302 | n = closed[nid]
303 | push!(rx, n.x)
304 | push!(ry, n.y)
305 | nid = n.pind
306 |
307 | if rx[end] == nstart.x && ry[end] == nstart.y
308 | # println("done")
309 | break
310 | end
311 | end
312 |
313 | rx = reverse(rx) .* reso
314 | ry = reverse(ry) .* reso
315 |
316 | return rx, ry
317 | end
318 |
319 |
320 | function search_min_cost_node(open::Dict{Int64, Node}, ngoal::Node)
321 | mnode = nothing
322 | mcost = Inf
323 | for n in values(open)
324 | # println(n)
325 | cost = n.cost + h(n.x - ngoal.x, n.y - ngoal.y)
326 | if mcost > cost
327 | mnode = n
328 | mcost = cost
329 | end
330 | end
331 | # println("minnode:", mnode)
332 |
333 | return mnode
334 | end
335 |
336 |
337 | function h(x::Int64, y::Int64)
338 | """
339 | Heuristic cost function
340 | """
341 | return sqrt(x^2+y^2);
342 | end
343 |
344 |
345 | function main()
346 | println(PROGRAM_FILE," start!!")
347 |
348 | sx = 10.0 # [m]
349 | sy = 10.0 # [m]
350 | gx = 50.0 # [m]
351 | gy = 50.0 # [m]
352 |
353 | ox = Float64[]
354 | oy = Float64[]
355 |
356 | for i in 0:60
357 | push!(ox, Float64(i))
358 | push!(oy, 0.0)
359 | end
360 | for i in 0:60
361 | push!(ox, 60.0)
362 | push!(oy, Float64(i))
363 | end
364 | for i in 0:60
365 | push!(ox, Float64(i))
366 | push!(oy, 60.0)
367 | end
368 | for i in 0:60
369 | push!(ox, 0.0)
370 | push!(oy, Float64(i))
371 | end
372 | for i in 0:40
373 | push!(ox, 20.0)
374 | push!(oy, Float64(i))
375 | end
376 | for i in 0:40
377 | push!(ox, 40.0)
378 | push!(oy, 60.0-Float64(i))
379 | end
380 |
381 | @time rx, ry = calc_astar_path(sx, sy, gx, gy, ox, oy, GRID_RESOLUTION, VEHICLE_RADIUS)
382 |
383 | plot(ox, oy, ".k",label="obstacles")
384 | plot(sx, sy, "xr",label="start")
385 | plot(gx, gy, "xb",label="goal")
386 | plot(rx, ry, "-r",label="A* path")
387 | legend()
388 | grid(true)
389 | axis("equal")
390 | show()
391 |
392 | println(PROGRAM_FILE," Done!!")
393 | end
394 |
395 |
396 | if length(PROGRAM_FILE)!=0 &&
397 | contains(@__FILE__, PROGRAM_FILE)
398 |
399 | main()
400 | end
401 |
402 |
403 | end #module
404 |
405 |
--------------------------------------------------------------------------------
/collision_check.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | module collision_check
27 |
28 | using NearestNeighbors
29 | using PyPlot
30 |
31 | const B = 1.0 #[m] distance from rear to vehicle back end
32 | const C = 3.7 #[m] distance from rear to vehicle front end
33 | const I = 2.0 #[m] width of vehicle
34 | const WBUBBLE_DIST = (B+C)/2.0-B #[m] distance from rear and the center of whole bubble
35 | const WBUBBLE_R = (B+C)/2.0 #[m] whole bubble radius
36 |
37 | const vrx = [C, C, -B, -B, C ]
38 | const vry = [-I/2.0, I/2.0, I/2.0, -I/2.0, -I/2.0]
39 |
40 | function check_collision(x, y, yaw, kdtree, ox, oy)
41 |
42 | for (ix, iy, iyaw) in zip(x, y, yaw)
43 | cx = ix + WBUBBLE_DIST*cos(iyaw)
44 | cy = iy + WBUBBLE_DIST*sin(iyaw)
45 |
46 | # Whole bubble check
47 | ids = inrange(kdtree, [cx, cy], WBUBBLE_R, true)
48 | if length(ids) == 0 continue end
49 |
50 | if !rect_check(ix, iy, iyaw, ox[ids], oy[ids])
51 | return false #collision
52 | end
53 | end
54 |
55 | return true #OK
56 |
57 | end
58 |
59 |
60 | function rect_check(ix, iy, iyaw, ox, oy)
61 |
62 | c = cos(-iyaw)
63 | s = sin(-iyaw)
64 |
65 | for (iox, ioy) in zip(ox, oy)
66 | tx = iox - ix
67 | ty = ioy - iy
68 | lx = (c*tx - s*ty)
69 | ly = (s*tx + c*ty)
70 |
71 | sumangle = 0.0
72 | for i in 1:length(vrx)-1
73 | x1 = vrx[i] - lx
74 | y1 = vry[i] - ly
75 | x2 = vrx[i+1] - lx
76 | y2 = vry[i+1] - ly
77 | d1 = hypot(x1,y1)
78 | d2 = hypot(x2,y2)
79 | theta1 = atan2(y1,x1)
80 | tty = (-sin(theta1)*x2 + cos(theta1)*y2)
81 |
82 | tmp = (x1*x2+y1*y2)/(d1*d2)
83 | if tmp >= 1.0 tmp = 1.0 end
84 |
85 | if tty >= 0.0
86 | sumangle += acos(tmp)
87 | else
88 | sumangle -= acos(tmp)
89 | end
90 | end
91 |
92 | if sumangle >= pi
93 | return false
94 | end
95 | end
96 |
97 | return true #OK
98 | end
99 |
100 |
101 | function main()
102 |
103 | ox = rand(3)*30.0 - 30.0/2.0
104 | oy = rand(3)*30.0 - 30.0/2.0
105 |
106 | kdtree = KDTree(hcat(ox, oy)')
107 |
108 | x = [10.0, 5.0]
109 | y = [10.0, 5.0]
110 | yaw = [deg2rad(10.0), deg2rad(0.0)]
111 |
112 | flag = check_collision(x, y, yaw, kdtree, ox, oy)
113 | if flag
114 | println("OK")
115 | else
116 | println("Collision")
117 | end
118 |
119 | plot(ox, oy, ".r")
120 | grid(true)
121 | axis("equal")
122 | show()
123 |
124 | end
125 |
126 |
127 | if length(PROGRAM_FILE)!=0 &&
128 | contains(@__FILE__, PROGRAM_FILE)
129 |
130 | @time main()
131 | end
132 |
133 |
134 | end #module
135 |
136 |
137 |
--------------------------------------------------------------------------------
/hybrid_a_star.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # Hybrid A star: Julia implementation of Hybrid A* algorithm
28 | ###############
29 |
30 | module hybrid_a_star
31 |
32 | using PyPlot
33 | using DataFrames
34 | using NearestNeighbors
35 | using DataStructures
36 |
37 | include("./reeds_shepp.jl")
38 | include("./a_star.jl")
39 | include("./collision_check.jl")
40 |
41 |
42 | const VEHICLE_RADIUS = 1.0 #[m]; radius of rear ball;
43 | const BUBBLE_DIST = 1.7 #[m]; distance to "forward bubble";
44 |
45 | ##### Fast Comp Time values from Alex Liniger ######
46 | const OB_MAP_RESOLUTION = 0.1 #[m]; obstacle resolution
47 | const YAW_GRID_RESOLUTION = deg2rad(5.0) #[m]; 10.0 /// 5.0
48 | const N_STEER = 5.0 # number of steer command; 10.0 seems OK /// 5
49 | ## For Backwards Parking
50 | const XY_GRID_RESOLUTION = 0.3 #[m];
51 | const MOTION_RESOLUTION = 0.1 #[m];
52 | ###################################################
53 |
54 | const USE_HOLONOMIC_WITH_OBSTACLE_HEURISTIC = true
55 | const USE_NONHOLONOMIC_WITHOUT_OBSTACLE_HEURISTIC = false
56 |
57 | const SB_COST = 10.0 # switch back penalty cost
58 | const BACK_COST = 0.0 # backward penalty cost
59 | const STEER_CHANGE_COST = 10.0 # steer angle change penalty cost
60 | const STEER_COST = 0.0 # steer angle penalty cost
61 | const H_COST = 1. # Heuristic cost; higher -> heuristic; 1.0
62 |
63 | const WB = 2.7 #[m]; 7.0
64 | const MAX_STEER = 0.6 #[rad]
65 |
66 | type Node
67 | xind::Int64 #x index
68 | yind::Int64 #y index
69 | yawind::Int64 #yaw index
70 | direction::Bool # moving direction forword:true, backword:false
71 | x::Array{Float64} # x position [m]
72 | y::Array{Float64} # y position [m]
73 | yaw::Array{Float64} # yaw angle [rad]
74 | steer::Float64 # steer input
75 | cost::Float64 # cost
76 | pind::Int64 # parent index
77 | end
78 |
79 | type Config
80 | minx::Int64
81 | miny::Int64
82 | minyaw::Int64
83 | maxx::Int64
84 | maxy::Int64
85 | maxyaw::Int64
86 | xw::Int64
87 | yw::Int64
88 | yaww::Int64
89 | xyreso::Float64
90 | yawreso::Float64
91 | obminx::Int64
92 | obminy::Int64
93 | obmaxx::Int64
94 | obmaxy::Int64
95 | obxw::Int64
96 | obyw::Int64
97 | obreso::Float64
98 | end
99 |
100 |
101 | function calc_hybrid_astar_path(sx::Float64, sy::Float64, syaw::Float64,
102 | gx::Float64, gy::Float64, gyaw::Float64,
103 | ox::Array{Float64}, oy::Array{Float64},
104 | xyreso::Float64, yawreso::Float64,
105 | obreso::Float64)
106 | """
107 | Calc hybrid astar path
108 | sx: start x position [m]
109 | sy: start y position [m]
110 | gx: goal x position [m]
111 | gx: goal x position [m]
112 | ox: x position list of Obstacles [m]
113 | oy: y position list of Obstacles [m]
114 | xyreso: grid resolution [m]
115 | yawreso: yaw angle resolution [rad]
116 | """
117 |
118 | syaw, gyaw = pi_2_pi(syaw), pi_2_pi(gyaw)
119 |
120 | const c = calc_config(ox, oy, xyreso, yawreso, obreso)
121 | kdtree = KDTree(hcat(ox, oy)')
122 | obmap, gkdtree = calc_obstacle_map(ox, oy, c)
123 | nstart = Node(round(Int64,sx/xyreso), round(Int64,sy/xyreso), round(Int64, syaw/yawreso),true,[sx],[sy],[syaw],0.0,0.0, -1)
124 | ngoal = Node(round(Int64,gx/xyreso), round(Int64,gy/xyreso), round(Int64,gyaw/yawreso),true,[gx],[gy],[gyaw],0.0,0.0, -1)
125 |
126 | if USE_HOLONOMIC_WITH_OBSTACLE_HEURISTIC
127 | h_dp = calc_holonomic_with_obstacle_heuristic(ngoal, ox, oy, xyreso)
128 | else
129 | h_dp = Array{Float64}()
130 | end
131 | if USE_NONHOLONOMIC_WITHOUT_OBSTACLE_HEURISTIC
132 | h_rs = calc_nonholonomic_without_obstacle_heuristic(ngoal, c)
133 | else
134 | h_rs = Array{Float64}()
135 | end
136 |
137 | open, closed = Dict{Int64, Node}(), Dict{Int64, Node}()
138 | open[calc_index(nstart, c)] = nstart
139 | pq = PriorityQueue()
140 | enqueue!(pq, calc_index(nstart, c), calc_cost(nstart, h_rs, h_dp, ngoal, c))
141 |
142 | u, d = calc_motion_inputs()
143 | nmotion = length(u)
144 |
145 | while true
146 | if length(open) == 0
147 | println("Error: Cannot find path, No open set")
148 | return nothing, nothing, nothing
149 | end
150 |
151 | c_id = dequeue!(pq)
152 | current = open[c_id]
153 |
154 | isupdated, current = update_node_with_analystic_expantion(current, ngoal, obmap, c, kdtree, ox, oy)
155 | if isupdated
156 | closed[calc_index(ngoal, c)] = current
157 | break #goal
158 | end
159 |
160 | #move current node from open to closed
161 | delete!(open, c_id)
162 | closed[c_id] = current
163 |
164 | for i in 1:nmotion
165 | node = calc_next_node(current, c_id, u[i], d[i], c, gkdtree)
166 |
167 | if !verify_index(node, obmap, c, kdtree, ox, oy) continue end
168 |
169 | node_ind = calc_index(node, c)
170 |
171 | # If it is already in the closed set, skip it
172 | if haskey(closed, node_ind) continue end
173 |
174 | if !haskey(open, node_ind)
175 | open[node_ind] = node
176 | enqueue!(pq, calc_index(node, c), calc_cost(node, h_rs, h_dp, ngoal, c))
177 | end
178 | end
179 |
180 | end
181 |
182 | # println("final expand node:", length(open) + length(closed))
183 |
184 | rx, ry, ryaw = get_final_path(closed, ngoal, nstart, c)
185 |
186 | return rx, ry, ryaw
187 | end
188 |
189 |
190 | function update_node_with_analystic_expantion(current::Node,
191 | ngoal::Node,
192 | obmap::Array{Bool,2},
193 | c::Config,
194 | kdtree::NearestNeighbors.KDTree,
195 | ox::Array{Float64},
196 | oy::Array{Float64}
197 | )
198 |
199 | apath = analystic_expantion(current, ngoal, obmap, c, kdtree, ox, oy)
200 | if apath != nothing
201 | # println("Find path! with analystic_expantion")
202 | current.x = vcat(current.x, apath.x[2:end-1])
203 | current.y = vcat(current.y, apath.y[2:end-1])
204 | current.yaw = vcat(current.yaw, apath.yaw[2:end-1])
205 | current.cost += calc_rs_path_cost(apath)
206 | return true, current
207 | end
208 |
209 | return false, current #no update
210 | end
211 |
212 |
213 | function calc_rs_path_cost(rspath::hybrid_a_star.reeds_shepp.Path)
214 |
215 | cost = 0.0
216 | for l in rspath.lengths
217 | if l >= 0 # forward
218 | cost += l
219 | else # back
220 | cost += abs(l) * BACK_COST
221 | end
222 | end
223 |
224 | # swich back penalty
225 | for i in 1:length(rspath.lengths) - 1
226 | if rspath.lengths[i] * rspath.lengths[i+1] < 0.0 # switch back
227 | cost += SB_COST
228 | end
229 | end
230 |
231 | # steer penalyty
232 | for ctype in rspath.ctypes
233 | if ctype != "S" # curve
234 | cost += STEER_COST*abs(MAX_STEER)
235 | end
236 | end
237 |
238 | # ==steer change penalty
239 | # calc steer profile
240 | nctypes = length(rspath.ctypes)
241 | ulist = fill(0.0, nctypes)
242 | for i in 1:nctypes
243 | if rspath.ctypes[i] == "R"
244 | ulist[i] = - MAX_STEER
245 | elseif rspath.ctypes[i] == "L"
246 | ulist[i] = MAX_STEER
247 | end
248 | end
249 |
250 | for i in 1:length(rspath.ctypes) - 1
251 | cost += STEER_CHANGE_COST*abs(ulist[i+1] - ulist[i])
252 | end
253 |
254 | # println("RS cost is ", cost)
255 | return cost
256 | end
257 |
258 |
259 | function analystic_expantion(n::Node, ngoal::Node, obmap::Array{Bool,2}, c::Config,
260 | kdtree::NearestNeighbors.KDTree,
261 | ox::Array{Float64},
262 | oy::Array{Float64}
263 | )
264 |
265 | sx = n.x[end]
266 | sy = n.y[end]
267 | syaw = n.yaw[end]
268 |
269 | max_curvature = tan(MAX_STEER)/WB
270 | path = reeds_shepp.calc_shortest_path(sx, sy, syaw,
271 | ngoal.x[end], ngoal.y[end], ngoal.yaw[end],
272 | max_curvature, step_size=MOTION_RESOLUTION)
273 |
274 | if path == nothing
275 | return nothing
276 | end
277 |
278 | if !collision_check.check_collision(path.x, path.y, path.yaw, kdtree, ox, oy)
279 | return nothing
280 | end
281 |
282 | # println(paths)
283 | return path # find good path
284 | end
285 |
286 |
287 | function calc_motion_inputs()
288 |
289 | up = [i for i in MAX_STEER/N_STEER:MAX_STEER/N_STEER:MAX_STEER]
290 | u = vcat([0.0], [i for i in up], [-i for i in up])
291 | d = vcat([1.0 for i in 1:length(u)], [-1.0 for i in 1:length(u)])
292 | u = vcat(u,u)
293 |
294 | return u, d
295 | end
296 |
297 |
298 | function verify_index(node::Node, obmap::Array{Bool,2}, c::Config,
299 | kdtree::NearestNeighbors.KDTree,
300 | ox::Array{Float64},
301 | oy::Array{Float64}
302 | )::Bool
303 |
304 | # overflow map
305 | if (node.xind - c.minx) >= c.xw
306 | return false
307 | elseif (node.xind - c.minx) <= 0
308 | return false
309 | end
310 | if (node.yind - c.miny) >= c.yw
311 | return false
312 | elseif (node.yind - c.miny) <= 0
313 | return false
314 | end
315 |
316 | # check collisiton
317 | # rectangle check
318 | if !collision_check.check_collision(node.x, node.y,node.yaw, kdtree, ox, oy)
319 | return false
320 | end
321 |
322 | return true #index is ok"
323 | end
324 |
325 |
326 | function pi_2_pi(iangle::Float64)
327 | while (iangle > pi)
328 | iangle -= 2.0 * pi
329 | end
330 | while (iangle < -pi)
331 | iangle += 2.0 * pi
332 | end
333 |
334 | return iangle
335 | end
336 |
337 |
338 | function calc_next_node(current::Node, c_id::Int64,
339 | u::Float64, d::Float64,
340 | c::Config,
341 | gkdtree::NearestNeighbors.KDTree)
342 |
343 |
344 | arc_l = XY_GRID_RESOLUTION
345 |
346 | nlist = round(Int64, arc_l/MOTION_RESOLUTION)+1
347 | xlist = fill(0.0, nlist)
348 | ylist = fill(0.0, nlist)
349 | yawlist = fill(0.0, nlist)
350 | xlist[1] = current.x[end] + d * MOTION_RESOLUTION*cos(current.yaw[end])
351 | ylist[1] = current.y[end] + d * MOTION_RESOLUTION*sin(current.yaw[end])
352 | yawlist[1] = pi_2_pi(current.yaw[end] + d*MOTION_RESOLUTION/WB * tan(u))
353 |
354 |
355 | for i in 1:(nlist - 1)
356 | xlist[i+1] = xlist[i] + d * MOTION_RESOLUTION*cos(yawlist[i])
357 | ylist[i+1] = ylist[i] + d * MOTION_RESOLUTION*sin(yawlist[i])
358 | yawlist[i+1] = pi_2_pi(yawlist[i] + d*MOTION_RESOLUTION/WB * tan(u))
359 | end
360 |
361 | xind = round(Int64, xlist[end]/c.xyreso)
362 | yind = round(Int64, ylist[end]/c.xyreso)
363 | yawind = round(Int64, yawlist[end]/c.yawreso)
364 |
365 | addedcost = 0.0
366 | if d > 0
367 | direction = true
368 | addedcost += abs(arc_l)
369 | else
370 | direction = false
371 | addedcost += abs(arc_l) * BACK_COST
372 | end
373 |
374 | # swich back penalty
375 | if direction != current.direction # switch back penalty
376 | addedcost += SB_COST
377 | end
378 |
379 | # steer penalyty
380 | addedcost += STEER_COST*abs(u)
381 |
382 | # steer change penalty
383 | addedcost += STEER_CHANGE_COST*abs(current.steer - u)
384 |
385 | cost = current.cost + addedcost
386 | node = Node(xind, yind, yawind, direction, xlist, ylist, yawlist, u, cost, c_id)
387 | # println(node)
388 |
389 | return node
390 | end
391 |
392 |
393 | function is_same_grid(node1::Node,node2::Node)
394 |
395 | if node1.xind != node2.xind
396 | return false
397 | end
398 | if node1.yind != node2.yind
399 | return false
400 | end
401 | if node1.yawind != node2.yawind
402 | return false
403 | end
404 |
405 | return true
406 |
407 | end
408 |
409 |
410 | function calc_index(node::Node, c::Config)
411 | ind = (node.yawind - c.minyaw)*c.xw*c.yw+(node.yind - c.miny)*c.xw + (node.xind - c.minx)
412 | if ind <= 0
413 | println("Error(calc_index):", ind)
414 | end
415 | return ind
416 | end
417 |
418 |
419 | function calc_holonomic_with_obstacle_heuristic(gnode::Node, ox::Array{Float64}, oy::Array{Float64}, xyreso::Float64)
420 | # println("Calc distance policy")
421 | h_dp = a_star.calc_dist_policy(gnode.x[end], gnode.y[end], ox, oy, xyreso, VEHICLE_RADIUS)
422 | return h_dp
423 | end
424 |
425 |
426 | function calc_nonholonomic_without_obstacle_heuristic(ngoal::Node,
427 | c::Config)
428 |
429 | h_rs = fill(0.0, (c.xw,c.yw,c.yaww))
430 | max_curvature = tan(MAX_STEER)/WB
431 |
432 | for ix in 1:c.xw
433 | for iy in 1:c.yw
434 | for iyaw in 1:c.yaww
435 | sx = (ix + c.minx)*c.xyreso
436 | sy = (iy + c.miny)*c.xyreso
437 | syaw = pi_2_pi((iyaw + c.minyaw)*c.yawreso)
438 | L = reeds_shepp.calc_shortest_path_length(sx, sy, syaw,
439 | ngoal.x[end], ngoal.y[end], ngoal.yaw[end],
440 | max_curvature, step_size=MOTION_RESOLUTION)
441 | h_rs[ix, iy, iyaw] = L
442 | end
443 | end
444 | end
445 |
446 | # println(h_rs[:,:,1])
447 |
448 | return h_rs
449 | end
450 |
451 |
452 | function calc_config(ox::Array{Float64}, oy::Array{Float64}, xyreso::Float64, yawreso::Float64, obreso::Float64)
453 | minx = round(Int64, minimum(ox)/xyreso)
454 | miny = round(Int64, minimum(oy)/xyreso)
455 | maxx = round(Int64, maximum(ox)/xyreso)
456 | maxy = round(Int64, maximum(oy)/xyreso)
457 | obminx = round(Int64, minimum(ox)/obreso)
458 | obminy = round(Int64, minimum(oy)/obreso)
459 | obmaxx = round(Int64, maximum(ox)/obreso)
460 | obmaxy = round(Int64, maximum(oy)/obreso)
461 | # println("minx:", minx)
462 | # println("miny:", miny)
463 | # println("maxx:", maxx)
464 | # println("maxy:", maxy)
465 |
466 | xw = round(Int64,(maxx - minx))
467 | yw = round(Int64,(maxy - miny))
468 | obxw = round(Int64,(obmaxx - obminx))
469 | obyw = round(Int64,(obmaxy - obminy))
470 |
471 | minyaw = round(Int64, - pi/yawreso) - 1
472 | maxyaw = round(Int64, pi/yawreso)
473 | yaww = round(Int64,(maxyaw - minyaw))
474 |
475 | config = Config(minx, miny, minyaw, maxx, maxy, maxyaw, xw, yw, yaww,
476 | xyreso, yawreso, obminx, obminy, obmaxx, obmaxy, obxw, obyw, obreso)
477 |
478 | return config
479 | end
480 |
481 |
482 | function calc_obstacle_map(ox::Array{Float64},
483 | oy::Array{Float64},
484 | c::Config)
485 |
486 | ox = [iox/c.obreso for iox in ox]
487 | oy = [ioy/c.obreso for ioy in oy]
488 |
489 | obmap = fill(false, (c.obxw, c.obyw))
490 |
491 | gkdtree = KDTree(hcat(ox, oy)')
492 | for ix in 1:c.obxw
493 | x = ix + c.obminx
494 | for iy in 1:c.obyw
495 | y = iy + c.obminy
496 | idxs, onedist = knn(gkdtree, [x, y] , 1)
497 | if onedist[1] <= VEHICLE_RADIUS/c.obreso
498 | obmap[ix,iy] = true
499 | end
500 | end
501 | end
502 |
503 | return obmap, gkdtree
504 | end
505 |
506 |
507 | function get_final_path(closed::Dict{Int64, Node},
508 | ngoal::Node,
509 | nstart::Node,
510 | c::Config)
511 |
512 | rx, ry, ryaw = Array{Float64}(ngoal.x),Array{Float64}(ngoal.y),Array{Float64}(ngoal.yaw)
513 | nid = calc_index(ngoal, c)
514 | # println("Final cost is ", closed[nid].cost)
515 | while true
516 | n = closed[nid]
517 | rx = vcat(rx, reverse(n.x))
518 | ry = vcat(ry, reverse(n.y))
519 | ryaw = vcat(ryaw, reverse(n.yaw))
520 | nid = n.pind
521 | if is_same_grid(n, nstart)
522 | # println("done")
523 | break
524 | end
525 | end
526 |
527 | rx = reverse(rx)
528 | ry = reverse(ry)
529 | ryaw = reverse(ryaw)
530 |
531 | dist = sum([sqrt(idx^2+idy^2) for (idx,idy) in zip(diff(rx), diff(ry))])
532 | # println("Final path distance is ", dist)
533 |
534 | return rx, ry, ryaw
535 | end
536 |
537 |
538 | function calc_cost(n::Node, h_rs::Array{Float64}, h_dp::Array{Float64}, ngoal::Node, c::Config)
539 |
540 | if length(h_rs) > 1 && length(h_dp) > 1 # Both heuristic cost are activated
541 | c_h_dp = h_dp[n.xind - c.minx, n.yind - c.miny]
542 | c_h_rs = h_rs[n.xind - c.minx, n.yind - c.miny, n.yawind - c.minyaw]
543 | return (n.cost + H_COST*max(c_h_dp, c_h_rs))
544 | elseif length(h_dp) > 1 # Distance policy heuristics is activated
545 | return (n.cost + H_COST*h_dp[n.xind - c.minx, n.yind - c.miny])
546 | elseif length(h_rs) > 1 # Reed Sheep path heuristics is activated
547 | return (n.cost + H_COST*h_rs[n.xind - c.minx, n.yind - c.miny, n.yawind - c.minyaw])
548 | end
549 |
550 | return (n.cost + H_COST*calc_euclid_dist(n.x[end] - ngoal.x[end],n.y[end] - ngoal.y[end], n.yaw[end] - ngoal.yaw[end]))
551 | end
552 |
553 |
554 | function calc_euclid_dist(x::Float64, y::Float64, yaw::Float64)
555 | """
556 | Heuristic cost function
557 | """
558 | if yaw >= pi
559 | yaw -= pi
560 | else yaw <= -pi
561 | yaw += pi
562 | end
563 | return sqrt(x^2+y^2+yaw^2)
564 | end
565 |
566 |
567 | function main()
568 | println(PROGRAM_FILE," start!!")
569 |
570 | sx = 20.0 # [m]
571 | sy = 20.0 # [m]
572 | syaw = deg2rad(90.0)
573 | gx = 180.0 # [m]
574 | gy = 100.0 # [m]
575 | gyaw = deg2rad(-90.0)
576 |
577 | ox = Float64[]
578 | oy = Float64[]
579 |
580 | for i in 0:200
581 | push!(ox, Float64(i))
582 | push!(oy, 0.0)
583 | end
584 | for i in 0:120
585 | push!(ox, 200.0)
586 | push!(oy, Float64(i))
587 | end
588 | for i in 0:200
589 | push!(ox, Float64(i))
590 | push!(oy, 120.0)
591 | end
592 | for i in 0:120
593 | push!(ox, 0.0)
594 | push!(oy, Float64(i))
595 | end
596 | for i in 0:80
597 | push!(ox, 40.0)
598 | push!(oy, Float64(i))
599 | end
600 | for i in 0:80
601 | push!(ox, 80.0)
602 | push!(oy, 120.0-Float64(i))
603 | end
604 | for i in 0:40
605 | push!(ox, 120.0)
606 | push!(oy, 120.0-Float64(i))
607 | push!(ox, 120.0)
608 | push!(oy, Float64(i))
609 | end
610 | for i in 0:80
611 | push!(ox, 160.0)
612 | push!(oy, 120.0-Float64(i))
613 | end
614 |
615 | @time rx, ry, ryaw = calc_hybrid_astar_path(sx, sy, syaw, gx, gy, gyaw, ox, oy, XY_GRID_RESOLUTION, YAW_GRID_RESOLUTION, OB_MAP_RESOLUTION)
616 |
617 | plot(ox, oy, ".k",label="obstacles")
618 | if rx != nothing
619 | plot(rx, ry, "-r",label="Hybrid A* path")
620 | end
621 |
622 | legend()
623 | grid(true)
624 | axis("equal")
625 |
626 | show()
627 |
628 | println(PROGRAM_FILE," Done!!")
629 | end
630 |
631 |
632 | if length(PROGRAM_FILE)!=0 &&
633 | contains(@__FILE__, PROGRAM_FILE)
634 |
635 | main()
636 | end
637 |
638 |
639 | end #module
640 |
641 |
--------------------------------------------------------------------------------
/images/TrajParallelHOBCA.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/XiaojingGeorgeZhang/H-OBCA/72503586429f81a4fd397edc5391db2f0308b8ed/images/TrajParallelHOBCA.gif
--------------------------------------------------------------------------------
/images/TrajReverseHOBCA.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/XiaojingGeorgeZhang/H-OBCA/72503586429f81a4fd397edc5391db2f0308b8ed/images/TrajReverseHOBCA.gif
--------------------------------------------------------------------------------
/main.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # Main file: computes Collision-Free and Minimum-Penetration trajectories for parking
28 | ###############
29 |
30 | # function defined in setup.jl
31 | clear()
32 | using PyCall
33 |
34 | close("all")
35 | ##################################################
36 |
37 | # define scenarios
38 | # scenario = "parallel"
39 | scenario = "backwards"
40 |
41 | # fixed or variable sampling time 1/0
42 | fixTime = 0 # default: 0 (variable time steps)
43 |
44 | #### problem parameters ####
45 | TsPF = 0.05
46 | if scenario == "backwards"
47 | # nominal sampling time
48 | sampleN = 3 # down-sampling from Hybrid A* to OBCA
49 | if fixTime == 1 #
50 | Ts = 0.65/3*sampleN # 0.65/3 must be compatible with motion resolution of Hybrid A* algorithm
51 | else
52 | Ts = 0.6/3*sampleN # 0.6/3 must be compatible with motion resolution of Hybrid A* algorithm
53 | end
54 | else
55 | sampleN = 3
56 | if fixTime == 1
57 | Ts = 0.95/3*sampleN # 0.95/3 must be compatible with motion resolution of Hybrid A* algorithm
58 | else
59 | Ts = 0.9/3*sampleN # 0.9/3 must be compatible with motion resolution of Hybrid A* algorithm
60 | end
61 | end
62 |
63 | # wheelbase
64 | L = 2.7
65 |
66 | motionStep = 0.1 # step length of Hybrid A*",
67 |
68 | # "nominal" shape of ego/controlled car, ego object is later rotated around the car center
69 | # center of rear wheel axis is reference point
70 | # size of car is: (x_upper + x_lower) + (y_upper + y_lower)
71 | # [x_upper, y_upper, -x_lower, -y_lower ]
72 | ego = [ 3.7 , 1 , 1 , 1 ]
73 |
74 | ##### define obstacles; for simplicity, only polyhedral obstacles are supported at this point
75 | # obstacles are defined by vertices, which are assumed to be enumerated in clock-wise direction
76 | # the first vertex must appear at the end of the list
77 |
78 | # for plotting
79 | nObPlot = 3 # number of obstacles
80 | vObPlot = [4 4 4] # number of vertices of each obstacle, vector of dimenion nOb
81 |
82 | # obstacle representation for optimization problem
83 | nOb = 3 # number of obstacles
84 | vOb = [3 3 2] # number of vertices of each obstacle, vector of dimenion nOb
85 | vObMPC = vOb-1 # adjustment for optimizaton problem
86 |
87 | if scenario == "backwards"
88 | println("Start Reverse Parking")
89 | elseif scenario == "parallel"
90 | println("Start Parallel Parking")
91 | else
92 | println("ERROR: please specify parking scenario")
93 | end
94 |
95 | # build environment
96 | if scenario == "backwards"
97 | # obstacles for backwards
98 | # [ [[obst1_x1;obst1_y1],[obst1_x2;obst1_y2],[obst1_x3;obst1_y4],...,[obst1_x1;obst1_y1]] , [[obst2_x1;obst2_y1],[obst2_x2;obst2_y2],[obst2_x3;obst2_y4],...,[obst2_x1;obst2_y1]] , ... ]
99 | lObPlot = [ [ [-20;5], [-1.3;5], [-1.3;-5], [-20;-5], [-20;5] ] ,
100 | [ [1.3;5], [20;5], [20;-5], [1.3;-5], [1.3;5] ] ,
101 | [ [-20;15], [20;15], [20;11], [-20,11], [-20;15] ] ] #vetices given in CLOCK-WISE direction
102 |
103 | # for optimization problem
104 | lOb = [ [ [-20;5], [-1.3;5], [-1.3;-5]] ,
105 | [ [1.3;-5] , [1.3;5] , [20;5] ] ,
106 | [ [20;11], [-20;11]] ] #vetices given in CLOCK-WISE direction
107 |
108 | # final state
109 | xF = [ 0 1.3 pi/2 0]
110 |
111 | # build obstacles for Hybrid A* algorithm
112 | ox = Float64[]
113 | oy = Float64[]
114 | # obstacle 1
115 | for i = -12:0.1:-1.3
116 | push!(ox, Float64(i))
117 | push!(oy, 5.0)
118 | end
119 | for i in -2:5
120 | push!(ox, -1.3)
121 | push!(oy, Float64(i))
122 | end
123 | # obstacle 2
124 | for i in -2:5
125 | push!(ox, 1.3)
126 | push!(oy, Float64(i))
127 | end
128 | for i = 1.3:0.1:12
129 | push!(ox, Float64(i))
130 | push!(oy, 5.0)
131 | end
132 | # obstacle 3
133 | for i = -12:12
134 | push!(ox, Float64(i))
135 | push!(oy, 11.0)
136 | end
137 |
138 | elseif scenario == "parallel"
139 | # obstacles for backwards
140 | # [ [[obst1_x1;obst1_y1],[obst1_x2;obst1_y2],[obst1_x3;obst1_y4],...,[obst1_x1;obst1_y1]] , [[obst2_x1;obst2_y1],[obst2_x2;obst2_y2],[obst2_x3;obst2_y4],...,[obst2_x1;obst2_y1]] , ... ]
141 | lObPlot = [ [ [-15;5], [-3;5], [-3;0], [-15;0], [-15;5] ] ,
142 | [ [3;5], [15;5], [15;0], [3;0], [3;5] ] ,
143 | [ [-3;0], [-3;2.5], [3;2.5], [3,0], [-3;0] ] ] #vetices given in CLOCK-WISE direction
144 | # obstacle representation for optimization problem
145 | lOb = [ [ [-20;5], [-3.;5], [-3.;0]] ,
146 | [ [3.;0] , [3.;5] , [20;5] ] ,
147 | [ [-3;2.5], [ 3;2.5]]] #vetices given in CLOCK-WISE direction
148 |
149 |
150 | # final state
151 | xF = [-L/2 4 0 0]
152 |
153 | # obstacles for Hybrid A* algorithms
154 | ox = Float64[]
155 | oy = Float64[]
156 | # obstacle 1
157 | for i in -12:0.1: -3.
158 | push!(ox,Float64(i))
159 | push!(oy,5.0)
160 | end
161 | for i in -2 : 5
162 | push!(ox,-3.0)
163 | push!(oy,Float64(i))
164 | end
165 | # obstacle 2
166 | for i in -3 : 3
167 | push!(ox,Float64(i))
168 | push!(oy,2.5)
169 | end
170 | # obstacle 3
171 | for i in -2 : 5
172 | push!(ox,3.0)
173 | push!(oy,Float64(i))
174 | end
175 | for i in 3 :0.1: 12
176 | push!(ox,Float64(i))
177 | push!(oy,5.0)
178 | end
179 | # obstacle 4
180 | for i in -12 : 12
181 | push!(ox,Float64(i))
182 | push!(oy,11.0)
183 | end
184 | end
185 |
186 | # [x_lower, x_upper, -y_lower, y_upper ]
187 | XYbounds = [ -15 , 15 , 1 , 10 ]
188 |
189 | # set initial state
190 | x0 = [-6 9.5 0.0 0.0]
191 | # x0 = [9 7 0.0 0.0]
192 |
193 | # call Hybrid A*
194 | tic()
195 | rx, ry, ryaw = hybrid_a_star.calc_hybrid_astar_path(x0[1], x0[2], x0[3], xF[1], xF[2], xF[3], ox, oy, hybrid_a_star.XY_GRID_RESOLUTION, hybrid_a_star.YAW_GRID_RESOLUTION, hybrid_a_star.OB_MAP_RESOLUTION)
196 | timeHybAstar = toq();
197 |
198 |
199 | ### extract (smooth) velocity profile from Hybrid A* solution ####
200 | rv = zeros(length(rx),1)
201 | for i=1:length(rx)
202 | if i < length(rx)
203 | rv[i] = (rx[i+1] - rx[i])/(Ts/sampleN)*cos(ryaw[i]) + (ry[i+1]-ry[i])/(Ts/sampleN)*sin(ryaw[i])
204 | else
205 | rv[i] = 0
206 | end
207 | end
208 | ### Smoothen velocity 0.3 m/s^2 max acceleration ###
209 | v,a = veloSmooth(rv,0.3,Ts/sampleN)
210 | ### compute steering angle ###
211 | delta = atan(diff(ryaw)*L/motionStep.*sign(v[1:end-1]));
212 |
213 | ### Down-sample for Warmstart ##########
214 | rx_sampled = rx[1:sampleN:end]
215 | ry_sampled = ry[1:sampleN:end]
216 | ryaw_sampled = ryaw[1:sampleN:end]
217 | v_sampled = v[1:sampleN:end]
218 |
219 | a_sampled = a[1:sampleN:end]
220 | delta_sampled = delta[1:sampleN:end]
221 |
222 | ## initialize warm start solution
223 | xWS = [rx_sampled ry_sampled ryaw_sampled v_sampled]
224 | uWS = [delta_sampled a_sampled]
225 |
226 | ### solve OBCA step ###
227 | N = length(rx_sampled)-1
228 | AOb, bOb = obstHrep(nOb, vOb, lOb) # obtain H-representation of obstacles
229 | xp10, up10, scaleTime10, exitflag10, time10, lp10, np10 = ParkingSignedDist(x0,xF,N,Ts,L,ego,XYbounds,nOb,vObMPC,AOb,bOb,fixTime,xWS,uWS)
230 |
231 |
232 | ### plot H-OBCA solution ###
233 | if exitflag10==1
234 | println("H-OBCA successfully completed.")
235 | figure(1)
236 | hold(1)
237 | plot(xp10[1,:],xp10[2,:],"b")
238 |
239 | plotTraj(xp10',up10',length(rx_sampled)-1,ego,L,nObPlot,vObPlot,lObPlot,"Trajectory generated by H-OBCA",1)
240 | else
241 | println(" WARNING: Problem could not be solved.")
242 | end
243 |
244 | ### comparison with Hybrid A* ###
245 | figure(2)
246 | title("Trajectory Comparison")
247 | hold(1)
248 | for j = 1 : nObPlot # plot obstacles
249 | for k = 1 : vObPlot[j]
250 | plot([lObPlot[j][k][1],lObPlot[j][k+1][1]] , [lObPlot[j][k][2],lObPlot[j][k+1][2]] ,"k")
251 | end
252 | end
253 | plot(xp10[1,:],xp10[2,:], "-b", label="H-OBCA")
254 | plot(rx, ry, "--r", label="Hybrid A*")
255 | plot(x0[1],x0[2],"ob")
256 | plot(xF[1],xF[2],"ob")
257 | legend()
258 | axis("equal")
259 |
260 | totTime = timeHybAstar+time10 # total execution time of H-OBCA
261 | println("Total run time: " , totTime, " s")
262 | println(" Hybrid A* time: ", timeHybAstar, " s")
263 | println(" optimization (OBCA) time: ", time10, " s")
--------------------------------------------------------------------------------
/obstHrep.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # Function computes H-representation for obstacles given their vertices
28 | # it is assumed that the vertices are given in CLOCK-WISE, and that the first vertex is repeated at the end of the vertex list
29 | ###############
30 |
31 |
32 |
33 | function obstHrep(nOb, vOb, lOb)
34 |
35 | # do simple checks
36 | if nOb != length(lOb)
37 | println("ERROR in number of obstacles")
38 | end
39 |
40 | # these matrices contain the H-rep
41 | A_all = zeros(sum(vOb)-nOb,2)
42 | b_all = zeros(sum(vOb)-nOb,1)
43 |
44 | # counter for lazy people
45 | lazyCounter = 1;
46 |
47 | for i = 1 : nOb # building H-rep
48 |
49 | A_i = zeros(vOb[i]-1,2)
50 | b_i = zeros(vOb[i]-1,1)
51 |
52 | # take two subsequent vertices, and compute hyperplane
53 | for j = 1 : vOb[i]-1
54 |
55 | # extract two vertices
56 | v1 = lOb[i][j] # vertex 1
57 | v2 = lOb[i][j+1] # vertex 2
58 |
59 | # find hyperplane passing through v1 and v2
60 | if v1[1] == v2[1] # perpendicular hyperplane, not captured by general formula
61 | if v2[2] < v1[2] # line goes "down"
62 | A_tmp = [1 0]
63 | b_tmp = v1[1]
64 | else
65 | A_tmp = [-1 0]
66 | b_tmp = -v1[1]
67 | end
68 | elseif v1[2] == v2[2] # horizontal hyperplane, captured by general formula but included for numerical stability
69 | if v1[1] < v2[1]
70 | A_tmp = [0 1]
71 | b_tmp = v1[2]
72 | else
73 | A_tmp = [0 -1]
74 | b_tmp = -v1[2]
75 | end
76 | else # general formula for non-horizontal and non-vertical hyperplanes
77 | ab = [v1[1] 1 ; v2[1] 1] \ [v1[2] ; v2[2]]
78 | a = ab[1]
79 | b = ab[2]
80 |
81 | if v1[1] < v2[1] # v1 --> v2 (line moves right)
82 | A_tmp = [-a 1]
83 | b_tmp = b
84 | else # v2 <-- v1 (line moves left)
85 | A_tmp = [a -1]
86 | b_tmp = -b
87 |
88 | end
89 | end
90 | # store vertices
91 | A_i[j,:] = A_tmp
92 | b_i[j] = b_tmp
93 | end
94 |
95 | # store everything
96 | A_all[lazyCounter : lazyCounter+vOb[i]-2,:] = A_i
97 | b_all[lazyCounter : lazyCounter+vOb[i]-2] = b_i
98 |
99 | # update counter
100 | lazyCounter = lazyCounter + vOb[i]-1
101 | end
102 |
103 | return A_all, b_all
104 |
105 | end
106 |
--------------------------------------------------------------------------------
/plotTraj.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # function plots trajectory
28 | ###############
29 |
30 |
31 | function plotTraj(xp,up,N,ego,L,nOb,vOb,lOb,disp_title,plotNumb)
32 |
33 | # obcenter1 = [(ob1[1]+ob1[3])/2-ob1[3];
34 | # (ob1[2]+ob1[4])/2-ob1[4]]
35 | #
36 | # obcenter2 = [(ob2[1]+ob2[3])/2-ob2[3];
37 | # (ob2[2]+ob2[4])/2-ob2[4]]
38 | #
39 | # obcenter3 = [(ob3[1]+ob3[3])/2-ob3[3];
40 | # (ob3[2]+ob3[4])/2-ob3[4]]
41 |
42 | W_ev = ego[2]+ego[4]
43 | L_ev = ego[1]+ego[3]
44 |
45 | # W_tv1 = ob1[2]+ob1[4]
46 | # L_tv1 = ob1[1]+ob1[3]
47 | #
48 | # W_tv2 = ob2[2]+ob2[4]
49 | # L_tv2 = ob2[1]+ob2[3]
50 | #
51 | # W_tv3 = ob3[2]+ob3[4]
52 | # L_tv3 = ob3[1]+ob3[3]
53 |
54 | up = [up ; zeros(1,2)] # final position no input
55 |
56 | w = W_ev/2;
57 | offset = L_ev/2 - ego[3]
58 |
59 | # initial state
60 | x0_s = xp[1,:]
61 | Rot0 = [cos(x0_s[3]) -sin(x0_s[3]); sin(x0_s[3]) cos(x0_s[3])]
62 | x0 = [x0_s[1]; x0_s[2]]
63 | centerCar0 = x0 + Rot0*[offset;0]
64 |
65 | # end state
66 | xF_s = xp[end,:]
67 | RotF = [cos(xF_s[3]) -sin(xF_s[3]); sin(xF_s[3]) cos(xF_s[3])]
68 | xF = [xF_s[1]; xF_s[2]]
69 | centerCarF = xF + RotF*[offset;0]
70 |
71 | for i = 1:1:N+1
72 |
73 | figure(plotNumb)
74 | plot(xp[1:i,1],xp[1:i,2],"b") # plot trajectory so far
75 | title(disp_title)
76 | hold(1)
77 |
78 | # plot trajectory
79 | for j = 1 : nOb
80 | for k = 1 : vOb[j]
81 | plot([lOb[j][k][1],lOb[j][k+1][1]] , [lOb[j][k][2],lOb[j][k+1][2]] ,"k")
82 | end
83 | end
84 |
85 | Rot = [cos(xp[i,3]) -sin(xp[i,3]);sin(xp[i,3]) cos(xp[i,3])]
86 |
87 | x_cur = [xp[i,1];
88 | xp[i,2]]
89 |
90 | centerCar = x_cur + Rot*[offset;0]
91 |
92 | carBox(centerCar,xp[i,3],W_ev/2,L_ev/2)
93 | carBox(x_cur + (Rot*[L;w-0.15]), xp[i,3] + up[i,1],0.15,0.3)
94 | carBox(x_cur + (Rot*[L;-w+0.15]),xp[i,3] + up[i,1],0.15,0.3)
95 | carBox(x_cur + (Rot*[0; w-0.15]) ,xp[i,3],0.15,0.3)
96 | carBox(x_cur + (Rot*[0;-w+0.15]) ,xp[i,3],0.15,0.3)
97 |
98 | # plot start position
99 | plot(x0[1],x0[2],"ob")
100 | carBox(centerCar0,x0_s[3],W_ev/2,L_ev/2)
101 | carBox(x0 + (Rot0*[L;w-0.15]) ,x0_s[3],0.15,0.3)
102 | carBox(x0 + (Rot0*[L;-w+0.15]) ,x0_s[3],0.15,0.3)
103 | carBox(x0 + (Rot0*[0; w-0.15]) ,x0_s[3], 0.15,0.3)
104 | carBox(x0 + (Rot0*[0;-w+0.15]) ,x0_s[3], 0.15,0.3)
105 |
106 | # plot end position
107 | carBox_dashed(centerCarF,xF_s[3],W_ev/2,L_ev/2)
108 | carBox_dashed(xF + (RotF*[L;w-0.15]) ,xF_s[3],0.15,0.3)
109 | carBox_dashed(xF + (RotF*[L;-w+0.15]) ,xF_s[3],0.15,0.3)
110 | carBox_dashed(xF + (RotF*[0; w-0.15]) ,xF_s[3], 0.15,0.3)
111 | carBox_dashed(xF + (RotF*[0;-w+0.15]) ,xF_s[3], 0.15,0.3)
112 | if i == N+1
113 | plot(xF[1],xF[2],"ob")
114 | end
115 |
116 | axis("equal")
117 |
118 | hold(0)
119 |
120 | sleep(0.05)
121 | end
122 | end
123 |
124 | # plot cars
125 | function carBox(x0,phi,w,l)
126 | car1 = x0[1:2] + [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w];
127 | car2 = x0[1:2] + [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w];
128 | car3 = x0[1:2] - [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w];
129 | car4 = x0[1:2] - [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w];
130 | plot([car1[1],car2[1],car4[1],car3[1],car1[1]],[car1[2],car2[2],car4[2],car3[2],car1[2]],"k")
131 | end
132 |
133 | # plot cars
134 | function carBox_dashed(x0,phi,w,l)
135 | car1 = x0[1:2] + [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w];
136 | car2 = x0[1:2] + [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w];
137 | car3 = x0[1:2] - [cos(phi)*l;sin(phi)*l] + [sin(phi)*w;-cos(phi)*w];
138 | car4 = x0[1:2] - [cos(phi)*l;sin(phi)*l] - [sin(phi)*w;-cos(phi)*w];
139 | plot([car1[1],car2[1],car4[1],car3[1],car1[1]],[car1[2],car2[2],car4[2],car3[2],car1[2]],":k")
140 | end
141 |
--------------------------------------------------------------------------------
/reeds_shepp.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
5 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
6 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # Reeds Shepp path planner
28 | ###############
29 |
30 |
31 | module reeds_shepp
32 |
33 | using PyPlot
34 |
35 | const STEP_SIZE = 0.1
36 |
37 | type Path
38 | lengths::Array{Float64} #lengths of each part of the path +: forward, -: backward
39 | ctypes::Array{String} # type of each part of the path
40 | L::Float64 # total path length
41 | x::Array{Float64} # final x positions [m]
42 | y::Array{Float64} # final y positions [m]
43 | yaw::Array{Float64} # final yaw angles [rad]
44 | directions::Array{Int8} # forward:1, backward:-1
45 | end
46 |
47 | function pi_2_pi(iangle::Float64)::Float64
48 | while (iangle > pi)
49 | iangle -= 2.0 * pi
50 | end
51 | while (iangle < -pi)
52 | iangle += 2.0 * pi
53 | end
54 |
55 | return iangle
56 | end
57 |
58 |
59 | function calc_shortest_path(sx::Float64, sy::Float64, syaw::Float64,
60 | gx::Float64, gy::Float64, gyaw::Float64,
61 | maxc::Float64;
62 | step_size::Float64 = STEP_SIZE)
63 | # println("Find Shortest Path")
64 | paths = calc_paths(sx,sy,syaw,gx,gy,gyaw,maxc,step_size=step_size)
65 |
66 | minL = Inf
67 | best_path_index = -1
68 | for i in 1:length(paths)
69 | if paths[i].L <= minL
70 | minL = paths[i].L
71 | best_path_index = i
72 | end
73 | end
74 |
75 | return paths[best_path_index]
76 | end
77 |
78 |
79 | function calc_shortest_path_length(sx::Float64, sy::Float64, syaw::Float64,
80 | gx::Float64, gy::Float64, gyaw::Float64,
81 | maxc::Float64;
82 | step_size::Float64 = STEP_SIZE)
83 | q0 = [sx, sy, syaw]
84 | q1 = [gx, gy, gyaw]
85 | paths = generate_path(q0, q1, maxc)
86 |
87 | minL = Inf
88 | for i in 1:length(paths)
89 | L = paths[i].L/maxc
90 | if L <= minL
91 | minL = L
92 | end
93 | end
94 |
95 | return minL
96 | end
97 |
98 |
99 | function calc_paths(sx::Float64, sy::Float64, syaw::Float64,
100 | gx::Float64, gy::Float64, gyaw::Float64,
101 | maxc::Float64; step_size::Float64 = STEP_SIZE)::Array{Path}
102 | q0 = [sx, sy, syaw]
103 | q1 = [gx, gy, gyaw]
104 |
105 | paths = generate_path(q0, q1, maxc)
106 | for path in paths
107 | x, y, yaw, directions = generate_local_course(path.L, path.lengths, path.ctypes, maxc, step_size*maxc)
108 |
109 | # convert global coordinate
110 | path.x = [cos(-q0[3]) * ix + sin(-q0[3]) * iy + q0[1] for (ix, iy) in zip(x, y)]
111 | path.y = [-sin(-q0[3]) * ix + cos(-q0[3]) * iy + q0[2] for (ix, iy) in zip(x, y)]
112 | path.yaw = pi_2_pi.([iyaw + q0[3] for iyaw in yaw])
113 | path.directions = directions
114 | path.lengths = [l/maxc for l in path.lengths]
115 | path.L = path.L/maxc
116 |
117 | end
118 |
119 | return paths
120 | end
121 |
122 |
123 | function get_label(path::Path)
124 | label =""
125 |
126 | for (m,l) in zip(path.ctypes, path.lengths)
127 | label = string(label, m)
128 | if l > 0.0
129 | label = string(label, "+")
130 | else
131 | label = string(label, "-")
132 | end
133 | end
134 |
135 | return label
136 | end
137 |
138 |
139 | function polar(x::Float64, y::Float64)
140 | r = sqrt(x^2+y^2)
141 | theta = atan2(y, x)
142 | return r, theta
143 | end
144 |
145 |
146 | function mod2pi(x::Float64)
147 | v = mod(x, 2.0*pi)
148 | if v < -pi
149 | v += 2.0*pi;
150 | else
151 | if v > pi
152 | v -= 2.0*pi
153 | end
154 | end
155 | return v
156 | end
157 |
158 |
159 | function LSL(x::Float64, y::Float64, phi::Float64)
160 | u, t = polar(x - sin(phi), y - 1.0 + cos(phi))
161 | if t >= 0.0
162 | v = mod2pi(phi - t)
163 | if (v >= 0.0)
164 | return true, t, u, v
165 | end
166 | end
167 |
168 | return false, 0.0, 0.0, 0.0
169 | end
170 |
171 |
172 | function LSR(x::Float64, y::Float64, phi::Float64)
173 | u1, t1 = polar(x + sin(phi), y - 1.0 - cos(phi))
174 | u1 = u1^2;
175 | if u1 >= 4.0
176 | u = sqrt(u1 - 4.0)
177 | theta = atan2(2.0, u)
178 | t = mod2pi(t1 + theta)
179 | v = mod2pi(t - phi)
180 |
181 | if t >= 0.0 && v >= 0.0
182 | return true, t, u, v
183 | end
184 | end
185 |
186 | return false, 0.0, 0.0, 0.0
187 | end
188 |
189 |
190 | function LRL(x::Float64, y::Float64, phi::Float64)
191 | u1, t1 = polar(x - sin(phi), y - 1.0 + cos(phi))
192 |
193 | if u1 <= 4.0
194 | u = -2.0*asin(0.25 * u1)
195 | t = mod2pi(t1 + 0.5 * u + pi);
196 | v = mod2pi(phi - t + u);
197 |
198 | if t >= 0.0 && u <= 0.0
199 | return true, t, u, v
200 | end
201 | end
202 |
203 | return false, 0.0, 0.0, 0.0
204 | end
205 |
206 |
207 | function set_path(paths::Array{Path}, lengths::Array{Float64}, ctypes::Array{String})
208 |
209 | path = Path([],[],0.0,[],[],[],[])
210 | path.ctypes = ctypes
211 | path.lengths = lengths
212 |
213 | # check same path exist
214 | for tpath in paths
215 | typeissame = (tpath.ctypes == path.ctypes)
216 | if typeissame
217 | if sum(tpath.lengths - path.lengths) <= 0.01
218 | return paths # not insert path
219 | end
220 | end
221 | end
222 |
223 | path.L = sum([abs(i) for i in lengths])
224 |
225 | Base.Test.@test path.L >= 0.01
226 |
227 | push!(paths, path)
228 |
229 | return paths
230 | end
231 |
232 |
233 | function SCS(x::Float64, y::Float64, phi::Float64, paths::Array{Path})::Array{Path}
234 | flag, t, u, v = SLS(x, y, phi)
235 | if flag
236 | # println("SCS1")
237 | paths = set_path(paths, [t, u, v], ["S","L","S"])
238 | end
239 | flag, t, u, v = SLS(x, -y, -phi)
240 | if flag
241 | # println("SCS2")
242 | paths = set_path(paths, [t, u, v], ["S","R","S"])
243 | end
244 |
245 | return paths
246 | end
247 |
248 |
249 | function SLS(x::Float64, y::Float64, phi::Float64)
250 | # println(x,",", y,",", phi, ",", mod2pi(phi))
251 | phi = mod2pi(phi)
252 | if y > 0.0 && phi > 0.0 && phi < pi*0.99
253 | xd = - y/tan(phi) + x
254 | t = xd - tan(phi/2.0)
255 | u = phi
256 | v = sqrt((x-xd)^2+y^2)-tan(phi/2.0)
257 | # println("1,",t,",",u,",",v)
258 | return true, t, u, v
259 | elseif y < 0.0 && phi > 0.0 && phi < pi*0.99
260 | xd = - y/tan(phi) + x
261 | t = xd - tan(phi/2.0)
262 | u = phi
263 | v = -sqrt((x-xd)^2+y^2)-tan(phi/2.0)
264 | # println("2,",t,",",u,",",v)
265 | return true, t, u, v
266 | end
267 |
268 | return false, 0.0, 0.0, 0.0
269 | end
270 |
271 |
272 | function CSC(x::Float64, y::Float64, phi::Float64, paths::Array{Path})
273 | flag, t, u, v = LSL(x, y, phi)
274 | if flag
275 | # println("CSC1")
276 | paths = set_path(paths, [t, u, v], ["L","S","L"])
277 | end
278 | flag, t, u, v = LSL(-x, y, -phi)
279 | if flag
280 | # println("CSC2")
281 | paths = set_path(paths, [-t, -u, -v], ["L","S","L"])
282 | end
283 | flag, t, u, v = LSL(x, -y, -phi)
284 | if flag
285 | # println("CSC3")
286 | paths = set_path(paths, [t, u, v], ["R","S","R"])
287 | end
288 | flag, t, u, v = LSL(-x, -y, phi)
289 | if flag
290 | # println("CSC4")
291 | paths = set_path(paths, [-t, -u, -v], ["R","S","R"])
292 | end
293 | flag, t, u, v = LSR(x, y, phi)
294 | if flag
295 | # println("CSC5")
296 | paths = set_path(paths, [t, u, v], ["L","S","R"])
297 | end
298 | flag, t, u, v = LSR(-x, y, -phi)
299 | if flag
300 | # println("CSC6")
301 | paths = set_path(paths, [-t, -u, -v], ["L","S","R"])
302 | end
303 | flag, t, u, v = LSR(x, -y, -phi)
304 | if flag
305 | # println("CSC7")
306 | paths = set_path(paths, [t, u, v], ["R","S","L"])
307 | end
308 | flag, t, u, v = LSR(-x, -y, phi)
309 | if flag
310 | # println("CSC8")
311 | paths = set_path(paths, [-t, -u, -v], ["R","S","L"])
312 | end
313 |
314 | return paths
315 | end
316 |
317 |
318 | function CCC(x::Float64, y::Float64, phi::Float64, paths::Array{Path})
319 |
320 | flag, t, u, v = LRL(x, y, phi)
321 | if flag
322 | # println("CCC1")
323 | paths = set_path(paths, [t, u, v], ["L","R","L"])
324 | end
325 | flag, t, u, v = LRL(-x, y, -phi)
326 | if flag
327 | # println("CCC2")
328 | paths = set_path(paths, [-t, -u, -v], ["L","R","L"])
329 | end
330 | flag, t, u, v = LRL(x, -y, -phi)
331 | if flag
332 | # println("CCC3")
333 | paths = set_path(paths, [t, u, v], ["R","L","R"])
334 | end
335 | flag, t, u, v = LRL(-x, -y, phi)
336 | if flag
337 | # println("CCC4")
338 | paths = set_path(paths, [-t, -u, -v], ["R","L","R"])
339 | end
340 |
341 | # backwards
342 | xb = x*cos(phi) + y*sin(phi)
343 | yb = x*sin(phi) - y*cos(phi)
344 | # println(xb, ",", yb,",",x,",",y)
345 |
346 | flag, t, u, v = LRL(xb, yb, phi)
347 | if flag
348 | # println("CCC5")
349 | paths = set_path(paths, [v, u, t], ["L","R","L"])
350 | end
351 | flag, t, u, v = LRL(-xb, yb, -phi)
352 | if flag
353 | # println("CCC6")
354 | paths = set_path(paths, [-v, -u, -t], ["L","R","L"])
355 | end
356 | flag, t, u, v = LRL(xb, -yb, -phi)
357 | if flag
358 | # println("CCC7")
359 | paths = set_path(paths, [v, u, t], ["R","L","R"])
360 | end
361 | flag, t, u, v = LRL(-xb, -yb, phi)
362 | if flag
363 | # println("CCC8")
364 | paths = set_path(paths, [-v, -u, -t], ["R","L","R"])
365 | end
366 |
367 | return paths
368 | end
369 |
370 |
371 | function calc_tauOmega(u::Float64, v::Float64, xi::Float64, eta::Float64, phi::Float64)
372 | delta = mod2pi(u-v)
373 | A = sin(u) - sin(delta)
374 | B = cos(u) - cos(delta) - 1.0
375 |
376 | t1 = atan2(eta*A - xi*B, xi*A + eta*B)
377 | t2 = 2.0 * (cos(delta) - cos(v) - cos(u)) + 3.0;
378 |
379 | if t2 < 0
380 | tau = mod2pi(t1+pi)
381 | else
382 | tau = mod2pi(t1)
383 | end
384 | omega = mod2pi(tau - u + v - phi)
385 |
386 | return tau, omega
387 | end
388 |
389 |
390 | function LRLRn(x::Float64, y::Float64, phi::Float64)
391 | xi = x + sin(phi)
392 | eta = y - 1.0 - cos(phi)
393 | rho = 0.25 * (2.0 + sqrt(xi*xi + eta*eta))
394 |
395 | if rho <= 1.0
396 | u = acos(rho)
397 | t, v = calc_tauOmega(u, -u, xi, eta, phi);
398 | if t >= 0.0 && v <= 0.0
399 | return true, t, u, v
400 | end
401 | end
402 |
403 | return false, 0.0, 0.0, 0.0
404 | end
405 |
406 |
407 | function LRLRp(x::Float64, y::Float64, phi::Float64)
408 | xi = x + sin(phi)
409 | eta = y - 1.0 - cos(phi)
410 | rho = (20.0 - xi*xi - eta*eta) / 16.0;
411 | # println(xi,",",eta,",",rho)
412 |
413 | if (rho>=0.0 && rho<=1.0)
414 | u = -acos(rho);
415 | if (u >= -0.5 * pi)
416 | t, v = calc_tauOmega(u, u, xi, eta, phi);
417 | if t >= 0.0 && v >= 0.0
418 | return true, t, u, v
419 | end
420 | end
421 | end
422 |
423 | return false, 0.0, 0.0, 0.0
424 | end
425 |
426 |
427 | function CCCC(x::Float64, y::Float64, phi::Float64, paths::Array{Path})
428 |
429 | flag, t, u, v = LRLRn(x, y, phi)
430 | if flag
431 | # println("CCCC1")
432 | paths = set_path(paths, [t, u, -u, v], ["L","R","L","R"])
433 | end
434 |
435 | flag, t, u, v = LRLRn(-x, y, -phi)
436 | if flag
437 | # println("CCCC2")
438 | paths = set_path(paths, [-t, -u, u, -v], ["L","R","L","R"])
439 | end
440 |
441 | flag, t, u, v = LRLRn(x, -y, -phi)
442 | if flag
443 | # println("CCCC3")
444 | paths = set_path(paths, [t, u, -u, v], ["R","L","R","L"])
445 | end
446 |
447 | flag, t, u, v = LRLRn(-x, -y, phi)
448 | if flag
449 | # println("CCCC4")
450 | paths = set_path(paths, [-t, -u, u, -v], ["R","L","R","L"])
451 | end
452 |
453 | flag, t, u, v = LRLRp(x, y, phi)
454 | if flag
455 | # println("CCCC5")
456 | paths = set_path(paths, [t, u, u, v], ["L","R","L","R"])
457 | end
458 |
459 | flag, t, u, v = LRLRp(-x, y, -phi)
460 | if flag
461 | # println("CCCC6")
462 | paths = set_path(paths, [-t, -u, -u, -v], ["L","R","L","R"])
463 | end
464 |
465 | flag, t, u, v = LRLRp(x, -y, -phi)
466 | if flag
467 | # println("CCCC7")
468 | paths = set_path(paths, [t, u, u, v], ["R","L","R","L"])
469 | end
470 |
471 | flag, t, u, v = LRLRp(-x, -y, phi)
472 | if flag
473 | # println("CCCC8")
474 | paths = set_path(paths, [-t, -u, -u, -v], ["R","L","R","L"])
475 | end
476 |
477 | return paths
478 | end
479 |
480 |
481 | function LRSR(x::Float64, y::Float64, phi::Float64)
482 |
483 | xi = x + sin(phi)
484 | eta = y - 1.0 - cos(phi)
485 | rho, theta = polar(-eta, xi)
486 |
487 | if rho >= 2.0
488 | t = theta
489 | u = 2.0 - rho
490 | v = mod2pi(t + 0.5*pi - phi)
491 | if t >= 0.0 && u <= 0.0 && v <=0.0
492 | return true, t, u, v
493 | end
494 | end
495 |
496 | return false, 0.0, 0.0, 0.0
497 | end
498 |
499 |
500 | function LRSL(x::Float64, y::Float64, phi::Float64)
501 | xi = x - sin(phi)
502 | eta = y - 1.0 + cos(phi)
503 | rho, theta = polar(xi, eta)
504 |
505 | if rho >= 2.0
506 | r = sqrt(rho*rho - 4.0);
507 | u = 2.0 - r;
508 | t = mod2pi(theta + atan2(r, -2.0));
509 | v = mod2pi(phi - 0.5*pi - t);
510 | if t >= 0.0 && u<=0.0 && v<=0.0
511 | return true, t, u, v
512 | end
513 | end
514 |
515 | return false, 0.0, 0.0, 0.0
516 | end
517 |
518 |
519 | function CCSC(x::Float64, y::Float64, phi::Float64, paths::Array{Path})
520 |
521 | flag, t, u, v = LRSL(x, y, phi)
522 | if flag
523 | # println("CCSC1")
524 | paths = set_path(paths, [t, -0.5*pi, u, v], ["L","R","S","L"])
525 | end
526 |
527 | flag, t, u, v = LRSL(-x, y, -phi)
528 | if flag
529 | # println("CCSC2")
530 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["L","R","S","L"])
531 | end
532 |
533 | flag, t, u, v = LRSL(x, -y, -phi)
534 | if flag
535 | # println("CCSC3")
536 | paths = set_path(paths, [t, -0.5*pi, u, v], ["R","L","S","R"])
537 | end
538 |
539 | flag, t, u, v = LRSL(-x, -y, phi)
540 | if flag
541 | # println("CCSC4")
542 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["R","L","S","R"])
543 | end
544 |
545 | flag, t, u, v = LRSR(x, y, phi)
546 | if flag
547 | # println("CCSC5")
548 | paths = set_path(paths, [t, -0.5*pi, u, v], ["L","R","S","R"])
549 | end
550 |
551 | flag, t, u, v = LRSR(-x, y, -phi)
552 | if flag
553 | # println("CCSC6")
554 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["L","R","S","R"])
555 | end
556 |
557 | flag, t, u, v = LRSR(x, -y, -phi)
558 | if flag
559 | # println("CCSC7")
560 | paths = set_path(paths, [t, -0.5*pi, u, v], ["R","L","S","L"])
561 | end
562 |
563 | flag, t, u, v = LRSR(-x, -y, phi)
564 | if flag
565 | # println("CCSC8")
566 | paths = set_path(paths, [-t, 0.5*pi, -u, -v], ["R","L","S","L"])
567 | end
568 |
569 | # backwards
570 | xb = x*cos(phi) + y*sin(phi)
571 | yb = x*sin(phi) - y*cos(phi)
572 | flag, t, u, v = LRSL(xb, yb, phi)
573 | if flag
574 | # println("CCSC9")
575 | paths = set_path(paths, [v, u, -0.5*pi, t], ["L","S","R","L"])
576 | end
577 |
578 | flag, t, u, v = LRSL(-xb, yb, -phi)
579 | if flag
580 | # println("CCSC10")
581 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["L","S","R","L"])
582 | end
583 |
584 | flag, t, u, v = LRSL(xb, -yb, -phi)
585 | if flag
586 | # println("CCSC11")
587 | paths = set_path(paths, [v, u, -0.5*pi, t], ["R","S","L","R"])
588 | end
589 |
590 | flag, t, u, v = LRSL(-xb, -yb, phi)
591 | if flag
592 | # println("CCSC12")
593 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["R","S","L","R"])
594 | end
595 |
596 | flag, t, u, v = LRSR(xb, yb, phi)
597 | if flag
598 | # println("CCSC13")
599 | paths = set_path(paths, [v, u, -0.5*pi, t], ["R","S","R","L"])
600 | end
601 |
602 | flag, t, u, v = LRSR(-xb, yb, -phi)
603 | if flag
604 | # println("CCSC14")
605 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["R","S","R","L"])
606 | end
607 |
608 | flag, t, u, v = LRSR(xb, -yb, -phi)
609 | if flag
610 | # println("CCSC15")
611 | paths = set_path(paths, [v, u, -0.5*pi, t], ["L","S","L","R"])
612 | end
613 |
614 | flag, t, u, v = LRSR(-xb, -yb, phi)
615 | if flag
616 | # println("CCSC16")
617 | paths = set_path(paths, [-v, -u, 0.5*pi, -t], ["L","S","L","R"])
618 | end
619 |
620 | return paths
621 | end
622 |
623 |
624 | function LRSLR(x::Float64, y::Float64, phi::Float64)
625 | # formula 8.11 *** TYPO IN PAPER ***
626 | xi = x + sin(phi)
627 | eta = y - 1.0 - cos(phi)
628 | rho, theta = polar(xi, eta)
629 | if rho >= 2.0
630 | u = 4.0 - sqrt(rho*rho - 4.0)
631 | if u <= 0.0
632 | t = mod2pi(atan2((4.0-u)*xi -2.0*eta, -2.0*xi + (u-4.0)*eta));
633 | v = mod2pi(t - phi);
634 |
635 | if t >= 0.0 && v >=0.0
636 | return true, t, u, v
637 | end
638 | end
639 | end
640 |
641 | return false, 0.0, 0.0, 0.0
642 | end
643 |
644 |
645 | function CCSCC(x::Float64, y::Float64, phi::Float64, paths::Array{Path})
646 | flag, t, u, v = LRSLR(x, y, phi)
647 | if flag
648 | # println("CCSCC1")
649 | paths = set_path(paths, [t, -0.5*pi, u, -0.5*pi, v], ["L","R","S","L","R"])
650 | end
651 | flag, t, u, v = LRSLR(-x, y, -phi)
652 | if flag
653 | # println("CCSCC2")
654 | paths = set_path(paths, [-t, 0.5*pi, -u, 0.5*pi, -v], ["L","R","S","L","R"])
655 | end
656 |
657 | flag, t, u, v = LRSLR(x, -y, -phi)
658 | if flag
659 | # println("CCSCC3")
660 | paths = set_path(paths, [t, -0.5*pi, u, -0.5*pi, v], ["R","L","S","R","L"])
661 | end
662 |
663 | flag, t, u, v = LRSLR(-x, -y, phi)
664 | if flag
665 | # println("CCSCC4")
666 | paths = set_path(paths, [-t, 0.5*pi, -u, 0.5*pi, -v], ["R","L","S","R","L"])
667 | end
668 |
669 | return paths
670 | end
671 |
672 |
673 | function generate_local_course(L::Float64,
674 | lengths::Array{Float64},
675 | mode::Array{String},
676 | maxc::Float64,
677 | step_size::Float64)
678 | npoint = trunc(Int64, L/step_size) + length(lengths)+3
679 | # println(npoint, ",", L, ",", step_size, ",", L/step_size)
680 |
681 | px = fill(0.0, npoint)
682 | py = fill(0.0, npoint)
683 | pyaw = fill(0.0, npoint)
684 | directions = fill(0, npoint)
685 | ind = 2
686 |
687 | if lengths[1] > 0.0
688 | directions[1] = 1
689 | else
690 | directions[1] = -1
691 | end
692 |
693 | if lengths[1] > 0.0
694 | d = step_size
695 | else
696 | d = -step_size
697 | end
698 |
699 | pd = d
700 | ll = 0.0
701 |
702 | for (m, l, i) in zip(mode, lengths, 1:length(mode))
703 |
704 | if l > 0.0
705 | d = step_size
706 | else
707 | d = -step_size
708 | end
709 |
710 | # set prigin state
711 | ox, oy, oyaw = px[ind], py[ind], pyaw[ind]
712 |
713 | ind -= 1
714 | if i >= 2 && (lengths[i-1]*lengths[i])>0
715 | pd = - d - ll
716 | else
717 | pd = d - ll
718 | end
719 |
720 | while abs(pd) <= abs(l)
721 | ind += 1
722 | px, py, pyaw, directions = interpolate(ind, pd, m, maxc, ox, oy, oyaw, px, py, pyaw, directions)
723 | pd += d
724 | end
725 |
726 | ll = l - pd - d # calc remain length
727 |
728 | ind += 1
729 | px, py, pyaw, directions = interpolate(ind, l, m, maxc, ox, oy, oyaw, px, py, pyaw, directions)
730 | end
731 |
732 | #remove unused data
733 | while px[end] == 0.0
734 | pop!(px)
735 | pop!(py)
736 | pop!(pyaw)
737 | pop!(directions)
738 | end
739 |
740 | return px, py, pyaw, directions
741 | end
742 |
743 |
744 | function interpolate(ind::Int64, l::Float64, m::String, maxc::Float64,
745 | ox::Float64, oy::Float64, oyaw::Float64,
746 | px::Array{Float64}, py::Array{Float64}, pyaw::Array{Float64},
747 | directions::Array{Int64})
748 |
749 | if m == "S"
750 | px[ind] = ox + l / maxc * cos(oyaw)
751 | py[ind] = oy + l / maxc * sin(oyaw)
752 | pyaw[ind] = oyaw
753 | else # curve
754 | ldx = sin(l) / maxc
755 | if m == "L" # left turn
756 | ldy = (1.0 - cos(l)) / maxc
757 | elseif m == "R" # right turn
758 | ldy = (1.0 - cos(l)) / -maxc
759 | end
760 | gdx = cos(-oyaw) * ldx + sin(-oyaw) * ldy
761 | gdy = -sin(-oyaw) * ldx + cos(-oyaw) * ldy
762 | px[ind] = ox + gdx
763 | py[ind] = oy + gdy
764 | end
765 |
766 | if m == "L" # left turn
767 | pyaw[ind] = oyaw + l
768 | elseif m == "R" # right turn
769 | pyaw[ind] = oyaw - l
770 | end
771 |
772 | if l > 0.0
773 | directions[ind] = 1
774 | else
775 | directions[ind] = -1
776 | end
777 |
778 | return px, py, pyaw, directions
779 | end
780 |
781 |
782 | function generate_path(q0::Array{Float64}, q1::Array{Float64}, maxc::Float64)::Array{Path}
783 | dx = q1[1] - q0[1]
784 | dy = q1[2] - q0[2]
785 | dth = q1[3] - q0[3]
786 | c = cos(q0[3])
787 | s = sin(q0[3]);
788 | x = (c*dx + s*dy)*maxc
789 | y = (-s*dx + c*dy)*maxc
790 |
791 | paths = Path[]
792 | paths = SCS(x, y, dth, paths)
793 | paths = CSC(x, y, dth, paths)
794 | paths = CCC(x, y, dth, paths)
795 | paths = CCCC(x, y, dth, paths)
796 | paths = CCSC(x, y, dth, paths)
797 | paths = CCSCC(x, y, dth, paths)
798 |
799 | return paths
800 | end
801 |
802 |
803 | function calc_curvature(x,y,yaw, directions)
804 |
805 | c = Float64[]
806 | ds = Float64[]
807 |
808 | for i in 2:length(x)-1
809 | dxn = x[i]-x[i-1]
810 | dxp = x[i+1]-x[i]
811 | dyn = y[i]-y[i-1]
812 | dyp = y[i+1]-y[i]
813 | dn =sqrt(dxn^2.0+dyn^2.0)
814 | dp =sqrt(dxp^2.0+dyp^2.0)
815 | dx = 1.0/(dn+dp)*(dp/dn*dxn+dn/dp*dxp)
816 | ddx = 2.0/(dn+dp)*(dxp/dp-dxn/dn)
817 | dy = 1.0/(dn+dp)*(dp/dn*dyn+dn/dp*dyp)
818 | ddy = 2.0/(dn+dp)*(dyp/dp-dyn/dn)
819 | curvature = (ddy*dx-ddx*dy)/(dx^2+dy^2)
820 | d = (dn+dp)/2.0
821 |
822 | if isnan(curvature)
823 | curvature = 0.0
824 | end
825 |
826 | if directions[i] <= 0.0
827 | curvature = -curvature
828 | end
829 |
830 | if length(c) == 0
831 | push!(ds, d)
832 | push!(c, curvature)
833 | end
834 |
835 | push!(ds, d)
836 | push!(c, curvature)
837 | end
838 |
839 | push!(ds, ds[end])
840 | push!(c, c[end] )
841 |
842 | return c, ds
843 | end
844 |
845 |
846 | function check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
847 | # println("Test")
848 | # println(start_x,",", start_y, "," ,start_yaw, ",", max_curvature)
849 | paths = calc_paths(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
850 |
851 | Base.Test.@test length(paths) >= 1
852 |
853 | for path in paths
854 | Base.Test.@test abs(path.x[1] - start_x) <= 0.01
855 | Base.Test.@test abs(path.y[1] - start_y) <= 0.01
856 | Base.Test.@test abs(path.yaw[1] - start_yaw) <= 0.01
857 | Base.Test.@test abs(path.x[end] - end_x) <= 0.01
858 | Base.Test.@test abs(path.y[end] - end_y) <= 0.01
859 | Base.Test.@test abs(path.yaw[end] - end_yaw) <= 0.01
860 |
861 | #course distance check
862 | d = [sqrt(dx^2+dy^2) for (dx, dy) in zip(diff(path.x[1:end-1]), diff(path.y[1:end-1]))]
863 |
864 | for i in length(d)
865 | Base.Test.@test abs(d[i] - STEP_SIZE) <= 0.001
866 | end
867 | end
868 |
869 | end
870 |
871 | function test()
872 | println("Test1")
873 | start_x = 0.0 # [m]
874 | start_y = 0.0 # [m]
875 | start_yaw = deg2rad(10.0) # [rad]
876 | end_x = 7.0 # [m]
877 | end_y = -8.0 # [m]
878 | end_yaw = deg2rad(50.0) # [rad]
879 | max_curvature = 2.0
880 |
881 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
882 |
883 | start_x = 0.0 # [m]
884 | start_y = 0.0 # [m]
885 | start_yaw = deg2rad(10.0) # [rad]
886 | end_x = 7.0 # [m]
887 | end_y = -8.0 # [m]
888 | end_yaw = deg2rad(-50.0) # [rad]
889 | max_curvature = 2.0
890 |
891 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
892 |
893 | start_x = 0.0 # [m]
894 | start_y = 10.0 # [m]
895 | start_yaw = deg2rad(-10.0) # [rad]
896 | end_x = -7.0 # [m]
897 | end_y = -8.0 # [m]
898 | end_yaw = deg2rad(-50.0) # [rad]
899 | max_curvature = 2.0
900 |
901 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
902 |
903 | start_x = 0.0 # [m]
904 | start_y = 10.0 # [m]
905 | start_yaw = deg2rad(-10.0) # [rad]
906 | end_x = -7.0 # [m]
907 | end_y = -8.0 # [m]
908 | end_yaw = deg2rad(150.0) # [rad]
909 | max_curvature = 1.0
910 |
911 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
912 |
913 | start_x = 0.0 # [m]
914 | start_y = 10.0 # [m]
915 | start_yaw = deg2rad(-10.0) # [rad]
916 | end_x = 7.0 # [m]
917 | end_y = 8.0 # [m]
918 | end_yaw = deg2rad(150.0) # [rad]
919 | max_curvature = 2.0
920 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
921 |
922 | start_x = -40.0 # [m]
923 | start_y = 549.0 # [m]
924 | start_yaw = 2.44346 # [rad]
925 | end_x = 36.0 # [m]
926 | end_y = 446.0 # [m]
927 | end_yaw = -0.698132
928 | max_curvature = 0.05890904077226434
929 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
930 |
931 | # Random test
932 | for i in 1:100
933 | start_x = rand()*100.0 - 50.0
934 | start_y = rand()*100.0 - 50.0
935 | start_yaw = deg2rad(rand()*360.0 - 180.0)
936 | end_x = rand()*100.0 - 50.0
937 | end_y = rand()*100.0 - 50.0
938 | end_yaw = deg2rad(rand()*360.0 - 180.0)
939 | max_curvature = rand()/10.0
940 | # println(i, ",", start_x, ",", start_y,",", start_yaw,",",end_x,",",end_y,",", end_yaw)
941 | check_path(start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
942 | end
943 | end
944 |
945 |
946 | function main()
947 | println(PROGRAM_FILE," start!!")
948 | test()
949 |
950 | start_x = 3.0 # [m]
951 | start_y = 10.0 # [m]
952 | start_yaw = deg2rad(40.0) # [rad]
953 | end_x = 0.0 # [m]
954 | end_y = 1.0 # [m]
955 | end_yaw = deg2rad(0.0) # [rad]
956 | max_curvature = 0.1
957 |
958 | @time bpath = calc_shortest_path(
959 | start_x, start_y, start_yaw, end_x, end_y, end_yaw, max_curvature)
960 |
961 | rc, rds = calc_curvature(bpath.x, bpath.y, bpath.yaw, bpath.directions)
962 |
963 | subplots(1)
964 | plot(bpath.x, bpath.y,"-r", label=get_label(bpath))
965 |
966 | plot(start_x, start_y)
967 | plot(end_x, end_y)
968 |
969 | legend()
970 | grid(true)
971 | axis("equal")
972 |
973 | subplots(1)
974 | plot(rc, ".r", label="reeds shepp")
975 | grid(true)
976 | title("Curvature")
977 |
978 | show()
979 |
980 | println(PROGRAM_FILE," Done!!")
981 | end
982 |
983 |
984 | if length(PROGRAM_FILE)!=0 &&
985 | contains(@__FILE__, PROGRAM_FILE)
986 |
987 | main()
988 | end
989 |
990 | end #module
991 |
992 |
--------------------------------------------------------------------------------
/setup.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # setup.jl: run this file before running main.jl
28 | ###############
29 |
30 | ##############################
31 | # include JuMP -> Optimization modeling tool
32 | # include IPOPT -> Interior point based nonlinear solver
33 | # include PyPlot -> Ploting library (matplotlib python)
34 | ##############################
35 | using JuMP, Ipopt, PyPlot, NearestNeighbors, ControlSystems
36 | ##############################
37 | # register warm start function
38 | ##############################
39 | # register Distance and Signeddistance
40 | # parking optimal control problems
41 |
42 | include("ParkingSignedDist.jl")
43 | include("ParkingConstraints.jl")
44 | ##############################
45 | # register polytope converter
46 | include("obstHrep.jl")
47 | ##############################
48 | # register ploting function
49 | include("plotTraj.jl")
50 | include("hybrid_a_star.jl")
51 | ##############################
52 | include("DualMultWS.jl")
53 | include("veloSmooth.jl")
54 |
55 | # function that clears terminal output
56 | clear() = run(@static is_unix() ? `clear` : `cmd /c cls`)
57 |
58 |
--------------------------------------------------------------------------------
/veloSmooth.jl:
--------------------------------------------------------------------------------
1 | ###############
2 | # H-OBCA: Hierarchical Optimization-based Collision Avoidance - a path planner for autonomous parking
3 | # Copyright (C) 2018
4 | # Alexander LINIGER [liniger@control.ee.ethz.ch; Automatic Control Lab, ETH Zurich]
5 | # Xiaojing ZHANG [xiaojing.zhang@berkeley.edu; MPC Lab, UC Berkeley]
6 | # Atsushi SAKAI [atsushisakai@global.komatsu; Komatsu Ltd / MPC Lab]
7 | #
8 | # This program is free software: you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation, either version 3 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # This program is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | #
18 | # You should have received a copy of the GNU General Public License
19 | # along with this program. If not, see .
20 | ###############
21 | # The paper describing the theory can be found here:
22 | # X. Zhang, A. Liniger and F. Borrelli; "Optimization-Based Collision Avoidance"; Technical Report, 2017, [https://arxiv.org/abs/1711.03449]
23 | # X. Zhang, A. Liniger, A. Sakai and F. Borrelli; "Autonomous Parking using Optimization-Based Collision Avoidance"; Technical Report, 2018 [add URL]
24 | ###############
25 |
26 | ###############
27 | # veloSmooth: a velocity smoother
28 | ###############
29 |
30 |
31 | function veloSmooth(v,amax,Ts)
32 | v_ex = zeros(length(v)+40,1)
33 | v_bar = zeros(4,length(v)+40)
34 | v_bar2 = zeros(4,length(v)+40)
35 | v_barMM = zeros(1,length(v))
36 |
37 | for i = 1:length(v)
38 | for j = 1:4
39 | v_bar[j,i+19] = v[i];
40 | v_ex[i+19] = v[i];
41 | end
42 | end
43 |
44 | v_cut1 = 0.25*abs(v[1])
45 | v_cut2 = 0.25*abs(v[1])+abs(v[1])
46 |
47 | accPhase = Int(round(abs(v[1])/amax/Ts))
48 |
49 | index1 = find((diff(v_ex).>v_cut1) & (diff(v_ex).v_cut2)
51 |
52 | index3 = find((diff(v_ex).<-v_cut1) & (diff(v_ex).>-v_cut2))
53 | index4 = find(diff(v_ex).<-v_cut2)
54 |
55 | if length(index1) >=1 && index1[1]==19
56 | index1[1] = index1[1]+1
57 | end
58 | if length(index3) >=1 && index3[1]==19
59 | index3[1] = index3[1]+1
60 | end
61 |
62 |
63 | for j = 1:length(index1)
64 | if v_ex[index1[j]] > v_cut1 || v_ex[index1[j]+1] > v_cut1
65 | v_bar[1,index1[j]:index1[j]+accPhase] = linspace(0,abs(v[1]),accPhase+1)''
66 | elseif v_ex[index1[j]] < -v_cut1 || v_ex[index1[j]+1] < -v_cut1
67 | v_bar[1,index1[j]-accPhase+1:index1[j]+1] = linspace(-abs(v[1]),0,accPhase+1)''
68 | end
69 | end
70 |
71 | for j = 1:length(index3)
72 | if v_ex[index3[j]] > v_cut1 || v_ex[index3[j]+1] > v_cut1
73 | v_bar[2,index3[j]-accPhase+1:index3[j]+1] = linspace(abs(v[1]),0,accPhase+1)''
74 | elseif v_ex[index3[j]] < -v_cut1 || v_ex[index3[j]+1] < -v_cut1
75 | v_bar[2,index3[j]:index3[j]+accPhase] = linspace(0,-abs(v[1]),accPhase+1)''
76 | end
77 | end
78 |
79 | for j = 1:length(index2)
80 | v_bar[3,index2[j]-accPhase:index2[j]+accPhase] = linspace(-abs(v[1]),abs(v[1]),2*accPhase+1)''
81 | end
82 |
83 | for j = 1:length(index4)
84 | v_bar[4,index4[j]-accPhase:index4[j]+accPhase] = linspace(abs(v[1]),-abs(v[1]),2*accPhase+1)''
85 | end
86 |
87 | for i = 20:length(v)+19
88 | for j = 1:4
89 | if v_bar[j,i] == 0
90 | v_bar2[j,i] = v_bar[j,i]
91 | elseif sign(v_ex[i]) != sign(v_bar[j,i])
92 | v_bar2[j,i] = v_ex[i]
93 | else
94 | v_bar2[j,i] = v_bar[j,i]
95 | end
96 | end
97 | end
98 |
99 | for i = 20:length(v)+19
100 | if v_ex[i] > 0
101 | v_barMM[i-19] = minimum(v_bar2[:,i])
102 | else
103 | v_barMM[i-19] = maximum(v_bar2[:,i])
104 | end
105 | end
106 |
107 | a = diff(v_barMM')./Ts
108 |
109 | return v_barMM', a
110 |
111 | end
112 |
--------------------------------------------------------------------------------