Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tilt max and min angle constraints #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ This is the most basic `useful` configuration. You can find [this](examples/basi
- **name**(**Optional**, string): The name of this sensor, which is used during logging. Defaults to `LD2450`.
- **flip_x_axis**(**Optional**, boolean): If set to true, values along the X-axis will be flipped. Defaults to `false`.
- **fast_off_detection**(**Optional**, boolean): If set to true, fast-away detection will be used for targets, which leave the visible range of the sensor. Defaults to `false`.
- **max_detection_tilt_angle**(**Optional**, number or angle): The highest allowed detection tilt angle. All targets outside this angle will not be tracked. Either a configuration for a number input or a fixed angle value. See: [Max Tilt Angle Number](#max-tilt-angle-number).
- **min_detection_tilt_angle**(**Optional**, number or angle): The lowest allowed detection tilt angle. All targets outside this angle will not be tracked. Either a configuration for a number input or a fixed angle value. See: [Min Tilt Angle Number](#min-tilt-angle-number).
- **tilt_angle_margin**(**Optional**, ): The margin which is added to the maximum/minimum allowed tilt angle. Targets that are already being tracked, will still be tracked within the additional margin. This prevents on-off-flickering of related sensors. Defaults to `5°`.
- **max_detection_distance**(**Optional**, number or distance): The furthest allowed detection distance. All targets further than this distance will not be tracked. Either a configuration for a number input or a fixed distance value. See: [Max Distance Number](#max-distance-number).
- **max_distance_margin**(**Optional**, ): The margin which is added to the maximum allowed distance. Targets that are already being tracked, will still be tracked within the additional margin. This prevents on-off-flickering of related sensors. Defaults to `25cm`.
- **occupancy**(**Optional**, binary sensor): A binary sensor, which will be triggered if at least one target is present. `id` or `name` required. All options from [Binary Sensor](https://esphome.io/components/binary_sensor/#config-binary-sensor).
Expand All @@ -51,6 +54,28 @@ This is the most basic `useful` configuration. You can find [this](examples/basi
- **targets**(**Optional**, list of targets): A list of at most `3` Targets. Each target has its own configuration and sensors. See [Target](#target).
- **zones**(**Optional**, list of zones): A list Zones. Each zone has its own configuration and sensors. See [Zone](#zone).

### Max Tilt Angle Number

Instead of a fixed maximum tilt angle, a user-defined number component can be defined. The values of this component will be used as the highest allowed angle.

- **name**(**Required**, string): Name of the maximum tilt angle number component.
- **initial_value**(**Optional**, distance): The initial value of this sensor. Defaults to `90°` - required unit is `°` or `deg`. This value will be ignored if `restore_value` is enabled.
- **step**(**Optional**, distance): Step size of the input number. Defaults to `1°`.
- **restore_value**(**Optional**, boolean): Tries to restore the last value from Flash upon reboot. Defaults to `true`.

All other options from [number](https://esphome.io/components/number/#base-number-configuration).

### Min Tilt Angle Number

Instead of a fixed minimum tilt angle, a user-defined number component can be defined. The values of this component will be used as the highest allowed angle.

- **name**(**Required**, string): Name of the minimum tilt angle number component.
- **initial_value**(**Optional**, distance): The initial value of this sensor. Defaults to `-90°` - required unit is `°` or `deg`. This value will be ignored if `restore_value` is enabled.
- **step**(**Optional**, distance): Step size of the input number. Defaults to `1°`.
- **restore_value**(**Optional**, boolean): Tries to restore the last value from Flash upon reboot. Defaults to `true`.

All other options from [number](https://esphome.io/components/number/#base-number-configuration).

### Max Distance Number

Instead of a fixed maximum distance, a user-defined number component can be defined. The values of this component will be used as the furthest allowed distance.
Expand Down
20 changes: 17 additions & 3 deletions components/LD2450/LD2450.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ namespace esphome::ld2450
ESP_LOGCONFIG(TAG, "LD2450 Hub: %s", name_);
ESP_LOGCONFIG(TAG, " fast_off_detection: %s", fast_off_detection_ ? "True" : "False");
ESP_LOGCONFIG(TAG, " flip_x_axis: %s", flip_x_axis_ ? "True" : "False");
ESP_LOGCONFIG(TAG, " max_detection_tilt_angle: %i °", max_detection_tilt_angle_);
ESP_LOGCONFIG(TAG, " min_detection_tilt_angle: %i °", min_detection_tilt_angle_);
ESP_LOGCONFIG(TAG, " max_detection_distance: %i mm", max_detection_distance_);
ESP_LOGCONFIG(TAG, " max_distance_margin: %i mm", max_distance_margin_);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

angle margin missing

ESP_LOGCONFIG(TAG, " tilt_angle_margin: %i °", tilt_angle_margin_);
#ifdef USE_BINARY_SENSOR
LOG_BINARY_SENSOR(" ", "OccupancyBinarySensor", occupancy_binary_sensor_);
#endif
#ifdef USE_NUMBER
LOG_NUMBER(" ", "MaxTiltAngleNumber", max_angle_number_);
LOG_NUMBER(" ", "MinTiltAngleNumber", min_angle_number_);
LOG_NUMBER(" ", "MaxDistanceNumber", max_distance_number_);
#endif
#ifdef USE_BUTTON
Expand Down Expand Up @@ -302,11 +307,20 @@ namespace esphome::ld2450

targets_[i]->update_values(x, y, speed, distance_resolution);

// Filter targets further than max detection distance
if (y <= max_detection_distance_ || (targets_[i]->is_present() && y <= max_detection_distance_ + max_distance_margin_))
// Filter targets further than max detection distance and max angle
float angle = -(atan2(y, x) * (180 / M_PI) - 90);
if ((y <= max_detection_distance_ || (targets_[i]->is_present() && y <= max_detection_distance_ + max_distance_margin_)) &&
(angle <= max_detection_tilt_angle_ || (targets_[i]->is_present() && angle <= max_detection_tilt_angle_ + tilt_angle_margin_)) &&
(angle >= min_detection_tilt_angle_ || (targets_[i]->is_present() && angle >= min_detection_tilt_angle_ - tilt_angle_margin_)))
{
targets_[i]->update_values(x, y, speed, distance_resolution);
else if (y >= max_detection_distance_ + max_distance_margin_)
}
else if (y > max_detection_distance_ + max_distance_margin_ ||
angle > max_detection_tilt_angle_ + tilt_angle_margin_ ||
angle < min_detection_tilt_angle_ - tilt_angle_margin_)
{
targets_[i]->clear();
}
}

int target_count = 0;
Expand Down
50 changes: 49 additions & 1 deletion components/LD2450/LD2450.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ namespace esphome::ld2450
#endif
#ifdef USE_NUMBER
SUB_NUMBER(max_distance)
SUB_NUMBER(max_angle)
SUB_NUMBER(min_angle)
#endif
#ifdef USE_BUTTON
SUB_BUTTON(restart)
Expand Down Expand Up @@ -148,14 +150,51 @@ namespace esphome::ld2450
fast_off_detection_ = value;
}

/**
* @brief Sets the maximum tilt angle which is detected (clamped between min tilt angle and 90)
* @param angle maximum detected angle in degrees
* @return the new (clamped) max tilt value
*/
float set_max_tilt_angle(float angle)
{
if (!std::isnan(angle))
max_detection_tilt_angle_ = std::max(std::min(angle, 90.0f), min_detection_tilt_angle_ + 1.0f);
return max_detection_tilt_angle_;
}

/**
* @brief Sets the minimum tilt angle which is detected (clamped between max tilt angle and -90)
* @param angle minimum detected angle in degrees
* @return the new (clamped) min tilt value
*/
float set_min_tilt_angle(float angle)
{
if (!std ::isnan(angle))
min_detection_tilt_angle_ = std::min(std::max(angle, -90.0f), max_detection_tilt_angle_ - 1.0f);
return min_detection_tilt_angle_;
}

/**
* @brief Sets the margin which used for angle limitations
* This margin is added to the min/max tilt angle, such that detected targets still counts as present, even though they are outside of the min/max detection angle. This can be used to reduce flickering.
* @param angle angle in degrees
*/
void set_tilt_angle_margin(float angle)
{
if (!std ::isnan(angle))
tilt_angle_margin_ = angle;
}

/**
* @brief Sets the maximum detection distance
* @param distance maximum distance in meters
* @return the new maximum distance
*/
void set_max_distance(float distance)
float set_max_distance(float distance)
{
if (!std ::isnan(distance))
max_detection_distance_ = int(distance * 1000);
return distance;
}

/**
Expand Down Expand Up @@ -358,6 +397,15 @@ namespace esphome::ld2450
/// @brief Nr of times the command has been written to UART
int command_send_retries_ = 0;

/// @brief The maximum detection angle in degrees
float max_detection_tilt_angle_ = 90;

/// @brief The minimum detection angle in degrees
float min_detection_tilt_angle_ = -90;

/// @brief The margin added to tilt angle detection limitations
float tilt_angle_margin_ = 5;

/// @brief The maximum detection distance in mm
int16_t max_detection_distance_ = 6000;

Expand Down
Loading
Loading