multiple wiimotes

Official forum for open source FreePIE discussion and development.
Post Reply
Reappearance
One Eyed Hopeful
Posts: 2
Joined: Sun Sep 04, 2016 11:47 am

multiple wiimotes

Post by Reappearance »

I have a problem with using acceleration data from multiple wiimotes.
For 1 wiimote it is easy to get all axis (wimote[0].acceleration.x). Same for Nunchuck.
The problem starts when I try to use all axis from both the wiimote and the nunchuck for 2 controlers.
It seems to be random what will work when I start a script.
It's either no axis working at all, or only data from wiimote / nunchuck are available.
However all the buttons are always working correctly.

There is also anoher issue:
filters.stopWatch does not work correcty for 2 controlers with nunchucks.

Have you experienced any of those problems?
Reappearance
One Eyed Hopeful
Posts: 2
Joined: Sun Sep 04, 2016 11:47 am

Re: multiple wiimotes

Post by Reappearance »

here's he script that I use:

Code: Select all

def map_pov(n):
	map_pov_digital(n)

def map_pov_analog(n):
	val = 0
	maxVal = vJoy[n].continuousPovMax
	
	if wiimote[n].buttons.button_down(WiimoteButtons.DPadUp):
		val = 0 # UP
	if wiimote[n].buttons.button_down(WiimoteButtons.DPadRight):
		val += maxVal/8 # UP/RIGHT
	elif wiimote[n].buttons.button_down(WiimoteButtons.DPadLeft):
		val = 7 * maxVal/8 # UP/LEFT
	elif wiimote[n].buttons.button_down(WiimoteButtons.DPadRight):
		val = maxVal/4 # RIGHT
	if wiimote[n].buttons.button_down(WiimoteButtons.DPadDown):
		val += maxVal/8 # DOWN/RIGHT
	elif wiimote[n].buttons.button_down(WiimoteButtons.DPadDown):
		val = maxVal/2 # DOWN
	if wiimote[n].buttons.button_down(WiimoteButtons.DPadLeft):
		val += maxVal/8 # DOWN/LEFT
	elif wiimote[n].buttons.button_down(WiimoteButtons.DPadLeft):
		val = 3 * maxVal/4 # LEFT
	else:
		val = maxVal
	
	vJoy[n].setAnalogPov(0, val)

def map_pov_digital(n):
	map_button(n, WiimoteButtons.DPadUp, 9)
	map_button(n, WiimoteButtons.DPadRight, 10)
	map_button(n, WiimoteButtons.DPadLeft, 11)
	map_button(n, WiimoteButtons.DPadDown, 12)


def map_axis_wiimote(n):
	global wiimote_calibration
	map_vJoy_slider2(n,
		filters.simple(wiimote[n].acceleration.x, 0.1),
		wiimote_calibration[n].minX,
		wiimote_calibration[n].maxX,
		vJoy[n].axisMax)
	map_vJoy_ry(n,
		filters.simple(wiimote[n].acceleration.y, 0.1),
		wiimote_calibration[n].minY,
		wiimote_calibration[n].maxY,
		-vJoy[n].axisMax)
#	map_vJoy_rx(n,
#		filters.deadband(wiimote[n].acceleration.z, 0.5),
#		wiimote_calibration[n].minZ,
#		wiimote_calibration[n].maxZ,
#		vJoy[n].axisMax)
	
def map_axis_nunchuck(n):
	global nunchuch_calibration
	vJoy[n].x = filters.mapRange(filters.deadband(wiimote[n].nunchuck.stick.x, 5), -95, 95, -vJoy[n].axisMax,  vJoy[n].axisMax)
	vJoy[n].y = -filters.mapRange(filters.deadband(wiimote[n].nunchuck.stick.y, 5), -95, 95, -vJoy[n].axisMax,  vJoy[n].axisMax)
	map_vJoy_slider(n,
		filters.deadband(wiimote[n].nunchuck.acceleration.x, 0.1),
		nunchuch_calibration[n].minX,
		nunchuch_calibration[n].maxX,
		vJoy[n].axisMax)		
	map_vJoy_z(n,
		filters.deadband(wiimote[n].nunchuck.acceleration.y, 0.1),
		nunchuch_calibration[n].minY,
		nunchuch_calibration[n].maxY,
		vJoy[n].axisMax)
#	map_vJoy_rz(n,
#		filters.deadband(wiimote[n].nunchuck.acceleration.z, 0.5),
#		nunchuch_calibration[n].minZ,
#		nunchuch_calibration[n].maxZ,
#		vJoy[n].axisMax)			

def map_vJoy_slider(n, map_from, min_from, max_from, axisMax): 
	try:
		vJoy[n].slider = filters.mapRange(
			map_from,
			min_from,
			max_from,
			-axisMax,
			axisMax)
	except (OverflowError):
		pass

def map_vJoy_slider2(n, map_from, min_from, max_from, axisMax): 
	try:
		vJoy[n].dial = filters.mapRange(
			map_from,
			min_from,
			max_from,
			-axisMax,
			axisMax)
	except (OverflowError):
		pass

def map_vJoy_z(n, map_from, min_from, max_from, axisMax): 
	try:
		vJoy[n].z = -filters.mapRange(
			map_from,
			min_from,
			max_from,
			-axisMax,
			axisMax)
	except (OverflowError):
		pass
		
def map_vJoy_rz(n, map_from, min_from, max_from, axisMax): 
	try:
		vJoy[n].rz = filters.mapRange(
			map_from,
			min_from,
			max_from,
			-axisMax,
			axisMax)
	except (OverflowError):
		pass

def map_vJoy_rx(n, map_from, min_from, max_from, axisMax): 
	try:
		vJoy[n].rx = -filters.mapRange(
			map_from,
			min_from,
			max_from,
			-axisMax,
			axisMax)
	except (OverflowError):
		pass

def map_vJoy_ry(n, map_from, min_from, max_from, axisMax): 
	try:
		vJoy[n].ry = filters.mapRange(
			map_from,
			min_from,
			max_from,
			-axisMax,
			axisMax)
	except (OverflowError):
		pass

def map_button(n, wiimote_button, vjoy_button):
	vJoy[n].setButton(vjoy_button, wiimote[n].buttons.button_down(wiimote_button))

def map_nunchuck_button(n, nunchuck_button, vjoy_button):
	vJoy[n].setButton(vjoy_button, wiimote[n].nunchuck.buttons.button_down(nunchuck_button))


def map_buttons(n):
	map_button(n, WiimoteButtons.A, 0)
	map_button(n, WiimoteButtons.B, 1)
	map_button(n, WiimoteButtons.One, 2)
	map_button(n, WiimoteButtons.Two, 3)
	map_button(n, WiimoteButtons.Minus, 4)
	map_button(n, WiimoteButtons.Plus, 5)
	map_button(n, WiimoteButtons.Home, 6)

def map_nunchuck_buttons(n):
	map_nunchuck_button(n, NunchuckButtons.C, 7)
	map_nunchuck_button(n, NunchuckButtons.Z, 8)

# Wiimote 1
def updateWiimote1():
	calibrateWiimote(0)
	map_buttons(0)
	map_pov(0)
	map_axis_wiimote(0)
	
def updateNunchuck1():
	calibrateNunChuck(0)
	map_nunchuck_buttons(0)
	map_axis_nunchuck(0)
	

# Wiimote 2
def updateWiimote2():
	calibrateWiimote(1)
	map_buttons(1)
	map_pov(1)
	map_axis_wiimote(1)
	
def updateNunchuck2():
	calibrateNunChuck(1)
	map_axis_nunchuck(1)
	map_nunchuck_buttons(1)
	

# Wiimote 3
def updateWiimote3():
	calibrateWiimote(2)
	map_buttons(2)
	map_pov(2)
	map_axis_wiimote(2)
	
def updateNunchuck3():
	calibrateNunChuck(2)
	map_axis_nunchuck(2)
	map_nunchuck_buttons(2)
	

# Wiimote 4
def updateWiimote4():
	calibrateWiimote(3)
	map_buttons(3)
	map_pov(3)
	map_axis_wiimote(3)

def updateNunchuck4():
	calibrateNunChuck(3)
	map_axis_nunchuck(3)
	map_nunchuck_buttons(3)
	

class Calibration_base(object):
	def __init__(self):
		self.calibration_mode = False
		self.first_is_pressed = False
		self.second_is_pressed = False
		self.minX = -3
		self.minY = -1
		self.minZ = 99.0
		self.maxX = 3
		self.maxY = 4
		self.maxZ = -99.0
	
	def clear_calibration_mode(self):
		self.calibration_mode = False
		self.first_is_pressed = False
		self.second_is_pressed = False
		
	def try_to_set_calibration_mode(self, name=""):
		diagnostics.debug("Trying to set mode Calibration"+name)
		if self.first_is_pressed and self.second_is_pressed and not self.calibration_mode:
			self.minX = 99.0
			self.minY = 99.0
			self.minZ = 99.0
			self.maxX = -99.0
			self.maxY = -99.0
			self.maxZ = -99.0
			self.calibration_mode = True
			diagnostics.debug("Starting Calibration"+name)
		return self.calibration_mode
		
	def try_to_callibrate(self,x,y,z):
		if self.calibration_mode:
			#diagnostics.debug("setting axis")
			self.set_min_X(x)
			self.set_min_Y(y)
			self.set_min_Z(z)
			self.set_max_X(x)
			self.set_max_Y(y)
			self.set_max_Z(z)
	def set_min_X(self, candidate_x):
		if self.minX > candidate_x:
			self.minX = candidate_x
	def set_min_Y(self, candidate_y):
		if self.minY > candidate_y:
			self.minY = candidate_y
	def set_min_Z(self, candidate_z):
		if self.minZ > candidate_z:
			self.minZ = candidate_z		
	def set_max_X(self, candidate_x):
		if self.maxX < candidate_x:
			self.maxX = candidate_x
	def set_max_Y(self, candidate_y):
		if self.maxY < candidate_y:
			self.maxY = candidate_y
	def set_max_Z(self, candidate_z):
		if self.maxZ < candidate_z:
			self.maxZ = candidate_z
			

class NunchuckCalibration(Calibration_base):
	def __init__(self):
		super(NunchuckCalibration, self).__init__()
		self.minX = -3
		self.minY = -1
		self.minZ = 99.0
		self.maxX = 3
		self.maxY = 4
		self.maxZ = -99.0	
	def set_c_is_pressed(self,n):
		diagnostics.debug("First N"+str(n))
		self.first_is_pressed = True
		self.try_to_set_calibration_mode("Nunchuck "+str(n))
	def set_z_is_pressed(self,n):
		diagnostics.debug("Second N"+str(n))
		self.second_is_pressed = True
		self.try_to_set_calibration_mode("Nunchuck"+str(n))
			
		
class WiimoteCalibration(Calibration_base):
	def __init__(self):
		super(WiimoteCalibration, self).__init__()
		self.minX = -3
		self.minY = -3
		self.minZ = 99.0
		self.maxX = 3
		self.maxY = 3
		self.maxZ = -99.0	
	def set_a_is_pressed(self,n):
		diagnostics.debug("First W "+str(n))
		self.first_is_pressed = True
		self.try_to_set_calibration_mode("Wiimote"+str(n))	
	def set_b_is_pressed(self,n):
		diagnostics.debug("Second W"+str(n))
		self.second_is_pressed = True
		self.try_to_set_calibration_mode("Wiimote"+str(n))



def calibrateNunChuck(n):
	global nunchuch_calibration
	if filters.stopWatch(wiimote[n].nunchuck.buttons.button_down(NunchuckButtons.C), 5000):
		nunchuch_calibration[n].set_c_is_pressed(n)
		diagnostics.debug("C")
	if filters.stopWatch(wiimote[n].nunchuck.buttons.button_down(NunchuckButtons.Z), 5000):
		nunchuch_calibration[n].set_z_is_pressed(n)
		diagnostics.debug("Z")
	
	if wiimote[n].nunchuck.buttons.button_down(NunchuckButtons.C) and wiimote[n].nunchuck.buttons.button_down(NunchuckButtons.Z):
		nunchuch_calibration[n].try_to_callibrate(wiimote[n].nunchuck.acceleration.x, wiimote[n].nunchuck.acceleration.y, wiimote[n].nunchuck.acceleration.z)
	else:
		nunchuch_calibration[n].clear_calibration_mode()
		
	
def calibrateWiimote(n):
	global wiimote_calibration
	if filters.stopWatch(wiimote[n].buttons.button_down(WiimoteButtons.A), 5000):
		wiimote_calibration[n].set_a_is_pressed()
		diagnostics.debug("A")
	if filters.stopWatch(wiimote[n].buttons.button_down(WiimoteButtons.B), 5000):
		wiimote_calibration[n].set_b_is_pressed()
		diagnostics.debug("B")
	
	if wiimote[n].buttons.button_down(WiimoteButtons.A) and wiimote[n].buttons.button_down(WiimoteButtons.B):
		wiimote_calibration[n].try_to_callibrate(wiimote[n].acceleration.x, wiimote[n].acceleration.y, wiimote[n].acceleration.z)
	else:
		wiimote_calibration[n].clear_calibration_mode()
		
	
