WiiLi Wiki frontpage Include your post in the News Get links Hoteles Quito
WiiLi.org Forum Index WiiLi.org
a new revolution
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Wii-G! Use your WiiMote as a g-meter!!

 
Post new topic   Reply to topic    WiiLi.org Forum Index -> Wii Scripts
View previous topic :: View next topic  
Author Message
jamesjhs



Joined: 23 Apr 2008
Posts: 3
Location: England

Digg It
PostPosted: Wed Apr 23, 2008 1:41 pm    Post subject: Wii-G! Use your WiiMote as a g-meter!!

Version as of 20080428. Allows manual and automatic (precise to infinite iterations) calibration of Wii's 3 axes, two for each (+ve and -ve limits). Acts as on-screen accelerometer when used with provided http://tinyurl.com/6qdjks image file in Paint (Fullscreen). Cursor2 moves around the screen inside the limits set by the image to display acceleration values, or the Debug line displays absolutes.

WiiMote LEDs display g-force by lighting as follows:
1 1 1 1 : < 0.125 g
0 1 1 1 : < 0.25 g
0 0 1 1 : < 0.5 g
0 0 0 1 : <1 g
1 0 0 0 : > 1 g
1 1 0 0 : > 2 g
1 1 1 0 : > 4 g
1 1 1 1 : > 8 g

Output.txt file records events as program loops, recording total g, and acceleration in x, y and z axes for analysis in external editors.

Code:

//G-Force meter for Wii Controller
// x-axis = side-to-side (left down = +ve)
// y-axis = up-down (back down = +ve)
// z-axis = forward-backward (base down = +ve)
//
// This script allows you to use your WiiMote as a g-force meter, viewing
// g-values by the LEDs on the WiiMote and logging output (output.txt) of
// resultant g, and x-, y-, and z-axis acceleration in m/s-2.
//
// Script written by James Rowson, 2008.

// BEGIN SCRIPT

// This section sets the variables R_x, R_y, R_z from the initial WiiMote input for later use.

var.R_x = Wiimote.RawForceX
var.R_y = Wiimote.RawForceY
var.R_z = Wiimote.RawForceZ

var.xres = 1024  // Input screen X resolution
var.yres = 768   // Input screen Y resolution
var.iterations = 200 // Iterations in WM Calibration

if var.res = 0 then var.res = var.xres
if var.res > var.yres then var.res = var.yres / 2.5

var.g = 9.8105 // Acceleration due to gravity (m/s-2)
if Pressed(Esc) then ExitScript  // End Script key assignment

// I have included two modes of calibration, manual, and automatic. Automatic operation prompts to
// put the WiiMote into various positions on a flat surface to determine the max/min values of all three
// accelerometers in a more detailed way than WiiMote Calibration in GlovePIE. To skip calibration to enter
// manual values, hit 'M' at the prompt. This will show a debug showing the calibrated and (true) values from
// each accelerometer, in accordance to values you input for the max and min of each axis (also using said debug).
//
// Auto calibration mode doesn't save the calibration data so it's worth writing down values for calminX, calmaxX,
// etc., from the Variables screen before ending the script, and inserting them into the manual mode's variables.
//
// Following auto calibration or manual calibration, you will see acceleration values in m/s-2 for each
// accelerometer in the debug, as well as total (absolute) g-force reading. The WiiMote's LEDs light in the
// following order - <1g: 0 0 0 0; 1-2g: 1 0 0 0; 2-4g: 1 1 0 0; 4-8g: 1 1 1 0; >8g: 1 1 1 1.

if var.CalibMode = -1
var.calminX = -27.47125
var.calmaxX = 25.085
var.calX = (var.calminX + var.calmaxX) / 2
var.calminY = -23.9725
var.calmaxY = 27.01
var.calY = (var.calminY + var.calmaxY) / 2
var.calminZ = -26.605
var.calmaxZ = 26.98
var.calZ = (var.calminZ + var.calmaxZ) / 2

var.x2 = var.R_x - var.calX
var.y2 = var.R_y - var.calY
var.z2 = var.R_z - var.calZ

