Skip to main content

No project description provided

Project description

About RobokitRS Package

RS KIT & DIYGO 제품에 포함되어 있는 RS CPU 및 I/O 하드웨어 제어용 Python Package입니다.

while문 사용 시 주의 사항

명령 누적 방지

from RobokitRS import *
from time import sleep
import keyboard

rs = RobokitRS.RobokitRS()
rs.port_open("COM5")
rs.sonar_begin(13)  

while(True):
    SONAR = rs.sonar_read(13)
    if SONAR >= 100:
        rs.motor_write(0,0,15)
        sleep(0.1)

    elif SONAR < 30:
        rs.motor_write(0,0,0)

위 코드는 잘못된 코드입니다. elif 조건문 내부의 명령이 시간 지연 없이 계속하여 실행되어 무수한 명령을 생성합니다. 이때 명령을 생성하는 속도가 기기와 통신하는 속도보다 빨라 처리되지 않은 명령이 누적되어 코드가 올바르게 동작하지 않습니다. 따라서 아래와 같이 sleep 문을 추가하여 실행해야 합니다.

from RobokitRS import *
from time import sleep
import keyboard

rs = RobokitRS.RobokitRS()
rs.port_open("COM5")
rs.sonar_begin(13)  

while(True):
    SONAR = rs.sonar_read(13)
    if SONAR >= 100:
        rs.motor_write(0,0,15)
        sleep(0.1)

    elif SONAR < 30:
        rs.motor_write(0,0,0)
        sleep(0.1)

while문 탈출

로봇이 장시간동안 동작하기 위한 코드에 무한 반복을 위한 'while true:' 가 있다면 while을 탈출할 수 있는 방법이 있어야 합니다.(실행중인 코드를 강제 종료하는 것은 권장하지 않습니다.) 다음은 ESC 키를 눌러서 무한 반복문을 탈출하는 것에 대한 가이드입니다.

Keyboard package 설치

Terminal(Powershell, VSCode Terminal 등)에 다음과 같이 입력하여 Keyboard package를 설치합니다.

pip install keyboard

예시 코드

from RobokitRS import *
from time import sleep
import keyboard

rs = RobokitRS.RobokitRS()
rs.port_open("COM5")
rs.sonar_begin(13)  

while(True):
    if keyboard.is_pressed('ESC'):
        rs.end()
        break

    SONAR = rs.sonar_read(13)
    if SONAR >= 100:
        rs.motor_write(0,0,15)
        sleep(0.1)

    elif SONAR < 30:
        rs.motor_write(0,0,0)
        sleep(0.1)

while loop 바로 아래에 ESC 키를 누르는 것을 검출하는 조건문이 있습니다. 따라서 위와 같이 해당 조건문 내에 종료를 위한 코드를 작성하면, ESC를 눌러 실행중인 코드를 종료할 수 있습니다. 사용자가 추가적으로 원하는 코드가 있다면 rs.end() 함수 앞에 추가로 작성하는 것을 권장합니다..

버전 업데이트 정보

1.0.1.29 (2023.09.12.)

end() 함수 업데이트

초기화 신호 송신을 통해 현재 동작중인 명령을 모두 종료하고 코드 실행을 종료.

1.0.1.30 (2024.11.16.)

digital_reads(), analog_reads() 함수 수정

3.11 버전 기준 기존 코드가 range(list) 로 인해 에러가 발생하여 수정.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

RobokitRS-1.0.1.30-py3-none-any.whl (15.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page