title: mouse controlled by an IMU
body: <p>I am using pitch and yaw angle from an IMU to move a mouse pointer for PC in vb.net. 
  Unfortunately it doesn't work steady.</p>
  
  <p>First I set the max angles which should be performed
  LOAD-Event  </p>
  
  <pre><code> 'set max angles in degree
    maxAngle_YawRight = 30
    maxAngle_YawLeft = -30
    maxAngle_PitchUp = 20
    maxAngle_PitchDown = -20
    calculateCurParameter(maxAngle_YawRight, Panel_Right.Location.X, maxAngle_YawLeft, Panel_Left.Location.X + Panel_Left.Width, maxAngle_PitchUp, Panel_Top.Location.Y+Panel_Top.Height, maxAngle_NickDown, Panel_Bottom.Location.Y)
  </code></pre>
  
  <p>I use four these max angles (pitch_up, pitch_down, yaw_right, yaw_left) to assign them to a part of my form, to get 4 points, e.g. (pitch_up,panel_2.location.Y)
  pitch_up and pitch_down are used for the y coordinate of the mouse pointer and yaw_right and yaw_left are used for the x coordinate. This is done by function calculateCurParameter.</p>
  
  <p>Then I use this four points to calculate parameters for an linear equation:</p>
  
  <p>CursorPos=m*angle+b </p>
  
  <p>So you get for every new angle a new Cursor Position.</p>
  
  <pre><code>Private mX As Double
  Private bX As Double
  Private mY As Double
  Private bY As Double
  </code></pre>
  
  <p>...</p>
  
  <pre><code>Private Sub calculateCurParameter(ByVal maxYawRight As Double, ByVal rightBorder As Double, ByVal maxYawLeft As Double, ByVal leftBorder As Double, ByVal maxPitchUp As Double, ByVal topBorder As Double, ByVal maxPitchDown As Double, ByVal bottomBorder As Double)
  
  'this function calculates the parameter for Ycur=m*Xangle+b for each Pitch and Yaw
  
  'calculate the parameter for the CursorPosition X
  dim deltaX= maxYawLeft-maxYawRight
  dim deltaY=leftBorder-rightBorder
  mX=deltaY/deltaX
  bX=rightBorder-mX*maxYawRight
  
  'calculate the parameter for the CursorPosition Y
  deltaX=bottomBorder-topBorder
  deltaY=maxPitchDown-maxPitchUp
  mY=deltaY/deltaX
  bY=topBorder-mY*maxPitchUp
  end Sub
  </code></pre>
  
  <p>After that calculation, I use a quadratic weighted moving average (QWMA) as a low pass filter cursor outputs calculated by the function y=m*x+b.</p>
  
  <p>For that moving average I use the last 50 samples of each angle.
  After that I put the two angles in each linear equation(cursorPos=m*angle+b) as described.</p>
  
  <pre><code>            'Timer-Event which is triggered each 8ms
          ' yaw and pitch are in degrees and are updated in this timer event before
                      Dim xCoordCur = Math.Round(mX * yaw + bX)
                      Dim yCoordCur = Math.Round(mY * pitch + bY)
                      arrayCurY(cursorCounter) = yCoordCur
                      arrayCurX(cursorCounter) = xCoordCur
                      If cursorCounter =50 Then 'need 50 samples to do the QWMA
                          Dim aqwmY = qwma_calculating(arrayCurY)'function to calculate the QWMA, seems to be working
                          Dim aqwmX = qwma_calculating(arrayCurX)
              'the mouse pointer should not leave the form    
                          If aqwmX &gt; Panel_Right.Location.X Then
                              aqwmX = Panel_Right.Location.X
                          ElseIf aqwmX &lt; (Panel_Left.Location.X + Panel_Left.Width) Then
                              aqwmX = Panel_Left.Location.X + Panel_Left.Width
                          End If
                          If aqwmY &gt; Panel_Bottom.Location.Y Then
                              aqwmY = Panel_Bottom.Location.Y
                          ElseIf aqwmY &lt; (Panel_Top.Location.Y + Panel_Top.Height) Then
                              aqwmY = Panel_Top.Location.Y + Panel_Top.Height
                          End If
  
              'Set the new Cursor Position
                          Cursor.Position = New Point(aqwmX, aqwmY)
                          arrayValuesMoving(arrayCurX)'function to move the values one index forward
                          arrayValuesMoving(arrayCurY)
                      Else
                          cursorCounter += 1
                      End If
  </code></pre>
  
  <p>In the my last step I set the new Cursor-Position </p>
  
  <pre><code>Cursor.Position=New Point(xCoor,yCoor)
  </code></pre>
  
  <p>Now I can control the mouse pointer by moving the IMU, but it is very unstable.
  For example the mouse pointer still moves although the IMU doesn't move.</p>
  
  <p>It's even impossible to hover over some form elements.
  What did I do wrong?</p>
  
  <p>Thanks in advance!</p>