forked from arpg/CarPlanner
/
CarParameters.cpp
107 lines (97 loc) · 3.98 KB
/
CarParameters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "CarParameters.h"
#include "BulletCarModel.h"
#include "boost/lexical_cast.hpp"
////////////////////////////////////////////////////////////////
const char * const CarParameters::Names[] = {"WheelBase", "Width", "Height", "DynamicFrictionCoef",
"StaticSideFrictionCoef", "SlipCoefficient", "ControlDelay",
"Mass", "WheelRadius", "WheelWidth","TractionFriction",
"SuspConnectionHeight","Stiffness","MaxSuspForce","MaxSuspTravel",
"SuspRestLength","CompDamping","ExpDamping","RollInfluence",
"SteeringCoef","MaxSteering","MaxSteeringRate","AccelOffset",
"SteeringOffset","StallTorqueCoef","TorqueSpeedSlope",
"Magic_B","Magic_C","Magic_E"};
////////////////////////////////////////////////////////////////
bool CarParameters::SaveToFile(const std::string sFile,const CarParameterMap& map)
{
dout("Writing parameter map to " << sFile << "-----------------");
std::ofstream file;
file.open(sFile);
for(const CarParameterPair& pair: map ){
char cLine[256];
sprintf(cLine,"%s,%.9f\n",Names[pair.first],pair.second);
file.write(cLine,strlen(cLine));
}
file.close();
return true;
}
////////////////////////////////////////////////////////////////
bool CarParameters::LoadFromFile(const std::string sFile,CarParameterMap& map)
{
dout("Reading parameter map from " << sFile << "-----------------");
std::ifstream file;
//open the file for read
file.open(sFile.c_str());
std::string line;
while(std::getline(file,line))
{
std::vector<std::string> vals;
std::stringstream lineStream(line);
std::string cell;
while(std::getline(lineStream,cell,','))
{
vals.push_back(cell.c_str());
}
if(vals.size() == 2){
//find the name
bool bFound = false;
for(size_t ii = 0; ii < sizeof(Names) ; ii++){
if(vals[0].compare(std::string(Names[ii])) == 0){
bFound = true;
double val = boost::lexical_cast<double>(vals[1]);
map[ii] = val;
dout("Loading parameter " << Names[ii] << " with value " << val);
break;
}
}
if(bFound == false){
dout("ERROR - parameter " << vals[0] << " not recognized.");
return false;
}
}else{
dout("ERROR - line with less than 2 comma delimited items found when parsing parameters.");
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////
void CarParameters::PrintAllParams(const CarParameterMap &map)
{
dout("Printing parameter map -----------------");
for(const CarParameterPair& pair: map ){
dout(Names[pair.first] << ":" << pair.second);
}
}
////////////////////////////////////////////////////////////////
RegressionParameter::RegressionParameter(CarParameterMap& map, int nKey, BulletCarModel* pModel/* = NULL*/):
m_dVal(map[nKey]),
m_nKey(nKey),
m_sName(CarParameters::Names[nKey]),
m_pModel(pModel){}
////////////////////////////////////////////////////////////////
bool RegressionParameter::AreEqual(const std::vector<RegressionParameter>& params1, const std::vector<RegressionParameter>& params2)
{
for(size_t ii = 0 ; ii < params1.size() ; ii++) {
if(params1[ii].m_dVal != params2[ii].m_dVal){
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////
void RegressionParameter::UpdateValue(const double newVal)
{
m_dVal = newVal;
std::vector<RegressionParameter> params = {*this};
m_pModel->UpdateParameters(params);
}