Apollo 10.0
自动驾驶开放平台
protocol_data.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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
22#pragma once
23
24#include <cmath>
25#include <numeric>
26
27#include "cyber/common/log.h"
29
34namespace apollo {
35namespace drivers {
36namespace canbus {
37
43template <typename SensorType>
45 public:
52 static std::uint8_t CalculateCheckSum(const uint8_t *input,
53 const uint32_t length);
57 ProtocolData() = default;
58
62 virtual ~ProtocolData() = default;
63
64 /*
65 * @brief get interval period for canbus messages
66 * @return the interval period in us (1e-6s)
67 */
68 virtual uint32_t GetPeriod() const;
69
70 /*
71 * @brief get the length of protocol data. The length is usually 8.
72 * @return the length of protocol data.
73 */
74 virtual int32_t GetLength() const;
75
76 /*
77 * @brief parse received data
78 * @param bytes a pointer to the input bytes
79 * @param length the length of the input bytes
80 * @param sensor_data the parsed sensor_data
81 */
82 virtual void Parse(const uint8_t *bytes, int32_t length,
83 SensorType *sensor_data) const;
84
85 /*
86 * @brief update the data
87 */
88 virtual void UpdateData(uint8_t *data);
89
90 /*
91 * @brief update the heartbeat data
92 */
93 virtual void UpdateData_Heartbeat(uint8_t *data);
94
95 /*
96 * @brief reset the protocol data
97 */
98 virtual void Reset();
99
100 /*
101 * @brief check if the value is in [lower, upper], if not , round it to bound
102 */
103 template <typename T>
104 static T BoundedValue(T lower, T upper, T val);
105
106 private:
107 const int32_t data_length_ = CANBUS_MESSAGE_LENGTH;
108};
109
110template <typename SensorType>
111template <typename T>
112T ProtocolData<SensorType>::BoundedValue(T lower, T upper, T val) {
113 if (lower > upper) {
114 return val;
115 }
116 if (val < lower) {
117 return lower;
118 }
119 if (val > upper) {
120 return upper;
121 }
122 return val;
123}
124
125// (SUM(input))^0xFF
126template <typename SensorType>
128 const uint32_t length) {
129 return static_cast<uint8_t>(std::accumulate(input, input + length, 0) ^ 0xFF);
130}
131
132template <typename SensorType>
134 const uint32_t CONST_PERIOD = 100 * 1000;
135 return CONST_PERIOD;
136}
137
138template <typename SensorType>
140 return data_length_;
141}
142
143template <typename SensorType>
144void ProtocolData<SensorType>::Parse(const uint8_t *bytes, int32_t length,
145 SensorType *sensor_data) const {}
146
147template <typename SensorType>
148void ProtocolData<SensorType>::UpdateData(uint8_t * /*data*/) {}
149
150template <typename SensorType>
152
153template <typename SensorType>
155
156} // namespace canbus
157} // namespace drivers
158} // namespace apollo
This is the base class of protocol data.
virtual void UpdateData_Heartbeat(uint8_t *data)
ProtocolData()=default
construct protocol data.
static T BoundedValue(T lower, T upper, T val)
virtual void Parse(const uint8_t *bytes, int32_t length, SensorType *sensor_data) const
virtual void UpdateData(uint8_t *data)
static std::uint8_t CalculateCheckSum(const uint8_t *input, const uint32_t length)
static function, used to calculate the checksum of input array.
virtual ~ProtocolData()=default
destruct protocol data.
virtual uint32_t GetPeriod() const
const int32_t CANBUS_MESSAGE_LENGTH
class register implement
Definition arena_queue.h:37