본문 바로가기
robotics/raspberry pi

라즈베리 파이 4에 DS4 조이스틱 연결

by monotics 2021. 10. 24.

블루투스 준비

라즈베리 파이 4의 내장 BT를 사용해보려 하였지만 잘 되지 않았다. 그래서 가지고 있던 BT 동글을 이용 하여 DS4와 연결해보았다.

Bluetooth USB 동글을 라즈베리 파이에 연결한다.

Bluetooth 동글을 Raspberry Pi 4 USB 포트에 연결

ds4drv을 이용하여 joystick 디바이스를 확인한다.

  • /dev/input/js0
ubuntu@ubuntu:~$ sudo ds4drv --hidraw
[info][controller 1] Created devices /dev/input/js0 (joystick) /dev/input/event0 (evdev)
[info][hidraw] Scanning for devices

블루투스 연결

DS4의 "SHARE" 버튼과 "PS4" 버튼을 동시에 눌러 페어링 모드로 진입한다.

DS4 조이스틱의 SHARE 버튼과 PS4 버튼

'bluetoothctl'을 실행하면 [bluetooth] 프롬프트가 나타난다. 아래와 같은 순서로 실행하고 "scan on"를 통해 검색된 장치 중 DS4에 해당하는 "Wireless Controller"과 "connect"를 사용하여 연결한다.

  • agent on
  • default-agent
  • power on
  • scan on
  • connect 5E:A6:57:AB:AF:53
$ bluetoothctl
[bluetooth]# agent on
Agent is already registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
 Discovery started
[CHG] Controller 00:1A:7D:DA:71:13 Discovering: yes
[NEW] Device 8C:08:8B:F4:76:1C LGU tvG2 RCU
[NEW] Device 6F:4A:53:E2:D7:07 6F-4A-53-E2-D7-07
[NEW] Device 5E:A6:57:AB:AF:53 Wireless Controller
[bluetooth]# connect 5E:A6:57:AB:AF:53

테스트 코드

pyPS4Controller를 pip를 통해 설치한다.

$ sudo pip3 install pyPS4Controller

'joytest.py' 파일을 생성한다. 이때 MyController의 인자 중 interface를 앞서 ds4drv로 확인 한 조이스틱 디바이스의 이름인 "/dev/input/js0"와 동일하게 한다.

from pyPS4Controller.controller import Controller

class MyController(Controller):
    def __init__(self, **kwargs):
        Controller.__init__(self, **kwargs)

controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)
controller.listen()

"python3 joytest.py"를 실행시켜 파이썬 코드를 테스트한다. 이때 DS4의 조이스틱과 버튼들을 이것저것 눌러본다. 해당하는 이벤트가 로그로 출력된다.

ubuntu@ubuntu:~/workspace$ python3 joytest.py
Waiting for interface: /dev/input/js0 to become available . . .
Successfully bound to: /dev/input/js0.
on_square_press
on_square_release
on_circle_press
on_circle_release
on_left_arrow_press
on_left_right_arrow_release
on_right_arrow_press
on_left_right_arrow_release
on_R1_press
on_R1_release
on_R2_press: 13174
on_R2_press: 32767
on_R2_press: 28376
on_R2_press: 6756
on_R2_release
on_L2_press: 6418
on_L2_press: 32767
on_L2_press: 5405
on_L2_release
on_R3_right: 1689
on_R3_right: 8445
on_R3_right: 13850
on_R3_right: 19593
on_R3_right: 22295
on_R3_right: 23309
on_R3_right: 23647
on_R3_right: 23309
on_R3_right: 20606
on_R3_right: 13174
on_R3_right: 2702
on_R3_x_at_rest

DualShock4 장치 끄기

'PS4' 버튼을 10초 간 누르고 있으면 DS4가 끄진다.

댓글