User Tools

Site Tools


wh1080_usb_missile_launcher_en_gnu_linux

Modelo de Lanzacohetes USB: Dream Link WH1080 Weather Station / USB Missile Launcher (imagen).

$ lsusb
...
Bus 002 Device 003: ID 1941:8021 Dream Link WH1080 Weather Station / USB Missile Launcher
...

Dependencias: Pyusb / TK

Instalación de Pyusb / TK en Arch GNU/Linux

yaourt pyusb
#Opción 1: 1 aur/python-pyusb 1.0.0b1-1 (8)
pacman -S tk

Script en Python para manejar el lanzacohetes USB

usblauncher.py
#!/usr/bin/python
import sys, Tkinter
sys.modules['tkinter'] = Tkinter # put the module where python looks first for modules
import tkinter # now works!
import sys
import usb.core
import time
import tkinter as tk
 
class LauncherDriver():
    def __init__(self):
        self.dev = usb.core.find(idVendor=0x1941, idProduct=0x8021)
        if self.dev is None:
            raise ValueError('Missile launcher not found.')
        #if sys.platform == 'linux2' and self.dev.is_kernel_driver_active(0) is True:
        self.dev.detach_kernel_driver(0)
        self.dev.set_configuration()
        """self.handle = self.dev.open()
try:
self.handle.claimInterface(0)
except:
if e.message.find("could not claim interface") >= 0:
self.handle.detachKernelDriver(0)
self.handle.claimInterface(0)
self.handle.setAltInterface(0)
"""
    def turretDown(self):
        try:
            self.dev.ctrl_transfer(0x21, 0x09, 0x200, 0, [0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
        except:
            pass
    def turretRight(self):
        try:
            self.dev.ctrl_transfer(0x21, 0x09, 0x200, 0, [0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
        except:
            pass
    def turretLeft(self):
        try:
            self.dev.ctrl_transfer(0x21, 0x09, 0x200, 0, [0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
        except:
            pass
    def turretUp(self):
        try:
            self.dev.ctrl_transfer(0x21, 0x09, 0x200, 0, [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
        except:
            pass
    def turretStop(self):
        try:
            self.dev.ctrl_transfer(0x21, 0x09, 0x200, 0, [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
        except:
            pass
    def turretFire(self):
        try:
            self.dev.ctrl_transfer(0x21, 0x09, 0x200, 0, [0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
        except:
            pass
 
class Launcher():
    def __init__(self, root):
        self.launcher = LauncherDriver()
        frame = tk.Frame(root)
        frame.master.title("Missle Control")
        frame.master.geometry("100x100")
 
        frame.master.bind("<KeyPress-Up>", self.up)
        frame.master.bind("<KeyRelease-Up>", self.stop)
 
        frame.master.bind("<KeyPress-Down>", self.down)
        frame.master.bind("<KeyRelease-Down>", self.stop)
 
        frame.master.bind("<KeyPress-Left>", self.left)
        frame.master.bind("<KeyRelease-Left>", self.stop)
 
        frame.master.bind("<KeyPress-Right>", self.right)
        frame.master.bind("<KeyRelease-Right>", self.stop)
 
        frame.master.bind("<KeyPress-Return>", self.shoot)
        frame.master.bind("<KeyRelease-Return>", self.stop)
 
        frame.master.bind("<KeyPress-Escape>", self.exit)
 
        frame.pack()
        self.frame = frame
    def left(self, event):
        self.frame.focus_set()
        self.launcher.turretLeft()
    def right(self, event):
        self.frame.focus_set()
        self.launcher.turretRight()
    def up(self, event):
        self.frame.focus_set()
        self.launcher.turretUp()
    def down(self, event):
        self.frame.focus_set()
        self.launcher.turretDown()
    def shoot(self, event):
        self.frame.focus_set()
        self.launcher.turretFire()
    def stop(self, event):
        self.frame.focus_set()
        self.launcher.turretStop()
    def exit(self, event):
        print("Exitting...")
        self.launcher.turretStop()
        sys.exit(0)
 
root = tk.Tk()
launcher = Launcher(root)
root.mainloop()

El script original sin modificar se puede descargar de aquí, pero puede que se visualice el siguiente error al ejecutarlo.

Traceback (most recent call last):
  File "USBMissile-master/control.py", line 5, in <module>
    import tkinter as tk
ImportError: No module named tkinter

Problema típico al volver a ejecutar el script.

Traceback (most recent call last):
  File "control.py", line 108, in <module>
    launcher = Launcher(root)
  File "control.py", line 60, in __init__
    self.launcher = LauncherDriver()
  File "control.py", line 16, in __init__
    self.dev.detach_kernel_driver(0)
  File "/usr/lib/python2.7/site-packages/usb/core.py", line 1005, in detach_kernel_driver
    interface)
  File "/usr/lib/python2.7/site-packages/usb/backend/libusb1.py", line 838, in detach_kernel_driver
    _check(self.lib.libusb_detach_kernel_driver(dev_handle.handle, intf))
  File "/usr/lib/python2.7/site-packages/usb/backend/libusb1.py", line 552, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 2] Entity not found

Solución: Reconectar el lanzamisiles USB.

wh1080_usb_missile_launcher_en_gnu_linux.txt · Last modified: 2020/12/25 22:57 by 127.0.0.1