Python Arduino Command API는 물리적 와이어 또는 무선으로 표준 직렬 IO를 사용하여 연결된 컴퓨터의 Arduino 마이크로 컨트롤러 보드와 통신하기위한 가벼운 파이썬 라이브러리입니다. Firmata와 유사한 사용자 정의 프로토콜을 사용하여 작성되었습니다.
이를 통해 사용자는 Python 코드를 사용하여 Arduino의 Protoype 프로그램을 신속하게 또는 보드 자체에 스케치를 다시 컴파일하고 다시로드 할 필요없이 Arduino 보드에 연결된 Harware를 간단히 읽고/제어/문제 해결/실험 할 수 있습니다.
Python Arduino Command API 내의 메소드 이름은 Arduino 프로그래밍 언어에 대한 가능한 한 가깝게 설계되었습니다.
#!/usr/bin/env python
"""
Blinks an LED on digital pin 13
in 1 second intervals
"""
from Arduino import Arduino
import time
board = Arduino ( '9600' ) #plugged in via USB, serial com at rate 9600
board . pinMode ( 13 , "OUTPUT" )
while True :
board . digitalWrite ( 13 , "LOW" )
time . sleep ( 1 )
board . digitalWrite ( 13 , "HIGH" )
time . sleep ( 1 )
명령 줄에서 pip install arduino-python
하거나 python setup.py build install
실행 하여이 라이브러리를 설치하십시오.
prototype.ino
의 setup()
함수 (Line 348)에 지정된 보드 속도로 통신하는지 확인하십시오. 필요한 경우 변경하십시오.prototype.ino
타입을로드하십시오.from Arduino import Arduino
Arduino와 통신하십시오. 예제 모음은 examples.py
참조하십시오. 이 파일에는 많은 Arduino 데모 스케치의 기능을 복제하는 방법이 포함되어 있습니다.
tests
디렉토리에는 라이브러리에 대한 몇 가지 기본 테스트가 포함되어 있습니다. 긍정적 인 테스트에는 실제로 특정 함수를 테스트하는 데 필요한 하드웨어를 호스팅하는 긍정적 인 테스트에는 실제로 살아있는 Arduino에 명령을 연결하고 발행하는 것이 포함되므로 광범위한 코드 적용 범위는 약간 어렵습니다. 그러나 기본 커뮤니케이션 테스트의 핵심은 적어도 여기에서 유지되고 master
브랜치로 병합되기 전에 사용해야합니다.
설치 후 대화식 테스트는 소스 디렉토리에서 실행할 수 있습니다.
$ python tests/test_main.py
자동화 된 테스트는 다음과 같이 소스 디렉토리에서 실행할 수 있습니다.
$ python tests/test_arduino.py
Arduino(baud)
- 현재 연결된 및 전원 Arduino와의 커뮤니케이션을 설정하십시오. board = Arduino ( "9600" ) #Example
연결된 Arduino의 장치 이름 / com 포트는 자동 감지됩니다. 둘 이상의 Arduino 보드가 연결되어있는 경우 원하는 COM 포트를 선택적 인수로 전달할 수 있습니다.
board = Arduino ( "9600" , port = "COM3" ) #Windows example
board = Arduino ( "9600" , port = "/dev/tty.usbmodemfa141" ) #OSX example
Arduino의 읽기를위한 타임 아웃은 선택적 인수로 지정할 수도 있습니다.
board = Arduino ( "9600" , timeout = 2 ) #Serial reading functions will
#wait for no more than 2 seconds
디지털 I/O
Arduino.digitalWrite(pin_number, state)
디지털 핀을 켜거나 끄십시오Arduino.digitalRead(pin_number)
디지털 핀 상태를 읽습니다 #Digital read / write example
board . digitalWrite ( 13 , "HIGH" ) #Set digital pin 13 voltage
state_1 = board . digitalRead ( 13 ) #Will return integer 1
board . digitalWrite ( 13 , "LOW" ) #Set digital pin 13 voltage
state_2 = board . digitalRead ( 13 ) #Will return integer 0
Arduino.pinMode(pin_number, io_mode)
SET PIN I/O 모드Arduino.pulseIn(pin_number, state)
맥박을 측정합니다Arduino.pulseIn_set(pin_number, state)
전제 조건으로 맥박을 측정합니다 #Digital mode / pulse example
board . pinMode ( 7 , "INPUT" ) #Set digital pin 7 mode to INPUT
duration = board . pulseIn ( 7 , "HIGH" ) #Return pulse width measurement on pin 7
아날로그 I/O
Arduino.analogRead(pin_number)
아날로그 값을 반환합니다Arduino.analogWrite(pin_number, value)
아날로그 값을 설정합니다 #Analog I/O examples
val = board . analogRead ( 5 ) #Read value on analog pin 5 (integer 0 to 1023)
val = val / 4 # scale to 0 - 255
board . analogWrite ( 11 ) #Set analog value (PWM) based on analog measurement
시프트 레지스터
Arduino.shiftIn(dataPin, clockPin, bitOrder)
바이트를 이동하여 반환합니다.Arduino.shiftOut(dataPin, clockPin, bitOrder, value)
주어진 바이트를 전환합니다 bitOrder
"MSBFIRST"
또는 "LSBFIRST"
여야합니다.
서보 라이브러리 기능 지원은 최대 8 서보에 포함됩니다.
Arduino.Servos.attach(pin, min=544, max=2400)
서보 인스턴스를 만듭니다. 한 번에 8 개의 서보 만 사용할 수 있습니다.Arduino.Servos.read(pin)
지정된 핀에 연결된 서보 각도를 반환합니다.Arduino.Servos.write(pin, angle)
핀에 첨부 된 서보를 지정된 각도로 이동Arduino.Servos.writeMicroseconds(pin, uS)
지정된 핀의 서보에 마이크로 초 값을 작성하십시오.Arduino.Servos.detach(pin)
지정된 핀에서 서보를 분리합니다 #Servo example
board . Servos . attach ( 9 ) #declare servo on pin 9
board . Servos . write ( 9 , 0 ) #move servo on pin 9 to 0 degrees
print board . Servos . read ( 9 ) # should be 0
board . Servos . detach ( 9 ) #free pin 9
소프트웨어 직렬 기능
Arduino.SoftwareSerial.begin(ss_rxPin, ss_txPin, ss_device_baud)
지정된 핀에서 소프트웨어 직렬 장치 초기화. 한 번에 하나의 SOFWare 직렬 장치 만 사용할 수 있습니다. 기존 소프트웨어 직렬 인스턴스는 파이썬과 Arduino 보드 모두 에서이 방법을 호출하여 덮어 씁니다.Arduino.SoftwareSerial.write(data)
기존 소프트웨어 직렬 연결에 Arduino 'Write'기능을 사용하여 데이터를 보냅니다.Arduino.SoftwareSerial.read()
기존 소프트웨어 직렬 연결에서 1 바이트를 반환합니다. #Software serial example
board . SoftwareSerial . begin ( 0 , 7 , "19200" ) # Start software serial for transmit only (tx on pin 7)
board . SoftwareSerial . write ( " test " ) #Send some data
response_char = board . SoftwareSerial . read () #read response character
eeprom
Arduino.EEPROM.read(address)
eeprom에서 바이트를 읽습니다Arduino.EEPROM.write(address, value)
eeprom에 바이트를 씁니다Arduino.EEPROM.size()
EEPROM의 크기를 반환합니다 #EEPROM read and write examples
location = 42
value = 10 # 0-255(byte)
board . EEPROM . write ( location , 10 )
print ( board . EEPROM . read ( location ))
print ( 'EEPROM size {size}' . format ( size = board . EEPROM . size ()))
기타
Arduino.close()
Arduino에 대한 직렬 연결을 닫습니다. print()
및 println()
)Wire.h
명령)Serial1.read()
) 등에 대한 다중 서식 지원