-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
136 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
% MAIN - Minimum Time Boundary Value Problem | ||
% | ||
% Solve a minimum-time boundary value problem with simple dynamics (chain | ||
% integrator) and limits on the state and control. Scalar trajectory. | ||
% | ||
% Here we will solve a scalar trajectory, where the position, velocity, | ||
% and acceleration are states. The jerk (derivative of acceleration) will | ||
% be the only control. | ||
% | ||
|
||
clc; clear; | ||
addpath ../../ | ||
|
||
% Kinematic Limits: | ||
xLim = [0, 4]; % position | ||
vLim = [-2, 2]; % velocity | ||
aLim = [-4, 4]; % acceleration | ||
jLim = 5*[-8, 8]; % jerk | ||
|
||
% Boundary value problem: | ||
xBegin = xLim(1); % initial state | ||
vBegin = 0; | ||
aBegin = 0; | ||
xFinal = xLim(2); % final state | ||
vFinal = 0; | ||
aFinal = 0; | ||
|
||
% User-defined dynamics and objective functions | ||
problem.func.dynamics = @(t,x,u)( scalarChainIntegrator(x,u) ); | ||
problem.func.bndObj = @(t0,x0,tF,xF)( tF - t0 ); % minimum time -- primary objective | ||
problem.func.pathObj = @(t,x,u)( 0.001*u.^2 ); %minimum jerk -- regularization | ||
|
||
% Problem boundsTime | ||
problem.bounds.initialTime.low = 0; | ||
problem.bounds.initialTime.upp = 0; | ||
problem.bounds.finalTime.low = 0.1; | ||
problem.bounds.finalTime.upp = 10; | ||
|
||
problem.bounds.state.low = [xLim(1); vLim(1); aLim(1)]; | ||
problem.bounds.state.upp = [xLim(2); vLim(2); aLim(2)]; | ||
problem.bounds.initialState.low = [xBegin; vBegin; aBegin]; | ||
problem.bounds.initialState.upp = [xBegin; vBegin; aBegin]; | ||
problem.bounds.finalState.low = [xFinal; vFinal; aFinal]; | ||
problem.bounds.finalState.upp = [xFinal; vFinal; aFinal]; | ||
|
||
problem.bounds.control.low = jLim(1); | ||
problem.bounds.control.upp = jLim(2); | ||
|
||
% Guess at the initial trajectory | ||
problem.guess.time = [0,2]; | ||
problem.guess.state = [[xBegin; vBegin; aBegin], [xFinal; vFinal; aFinal]]; | ||
problem.guess.control = [0, 0]; | ||
|
||
% Select a solver: | ||
problem.options(1).method = 'trapezoid'; | ||
problem.options(1).trapezoid.nGrid = 8; | ||
problem.options(2).method = 'trapezoid'; | ||
problem.options(2).trapezoid.nGrid = 16; | ||
problem.options(3).method = 'hermiteSimpson'; | ||
problem.options(3).hermiteSimpson.nSegment = 15; | ||
|
||
% Solve the problem | ||
soln = optimTraj(problem); | ||
t = soln(end).grid.time; | ||
q = soln(end).grid.state(1,:); | ||
dq = soln(end).grid.state(2,:); | ||
ddq = soln(end).grid.state(3,:); | ||
u = soln(end).grid.control; | ||
|
||
% Plot the solution: | ||
figure(1); clf; | ||
|
||
subplot(4,1,1) | ||
plot(t,q) | ||
ylabel('q') | ||
title('Minimum-time boundary value problem'); | ||
|
||
subplot(4,1,2) | ||
plot(t,dq) | ||
ylabel('dq') | ||
|
||
subplot(4,1,3) | ||
plot(t,ddq) | ||
ylabel('ddq') | ||
|
||
subplot(4,1,4) | ||
plot(t,u) | ||
ylabel('dddq') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# README -- Minimum-Time Boundary Value Problem | ||
|
||
This example show how to compute the a minimum-time solution for boundary value problem with kinematic limits: position, velocity, acceleration, and jerk. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function dx = scalarChainIntegrator(x,u) | ||
% dx = scalarChainIntegrator(x,u) | ||
% | ||
% Computes the dynamics for a scalar chain integrator: | ||
% dx(1) = x(2) | ||
% dx(2) = x(3) | ||
% ... | ||
% dx(n) = u | ||
% | ||
|
||
if size(x,1) == 1 | ||
dx = u; | ||
else | ||
dx = [x(2:end, :); u]; | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,39 @@ | ||
# README.txt -- OptimTraj/demo | ||
# README.txt -- `OptimTraj/demo` | ||
|
||
This directory contains a collection of example problems for trajectory optimization, all solved using OptimTraj. Each example contains a MAIN.m file, which is the entry-point file for the example. | ||
This directory contains a collection of example problems for trajectory optimization, all solved using `OptimTraj`. Each example contains a MAIN.m file, which is the entry-point file for the example. | ||
|
||
If the dynamics or constraints are complicated, then a script Derive_*.m is provided to use the symbolic toolbox to derive these equations. Any files with the autoGen_fileName.m are created by the Matlab symbolic toolbox, and should not be edited. In each case, they will be called by a regular function named fileName.m. | ||
If the dynamics or constraints are complicated, then a script `Derive_*.m` is provided to use the symbolic toolbox to derive these equations. Any files with the `autoGen_fileName.m` are created by the Matlab symbolic toolbox, and should not be edited. In each case, they will be called by a regular function named `fileName.m`. | ||
|
||
If you find any errors, have comments, or would like to suggest an example problem, just send me an email. Contact Info can be found at either page: | ||
--> https://github.com/MatthewPeterKelly | ||
--> www.matthewpeterkelly.com | ||
If you find any errors, have comments, or would like to suggest an example problem, just send me an email. Contact info can be found at either page: | ||
- [https://github.com/MatthewPeterKelly](https://github.com/MatthewPeterKelly) | ||
- [www.matthewpeterkelly.com](www.matthewpeterkelly.com) | ||
|
||
## List of Examples: | ||
The examples are arranged in order from most simple to most complex. | ||
|
||
List of Examples: (Easy --> Hard) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
- **pendulum:** | ||
a simple demo showing basic functionality. Shows how to use analytic gradients. | ||
|
||
pendulum: | ||
--> a simple demo showing basic functionality. Shows how to use analytic gradients. | ||
- **minTimeBvp** | ||
solves a minimum-time trajectory for a scalar system that has limits on position, velocity, acceleration, and jerk (derivative of acceleration). | ||
|
||
cart-pole: | ||
--> A standard benchmark for trajectory optimization. Easy to solve for most choices of parameters. | ||
- **cart-pole:** | ||
A standard benchmark for trajectory optimization. Easy to solve for most choices of parameters. | ||
|
||
simpleWalker: | ||
--> A double-pendulum model for a walking robot. Pretty easy to set up and solve. | ||
- **simpleWalker:**, | ||
A double-pendulum model for a walking robot. Pretty easy to set up and solve. | ||
|
||
toyCar: | ||
--> A fun toy problem finding an optimal trajectory for a car driving over a hilly landscape. Turns out to be a bit tricky due to discontinuity in the solution to the problem. | ||
- **toyCar:** | ||
A fun toy problem finding an optimal trajectory for a car driving over a hilly landscape. Turns out to be a bit tricky due to discontinuity in the solution to the problem. | ||
|
||
acrobot: | ||
--> A standard trajectory optimization problem. Somewhat tricky to solve, but easy to set up. | ||
|
||
goddard roacket: | ||
--> Hard to solve, easy to set up. Optimal rocket thrust trajectory. | ||
|
||
five-link biped: | ||
--> Easy to solve, very difficult to set up. Requires advanced knowledge of dynamics and hybrid systems. | ||
|
||
pointMass: | ||
--> A pathological example. Shows various ways to handle discontinuities. | ||
- **acrobot:** | ||
A standard trajectory optimization problem. Somewhat tricky to solve, but easy to set up. | ||
|
||
- **goddard rocket:** | ||
Hard to solve, easy to set up. Optimal rocket thrust trajectory. | ||
|
||
- **five-link biped:** | ||
Easy to solve, very difficult to set up. Requires advanced knowledge of dynamics and hybrid systems. | ||
|
||
- **pointMass:** | ||
A pathological example. Shows various ways to handle discontinuities. |