Last active
March 17, 2021 22:53
-
-
Save mipsparc/42e74ff57b615d02a511230df7753208 to your computer and use it in GitHub Desktop.
三鶯重工(サンイン重工)のワンハンドルマスコンをPythonで扱うためのサンプルコード・プロトタイプです。一応動きます
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
#coding: utf-8 | |
# サンイン重工 OHC-PC01AコントローラからPythonへ繋ぎこむライブラリ | |
import pygame | |
import time | |
class OHC_PC01A: | |
def __init__(self): | |
pygame.init() | |
pygame.joystick.init() | |
self.joy = pygame.joystick.Joystick(0) | |
self.joy.init() | |
# 主幹制御器状態から力行ノッチ・ブレーキノッチ指令に変換する | |
def convertPosToAccelBrake(self, pos): | |
if pos == [1, 0, 0, 1]: | |
# [力行ノッチ, ブレーキノッチ] | |
return [0, 1] | |
if pos == [1, 0, 0, 0]: | |
return [0, 2] | |
if pos == [0, 1, 1, 1]: | |
return [0, 3] | |
if pos == [0, 1, 1, 0]: | |
return [0, 4] | |
if pos == [0, 1, 0, 1]: | |
return [0, 5] | |
if pos == [0, 1, 0, 0]: | |
return [0, 6] | |
if pos == [0, 0, 1, 1]: | |
return [0, 7] | |
if pos == [0, 0, 1, 0]: | |
return [0, 8] | |
if pos == [0, 0, 0, 1]: | |
return [0, 9] | |
if pos == [1, 0, 1, 1]: | |
return [1, 0] | |
if pos == [1, 1, 0, 0]: | |
return [2, 0] | |
if pos == [1, 1, 0, 1]: | |
return [3, 0] | |
if pos == [1, 1, 1, 0]: | |
return [4, 0] | |
if pos == [1, 1, 1, 1]: | |
return [5, 0] | |
return [0,0] | |
# 主幹制御器全体の状態を返す | |
def getStatus(self): | |
pygame.event.get() | |
accel_knotch, brake_knotch = self.convertPosToAccelBrake([ | |
self.joy.get_button(6), | |
self.joy.get_button(7), | |
self.joy.get_button(8), | |
self.joy.get_button(9) | |
]) | |
ax = self.joy.get_axis(1) | |
if ax < 0: | |
way = -1 | |
elif ax == 0.0: | |
way = 0 | |
elif ax > 0: | |
way = 1 | |
return { | |
'accel_knotch': accel_knotch, | |
'brake_knotch': brake_knotch, | |
'way': way, | |
'black': bool(self.joy.get_button(0)), | |
'white': bool(self.joy.get_button(1)), | |
'yellow': bool(self.joy.get_button(2)), | |
} | |
if __name__ == '__main__': | |
m = OHC_PC01A() | |
while True: | |
print(m.getStatus()) | |
time.sleep(0.2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment