# -*- coding: utf-8 -*- import struct import serialServo import serial import wiringpi ### # servoの初期化 ### con = serial.Serial("/dev/ttyUSB0", 115200, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1) servo0 = serialServo.Servo(con, "0") # Servo ID = 0, 旋回 servo1 = serialServo.Servo(con, "1") # Servo ID = 1, L1 or L2 servo2 = serialServo.Servo(con, "2") # Servo ID = 2, R1 or R2 ### # servo操作関数 ### stick_th = 25000 def stick_infinite_loop_control(servo, val): if (-1 * stick_th <= val) and (val <= stick_th): _pos = servo.Pos(135) # 中立 if val > stick_th: _pos = servo.Pos(serialServo.MAX_ANGLE) if val < -1 * stick_th: _pos = servo.Pos(serialServo.MIN_ANGLE)
# GPIOを出力モード(1)に設定 wiringpi.pinMode(2, 1) con = None con = serial.Serial("/dev/ttyUSB0", 115200, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1) initial_angle = 135 # 中立 servos = [] for i in range(1): print("Servo", i, "initializing...") servos.append(serialServo.Servo(con, "{}".format(i))) print(servos) angles = [servo.GetPos() for servo in servos] th = 25000 def max_angle_stick_control(servo_id, val): if (-1 * th <= val) and (val <= th): _pos = servos[servo_id].Pos(servos[servo_id].GetPos()) _pos = servos[servo_id].Pos(_pos) if val > th: _pos = servos[servo_id].Pos(serialServo.MAX_ANGLE) if val < -1 * th: _pos = servos[servo_id].Pos(serialServo.MIN_ANGLE) print("servo_id = {}, angle postition = {}, value = {}".format(
import serialServo import sys import serial import time from getch import _Getch con = None con = serial.Serial("/dev/ttyUSB0", 115200, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, timeout=1) servo = serialServo.Servo(con, "0", 10) _angle = servo.GetPos() getch = _Getch() while True: ikey = getch() print(_angle) # A: Up, B: Down if ikey == "A": _angle = servo.Pos(_angle + 5) if ikey == "B": _angle = servo.Pos(_angle - 5) if ikey == '\x03': # <Ctrl-c> break
def setPort(event): global servo value = portBuff.get() serialServo.SetSerial(value) servo = serialServo.Servo(add) portLabel.configure(text="selected port")