54 int servoAngleRange = m_servo.getMaxAngle() - m_servo.getMinAngle();
55 auto angle = m_servo.getMinAngle() + servoAngleRange * index / m_image.size;
64 return m_servoPositionDegree;
83 int error(
float encoderDistance,
84 int borderDetectorError,
89 check(encoderDistance);
91 if (m_config.isDeactivate) {
92 return borderDetectorError;
96 return borderDetectorError;
99 return leftBorder - (64 - m_config.moveFromObstacle);
102 return rightBorder - (64 + m_config.moveFromObstacle);
113 return m_avoidingObstacle;
123 switch (m_avoidingObstacle) {
141 return m_distanceThatTriggered;
168 m_encoderDistanceStart = 0;
177 for (
int i = 0; i < 10; ++i) {
178 m_sensorValue += m_sensor.read_u16();
182 m_image[m_index] = m_sensorValue;
184 if (m_direction == 1) {
186 if (m_index == (m_image.size - 1)) {
189 }
else if (m_direction == -1) {
197 m_servo.setAngle(m_servoPositionDegree);
199 return m_sensorValue;
202 std::optional<int> checkObstacleAngle()
204 Image processed = m_image;
205 int maxElementIndex = std::max_element(processed.
begin(), processed.
end())
208 int maxElementValue = processed[maxElementIndex];
209 if (maxElementValue < m_config.thresholdDistance) {
212 m_distanceThatTriggered = maxElementValue;
216 void check(
float encoderDistance)
219 auto obstacle = checkObstacleAngle();
224 if (*obstacle < 90) {
229 m_encoderDistanceStart = encoderDistance;
231 if (m_encoderDistanceStart + m_config.encoderAvoidDistance < encoderDistance) {
242 float m_encoderDistanceStart = 0;
243 int m_sensorValue = 0;
244 int m_servoPositionDegree = 90;
247 int m_distanceThatTriggered = 0;
int error(float encoderDistance, int borderDetectorError, int leftBorder, int rightBorder)
Definition: ObstacleDetectorWithServo.h:83
int servoPositionDegree() const
Definition: ObstacleDetectorWithServo.h:62
void setConfig(Config &config)
Definition: ObstacleDetectorWithServo.h:157
ObstacleDetectorWithServo(Config config)
Definition: ObstacleDetectorWithServo.h:41
int servoAngle(int index)
Definition: ObstacleDetectorWithServo.h:52
const T * begin() const
Definition: Image.h:22
int sensorValue() const
Definition: ObstacleDetectorWithServo.h:70
int avoidingObstacleInteger() const
Definition: ObstacleDetectorWithServo.h:121
uint16_t ImageType
Definition: ObstacleDetectorWithServo.h:33
Definition: BorderDetector.h:6
int moveFromObstacle
Definition: ObstacleDetectorWithServo.h:17
Definition: ObstacleDetectorWithServo.h:11
Definition: ObstacleDetectorWithServo.h:13
void reset()
Definition: ObstacleDetectorWithServo.h:166
PinName sensorPin
Definition: ObstacleDetectorWithServo.h:14
Servo::Config servoConfig
Definition: ObstacleDetectorWithServo.h:15
int thresholdDistance
Definition: ObstacleDetectorWithServo.h:16
const T * end() const
Definition: Image.h:24
AvoidingObstacle avoidingObstacle() const
Definition: ObstacleDetectorWithServo.h:111
int distanceThatTriggered() const
Definition: ObstacleDetectorWithServo.h:139
static constexpr int SERVO_MAX_RANGE_DEGREE
Definition: ObstacleDetectorWithServo.h:24
float encoderAvoidDistance
Definition: ObstacleDetectorWithServo.h:18
bool isDeactivate
Definition: ObstacleDetectorWithServo.h:19
Config config() const
Definition: ObstacleDetectorWithServo.h:147
AvoidingObstacle
Definition: ObstacleDetectorWithServo.h:27
static constexpr int IMAGE_SIZE
Definition: ObstacleDetectorWithServo.h:23