Skip to content

Commit

Permalink
Merge pull request #6 from rogy-AquaLab/joystick
Browse files Browse the repository at this point in the history
Joystick
  • Loading branch information
H1rono committed Jun 1, 2024
2 parents fff79f4 + b00255f commit 088f8c2
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 0 deletions.
15 changes: 15 additions & 0 deletions device/joystick/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Joystick

ゲームコントローラー

## Nodes

- `Joystick`: ゲームコントローラーを読み取って`joystick`topicに`sensor_msgs/msg/Joy`型でpublishする

## Executables

- `joystick`: `Joystick`Nodeを起動する

## Launches

- `joystick_launch.py`: `Joystick`Nodeを`device`namespace下で起動する
Empty file.
45 changes: 45 additions & 0 deletions device/joystick/joystick/joystick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys

import pygame
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Joy


class Joystick(Node):
def __init__(self):
super().__init__("joystick")
self._timer = self.create_timer(0.5, self._timer_callback)
self._joy_publisher = self.create_publisher(Joy, "joystick", 10)
# TODO: ここparameterで変えられるようにしたい
self._joystick = pygame.joystick.Joystick(0)
# logging
name = self._joystick.get_name()
self.get_logger().info(f"Using controller: {name}")

def _timer_callback(self):
self.get_logger().info("tick")
pygame.event.pump()

numaxes = self._joystick.get_numaxes()
numbuttons = self._joystick.get_numbuttons()
self.get_logger().info(f"Found {numaxes} axes and {numbuttons} buttons")

axes = [self._joystick.get_axis(i) for i in range(numaxes)]
buttons = [int(self._joystick.get_button(i)) for i in range(numbuttons)]
msg = Joy(axes=axes, buttons=buttons)
self._joy_publisher.publish(msg)


def main(args=sys.argv):
rclpy.init(args=args)
pygame.init()
node = Joystick()
rclpy.spin(node)
node.destroy_node()
pygame.quit()
rclpy.shutdown()


if __name__ == "__main__":
main()
8 changes: 8 additions & 0 deletions device/joystick/launch/joystick_launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
return LaunchDescription([
Node(package="joystick", executable="joystick", namespace="device")
])
17 changes: 17 additions & 0 deletions device/joystick/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>joystick</name>
<version>0.1.0</version>
<description>ゲームコントローラー</description>
<maintainer email="[email protected]">h1rono</maintainer>
<license>MIT</license>

<exec_depend>rclpy</exec_depend>
<exec_depend>sensor_msgs</exec_depend>
<exec_depend>python3-pygame</exec_depend>

<export>
<build_type>ament_python</build_type>
</export>
</package>
Empty file.
4 changes: 4 additions & 0 deletions device/joystick/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/joystick
[install]
install_scripts=$base/lib/joystick
30 changes: 30 additions & 0 deletions device/joystick/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
from glob import glob

from setuptools import find_packages, setup

package_name = 'joystick'

setup(
name=package_name,
version='0.1.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*')))
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='h1rono',
maintainer_email='[email protected]',
description='ゲームコントローラー',
license='MIT',
tests_require=[],
entry_points={
'console_scripts': [
"joystick = joystick.joystick:main"
],
},
)
Empty file added device/joystick/test/.gitkeep
Empty file.

0 comments on commit 088f8c2

Please sign in to comment.