00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <fstream>
00020 #include <math.h>
00021
00022 #include "model.h"
00023 #include "defs.h"
00024
00025 Model::Model(string path) {
00026
00027 if ((path.length() > 0)&&(path[path.length()-1] != '/'))
00028 path += "/";
00029
00030 FilePath = path;
00031
00032 READ_PARAMETER_OR_DEFAULT(ModelDeltaT,1.0);
00033
00034 StateDim = 2;
00035 InputDim = 2;
00036
00037 }
00038
00039
00040
00041 MSLVector Model::EulerIntegrate(const MSLVector &x, const MSLVector &u,
00042 const double &h)
00043 {
00044 int s,i,k;
00045 double c;
00046 MSLVector nx;
00047
00048 s = (h > 0) ? 1 : -1;
00049
00050 c = s*h/ModelDeltaT;
00051 k = (int) c;
00052
00053 nx = x;
00054 for (i = 0; i < k; i++) {
00055 nx += s * ModelDeltaT * StateTransitionEquation(nx,u);
00056 }
00057
00058
00059 nx += s * (c - k) * ModelDeltaT * StateTransitionEquation(nx,u);
00060
00061 return nx;
00062 }
00063
00064
00065
00066 MSLVector Model::RungeKuttaIntegrate(const MSLVector &x, const MSLVector &u,
00067 const double &h)
00068 {
00069 MSLVector k1,k2,k3,k4;
00070 int s,i,k;
00071 double c,deltat;
00072 MSLVector nx;
00073
00074 s = (h > 0) ? 1 : -1;
00075
00076 c = s*h/ModelDeltaT;
00077 k = (int) c;
00078 deltat = s * ModelDeltaT;
00079
00080 nx = x;
00081 for (i = 0; i < k; i++) {
00082 k1 = StateTransitionEquation(nx,u);
00083 k2 = StateTransitionEquation(nx + (0.5*deltat)*k1,u);
00084 k3 = StateTransitionEquation(nx + (0.5*deltat)*k2,u);
00085 k4 = StateTransitionEquation(nx + deltat*k3,u);
00086 nx += deltat / 6.0 * (k1 + 2.0*k2 + 2.0*k3 + k4);
00087 }
00088
00089
00090 deltat = s * (c - k) * ModelDeltaT;
00091 k1 = StateTransitionEquation(nx,u);
00092 k2 = StateTransitionEquation(nx + (0.5*deltat)*k1,u);
00093 k3 = StateTransitionEquation(nx + (0.5*deltat)*k2,u);
00094 k4 = StateTransitionEquation(nx + deltat*k3,u);
00095 nx += deltat / 6.0 * (k1 + 2.0*k2 + 2.0*k3 + k4);
00096
00097 return nx;
00098 }
00099
00100
00101
00102 list<MSLVector> Model::GetInputs(const MSLVector &x) {
00103 return Inputs;
00104 }
00105
00106
00107
00108
00109 MSLVector Model::StateToConfiguration(const MSLVector &x) {
00110 return x;
00111 }
00112
00113
00114 bool Model::Satisfied(const MSLVector &x) {
00115 return true;
00116 }
00117
00118
00119
00120 double Model::Metric(const MSLVector &x1, const MSLVector &x2) {
00121
00122 double rho;
00123
00124 rho = (x1 - x2).length();
00125
00126 return rho;
00127 }
00128
00129
00130
00131
00132 MSLVector Model::LinearInterpolate(const MSLVector &x1, const MSLVector &x2,
00133 const double &a) {
00134 return (1.0-a)*x1 + a*x2;
00135 }
00136
00137
00138
00139 MSLVector Model::StateDifference(const MSLVector &x1, const MSLVector &x2) {
00140 return (x2 - x1);
00141 }
00142
00143