65std::map<std::string, int> ProtocolAsensing::protocolLengthMap{};
66std::map<std::string, ProtocolAsensing*> ProtocolAsensing::protocolMap{};
75 receive_data_.erase(receive_data_.begin(), receive_data_.end());
78bool ProtocolAsensing::analysisData(
const std::string& data) {
79 receive_data_ += data;
82 const uint8_t* data_address =
83 reinterpret_cast<const uint8_t*
>(receive_data_.c_str());
84 bool is_exit_while =
false;
86 while (start_index < receive_data_.size() - 11 && !is_exit_while) {
88 std::string packet_type =
"";
90 std::snprintf(str,
sizeof(str),
"%02X%02X%02X", data_address[start_index],
91 data_address[start_index + 1], data_address[start_index + 2]);
96 for (
auto it = protocolMap.begin(); it != protocolMap.end(); ++it) {
98 if (packet_type == it->first) {
100 cur_protocol_ = packet_type;
109 const uint8_t* sub_address = data_address + start_index;
110 if (protocolLengthMap.find(packet_type) == protocolLengthMap.end()) {
111 if (start_index <
static_cast<int>(receive_data_.size()) - 2) {
114 is_exit_while =
true;
118 if (
static_cast<int>(receive_data_.size()) - start_index - 1 >
119 protocolLengthMap[packet_type]) {
121 protocolMap[packet_type]->subData(sub_address, start_index);
123 is_exit_while =
true;
127 if (start_index <
static_cast<int>(receive_data_.size()) - 1) {
130 is_exit_while =
true;
135 if (start_index > 0 && receive_data_.size() > start_index) {
137 receive_data_.erase(receive_data_.begin(),
138 receive_data_.begin() + start_index);
139 }
else if (start_index > 0 && receive_data_.size() == start_index) {
140 receive_data_.clear();
148 protocolLengthMap.insert(std::pair<std::string, int>(protocolFlag, length));
150 std::pair<std::string, ProtocolAsensing*>(protocolFlag, sub));
156 double halfYaw = rpy[2] * 0.5;
157 double halfPitch = rpy[1] * 0.5;
158 double halfRoll = rpy[0] * 0.5;
159 double cosYaw = std::cos(halfYaw);
160 double sinYaw = std::sin(halfYaw);
161 double cosPitch = std::cos(halfPitch);
162 double sinPitch = std::sin(halfPitch);
163 double cosRoll = std::cos(halfRoll);
164 double sinRoll = std::sin(halfRoll);
166 quaterArray[0] = sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw;
167 quaterArray[1] = cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw;
168 quaterArray[2] = cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw;
169 quaterArray[3] = cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw;
173 const std::string& content) {
174 std::ifstream checkFile(filename);
175 if (checkFile.good()) {
176 std::cout <<
"文件已存在: " << filename << std::endl;
181 std::ofstream file(filename);
183 std::cerr <<
"无法创建文件: " << filename << std::endl;
187 file << content << std::endl;
190 std::cout <<
"已写入文件: " << filename << std::endl;
195 const std::vector<std::string>& data) {
196 std::ofstream file(filename, std::ios_base::app);
197 if (!file.is_open()) {
198 std::cerr <<
"无法打开文件 " << filename <<
"\n";
202 std::ostringstream oss;
203 for (
const auto& s : data) {
206 std::string line = oss.str();
void toQuaternion(double *rpy, double *quaterArray)
bool registProtocol(const std::string &protocolFlag, int length, ProtocolAsensing *sub)
virtual ~ProtocolAsensing()
void addData(const std::string &data)
bool createFileAndWrite(const std::string &filename, const std::string &content)
@ CALIB_MAG2D_LARGE_INCLIN
@ ACCEL_INITIAL_CHECK_ERR
bool AppendCsv(const std::string &filename, const std::vector< std::string > &data)