Guest User

Untitled

a guest
Nov 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. class DirectionSolver{
  2.   import oscP5.*;
  3.   import netP5.*;  
  4.  
  5.   OscP5 osc;
  6.  
  7.   float value;
  8.   float totalV;
  9.   int numReadings = 10;
  10.   float runningTotal = 0;
  11.   int thresh = 50;
  12.   float[] readings = new float[numReadings];
  13.   int index = 0;
  14.   float average = 0;
  15.  
  16.   float currentRot = 0;
  17.  
  18.   DirectionSolver(){
  19.     // initialize all the readings to 0:
  20.     for (int thisReading = 0; thisReading < numReadings; thisReading++)
  21.       readings[thisReading] = 0;
  22.      
  23.     // Listen to port
  24.     osc = new OscP5(this, 12000);
  25.  
  26.     // Osc plug      Function name      Plug name
  27.     //  |                |                |
  28.     osc.plug(this, "setValue", "/device/4/component/GyroZ");
  29.     osc.plug(this, "resetRotation", "/device/4/component/Btn_MOVE");
  30.   }
  31.  
  32.   void setValue(float theValue) {
  33.     value = theValue;
  34.   }
  35.  
  36.   void resetRotation(float moveButton){
  37.     if(moveButton == 1.0)
  38.       currentRot = 0;
  39.   }
  40.  
  41.   void update(){
  42.     runningTotal = runningTotal - readings[index];//subtract the last reading
  43.     readings[index] = value;//read from OSC
  44.  
  45.     runningTotal = runningTotal+readings[index];//add the next position to the total
  46.     index ++;
  47.  
  48.     if(index >= numReadings)
  49.       index = 0;
  50.    
  51.     average = runningTotal / numReadings;
  52.  
  53.     currentRot += average;
  54.   }
  55.  
  56.   float getDirection(){
  57.     return currentRot;
  58.   }
  59. }
Add Comment
Please, Sign In to add comment