Apollo  6.0
Open source self driving car software
protocol_data.h
Go to the documentation of this file.
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 
34 namespace apollo {
35 namespace drivers {
36 namespace canbus {
37 
43 template <typename SensorType>
44 class ProtocolData {
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 reset the protocol data
92  */
93  virtual void Reset();
94 
95  /*
96  * @brief check if the value is in [lower, upper], if not , round it to bound
97  */
98  template <typename T>
99  static T BoundedValue(T lower, T upper, T val);
100 
101  private:
102  const int32_t data_length_ = CANBUS_MESSAGE_LENGTH;
103 };
104 
105 template <typename SensorType>
106 template <typename T>
107 T ProtocolData<SensorType>::BoundedValue(T lower, T upper, T val) {
108  if (lower > upper) {
109  return val;
110  }
111  if (val < lower) {
112  return lower;
113  }
114  if (val > upper) {
115  return upper;
116  }
117  return val;
118 }
119 
120 // (SUM(input))^0xFF
121 template <typename SensorType>
122 uint8_t ProtocolData<SensorType>::CalculateCheckSum(const uint8_t *input,
123  const uint32_t length) {
124  return static_cast<uint8_t>(std::accumulate(input, input + length, 0) ^ 0xFF);
125 }
126 
127 template <typename SensorType>
129  const uint32_t CONST_PERIOD = 100 * 1000;
130  return CONST_PERIOD;
131 }
132 
133 template <typename SensorType>
135  return data_length_;
136 }
137 
138 template <typename SensorType>
139 void ProtocolData<SensorType>::Parse(const uint8_t *bytes, int32_t length,
140  SensorType *sensor_data) const {}
141 
142 template <typename SensorType>
143 void ProtocolData<SensorType>::UpdateData(uint8_t * /*data*/) {}
144 
145 template <typename SensorType>
147 
148 } // namespace canbus
149 } // namespace drivers
150 } // namespace apollo
virtual ~ProtocolData()=default
destruct protocol data.
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
This is the base class of protocol data.
Definition: protocol_data.h:44
static std::uint8_t CalculateCheckSum(const uint8_t *input, const uint32_t length)
static function, used to calculate the checksum of input array.
Definition: protocol_data.h:122
ProtocolData()=default
construct protocol data.
virtual int32_t GetLength() const
Definition: protocol_data.h:134
virtual void Parse(const uint8_t *bytes, int32_t length, SensorType *sensor_data) const
Definition: protocol_data.h:139
const int32_t CANBUS_MESSAGE_LENGTH
Definition: canbus_consts.h:37
virtual uint32_t GetPeriod() const
Definition: protocol_data.h:128
virtual void Reset()
Definition: protocol_data.h:146
SensorType
Sensor types are set in the order of lidar, radar, camera, ultrasonic Please make sure SensorType has...
Definition: sensor_meta.h:29
static T BoundedValue(T lower, T upper, T val)
Definition: protocol_data.h:107
virtual void UpdateData(uint8_t *data)
Definition: protocol_data.h:143