GithubHelp home page GithubHelp logo

Comments (19)

RobTillaart avatar RobTillaart commented on June 5, 2024

Thanks for your question,

It is not 100% clear what you need.
If I understand the question correctly you want the output always be in the range 0 .. 359 degrees?
Or is it just the opposite, that you want it to go to 360 then to 720, 1080 and so on.

Problem with the latter is that at some moment you loose precision causing the math to be incorrect.
(that is what the library does if I am correct, do not recall testing that.)

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

Is your goal to calculate the RPM rounds per minute ?

from gy521.

Estebanmister avatar Estebanmister commented on June 5, 2024

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

@Estebanmister

Quick patch is to change the following lines in the GY521.cpp file (around line 195)

  _gax += _gx * duration;
  _gay += _gy * duration;
  _gaz += _gz * duration;

to

  _gax += _gx * duration;
  _gay += _gy * duration;
  _gaz += _gz * duration;

  if (_gax < 0) _gax += 360.0;
  else if (_gax >= 360.0) _gax -= 360.0;
  if (_gay < 0) _gay += 360.0;
  else if (_gay >= 360.0) _gay -= 360.0;
  if (_gaz < 0) _gaz += 360.0;
  else if (_gaz >= 360.0) _gaz -= 360.0;

If you always rotate in the same direction you might need less tests, however this is the robust way.

give it a try

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

mmm, (thinking sounds)
at second thought pitch and roll still can under / overflow the 360.

you also need

  _yaw   = _gaz;   
  _pitch = 0.96 * _gay + 0.04 * _aay;
  _roll  = 0.96 * _gax + 0.04 * _aax;

  if (_pitch < 0) _pitch += 360;
  else if (_pitch >= 360) _pitch -= 360;
  if (_roll  < 0) _roll  += 360;
  else if (_roll  >= 360) _roll  -= 360;

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

You need both as you do still not want an overflow of the _gax, _gay and _gaz values.

I'll prepare a new branch

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

Develop branch created,
Please verify if it works for you

from gy521.

Estebanmister avatar Estebanmister commented on June 5, 2024

Develop branch created, Please verify if it works for you

Just tried the fixes above, it works!! thanks!

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

How fast does your setup rotate?
If possible could you post a photo?
Just interested.

from gy521.

Estebanmister avatar Estebanmister commented on June 5, 2024

image

Fast enough for persistence of vision to kick in :), currently using the gyro to calculate RPS

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

Looks pretty cool, well done 👍

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

How many RPS does it make, 25?

from gy521.

Estebanmister avatar Estebanmister commented on June 5, 2024

yeah about 20-25, but right now I'm having some problems with drifting(the position of the 'H' drifts in the direction that the device is spinning), probably because i depend on the gy521 for reaching the angle 0 (to then proceed with the function that turns the LEDs on) I should add a hall sensor and a magnet.

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

you can try to prevent drifting by predicting the angle 0 moment.
Say you have 25 RPS, then the angle 0 is 40.000 us after the last one.

Then use the GY521 to adjust the 40.000 us with the angle you measure after predicted time.
just in steps of 4-40 us.

suppose expected time = 40000
Instead of Angle 0 you measure 342 then you know you are 18 degrees == 1/20 too early
So 40000 = 342/360 x timeNeeded ==> timeNeeded = 40000 x 360/342 = 42105 us for 360°

As you are 18 degrees of the 0 angle the sync moment will be at 42105 - 40000 = 2105 usec.

This method should converge quite fast to a good value, and adapts to speed variations quite well.
(there are variations on this method possible)

A point of attention is that you need to include the time your math makes.

just some

BTW, you can add a round weight to the axis to get a flywheel effect which helps to dampen variations.

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

Will merge and release new version tomorrow.
Did also some cosmetic changes to the code.

from gy521.

Estebanmister avatar Estebanmister commented on June 5, 2024

you can try to prevent drifting by predicting the angle 0 moment. Say you have 25 RPS, then the angle 0 is 40.000 us after the last one.

Then use the GY521 to adjust the 40.000 us with the angle you measure after predicted time. just in steps of 4-40 us.

suppose expected time = 40000 Instead of Angle 0 you measure 342 then you know you are 18 degrees == 1/20 too early So 40000 = 342/360 x timeNeeded ==> timeNeeded = 40000 x 360/342 = 42105 us for 360°

As you are 18 degrees of the 0 angle the sync moment will be at 42105 - 40000 = 2105 usec.

This method should converge quite fast to a good value, and adapts to speed variations quite well. (there are variations on this method possible)

A point of attention is that you need to include the time your math makes.

just some

BTW, you can add a round weight to the axis to get a flywheel effect which helps to dampen variations.

But how would we know if we are late instead of early? methinks this would end up with the device doing two rotations just to get to 0.

image
So I tried to implement this, but it doesn't seem to work, the draw() function makes the LEDs turn at random locations, changing the speed doesn't seem to improve things (I ran it at like, 1 rps, just to be sure).

The 'Strt' variable is the time of the previous loop, 'previous_reading' is the yaw of the previous loop. The highlighted part is your algorithm

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

If the read is too late the idea is pretty similar.
Suppose the angle < 180, then you should add 360 to it. Then the factor 360.0 / yaw is smaller than 1 thus reducing the time.
Be sure to use 360.0 for constants so all your math is done in float. An integer division would break the algorithm.

Instead of calculating the new time you can just increase or decrease the estimate e.g with 20 us. Then the value will slowly converge to the right one.

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

Idea
Code wise it might be easier to detect and work with the 180 degrees. Then handling over and undershoot would be handled by the same lines of code.

The 0 angle would be halve a rotation later.

from gy521.

RobTillaart avatar RobTillaart commented on June 5, 2024

Note to myself.

Q: Is it possible to optimize the under- overflow correction math?

Now the overflow detection math is done at every call => expensive => always 10 to 15 float operations in read function.
There is no 100% guarantee that e.g. if _gax overflows and is corrected, that roll does not need correction anymore.

The only optimization I can think of is if you know the direction of rotation, the order of test < 0 and >360 will make a small difference. In fact if one direction is used consequently only one test is needed. For a generic library this is not knowable.

from gy521.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.