简体   繁体   中英

android: how to control the frequency of vibration via ON/OFF pattern

I'm trying to control the vibration frequency of android phone.

I understand android API provides an interface to control the vibration via ON/OFF patterns:

public void vibrate (long[] pattern, int repeat)

Vibrate with a given pattern.

Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

Therefore, I thought that, to get a vibration frequency N, maybe I can repetitively turn the vibrator on and off N times in one second.

For example, to get a 10-second vibration of 20Hz, I assign the pattern as below:

Vibrator vib = (Vibrator)this.context.getSystemService(Context.VIBRATOR_SERVICE);
...
long[] arrPattern = new long[20*10];
for (int i =0; i<arrPattern.length; ++i) // each duty circle is 50 ms
{
    arrPattern[i] = (i%2==0)?1:49;
}
vib.vibrate(pattern, -1);

Base on this naive idea, I have conducted several experiments, in which I set the vibration frequency N to different values and attached an accelerometer to the phone and captured the acceleration data during the vibration.

After transforming these time-domain acceleration data into frequency domain via DFT, I notice there is always a significant power around 10Hz and 175Hz, no matter which vibration frequency I set.

I was wondering why does not it work?

The documentation clearly states that:

The first parameter should be an array of long variables that is interpreted in the following way, the value on index[0] ((and every value of the array that is on an index that (index%2==0)) ) acts as a buffer (how much time should pass between each subsequent vibration), and the values that are on any index of the array that it is (index%2!=0) act as a duration of the vibrating process (how many milliseconds should the vibrator keep going).

The second one is pretty self-explanatory and you also do not seem to need it. (passing -1 will only loop through your array once and not repeat)

Lets head on to your implementation: you create an array of long values with a length of 300, you fill it with ones and fourty-nines and you invoke.

If you want the vibration process to work to be like: 1 millisecond between each vibration and the vibrations to last for 49 milliseconds (SUM 50milliseconds whole process) you are right.

But keep in mind that such kind of precision is very difficult to implement and any miscalculations will starting to "build-up" therefore miscalculating by a fraction the frequency.

Your code is probably correct.

The reason you only see significant power around 10 and 175Hz is that the motor has a resonant frequency of around 175Hz. When you try to drive it different frequencies, you will not see a lot of movement.

10Hz is probably a harmonic of the resonant frequency, or given the long period, your driver is just manually overloading the spring to force movement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM