Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <dbc/DbcBuilder.h>
00036 #include <dbc/DbcMessage.h>
00037 #include <dbc/DbcSignal.h>
00038
00039 namespace NewEagle
00040 {
00041 DbcBuilder::DbcBuilder()
00042 {
00043 MessageToken = std::string("BO_");
00044 SignalToken = std::string("SG_");
00045 CommentToken = std::string("CM_");
00046 EnumValueToken = std::string("VAL_");
00047 AttributeToken = std::string("BA_");
00048 SignalValueTypeToken = std::string("SIG_VALTYPE_");
00049 }
00050
00051 DbcBuilder::~DbcBuilder()
00052 {
00053 }
00054
00055 NewEagle::Dbc DbcBuilder::NewDbc(const std::string &dbcFile)
00056 {
00057
00058 NewEagle::Dbc dbc;
00059
00060 std::istringstream f(dbcFile);
00061 std::string line;
00062 uint32_t lineNumber = 0;
00063
00064 NewEagle::DbcMessage currentMessage;
00065
00066 int32_t cnt = 0;
00067
00068 while (std::getline(f, line, '\n'))
00069 {
00070 lineNumber++;
00071
00072 NewEagle::LineParser parser(line);
00073
00074
00075 std::string identifier;
00076 try
00077 {
00078 identifier = parser.ReadCIdentifier();
00079 }
00080 catch(std::exception& ex)
00081 {
00082 identifier = std::string();
00083 }
00084
00085 if (!MessageToken.compare(identifier))
00086 {
00087 try
00088 {
00089 currentMessage = ReadMessage(parser);
00090 currentMessage.SetRawText(line);
00091 dbc.AddMessage(currentMessage.GetName(), currentMessage);
00092 }
00093 catch(LineParserExceptionBase& exlp)
00094 {
00095 ROS_WARN("LineParser Exception: [%s]", exlp.what());
00096 }
00097 catch(std::exception& ex)
00098 {
00099 ROS_ERROR("DBC Message Parser Exception: [%s]", ex.what());
00100 }
00101 }
00102 else if (!SignalToken.compare(identifier))
00103 {
00104 try
00105 {
00106 NewEagle::DbcSignal signal = ReadSignal(parser);
00107
00108 NewEagle::DbcMessage* msg = dbc.GetMessage(currentMessage.GetName());
00109 msg->AddSignal(signal.GetName(), signal);
00110 }
00111 catch(LineParserExceptionBase& exlp)
00112 {
00113 ROS_WARN("LineParser Exception: [%s]", exlp.what());
00114 }
00115 catch(std::exception& ex)
00116 {
00117 ROS_ERROR("DBC Signal Parser Exception: [%s]", ex.what());
00118 }
00119 }
00120 else if (!CommentToken.compare(identifier))
00121 {
00122 try
00123 {
00124 std::string token = parser.ReadCIdentifier();
00125
00126 if (!MessageToken.compare(token))
00127 {
00128 NewEagle::DbcMessageComment dbcMessageComment = ReadMessageComment(parser);
00129
00130 std::map<std::string, NewEagle::DbcMessage>::iterator it;
00131 int32_t ttt = 0;
00132
00133 for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
00134 {
00135 ttt++;
00136 if (it->second.GetRawId() == dbcMessageComment.Id)
00137 {
00138 it->second.SetComment(dbcMessageComment);
00139 break;
00140 }
00141 }
00142 }
00143 else if (!SignalToken.compare(token))
00144 {
00145 NewEagle::DbcSignalComment dbcSignalComment = ReadSignalComment(parser);
00146
00147 std::map<std::string, NewEagle::DbcMessage>::iterator it;
00148 for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
00149 {
00150 if (it->second.GetRawId() == dbcSignalComment.Id)
00151 {
00152 DbcMessage msg = it->second;
00153 std::map<std::string, NewEagle::DbcSignal>::iterator its;
00154
00155 for (its = msg.GetSignals()->begin(); its != msg.GetSignals()->end(); ++its)
00156 {
00157 if (its->second.GetName() == dbcSignalComment.SignalName)
00158 {
00159 its->second.SetComment(dbcSignalComment);
00160 break;
00161 }
00162 }
00163 }
00164 }
00165 }
00166 }
00167 catch(LineParserExceptionBase& exlp)
00168 {
00169 ROS_WARN("LineParser Exception: [%s]", exlp.what());
00170 }
00171 catch(std::exception& ex)
00172 {
00173 ROS_ERROR("DBC Comment Parser Exception: [%s]", ex.what());
00174 }
00175 }
00176 else if (!AttributeToken.compare(identifier))
00177 {
00178 try
00179 {
00180 NewEagle::DbcAttribute dbcAttribute = ReadAttribute(parser);
00181
00182 if (dbc.GetMessageCount() > 0)
00183 {
00184 std::map<std::string, NewEagle::DbcMessage>::iterator it;
00185 for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
00186 {
00187 if (it->second.GetRawId() == dbcAttribute.Id)
00188 {
00189 std::map<std::string, NewEagle::DbcSignal>::iterator its;
00190 DbcMessage msg = it->second;
00191 for (its = msg.GetSignals()->begin(); its != msg.GetSignals()->end(); ++its)
00192 {
00193 if (its->second.GetName() == dbcAttribute.SignalName)
00194 {
00195 NewEagle::DbcSignal sig = its->second;
00196
00197 double gain = sig.GetGain();
00198 double offset = sig.GetOffset();
00199
00200 double f = 0.0;
00201
00202 std::stringstream ss;
00203 ss << dbcAttribute.Value;
00204 ss >> f;
00205
00206 double val = gain * f + offset;
00207 sig.SetInitialValue(val);
00208 break;
00209 }
00210 }
00211 }
00212 }
00213 }
00214 }
00215 catch(LineParserExceptionBase& exlp)
00216 {
00217 ROS_WARN("LineParser Exception: [%s]", exlp.what());
00218 }
00219 catch(std::exception& ex)
00220 {
00221 ROS_ERROR("DBC Signal Value Type Parser Exception: [%s]", ex.what());
00222 }
00223 }
00224 else if (!EnumValueToken.compare(identifier))
00225 {
00226
00227 }
00228 else if (!SignalValueTypeToken.compare(identifier))
00229 {
00230 try
00231 {
00232 NewEagle::DbcSignalValueType dbcSignalValueType = ReadSignalValueType(parser);
00233
00234 if (dbc.GetMessageCount() > 0)
00235 {
00236 std::map<std::string, NewEagle::DbcMessage>::iterator it;
00237 for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
00238 {
00239 if (it->second.GetRawId() == dbcSignalValueType.Id)
00240 {
00241 std::map<std::string, NewEagle::DbcSignal>::iterator its;
00242 DbcMessage msg = it->second;
00243 for (its = msg.GetSignals()->begin(); its != msg.GetSignals()->end(); ++its)
00244 {
00245 if (its->second.GetName() == dbcSignalValueType.SignalName)
00246 {
00247 NewEagle::DbcSignal sig = its->second;
00248 sig.SetDataType(dbcSignalValueType.Type);
00249 break;
00250 }
00251 }
00252 }
00253 }
00254 }
00255 }
00256 catch(LineParserExceptionBase& exlp)
00257 {
00258 ROS_WARN("LineParser Exception: [%s]", exlp.what());
00259 }
00260 catch(std::exception& ex)
00261 {
00262 ROS_ERROR("DBC Signal Value Type Parser Exception: [%s]", ex.what());
00263 }
00264
00265 }
00266 }
00267
00268 ROS_INFO("dbc.size() = %d", dbc.GetMessageCount());
00269
00270 return dbc;
00271 }
00272 }