Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"cSpell.words": [
"calcweight",
"comx",
"comy",
"fivethirtyeight",
"KEYDOWN",
"KEYUP",
"libardrone",
"navdata",
"NUNCHUK",
"Wiimote"
]
}
168 changes: 168 additions & 0 deletions Example/balanceGraph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import numpy as np
import cwiid, time # noqa: E401

button_delay = 0.5
TOP_RIGHT = 0
BOTTOM_RIGHT = 1
TOP_LEFT = 2
BOTTOM_LEFT = 3

style.use('fivethirtyeight')

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

print('Please press buttons 1 + 2 on your Wiimote now ...')
time.sleep(1)

# This code attempts to connect to your Wiimote and if it fails the program quits
try:
wii=cwiid.Wiimote()
except RuntimeError:
print("Cannot connect to your Wiimote. Run again and make sure you are holding buttons 1 + 2!")
quit()
wii.rpt_mode = cwiid.RPT_BALANCE
"""
b2i = lambda b: int(b.encode("hex"), 16) # noqa: E731

calibration = wii.get_balance_cal()

def get_mass(data):
return {
'top_right': calc_mass(data["right_top"], TOP_RIGHT),
'bottom_right': calc_mass(data["right_bottom"], BOTTOM_RIGHT),
'top_left': calc_mass(data["left_top"], TOP_LEFT),
'bottom_left': calc_mass(data["left_bottom"], BOTTOM_LEFT),
}

def calc_mass(raw, pos):
# Calculates the Kilogram weight reading from raw data at position pos
# calibration[0] is calibration values for 0kg
# calibration[1] is calibration values for 17kg
# calibration[2] is calibration values for 34kg
if raw < calibration[pos][0]:
return 0.0
elif raw < calibration[pos][1]:
return 17 * ((raw - calibration[pos][0]) /
float((calibration[pos][1] -
calibration[pos][0])))
else: # if raw >= calibration[pos][1]:
return 17 + 17 * ((raw - calibration[pos][1]) /
float((calibration[pos][2] -
calibration[pos][1])))

def animate(i):
mass = get_mass(wii.state["balance"])
comx = 1.0
comy = 1.0
try:
total_right = mass['top_right'] + mass['bottom_right']
total_left = mass['top_left'] + mass['bottom_left']
comx = total_right / total_left
if comx > 10:
comx = 10 - total_right / total_left
else:
comx -= 10
total_bottom = mass['bottom_left'] + mass['bottom_right']
total_top = mass['top_left'] + mass['top_right']
comy = total_bottom / total_top
if comy > 10:
comy = 10 - total_top / total_bottom
else:
comy -= 10
except BaseException:
pass
comx = round(comx, 2)
comy = round(comy, 2)
print("Center of mass: %s"%str({'x': comx, 'y': comy}))
# plot(x,y) using pygame or any other GUI
#ax1.clear()
ax1.plot(comx, comy, "bo")
"""

balance_calibration = wii.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],
}


def get_vals():
weights = []
readings = wii.state["balance"]
for sensor in ('right_top', 'right_bottom', 'left_top', 'left_bottom'):
reading = readings[sensor]
calibration = named_calibration[sensor]

if reading > calibration[2]*2:
print("Warning", sensor, "reading above upper calibration value")
# 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]:
weights.append(1700 * (reading - calibration[0]) / (calibration[1] - calibration[0]))
else:
weights.append(1700 * (reading - calibration[1]) / (calibration[2] - calibration[1]) + 1700)
return weights

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]

# 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

def calculate_centroid(sensor_readings, sensor_positions):
# Normalize sensor readings
normalized_readings = sensor_readings / np.sum(sensor_readings)

# Calculate relative positions
relative_positions = np.dot(normalized_readings, sensor_positions)

return relative_positions

def convert_relative_positions_to_coordinates(relative_positions, scaling_factor):
# Convert relative positions to coordinates
coordinates = relative_positions * scaling_factor

if round(((calcweight(wii.state['balance'], named_calibration) / 100.0)*2.205)-add, 1) > 35.0:
return coordinates
else:
ax1.clear()
return [0,0]

# Example usage
sensor_positions = np.array([[68, 68], [68, -68], [-68, 68], [-68, -68]])
scaling_factor = 1

time.sleep(3)
print("Type anything to balance weight")
c = input()
add = (calcweight(wii.state['balance'], named_calibration) / 100.0)*2.205
wii.request_status()
print("Step On!")
time.sleep(3)

def animate(i) -> None:
centroid = calculate_centroid(get_vals(), sensor_positions)
coordinates = convert_relative_positions_to_coordinates(centroid, scaling_factor)

ax1.plot(coordinates[0], coordinates[1], "go")

ani = animation.FuncAnimation(fig, animate, interval=10)
plt.show()
63 changes: 63 additions & 0 deletions Example/fly_readings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import cwiid, time

button_delay = 0.5

print('Please press buttons 1 + 2 on your Wiimote now ...')
time.sleep(1)

# This code attempts to connect to your Wiimote and if it fails the program quits
try:
Wii=cwiid.Wiimote()
except RuntimeError:
print("Cannot connect to your Wiimote. Run again and make sure you are holding buttons 1 + 2!")
quit()

print('Wiimote connection established!\n')
print('Go ahead and press some buttons\n')
print('Press PLUS and MINUS together to disconnect and quit.\n')

time.sleep(3)
Wii.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC | cwiid.RPT_EXT
Wii.led = 5
#Here we handle the nunchuk, along with the joystick and the buttons
while(1):
time.sleep(0.2)
if Wii.state.get('nunchuk'):
try:
#Here is the data for the nunchuk stick:
#X axis:LeftMax = 25, Middle = 125, RightMax = 225
NunchukStickX = (Wii.state['nunchuk']['stick'][cwiid.X])
#Y axis:DownMax = 30, Middle = 125, UpMax = 225
NunchukStickY = (Wii.state['nunchuk']['stick'][cwiid.Y])
#The 'NunchukStickX' and the 'NunchukStickY' variables now store the stick values

#Make it so that we can control the arm with the joystick
if (NunchukStickX < 60):
print("left")
if (NunchukStickX > 190):
print("right")
if (NunchukStickY < 60):
print("down")
if (NunchukStickY > 190):
print("up")

#Here we create a variable to store the nunchuck button data
#0 = no buttons pressed
#1 = Z is pressed
#2 = C is pressed
#3 = Both C and Z are pressed
ChukBtn = Wii.state['nunchuk']['buttons']
if (ChukBtn == 1):
print("Z btn")
if (ChukBtn == 2):
print("C btn")
#If both are pressed the led blinks
if (ChukBtn == 3):
print("both btn")


except:
pass
else:
print(Wii.state)
time.sleep(1)
62 changes: 62 additions & 0 deletions Example/weightdemo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import sys
from time import sleep
import cwiid

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
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],
}

sleep(3)
print("Type anything to balance weight")
c = sys.stdin.read(1)
add = (calcweight(wiimote.state['balance'], named_calibration) / 100.0)*2.205
wiimote.request_status()
print("Step On!")
sleep(3)

while True:
print("Type q to quit, or anything else to report your weight")
c = input()
if c == 'q':
break
wiimote.request_status()
print(round(((calcweight(wiimote.state['balance'], named_calibration) / 100.0)*2.205)-add, 1), "Lbs")

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]

if reading > calibration[2]:
print("Warning", sensor, "reading above upper calibration value")
# 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())
Loading