LoginSignup
6
5

More than 3 years have passed since last update.

Pygameを使ってWHILLをJoyConで動かす

Last updated at Posted at 2019-05-09

PythonでWHILLを制御できるらしい

先日、以下のSDKを発見しました。

WHILL Model CR SDK for Python

いじいじしているうちにMakerFaireKyoto2019に出展することになっていたので記事書きます

やってみる

あーだこーだ文字を連ねる文章力はないので早速まとめてみます

環境

OS : MacOS Mojave Version10.14.4
Python : Python3.7.3
Pygame : 1.9.6

ソースコード

main.py
import whillpy
import whill_data
import pygame
from pygame.locals import *
import time

def main() :
    whill = whillpy.connect(port='WHILLとPCを繋ぐポート')
    pygame.joystick.init()
    joystick0 = pygame.joystick.Joystick(0)
    joystick0.init()

    print ('joystick start')

    pygame.init()

    x, y = 0, 0
    while True:
         # コントローラーの操作を取得
        eventlist = pygame.event.get()

        # イベント処理
        for e in eventlist:
            if e.type == QUIT:
                return

            if e.type == pygame.locals.JOYAXISMOTION:
                x, y = joystick0.get_axis(0), joystick0.get_axis(1)
                print ('axis x:' + str(x) + ' axis y:' + str(y))
            elif e.type == pygame.locals.JOYHATMOTION:
                x, y = joystick0.get_hat(0)
                print ('hat x:' + str(x) + ' hat y:' + str(y))
            elif e.type == pygame.locals.JOYBUTTONDOWN:
                print ('button:' + str(e.button))

        whill.move(straight=int(-x * 30), turn=int(y * 30))
        time.sleep(0.01)

if __name__ == '__main__':
    try:
        main()
    except pygame.error:
        print ('joystickが見つかりませんでした。')

pywhillのOS SupportにMacが入っていませんが動きます(動きました)
WHILL Model CR のシリアルポートからRS232Cに繋いであげてそこからUSBに変換すればPCには繋げます(実行環境にもよる)
JoyConはBluetoothでPCに繋げてあげてください。
あとはwhillpyとwill_dataを同じ階層に移動してあげて
Pygame、pySerialをpip installしてあげるだけで動きます。

おしまい!

動くと楽しかった!!!そしてMakerFaireKyoto2019楽しかった!!!そしておすし美味しかった!!!!!!!!!!

追記

コメント欄よりMacで操作する場合のリンクをいただいたので紹介します。

Macはこっち
https://pypi.org/project/whillpy/
https://github.com/ShibataLab/whillpy/tree/master/whillpy

6
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5