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

[Request]: atan2 sensor #104

Open
leon0399 opened this issue Feb 28, 2024 · 1 comment
Open

[Request]: atan2 sensor #104

leon0399 opened this issue Feb 28, 2024 · 1 comment
Labels
enhancement New feature or request sweep Assigns Sweep to an issue or pull request.

Comments

@leon0399
Copy link
Member

leon0399 commented Feb 28, 2024

Implementation PR

No response

Reference Issues

No response

Summary

Create a new sensor that utilizes the atan2 function to calculate the angle from two analog inputs: cos and sin

Basic Example

auto* sin_sensor = new AnalogSensor(PIN_SIN);
auto* cos_sensor = new AnalogSensor(PIN_COS);

// (0.0F, 1.0F) => (-1.0F, 1.0F)
auto* sincos_filter = new LambdaFilter<float>([](float value) {
    return value * 2.0F - 1.0F;
});

sin_sensor->addFilter(sincos_filter);
cos_sensor->addFilter(sincos_filter);

atan2_sensor = new Atan2Sensor(sin_sensor, cos_sensor);
/// Sensor that calculates the atan2 of two sensors
template<typename Tp = float>
class Atan2Sensor : public Sensor<Tp> {
    static_assert(std::is_floating_point_v<Tp>, "Tp must be a floating point type");

  public:
    using Source = Sensor<Tp>;

    Atan2Sensor(Source* sin, Source* cos) : sin_(sin), cos_(cos){};

    void init() override
    {
        SS_SUBSENSOR_INIT(this->sin_, false, [this](float /*value*/) {
            this->recalculateState();
        });

        SS_SUBSENSOR_INIT(this->cos_, false, [this](float /*value*/) {
            this->recalculateState();
        });
    }

    inline void tick() override { this->recalculateState(); }

    void recalculateState()
    {
        const Tp sin = this->sin_->getValue();
        const Tp cos = this->cos_->getValue();

        if (sin == 0.0F && cos == 0.0F) {
            this->publishState(0.0F);
            return;
        }

        const Tp radians = std::atan2(sin, cos);

        this->publishState(radians);
    }

  private:
    Source* sin_;
    Source* cos_;
};

More examples:

  1. https://github.com/LucidVR/lucidgloves/blob/proto5/firmware/lucidgloves-firmware/input.ino#L315

Drawbacks

  1. The approach above requires applying a custom filter to the sensor.

Unresolved questions

No response

@leon0399 leon0399 added question Further information is requested enhancement New feature or request sweep Assigns Sweep to an issue or pull request. and removed question Further information is requested labels Feb 28, 2024
Copy link
Contributor

sweep-ai bot commented Feb 28, 2024

Sweeping

25%


Actions (click)

  • ↻ Restart Sweep

❌ Unable to Complete PR

Sorry, Sweep could not find any appropriate files to edit to address this issue. If this is a mistake, please provide more context and Sweep will retry!

@leon0399, please edit the issue description to include more details about this issue.

For bonus GPT-4 tickets, please report this bug on Discord (tracking ID: 3e0f09aa9a).


🎉 Latest improvements to Sweep:
  • New dashboard launched for real-time tracking of Sweep issues, covering all stages from search to coding.
  • Integration of OpenAI's latest Assistant API for more efficient and reliable code planning and editing, improving speed by 3x.
  • Use the GitHub issues extension for creating Sweep issues directly from your editor.

💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.

This is an automated message generated by Sweep AI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sweep Assigns Sweep to an issue or pull request.
Projects
None yet
Development

No branches or pull requests

1 participant