Advertisement
otakus

Untitled

Sep 7th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #define deadzone_xbox 15 //center deadzone
  2. #define offset_xbox 7 //compensate for center on stick
  3. #define deadzone_ps3 7
  4. #define offset_ps3 2
  5.  
  6. int vals[3]; //stoors the values, 1=steering position, 2=acceleration, 3=brake/reverse
  7. int pos, negOff, posOff, l2, r2;
  8.  
  9.  
  10. //The fallowing code works in a way to retain full range out of the sticks using deadzones. Each input range is calculated from the center offset+deadzone defined earlier to min and max values.
  11. //Corresponding adjustments are made for both >center position or < center position and added together. This way you can adjust the deadzone as much as you please and the value once you leave that deadzone will still be 1 instead of some higher value where the inputs are ignored until then.
  12.  
  13. void calcXboxInput(){
  14. pos=Xbox.getAnalogHat(LeftHatX_XBOX)>>8; //value returned is 16 bit(but only 10 bit ADC) and since lower bits are noisy just concatonate number to upper 8 bits, could also devide here but bit shifting is much faster
  15. negOff=offset_xbox-deadzone_xbox;
  16. posOff=offset_xbox+deadzone_xbox;
  17. l2=Xbox.getButton(L2_XBOX);
  18. r2=Xbox.getButton(R2_XBOX);
  19.  
  20. vals[0]=map(min(pos,negOff),negOff,-128,127,0)+map(max(pos,posOff),posOff,127,0,128);
  21. vals[1]=((l2>0)? l2: r2);
  22. vals[2]=((l2>0)? 1: 0);
  23. }
  24.  
  25. void calcPs3Input(){
  26. pos=PS3.getAnalogHat(LeftHatX)-128;
  27. negOff=offset_ps3-deadzone_ps3;
  28. posOff=offset_ps3+deadzone_ps3;
  29. l2=PS3.getAnalogButton(L2_ANALOG);
  30. r2=PS3.getAnalogButton(R2_ANALOG);
  31.  
  32. vals[0]=map(min(pos,negOff),negOff,-128,127,0)+map(max(pos,posOff),posOff,127,0,128);
  33. vals[1]=((l2>0)? l2: r2);
  34. vals[2]=((l2>0)? 1: 0);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement