Improve tracking

This commit is contained in:
redphx 2022-04-29 17:10:00 +07:00
parent 313c8b1ec3
commit 3c32efd6cf
3 changed files with 19 additions and 18 deletions

View file

@ -69,7 +69,7 @@ class JoyDance:
self.available_shortcuts = set()
self.accel_data = []
self.last_accel = (0, 0, 0)
self.last_accels = []
self.ws = None
self.disconnected = False
@ -282,18 +282,20 @@ class JoyDance:
max_runtime = FRAME_DURATION * 0.5
while time.time() - start < max_runtime:
# Make sure accelerometer axes are changed
accel = self.joycon.get_accels() # (x, y, z)
if accel != self.last_accel:
self.last_accel = accel
accels = self.joycon.get_accels() # [(x, y, z),...]
if accels != self.last_accels:
self.last_accels = accels
break
# Accelerator axes on phone & Joy-Con are different so we need to swap some axes
# https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md
x = accel[1] * -1
y = accel[0]
z = accel[2]
for accel in accels:
x = accel[1] * -1
y = accel[0]
z = accel[2]
self.accel_data.append([x, y, z])
self.accel_data.append([x, y, z])
await self.send_accelerometer_data(frames),
except OSError:
self.disconnect()

View file

@ -15,7 +15,7 @@ WS_SUBPROTOCOLS[WsSubprotocolVersion.V1.value] = 'v1.phonescoring.jd.ubisoft.com
WS_SUBPROTOCOLS[WsSubprotocolVersion.V2.value] = 'v2.phonescoring.jd.ubisoft.com'
FRAME_DURATION = 1 / 60
ACCEL_ACQUISITION_FREQ_HZ = 60 # Hz
ACCEL_ACQUISITION_FREQ_HZ = 200 # Hz
ACCEL_ACQUISITION_LATENCY = 40 # ms
ACCEL_MAX_RANGE = 8 # ±G

View file

@ -354,12 +354,15 @@ class JoyCon:
def get_accels(self):
input_report = bytes(self._input_report)
accels = []
x = self.get_accel_x(input_report)
y = self.get_accel_y(input_report)
z = self.get_accel_z(input_report)
for idx in range(3):
x = self.get_accel_x(input_report, sample_idx=idx)
y = self.get_accel_y(input_report, sample_idx=idx)
z = self.get_accel_z(input_report, sample_idx=idx)
accels.append((x, y, z))
return (x, y, z)
return accels
def get_accel_x(self, input_report=None, sample_idx=0):
if not input_report:
@ -441,11 +444,7 @@ class JoyCon:
"vertical": self.get_stick_right_vertical(),
},
},
"accel": {
"x": self.get_accel_x(),
"y": self.get_accel_y(),
"z": self.get_accel_z(),
},
"accel": self.get_accels(),
}
def disconnect_device(self):