Close

Ballistic Mouse Algorithm

A project log for Chording Hybrid Keyboard

Based on experience with the previous chording keyboard, fully 3D printable version with two joysticks.

ptravptrav 12/26/2017 at 00:370 Comments

Upon experimenting with the mouse pointer, I discovered that control it not accurate at short distances.  If you need to move the mouse pointer by several pixels, have to be very careful with the joystick. The solution is to implement a "ballistic" algorithm, and give the mouse pointer some "virtual mass".

#define _NULLMOUSE (signed char)0
    if( data < -10)
    {
      if( pot_Position[i] > 0) pot_Position[i] = _NULLMOUSE; 
      if( pot_Position[i] > -data*data/20) pot_Position[i]-=2; 
    }
    if( -10 <= data && data < -4) pot_Position[i] = (signed char)(-1);
    if( -4 <= data && data <= 4) pot_Position[i] = (signed char)0;
    if( 4 < data && data <= 10) pot_Position[i] = (signed char)1;
    if( 10 < data)
    {
      if( pot_Position[i] < 0) pot_Position[i] = _NULLMOUSE; 
      if( pot_Position[i] < data*data/20) pot_Position[i]+=2; 
    }
 

Basically, if the joystick cap moves more than 1 mm, the pointer starts accelerating towards the maximum speed of 80 pix/dt and reaches the max after 40*50=2000 ms. If the cap is moved less than 1  mm, the pointer crawls at 1 pix/dt, providing accurate pointing. Releasing the joystick (position less than 0.4 mm) causes an instantaneous stop; -- well -- the joystick itself has some minor mechanical inertia.

I left the dt=50 ms for now. 80 pix/dt is darn fast -- 1600 pix per second, so the mouse pointer flies from one end of a large monitor to the other in about a second, which is comparable to the classic mouse agility. After reaching the wanted screen corner, one just needs to release the joystick momentarily, and then precisely point the wanted pixel.
The algorithm is fully integer-based and suitable for Arduino.

Discussions