-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from rogy-AquaLab/joystick
Joystick
- Loading branch information
Showing
9 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.