def log_acceleration_1():
	diagnostics.watch(wiimote[0].acceleration.x)
	diagnostics.watch(wiimote[0].acceleration.y)
	diagnostics.watch(wiimote[0].acceleration.z)

def log_acceleration_2():
	diagnostics.watch(wiimote[1].acceleration.x)
	diagnostics.watch(wiimote[1].acceleration.y)
	diagnostics.watch(wiimote[1].acceleration.z)
	
def log_acceleration_nunchuck_1():
	diagnostics.watch(wiimote[0].nunchuck.acceleration.x)
	diagnostics.watch(wiimote[0].nunchuck.acceleration.y)
	diagnostics.watch(wiimote[0].nunchuck.acceleration.z)
	diagnostics.watch(wiimote[0].nunchuck.stick.x)
	diagnostics.watch(wiimote[0].nunchuck.stick.y)

def log_acceleration_nunchuck_2():
	diagnostics.watch(wiimote[1].nunchuck.acceleration.x)
	diagnostics.watch(wiimote[1].nunchuck.acceleration.y)
	diagnostics.watch(wiimote[1].nunchuck.acceleration.z)
	diagnostics.watch(wiimote[1].nunchuck.stick.x)
	diagnostics.watch(wiimote[1].nunchuck.stick.y)
	
# If we're starting up, then hook up our update function.
if starting:
	system.setThreadTiming(TimingTypes.HighresSystemTimer)
	system.threadExecutionInterval = 2
	
	
	wiimote[0].acceleration.update += log_acceleration_1
	wiimote[1].acceleration.update += log_acceleration_2
	wiimote[0].acceleration.update += log_acceleration_nunchuck_1
	wiimote[1].acceleration.update += log_acceleration_nunchuck_2
	

	wiimote[0].buttons.update += updateWiimote1
	wiimote[1].buttons.update += updateWiimote2
	#wiimote[2].buttons.update += updateWiimote3
	#wiimote[3].buttons.update += updateWiimote4
	
	
	wiimote[0].nunchuck.update += updateNunchuck1
	wiimote[1].nunchuck.update += updateNunchuck2
	#wiimote[2].nunchuck.update += updateNunchuck3
	#wiimote[3].nunchuck.update += updateNunchuck4

	nunchuch_calibration = []
	nunchuch_calibration.append(NunchuckCalibration())
	nunchuch_calibration.append(NunchuckCalibration())
	#nunchuch_calibration.append(NunchuckCalibration())
	#nunchuch_calibration.append(NunchuckCalibration())
	
	wiimote_calibration = []
	wiimote_calibration.append(WiimoteCalibration())
	wiimote_calibration.append(WiimoteCalibration())
	#wiimote_calibration.append(WiimoteCalibration())
	#wiimote_calibration.append(WiimoteCalibration())
	
	wiimote[0].enable(WiimoteCapabilities.Extension)
	wiimote[1].enable(WiimoteCapabilities.Extension)


	

if keyboard.getKeyDown(Key.LeftControl):
	wiimotes_to_monitor = [0,1]
	for n in wiimotes_to_monitor:
		diagnostics.debug("N["+str(n)+"]x:  "+str(nunchuch_calibration[n].minX)+" -| "+str(wiimote[n].nunchuck.acceleration.x)+" |- "+str(nunchuch_calibration[n].maxX))
		diagnostics.debug("N["+str(n)+"]y:  "+str(nunchuch_calibration[n].minY)+" -| "+str(wiimote[n].nunchuck.acceleration.y)+" |- "+str(nunchuch_calibration[n].maxY))
		diagnostics.debug("W["+str(n)+"]x:  "+str(wiimote_calibration[n].minX)+" -| "+str(wiimote[n].acceleration.x)+" |- "+str(wiimote_calibration[n].maxX))
		diagnostics.debug("W["+str(n)+"]y:  "+str(wiimote_calibration[n].minY)+" -| "+str(wiimote[n].acceleration.y)+" |- "+str(wiimote_calibration[n].maxY))
		diagnostics.debug("\n")
Post Reply

Return to “FreePIE”