wiimote.leds = 9
debug = "Manual Mode. X="+var.x2+" ("+var.R_x+")  Y="+var.y2+" ("+var.R_y+")  Z="+var.z2+" ("+var.R_z+")  Enter = Continue"
if Pressed(Return) then var.CalibMode = 8

elseif var.CalibMode = 0

 debug = "Calibration Mode. Six positions required, press Space to proceed, M for manual override."
  if Pressed(Space) then var.CalibMode = var.CalibMode + 1
  if Pressed(M) then var.CalibMode = -1
  wiimote.leds = 1

elseif var.CalibMode = 1 // WiiMote Min X
 debug = "Place WiiMote on its right side and press Space to continue..." + var.R_x
 if pressed(space) then var.go = 1
 if var.go = 1
  if var.cm1 < var.iterations
   var.time = var.cm1 / var.iterations * 100
   debug = "Please wait, calibrating " + var.time + "%..."
   var.cm1 = var.cm1 + 1
   var.calminX1 = var.calminX1 + var.R_x
  else
   var.calminX = var.calminX1 / var.iterations
   debug = "WiiMote min X value = " + var.calminX + " Press Space to Continue..."
    if Pressed(Space)
     var.CalibMode = var.CalibMode + 1
    endif
  endif
 endif

 wiimote.leds = 3

elseif var.CalibMode = 2 // WiiMote Max X
 debug = "Place WiiMote on its left side and press Space to continue..." + var.R_x
 if pressed(space) then var.go = 2
 if var.go = 2
  if var.cm2 < var.iterations
   var.time = var.cm2 / var.iterations * 100
   debug = "Please wait, calibrating " + var.time + "%..."
   var.cm2 = var.cm2 + 1
   var.calmaxX1 = var.calmaxX1 + var.R_x
  else
   var.calmaxX = var.calmaxX1 / var.iterations
   debug = "WiiMote max X value = " + var.calmaxX + " Press Space to Continue..."
    if Pressed(Space)
     var.CalibMode = var.CalibMode + 1
    endif
  endif
 endif   
 wiimote.leds = 3

elseif var.CalibMode = 3 // WiiMote Min Y
 debug = "Place WiiMote on its front and press Space to continue..." + var.R_y
 if pressed(space) then var.go = 3
 if var.go = 3
  if var.cm3 < var.iterations
   var.time = var.cm3 / var.iterations * 100
   debug = "Please wait, calibrating " + var.time + "%..."
   var.cm3 = var.cm3 + 1
   var.calminY1 = var.calminY1 + var.R_y
  else
   var.calminY = var.calminY1 / var.iterations
   debug = "WiiMote min Y value = " + var.calminY + " Press Space to Continue..."
    if Pressed(Space)
     var.CalibMode = var.CalibMode + 1
    endif
  endif
 endif   
 wiimote.leds = 7

elseif var.CalibMode = 4 // WiiMote Max Y
 debug = "Place WiiMote on its back and press Space to continue..." + var.R_y
 if pressed(space) then var.go = 4
 if var.go = 4
  if var.cm4 < var.iterations
   var.time = var.cm4 / var.iterations * 100
   debug = "Please wait, calibrating " + var.time + "%..."
   var.cm4 = var.cm4 + 1
   var.calmaxY1 = var.calmaxY1 + var.R_y
  else
   var.calmaxY = var.calmaxY1 / var.iterations
   debug = "WiiMote max Y value = " + var.calmaxY + " Press Space to Continue..."
    if Pressed(Space)
     var.CalibMode = var.CalibMode + 1
    endif
  endif
 endif     
 wiimote.leds = 7

elseif var.CalibMode = 5 // WiiMote Min Z
 debug = "Place WiiMote on its IR screen and press Space to continue..." + var.R_z
 if pressed(space) then var.go = 5
 if var.go = 5
  if var.cm5 < var.iterations
   var.time = var.cm5 / var.iterations * 100
   debug = "Please wait, calibrating " + var.time + "%..."
   var.cm5 = var.cm5 + 1
   var.calminZ1 = var.calminZ1 + var.R_z
  else
   var.calminZ = var.calminZ1 / var.iterations
   debug = "WiiMote min Z value = " + var.calminZ + " Press Space to Continue..."
    if Pressed(Space)
     var.CalibMode = var.CalibMode + 1
    endif
  endif
 endif   
 wiimote.leds = 15
 
elseif var.CalibMode = 6 // WiiMote Max Z
 debug = "Place WiiMote on its IO port and press Space to continue..." + var.R_z
 if pressed(space) then var.go = 6
 if var.go = 6
  if var.cm6 < var.iterations
   var.time = var.cm6 / var.iterations * 100
   debug = "Please wait, calibrating " + var.time + "%..."
   var.cm6 = var.cm6 + 1
   var.calmaxZ1 = var.calmaxZ1 + var.R_z
  else
   var.calmaxZ = var.calmaxZ1 / var.iterations
   debug = "WiiMote max Z value = " + var.calmaxZ + " Press Space to Continue..."
    if Pressed(Space)
     var.CalibMode = var.CalibMode + 1
    endif
  endif
 endif
 wiimote.leds = 15

elseif var.CalibMode = 7 // Display results
 var.calX = ( var.calminX + var.calmaxX ) / 2
 var.calY = ( var.calminY + var.calmaxY ) / 2
 var.calZ = ( var.calminZ + var.calmaxZ ) / 2

 debug = "Corrections: X = " + var.calX + "  Y = " + var.calY + "  Z = " + var.calZ + "  Enter = Go!"
 
 wiimote.leds = 15

 if Pressed(Return) then var.CalibMode = var.CalibMode + 1

elseif var.CalibMode = 8

// Enters main running script

// Time generation script to generate time code if needed for output.txt file
var.time = time()
var.hraw = var.time * 24
var.hour = round(var.hraw)
if var.hour - var.hraw > 0 then var.hour = var.hour - 1
var.mraw = (var.hraw - var.hour)*60
var.mins = round(var.mraw)
if var.mins - var.mraw > 0 then var.mins = var.mins - 1
var.secs = (var.mraw - var.mins)*60

var.x2 = var.R_x - var.calX
var.y2 = var.R_y - var.calY
var.z2 = var.R_z - var.calZ
var.x3 = (abs(var.calminX) + abs(var.calmaxX)) /2
var.y3 = (abs(var.calminY) + abs(var.calmaxY)) /2
var.z3 = (abs(var.calminZ) + abs(var.calmaxZ)) /2

var.accX = (var.x2) / var.x3 * var.g
var.accY = (var.y2) / var.y3 * var.g
var.accZ = (var.z2) / var.z3 * var.g
var.tot_g = sqrt(var.accX ^2 + var.accY-2 + var.accZ ^2) / var.g

// Displays actual acceleration values from WiiMote (m/s-2)
debug = "a_x = " + var.accX + "  a_y = " + var.accY + "  a_z = " + var.accZ + "  tot_g = " + var.tot_g

if var.tot_g < 0.125 then var.leds = 15
if var.tot_g >0.125 then var.leds = 14
if var.tot_g >=0.25 then var.leds = 12
if var.tot_g >=0.5 then var.leds = 8
if var.tot_g >=1 then var.leds = 1
if var.tot_g >=2 then var.leds = 3
if var.tot_g >=4 then var.leds = 7
if var.tot_g >=8 then var.leds = 15

wiimote.leds = var.leds

var.tick = var.tick + 1
OutputToFile(var.tick + " " + var.tot_g + " " + var.accX + " " + var.accY + " " + var.accZ)

// Using screen X and Y resolution and the g-forces, this part of the script moves the cursor
// from the centre of the screen as a 2-axis accelerometer would do. Use Microsoft Paint
// or similar to display full-screen (CTRL+F in Paint) the image at http://tinyurl.com/6qdjks
// to view concentric circles according to g-force in x and y directions. Fiddle around with var.accX,
// var.accY and var.accZ references below to vary which axes plot in which direction.
//
// As always, press Esc to ExitScript.

Cursor1.CursorPosX = var.accX / var.g * (768 / 2.5) / 4 + var.xres / 2
Cursor1.CursorPosY = var.accZ / var.g * (768 / 2.5) / 4 + var.yres / 2

endif

_________________
Wiiiiiii!!


Last edited by jamesjhs on Mon Apr 28, 2008 1:22 am; edited 3 times in total
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    WiiLi.org Forum Index -> Wii Scripts All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group