Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After looking at the problem for a while, and not being not very mathematically talented, I came up with an overly simplified algorithm (compared to some of the PWM formulas I found after Dithermaster pointed me in that direction). A couple of assumptions I made were first that the short pulse width is always 1, and the long pulse width is an integer between 1 and the vibration duration. I also assumed the long pulse width is a linear function of the vibration strength. In particular, the latter assumption is not accurate. I'm guessing the function should be something more like a decibel calculation ("strength" of vibration is akin to "loudness" of a sound).</p> <p>Posting my simplified solution in case it is useful for anyone else who ends up here. This is close enough for the application I am using it for, but I would still like something better. If anyone posts an alternative answer, I'll test and accept it if it is better.</p> <pre><code>public long[] genVibratorPattern( float intensity, long duration ) { float dutyCycle = Math.abs( ( intensity * 2.0f ) - 1.0f ); long hWidth = (long) ( dutyCycle * ( duration - 1 ) ) + 1; long lWidth = dutyCycle == 1.0f ? 0 : 1; int pulseCount = (int) ( 2.0f * ( (float) duration / (float) ( hWidth + lWidth ) ) ); long[] pattern = new long[ pulseCount ]; for( int i = 0; i &lt; pulseCount; i++ ) { pattern[i] = intensity &lt; 0.5f ? ( i % 2 == 0 ? hWidth : lWidth ) : ( i % 2 == 0 ? lWidth : hWidth ); } return pattern; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload