NXPCUP-libary
Library for car's control board on NXPCUP competition based on the Mbed framework.
ObstacleDetector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "mbed.h"
4 
5 namespace nxpcup {
6 
8 public:
9  struct Config {
10  PinName leftSensorPin;
11  PinName rightSensorPin;
15  bool isDeactivate = false;
16  };
17 
18  enum class AvoidingObstacle {
19  no,
20  onRightSide,
21  onLeftSide
22  };
23 
30  : m_leftSensor(config.leftSensorPin)
31  , m_rightSensor(config.rightSensorPin)
32  , m_config(config)
33  {
34  }
35 
44  int error(float encoderDistance,
45  int borderDetectorError,
46  int leftBorder,
47  int rightBorder)
48  {
49  updateSensorValue();
50  checkObstacle(encoderDistance);
51 
52  if (m_config.isDeactivate) {
53  return borderDetectorError;
54  }
55 
56  if (m_avoidingObstacle == AvoidingObstacle::no) {
57  return borderDetectorError;
58  }
59  if (m_avoidingObstacle == AvoidingObstacle::onLeftSide) {
60  return leftBorder - (64 - m_config.moveFromObstacle); // see just right line;
61  }
62  // obstacle on the right side
63  return rightBorder - (64 + m_config.moveFromObstacle);
64  }
65 
71  int leftSensorValue() const
72  {
73  return m_leftSensorValue;
74  }
75 
81  int rightSensorValue() const
82  {
83  return m_rightSensorValue;
84  }
85 
92  {
93  return m_avoidingObstacle;
94  }
95 
102  {
103  switch (m_avoidingObstacle) {
105  default:
106  return 0;
108  return -1;
110  return 1;
111  }
112  }
113 
117  Config config() const
118  {
119  return m_config;
120  }
121 
128  {
129  m_config = config;
130  reset();
131  }
132 
136  void reset()
137  {
138  m_encoderDistanceStart = 0;
139  m_avoidingObstacle = AvoidingObstacle::no;
140  }
141 
142 private:
146  void updateSensorValue()
147  {
148  m_leftSensorValue = m_leftSensor.read_u16();
149  m_rightSensorValue = m_rightSensor.read_u16();
150  }
151 
157  void checkObstacle(float encoderDistance)
158  {
159  if (m_avoidingObstacle == AvoidingObstacle::no) {
160  if (m_leftSensorValue < m_config.thresholdDistance
161  && m_rightSensorValue < m_config.thresholdDistance) {
162  return;
163  }
164  if (m_leftSensorValue > m_config.thresholdDistance) {
165  m_avoidingObstacle = AvoidingObstacle::onLeftSide; // see just right line;
166  } else {
167  m_avoidingObstacle = AvoidingObstacle::onRightSide; // see just left line
168  }
169  m_encoderDistanceStart = encoderDistance;
170  } else {
171  if (m_encoderDistanceStart + m_config.encoderAvoidDistance < encoderDistance) {
172  m_avoidingObstacle = AvoidingObstacle::no;
173  }
174  }
175  }
176 
177  AnalogIn m_leftSensor;
178  AnalogIn m_rightSensor;
179  Config m_config;
180 
181  AvoidingObstacle m_avoidingObstacle = AvoidingObstacle::no;
182  int m_leftSensorValue = 0;
183  int m_rightSensorValue = 0;
184  float m_encoderDistanceStart = 0;
185 };
186 
187 } // namespace nxpcup
int avoidingObstacleInteger() const
Definition: ObstacleDetector.h:101
Config config() const
Definition: ObstacleDetector.h:117
int error(float encoderDistance, int borderDetectorError, int leftBorder, int rightBorder)
Definition: ObstacleDetector.h:44
bool isDeactivate
Definition: ObstacleDetector.h:15
float encoderAvoidDistance
Definition: ObstacleDetector.h:14
int thresholdDistance
Definition: ObstacleDetector.h:12
AvoidingObstacle avoidingObstacle() const
Definition: ObstacleDetector.h:91
Definition: BorderDetector.h:6
void setConfig(Config &config)
Definition: ObstacleDetector.h:127
ObstacleDetector(Config &config)
Definition: ObstacleDetector.h:29
int leftSensorValue() const
Definition: ObstacleDetector.h:71
void reset()
Definition: ObstacleDetector.h:136
Definition: ObstacleDetector.h:7
int rightSensorValue() const
Definition: ObstacleDetector.h:81
int moveFromObstacle
Definition: ObstacleDetector.h:13
AvoidingObstacle
Definition: ObstacleDetector.h:18
PinName rightSensorPin
Definition: ObstacleDetector.h:11
PinName leftSensorPin
Definition: ObstacleDetector.h:10
Definition: ObstacleDetector.h:9