Apollo 10.0
自动驾驶开放平台
protocol_asensing.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <cmath>
20#include <fstream>
21#include <iostream>
22#include <sstream>
23#include <utility>
24
27 POLL_TIMEOUT, // Polling toggle bit failed
28 VERIFY_WRITE, // Verifying write to flash failed
29 INVALID_SECTOR, // Invalid Sector
30 INVALID_BLOCK, // Invalid Block
31 UNKNOWN_COMMAND, // Unknown Command
32 PROCESS_COMMAND_ERR, // Processing command
33 NOT_READ_ERROR, // Could not read memory from target
34 DRV_NOTAT_BREAK, // The drive was not at AFP_BreakReady
35 BUFFER_IS_NULL, // Could not allocate storage for the buffer
36 NO_ACCESS_SECTOR, // Cannot access the sector( could be locked or something
37 // is stored there that should not be touched )
39 UART_RX_FULL, // Lost one byte when the RX circular buffer is full.
40 UART_TX_FULL, // When TX circular buffer is full, push one byte will be lost
41 UART_RX_BUF_EMPTY, // Try to pop one byte from empty RX circular buffer
42 OUT_OF_FLASH_NUM, // Out of flash number (0-2)
43 FLASH_GET_CODE_FAIL, // Check codes of flash devices failed
44 FLASH_COMPARE_ERROR, // Compare read/write flash error
45 UNDEFINED_URX_STAT, // Undefined uart rx buf status
46 UNDEFINED_INS, // Undefined instruction
47 FAIL_ON_MALLOC, // Failed on malloc()
48 CHECK_SUM_ERR, // Check sum error, VR101 protocol
49 TOO_MUCH_DATA, // Too much data to print in a data rate period
50 OUT_OF_16BIT_RANGE, // Out of 16bit range
51 MATRIX_NO_INVERSE, // Matrix no inverse
52 WRITE_BACK_FLASH, // Try to write to a Flash address that have been written
53 SDRAM_TEST_ERR, // SDRAM self test error
54 COMP_SELFCHECK_FAIL, // Compass self check fail
55 UNFINISHED_HMC5883SEQ, // Unfinished HMC5883 continous read sequence while
56 // starting a new one
57 SENSOR_SATURATION, // Sensor saturation
58 GYRO_BIT_ERR, // Gyro Built-in-test error
59 CALIB_MAG2D_LARGE_INCLIN, // too large pitch or roll angle during 2D
60 // magnetometer calib
61 ACCEL_INITIAL_CHECK_ERR, // Accel initial check error
62 GYRO_INITIAL_CHECK_ERR // gyroscope initial check error
63};
64
65std::map<std::string, int> ProtocolAsensing::protocolLengthMap{};
66std::map<std::string, ProtocolAsensing*> ProtocolAsensing::protocolMap{};
67
69
71
72void ProtocolAsensing::addData(const std::string& data) { analysisData(data); }
73
75 receive_data_.erase(receive_data_.begin(), receive_data_.end());
76}
77
78bool ProtocolAsensing::analysisData(const std::string& data) {
79 receive_data_ += data;
80 // 解析自定义协议数据
81 int start_index = 0; // 当前读取到的字节下标
82 const uint8_t* data_address =
83 reinterpret_cast<const uint8_t*>(receive_data_.c_str());
84 bool is_exit_while = false;
85
86 while (start_index < receive_data_.size() - 11 && !is_exit_while) {
87 /* get message head */
88 std::string packet_type = "";
89 char str[16] = {0};
90 std::snprintf(str, sizeof(str), "%02X%02X%02X", data_address[start_index],
91 data_address[start_index + 1], data_address[start_index + 2]);
92 packet_type += str;
93
94 /* find message head */
95 bool isRight = false;
96 for (auto it = protocolMap.begin(); it != protocolMap.end(); ++it) {
97 // std::cout<<"==>asensing:"<<__LINE__<<std::endl;
98 if (packet_type == it->first) {
99 isRight = true;
100 cur_protocol_ = packet_type;
101 break;
102 } else {
103 continue;
104 }
105 }
106
107 if (isRight) {
108 /* get message length */
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) {
112 start_index += 2;
113 } else {
114 is_exit_while = true;
115 }
116 } else {
117 /* parse massage */
118 if (static_cast<int>(receive_data_.size()) - start_index - 1 >
119 protocolLengthMap[packet_type]) {
120 // std::cout<<"==>asensing: subData "<<std::endl;
121 protocolMap[packet_type]->subData(sub_address, start_index);
122 } else {
123 is_exit_while = true;
124 }
125 }
126 } else {
127 if (start_index < static_cast<int>(receive_data_.size()) - 1) {
128 start_index++;
129 } else {
130 is_exit_while = true;
131 }
132 }
133 } // end while
134
135 if (start_index > 0 && receive_data_.size() > start_index) {
136 // 去掉已遍历过的数据
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();
141 }
142
143 return true;
144}
145
146bool ProtocolAsensing::registProtocol(const std::string& protocolFlag,
147 int length, ProtocolAsensing* sub) {
148 protocolLengthMap.insert(std::pair<std::string, int>(protocolFlag, length));
149 protocolMap.insert(
150 std::pair<std::string, ProtocolAsensing*>(protocolFlag, sub));
151 // TinyLog::info("regist protocol:%s length: %d\n" , protocolFlag.c_str() ,
152 // length);
153 return true;
154}
155void ProtocolAsensing::toQuaternion(double* rpy, double* quaterArray) {
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);
165
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;
170}
171
172bool createFileAndWrite(const std::string& filename,
173 const std::string& content) {
174 std::ifstream checkFile(filename);
175 if (checkFile.good()) {
176 std::cout << "文件已存在: " << filename << std::endl;
177 return false;
178 }
179 checkFile.close();
180
181 std::ofstream file(filename);
182 if (!file) {
183 std::cerr << "无法创建文件: " << filename << std::endl;
184 return false;
185 }
186
187 file << content << std::endl;
188 file.close();
189
190 std::cout << "已写入文件: " << filename << std::endl;
191 return true;
192}
193
194bool AppendCsv(const std::string& filename,
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";
199 return false;
200 }
201
202 std::ostringstream oss;
203 for (const auto& s : data) {
204 oss << s << ",";
205 }
206 std::string line = oss.str();
207 if (!line.empty()) {
208 line.pop_back(); // 删除最后一个逗号
209 }
210 line += "\n";
211
212 file << line;
213
214 file.close();
215 return true;
216}
void toQuaternion(double *rpy, double *quaterArray)
bool registProtocol(const std::string &protocolFlag, int length, ProtocolAsensing *sub)
void addData(const std::string &data)
bool createFileAndWrite(const std::string &filename, const std::string &content)
@ BUFFER_IS_NULL
@ UNFINISHED_HMC5883SEQ
@ TOO_MUCH_DATA
@ VERIFY_WRITE
@ SDRAM_TEST_ERR
@ FLASH_GET_CODE_FAIL
@ POLL_TIMEOUT
@ CALIB_MAG2D_LARGE_INCLIN
@ FLASH_COMPARE_ERROR
@ DRV_NOTAT_BREAK
@ GYRO_BIT_ERR
@ PROCESS_COMMAND_ERR
@ NO_ACCESS_SECTOR
@ INVALID_SECTOR
@ ACCEL_INITIAL_CHECK_ERR
@ OUT_OF_FLASH_NUM
@ UART_TX_FULL
@ SENSOR_SATURATION
@ UNDEFINED_INS
@ FAIL_ON_MALLOC
@ COMP_SELFCHECK_FAIL
@ GYRO_INITIAL_CHECK_ERR
@ MATRIX_NO_INVERSE
@ INVALID_BLOCK
@ OUT_OF_16BIT_RANGE
@ NOT_READ_ERROR
@ UNDEFINED_URX_STAT
@ WRITE_BACK_FLASH
@ UART_RX_FULL
@ NUM_ERROR_CODES
@ CHECK_SUM_ERR
@ UNKNOWN_COMMAND
@ UART_RX_BUF_EMPTY
bool AppendCsv(const std::string &filename, const std::vector< std::string > &data)