NXPCUP-libary
Library for car's control board on NXPCUP competition based on the Mbed framework.
BorderDetector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <math.h>
4 #include <stdint.h>
5 
6 namespace nxpcup {
7 
9 public:
11 
12  struct Config {
13  static constexpr int dataSize = Camera::ImageSize;
14  static constexpr int CENTER = dataSize / 2;
15  static constexpr int RIGHT_BORDER = dataSize;
16  static constexpr int NEIGHBORHOOD = 6;
17 
18  static_assert(NEIGHBORHOOD >= 0);
19  };
20 
27  {
28  }
29 
37  void initalize(
38  const std::array<ImageType, Config::dataSize>& data,
39  const uint8_t percentCoefficient = 100)
40  {
41  uint16_t maxValue = findMaxValue(data);
42  m_threshold = (maxValue * percentCoefficient) / 100;
43  }
44 
51  int findBorder(const std::array<ImageType, Config::dataSize>& data)
52  {
53  int16_t previousLeftBorder = m_leftBorder;
54  int16_t previousRightBorder = m_rightBorder;
55  int16_t middle = (previousRightBorder + previousLeftBorder) / 2;
56  m_leftBorder = 0;
57  m_rightBorder = Config::RIGHT_BORDER;
58 
59  bool findPreviousLeft = isBorderAroundPreviousIndex(data, previousLeftBorder, m_leftBorder);
60  bool findPreviousRight = isBorderAroundPreviousIndex(data, previousRightBorder, m_rightBorder);
61 
62  if (findPreviousLeft && findPreviousRight) {
63  return 1;
64  }
65 
66  if (!findPreviousLeft) {
67  int16_t leftMaxBorderIndex = std::max_element(data.begin(), data.begin() + middle) - data.begin();
68  if (data[leftMaxBorderIndex] > m_threshold) {
69  m_leftBorder = leftMaxBorderIndex;
70  }
71  }
72 
73  if (!findPreviousRight) {
74  int16_t rightMaxBorderIndex = std::max_element(data.begin() + middle, data.end()) - data.begin();
75  if (data[rightMaxBorderIndex] > m_threshold) {
76  m_rightBorder = rightMaxBorderIndex;
77  }
78  }
79 
80  return 0;
81  }
82 
88  {
89  return m_distanceCenter - m_leftBorder;
90  }
91 
97  {
98  return m_rightBorder - m_distanceCenter;
99  }
100 
111  int error(const int percentCoefficient = 100) const
112  {
113  int rawError = (rightDistanceFromCenter() - leftDistanceFromCenter()) / 2;
114 
115  rawError = (rawError * percentCoefficient) / 100;
116 
117  return rawError;
118  }
119 
123  int leftBorder() const
124  {
125  return m_leftBorder;
126  }
127 
131  int rightBorder() const
132  {
133  return m_rightBorder;
134  }
135 
136 private:
144  int findMaxValue(
145  const std::array<ImageType, Config::dataSize>& data,
146  const uint8_t offset = 5) const
147  {
148  int at = 0;
149  for (int i = offset; i < (Config::RIGHT_BORDER - offset); i++) {
150  if (data[i] > data[at]) {
151  at = i;
152  }
153  }
154  return data[at];
155  }
156 
157 
166  bool isBorderAroundPreviousIndex(
167  const std::array<ImageType, Config::dataSize>& data,
168  const int previousBorder,
169  int& border)
170  {
171  bool find = false;
172  uint16_t maxValue = m_threshold;
173  int index = nxpcup::clamp<int>(previousBorder - Config::NEIGHBORHOOD, 0, Config::dataSize - 1);
174  int indexMax = nxpcup::clamp<int>(previousBorder + Config::NEIGHBORHOOD, 0, Config::dataSize - 1);
175  for (; index <= indexMax; ++index) {
176  if (data[index] > maxValue) {
177  maxValue = data[index];
178  border = index;
179  find = true;
180  }
181  }
182  return find;
183  }
184 
185  int m_leftBorder = 0;
186  int m_rightBorder = Config::RIGHT_BORDER;
187  int m_endflag = 0;
188 
189  int m_distanceCenter = Config::CENTER;
190  uint16_t m_threshold = 0;
191 };
192 
193 } // namespace nxpcup
Camera::ImageType ImageType
Definition: BorderDetector.h:10
static constexpr int ImageSize
Definition: Camera.h:12
int rightBorder() const
Definition: BorderDetector.h:131
int rightDistanceFromCenter() const
Definition: BorderDetector.h:96
int findBorder(const std::array< ImageType, Config::dataSize > &data)
Definition: BorderDetector.h:51
static constexpr int NEIGHBORHOOD
Definition: BorderDetector.h:16
static constexpr int dataSize
Definition: BorderDetector.h:13
int error(const int percentCoefficient=100) const
Definition: BorderDetector.h:111
static constexpr int RIGHT_BORDER
Definition: BorderDetector.h:15
Definition: BorderDetector.h:8
Definition: BorderDetector.h:6
int leftBorder() const
Definition: BorderDetector.h:123
BorderDetector(Config config)
Definition: BorderDetector.h:26
void initalize(const std::array< ImageType, Config::dataSize > &data, const uint8_t percentCoefficient=100)
Definition: BorderDetector.h:37
Definition: BorderDetector.h:12
static constexpr int CENTER
Definition: BorderDetector.h:14
int leftDistanceFromCenter() const
Definition: BorderDetector.h:87
uint16_t ImageType
Definition: Camera.h:13