Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calculating average in a high priority thread

Calculating average in a high priority thread

Scheduled Pinned Locked Moved C / C++ / MFC
game-devhelpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 9350237
    wrote on last edited by
    #1

    Hi Guys!, I'm developing a simple tracking game using two haptic devices as controllers. Haptic rendering is executed in a high priority thread. During the experiment, force experienced will depends on the position of each player, it's calculated as follows:

    coupledForce1 = cMul(Kp, (newPosition2-newPosition1))-(cMul(Kb, (linearVelocity1)));

    coupledForce2 = cMul(Kp, (newPosition1-newPosition2))-(cMul(Kb, (linearVelocity2)));

    I have 100 trials of tracking tasks and I have to calculate the average interaction force during each trial. How it can be done? my issue is: this manipulation have to be done in a thread with execution rate of around 8KHz Thanks!

    J 1 Reply Last reply
    0
    • M Member 9350237

      Hi Guys!, I'm developing a simple tracking game using two haptic devices as controllers. Haptic rendering is executed in a high priority thread. During the experiment, force experienced will depends on the position of each player, it's calculated as follows:

      coupledForce1 = cMul(Kp, (newPosition2-newPosition1))-(cMul(Kb, (linearVelocity1)));

      coupledForce2 = cMul(Kp, (newPosition1-newPosition2))-(cMul(Kb, (linearVelocity2)));

      I have 100 trials of tracking tasks and I have to calculate the average interaction force during each trial. How it can be done? my issue is: this manipulation have to be done in a thread with execution rate of around 8KHz Thanks!

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Please don't repost questions. You have already asked this here Calculating average in a thread[^].

      M 1 Reply Last reply
      0
      • J Jochen Arndt

        Please don't repost questions. You have already asked this here Calculating average in a thread[^].

        M Offline
        M Offline
        Member 9350237
        wrote on last edited by
        #3

        my bad!, just removed

        J 1 Reply Last reply
        0
        • M Member 9350237

          my bad!, just removed

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          Thank you. The problem with duplicate questions is that you may get answers from different users to both questions. A clarification question to yours: Do you need code to calculate the average? If so, you may search for "moving average" or "rolling average".

          M 1 Reply Last reply
          0
          • J Jochen Arndt

            Thank you. The problem with duplicate questions is that you may get answers from different users to both questions. A clarification question to yours: Do you need code to calculate the average? If so, you may search for "moving average" or "rolling average".

            M Offline
            M Offline
            Member 9350237
            wrote on last edited by
            #5

            Thank you!. I have already seen some implementation of moving average. But I'm not sure about how to implement for my application. Since it's in a thread which is executing at 8Khz, I'm wondering how to store forces trial by trial and to take avareage?

            J 1 Reply Last reply
            0
            • M Member 9350237

              Thank you!. I have already seen some implementation of moving average. But I'm not sure about how to implement for my application. Since it's in a thread which is executing at 8Khz, I'm wondering how to store forces trial by trial and to take avareage?

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              See this Stackoverflow thread: http://stackoverflow.com/questions/10990618/calculate-rolling-moving-average-in-c-or-c[^]. The fastest solution would be a ring buffer and a total sum variable (untested):

              int items = 0;
              int ndx = 0;
              int buffer[buffer_Size];
              // May need floating point here when max. value * buffer_size >= INT_MAX
              int sum = 0;

              int get_av(int val)
              {
              if (items >= buffer_size)
              sum -= buffer[ndx];
              else
              ++items;
              sum += val;
              buffer[ndx] = val;
              if (++ndx >= buffer_size)
              ndx = 0;
              return sum / items;
              }

              This should be fast enough for your requirements

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups