-
Notifications
You must be signed in to change notification settings - Fork 0
/
weighdemo2.py
55 lines (48 loc) · 1.71 KB
/
weighdemo2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import cwiid
import time
def main():
#Connect to address given on command-line, if present
print 'Put Wiimote in discoverable mode now (press 1+2)...'
global wiimote
if len(sys.argv) > 1:
wiimote = cwiid.Wiimote(sys.argv[1])
else:
wiimote = cwiid.Wiimote()
wiimote.rpt_mode = cwiid.RPT_BALANCE | cwiid.RPT_BTN
wiimote.request_status()
balance_calibration = wiimote.get_balance_cal()
named_calibration = { 'right_top': balance_calibration[0],
'right_bottom': balance_calibration[1],
'left_top': balance_calibration[2],
'left_bottom': balance_calibration[3],
}
while True:
#print "Type q to quit, or anything else to report your weight"
#c = sys.stdin.read(1)
#if c == 'q':
# exit = True
wiimote.request_status()
print "%.2fkg" % (calcweight(wiimote.state['balance'], named_calibration) / 100.0, )
time.sleep(1)
return 0
def calcweight( readings, calibrations ):
"""
Determine the weight of the user on the board in hundredths of a kilogram
"""
weight = 0
for sensor in ('right_top', 'right_bottom', 'left_top', 'left_bottom'):
reading = readings[sensor]
calibration = calibrations[sensor]
print(sensor + ' (' + str(reading) +' '+ str(calibration) +')')
if reading > calibration[2]:
print "Warning, %s reading above upper calibration value" % sensor
# 1700 appears to be the step the calibrations are against.
# 17kg per sensor is 68kg, 1/2 of the advertised Japanese weight limit.
if reading < calibration[1]:
weight += 1700 * (reading - calibration[0]) / (calibration[1] - calibration[0])
else:
weight += 1700 * (reading - calibration[1]) / (calibration[2] - calibration[1]) + 1700
return weight
if __name__ == "__main__":
sys.exit(main())