Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 07/07/2024, à 20:39

cyril_840

Utiliser wiiuse en python

Bonjour, je cherche a installer et faire fonctionner wiiuse afin de récuperer les touches de ma wiiimote, il y a longtemps de sa, j'avais reussi a faire cela en utilisant python-cwiid, mais il ne semble plus disponible.

J'utilise pycharm comme ide, j'arrive bien a l'installer mais j'ai un message d'erreur:

C:\Users\cyril\PycharmProjects\chemin_tondeuse\pythonProject1\venv\Scripts\python.exe C:\Users\cyril\OneDrive\Documents\projet_tondeuse\projet_tondeuse\wiimote2.py 
Traceback (most recent call last):
  File "C:\Users\cyril\OneDrive\Documents\projet_tondeuse\projet_tondeuse\wiimote2.py", line 93, in <module>
    wiimotes = wiiuse.init(nmotes)
  File "C:\Users\cyril\PycharmProjects\chemin_tondeuse\pythonProject1\venv\lib\site-packages\wiiuse\__init__.py", line 307, in init
    dll = ctypes.cdll.wiiuse
  File "C:\Users\cyril\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 444, in __getattr__
    dll = self._dlltype(name)
  File "C:\Users\cyril\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'wiiuse' (or one of its dependencies). Try using the full path with constructor syntax.

Voici le code que je voudrais utiliser:

import wiiuse
import sys
import time
import os

nmotes = 2


def handle_event(wmp):
    wm = wmp[0]
    print
    '--- EVENT [wiimote id %i] ---' % wm.unid

    if wm.btns:
        for name, b in wiiuse.button.items():
            if wiiuse.is_pressed(wm, b):
                print
                name, 'pressed'

        if wiiuse.is_just_pressed(wm, wiiuse.button['-']):
            wiiuse.motion_sensing(wmp, 0)
        if wiiuse.is_just_pressed(wm, wiiuse.button['+']):
            wiiuse.motion_sensing(wmp, 1)
        if wiiuse.is_just_pressed(wm, wiiuse.button['B']):
            wiiuse.toggle_rumble(wmp)
        if wiiuse.is_just_pressed(wm, wiiuse.button['Up']):
            wiiuse.set_ir(wmp, 1)
        if wiiuse.is_just_pressed(wm, wiiuse.button['Down']):
            wiiuse.set_ir(wmp, 0)

    if wiiuse.using_acc(wm):
        print
        'roll  = %f' % wm.orient.roll
        print
        'pitch = %f' % wm.orient.pitch
        print
        'yaw   = %f' % wm.orient.yaw

    if wiiuse.using_ir(wm):
        for i in range(4):
            if wm.ir.dot[i].visible:
                print
                'IR source %i: (%u, %u)' % (i, wm.ir.dot[i].x, wm.ir.dot[i].y)
        print
        'IR cursor: (%u, %u)' % (wm.ir.x, wm.ir.y)
        print
        'IR z distance: %f' % wm.ir.z

    if wm.exp.type == wiiuse.EXP_NUNCHUK:
        nc = wm.exp.u.nunchuk

        for name, b in wiiuse.nunchuk_button.items():
            if wiiuse.is_pressed(nc, b):
                print
                'Nunchuk: %s is pressed' % name

        print
        'nunchuk roll  = %f' % nc.orient.roll
        print
        'nunchuk pitch = %f' % nc.orient.pitch
        print
        'nunchuk yaw   = %f' % nc.orient.yaw
        print
        'nunchuk joystick angle:     %f' % nc.js.ang
        print
        'nunchuk joystick magnitude: %f' % nc.js.mag


def handle_ctrl_status(wmp, attachment, speaker, ir, led, battery_level):
    wm = wmp[0]
    print
    '--- Controller Status [wiimote id %i] ---' % wm.unid
    print
    'attachment', attachment
    print
    'speaker', speaker
    print
    'ir', ir
    print
    'leds', led[0], led[1], led[2], led[3]
    print
    'battery', battery_level


def handle_disconnect(wmp):
    print
    'disconnect'


if os.name != 'nt': print
'Press 1&2'

wiimotes = wiiuse.init(nmotes)

found = wiiuse.find(wiimotes, nmotes, 5)
if not found:
    print
    'not found'
    sys.exit(1)

connected = wiiuse.connect(wiimotes, nmotes)
if connected:
    print
    'Connected to %i wiimotes (of %i found).' % (connected, found)
else:
    print
    'failed to connect to any wiimote.'
    sys.exit(1)

for i in range(nmotes):
    wiiuse.set_leds(wiimotes[i], wiiuse.LED[i])
    wiiuse.status(wiimotes[0])
    wiiuse.set_ir(wiimotes[0], 1)
    wiiuse.set_ir_vres(wiimotes[i], 1000, 1000)

try:
    rum = 1
    while True:
        r = wiiuse.poll(wiimotes, nmotes)
        if r != 0:
            handle_event(wiimotes[0])
except KeyboardInterrupt:
    for i in range(nmotes):
        wiiuse.set_leds(wiimotes[i], 0)
        wiiuse.rumble(wiimotes[i], 0)
        wiiuse.disconnect(wiimotes[i])

print
'done'

Est ce que quelqu'un peu me renseigner?

Et t'il possible d'installer wiiuse depuis les sources? https://sourceforge.net/projects/wiiuse/

Merci d'avance

Dernière modification par cyril_840 (Le 07/07/2024, à 20:40)

Hors ligne

#2 Le 08/07/2024, à 13:33

alrito

Re : Utiliser wiiuse en python

Bonjour cyril_840,

Ce message "Could not find module 'wiiuse'" signifie que le module wiiuse n'est pas installé.

D'abord, nous allons installé pip3, il permettra d'installé ensuite wiiuse.

Installe pip3 comme ceci :

sudo apt-get install python3-pip

Puis, installe wiiuse :

pip3 install --break-system-packages wiiuse

Est-ce que cela fonctionne ?

smile

Hors ligne

#3 Le 08/07/2024, à 20:26

cyril_840

Re : Utiliser wiiuse en python

Bonjour, j'ai lancé le 2 commandes dans le terminal  (sous ubuntu) avec succé mais sous pycharm j'ai un message d'erreur dans la console python:

/home/cyril/Documents/projet_tondeuse/projet_tondeuse/.venv/bin/python /home/cyril/Documents/projet_tondeuse/projet_tondeuse/wiiuse2.py 
Traceback (most recent call last):
  File "/home/cyril/Documents/projet_tondeuse/projet_tondeuse/wiiuse2.py", line 1, in <module>
    import wiiuse
ModuleNotFoundError: No module named 'wiiuse'

j'ai 2 versions de python: python3.12 et python 3.9, je n'arrive pas a installer depuis pycharm que ce soit sur l'interpreteur de pyhton 3.9 et python 3.12.

Par contre sous windows toujours sur pycharm j'arrive a installer wiiuse mais je ne peux pas lancer le scripte car sa me dis que la librairie n'est pas installé.(pourtant elle semble l'être)

Curieux tout sa?

Auriez vous d'autres idées pour faire fonctionner le code?

Dernière modification par cyril_840 (Le 08/07/2024, à 20:28)

Hors ligne

#4 Le 08/07/2024, à 22:13

alrito

Re : Utiliser wiiuse en python

j'avais reussi a faire cela en utilisant python-cwiid...

As-tu essayer comme ceci :

sudo apt-get install python3-cwiid

Et t'il possible d'installer wiiuse depuis les sources?


Le liens de téléchargement que tu as mis contiens les sources en version windows mais ne contient pas la version linux du logiciels.

Dernière modification par alrito (Le 08/07/2024, à 22:16)

Hors ligne

#5 Le 09/07/2024, à 11:49

cyril_840

Re : Utiliser wiiuse en python

La commande s'execute bien, je teste ce code mais j'ai le message:No wiimotes found


import cwiid
import time

button_delay = 0.1

print
'Press 1 + 2 on your Wii Remote now ...'
time.sleep(1)

# Connect to the Wii Remote. If it times out
# then quit.
try:
    wii = cwiid.Wiimote()
except RuntimeError:
    print
    "Error opening wiimote connection"
    quit()

print
'Wii Remote connected...\n'
print
'Press some buttons!\n'
print
'Press PLUS and MINUS together to disconnect and quit.\n'

wii.rpt_mode = cwiid.RPT_BTN

while True:

    buttons = wii.state['buttons']

    # If Plus and Minus buttons pressed
    # together then rumble and quit.
    if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
        print
        '\nClosing connection ...'
        wii.rumble = 1
        time.sleep(1)
        wii.rumble = 0
        exit(wii)

    # Check if other buttons are pressed by
    # doing a bitwise AND of the buttons number
    # and the predefined constant for that button.
    if (buttons & cwiid.BTN_LEFT):
        print
        'Left pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_RIGHT):
        print
        'Right pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_UP):
        print
        'Up pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_DOWN):
        print
        'Down pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_1):
        print
        'Button 1 pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_2):
        print
        'Button 2 pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_A):
        print
        'Button A pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_B):
        print
        'Button B pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_HOME):
        print
        'Home Button pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_MINUS):
        print
        'Minus Button pressed'
        time.sleep(button_delay)

    if (buttons & cwiid.BTN_PLUS):
        print
        'Plus Button pressed'
        time.sleep(button_delay)

Hors ligne

#6 Le 10/07/2024, à 17:55

cyril_840

Re : Utiliser wiiuse en python

Est ce que quelqu'un arrive a faire marcher le code au dessus?

J'ai ceci:

/usr/bin/python3.12 /home/cyril/Documents/projet_tondeuse/projet_tondeuse/wiimote1.py 
No wiimotes found

Pourtant ma télécommande wiimote et bien appairé en mode bluetooth.


Lorsque j'essai d'installer cwiid, j'ai ce message d'erreur (j'ai mis a jour pip)

(test_python3) cyril@cyril-ThinkCentre-M93p:~/Documents/projet_tondeuse/projet_tondeuse$ pip install cwiid
Collecting cwiid
  Using cached cwiid-3.0.0.tar.gz (7.6 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: cwiid
  Building wheel for cwiid (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for cwiid (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [5 lines of output]
      Wiimote.c:27:10: fatal error: cwiid.h: Aucun fichier ou dossier de ce type
         27 | #include <cwiid.h>
            |          ^~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for cwiid
Failed to build cwiid
ERROR: Could not build wheels for cwiid, which is required to install pyproject.toml-based projects

[notice] A new release of pip is available: 23.2.1 -> 24.1.2
[notice] To update, run: pip install --upgrade pip
(test_python3) cyril@cyril-ThinkCentre-M93p:~/Documents/projet_tondeuse/projet_tondeuse$ pip install --upgrade pip
Requirement already satisfied: pip in /home/cyril/Documents/test_python3/lib/python3.12/site-packages (23.2.1)
Collecting pip
  Obtaining dependency information for pip from https://files.pythonhosted.org/packages/e7/54/0c1c068542cee73d8863336e974fc881e608d0170f3af15d0c0f28644531/pip-24.1.2-py3-none-any.whl.metadata
  Downloading pip-24.1.2-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-24.1.2-py3-none-any.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 1.7 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 23.2.1
    Uninstalling pip-23.2.1:
      Successfully uninstalled pip-23.2.1
Successfully installed pip-24.1.2
(test_python3) cyril@cyril-ThinkCentre-M93p:~/Documents/projet_tondeuse/projet_tondeuse$ pip install cwiid
Collecting cwiid
  Downloading cwiid-3.0.0.tar.gz (7.6 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: cwiid
  Building wheel for cwiid (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for cwiid (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [5 lines of output]
      Wiimote.c:27:10: fatal error: cwiid.h: Aucun fichier ou dossier de ce type
         27 | #include <cwiid.h>
            |          ^~~~~~~~~
      compilation terminated.
      error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for cwiid
Failed to build cwiid
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (cwiid)

Quelqu'un aurait t'il une idée?

Dernière modification par cyril_840 (Le 10/07/2024, à 18:02)

Hors ligne

#7 Le 12/07/2024, à 21:49

cyril_840

Re : Utiliser wiiuse en python

Je viens de tester avec une autre wiimote et sa ne change rien.

J'ai de groses difficultés a appairer la wiimote, j'ai essayé un tas de chose , sous windows et ubuntu...

Je vais essayer d'installer une ancienne version d'ubuntu qui supporte la commande (pip install python-cwiid)

Hors ligne

#8 Le 13/07/2024, à 16:00

alrito

Re : Utiliser wiiuse en python

Test ce code là que j'ai corriger de wiimote.py :

# This program utilises the cwiid Python library in order to get input over bluetooth from a wiimote.
# The following lines of code demonstrate many of the features realted to wiimotes, such as capturing button presses and rumbling the controller.
# I have managed to map the home button to the accelerometer - simply hold it and values will appear!

# Coded by The Raspberry Pi Guy. Work based on some of Matt Hawkins's!

import cwiid, time

button_delay = 0.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()

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

while True:

  buttons = wii.state['buttons']

  # Detects whether + and - are held down and if they are it quits the program
  if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
    print ('\nClosing connection ...')
    # NOTE: This is how you RUMBLE the Wiimote
    wii.rumble = 1
    time.sleep(1)
    wii.rumble = 0
    exit(wii)

  # The following code detects whether any of the Wiimotes buttons have been pressed and then prints a statement to the screen!
  if (buttons & cwiid.BTN_LEFT):
    print ('Left pressed')
    time.sleep(button_delay)

  if(buttons & cwiid.BTN_RIGHT):
    print ('Right pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_UP):
    print ('Up pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_DOWN):
    print ('Down pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_1):
    print ('Button 1 pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_2):
    print ('Button 2 pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_A):
    print ('Button A pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_B):
    print ('Button B pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_HOME):
    wii.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
    check = 0
    while check == 0:
      print(wii.state['acc'])
      time.sleep(0.01)
      check = (buttons & cwiid.BTN_HOME)
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_MINUS):
    print ('Minus Button pressed')
    time.sleep(button_delay)

  if (buttons & cwiid.BTN_PLUS):
    print ('Plus Button pressed')
    time.sleep(button_delay)

Dernière modification par alrito (Le 13/07/2024, à 16:00)

Hors ligne

#9 Le 13/07/2024, à 20:26

cyril_840

Re : Utiliser wiiuse en python

Merci beaucoup, je viens de tester ton code fonctionne, par contre il affiche 2 fois la touche appuyé.

J'aurais aimé savoir comment installer python3-cwiid sur mon python 3.9 , la sa fonctionne sur mon python de base qui est en 3.2.

Normalement on dois pouvoir installer python3-cwiid avec pip ?

Hors ligne

#10 Le 13/07/2024, à 20:56

alrito

Re : Utiliser wiiuse en python

Pour répondre à ta dernière question, je ne crois pas mais après peut-être.

Tu es sur quel système d'exploitation et quel version ?

Hors ligne

#11 Le 13/07/2024, à 21:03

cyril_840

Re : Utiliser wiiuse en python

Ubuntu 24.04 LTS

J'utilise pycharm, avec 2 versions de python , la 3.2 de base et la 3.9.

J'utilise egalement python avec pycharm sous windows10

Je suis sur un programme pour effectuer la tonte avec un robot tondeuse équipté de gps rtk (precision de quelques centimetres)... Je jongle entre les 2
systemes, windows et linux

J'aurais voulu arriver a faire marcher sous windows et linux...

Hors ligne

#12 Le 13/07/2024, à 21:07

cyril_840

Re : Utiliser wiiuse en python

Je viens de tester avec button_delay = 0.3 et la y a pas de doublons de la touche appuyé

Hors ligne