本文整理汇总了Python中arduino.Arduino.connect方法的典型用法代码示例。如果您正苦于以下问题:Python Arduino.connect方法的具体用法?Python Arduino.connect怎么用?Python Arduino.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类arduino.Arduino
的用法示例。
在下文中一共展示了Arduino.connect方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Neopixel
# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import connect [as 别名]
class Neopixel():
def __init__(self):
self.board = Arduino()
self.board.connect()
def reset(self):
return self.board.sendCommand(Commands.RESET_NEO_PIXELS,0,0)
def writePixel(self,pixel,r,g,b):
return self.board.sendCommand(Commands.WRITE_NEO_PIXEL,0,pixel,r,g,b)
示例2: Encoder
# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import connect [as 别名]
class Encoder():
def __init__(self):
self.board = Arduino()
self.board.connect()
def getRevsright(self):
rightrevs = self.board.sendCommand(Commands.READ_RIGHT_ENCODER,0,-1)
return rightrevs
def getRevsleft(self):
leftrevs = self.board.sendCommand(Commands.READ_LEFT_ENCODER,0,-1)
return leftrevs
示例3: Ultrasound
# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import connect [as 别名]
class Ultrasound():
def __init__(self):
self.board = Arduino()
self.board.connect()
def getDistance(self):
distance = int(self.board.sendCommand(Commands.READ_ULTRASOUND,TRIGGER_PIN,0))
# large distances are reported as 0, so change to max distance
if distance == 0:
distance = 100
# limit to 100
elif distance > 100:
distance = 100
return distance
示例4: Arduino
# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import connect [as 别名]
from arduino import Commands, Arduino
from time import sleep
from motors import Motors
from mission import Mission
board = Arduino()
board.connect()
move = Motors()
mission = Mission()
power_f = 100
power_t = 100
mission.startMission()
while True:
print("Forward")
move.forward(power_f)
sleep(4)
move.stop()
print("turn left")
move.leftMotor(power_f, 0)
move.rightMotor(power_f, 1)
sleep(1)
move.stop()
sleep(1)
print("turn right")
move.leftMotor(power_f, 1)
示例5: Mission
# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import connect [as 别名]
class Mission():
# tested
def __init__(self):
if pi is True:
self.board = Arduino()
self.board.connect()
self.move = Motors()
GPIO.setup(GREEN_LED_GPIO,GPIO.OUT)
GPIO.setup(RED_LED_GPIO,GPIO.OUT)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# load the sample hash
with open(rfid_hash) as fh:
self.rfid_hash = json.load(fh)
# won't test
def startMission(self):
if pi is True:
GPIO.output(GREEN_LED_GPIO,False)
GPIO.output(RED_LED_GPIO,True)
print("waiting for button...")
while GPIO.input(BUTTON) == False:
time.sleep(0.1)
GPIO.output(RED_LED_GPIO,False)
# won't test
def endMission(self):
if pi is True:
GPIO.output(GREEN_LED_GPIO,True)
# tested
def deleteData(self):
try:
os.unlink(data_file)
except OSError:
pass
# tested
# stores a dict as json on the disk
# appends to the data file
def saveData(self, sample):
with open(data_file,'a') as fh:
fh.write(json.dumps(sample) + "\n")
# tested
# returns a list of dicts
def loadData(self):
data = []
try:
with open(data_file,) as fh:
for sample in fh.readlines():
data.append(json.loads(sample))
except IOError:
pass
return data
# won't test
# fetches an RFID
def getLocation(self):
if pi is True:
rfid = ""
# keep moving until robot gets a location
while True:
rfid = self.board.sendCommand(Commands.READ_RFID,0,0)
# when I catalogued the RFIDs I missed the last char!
rfid = rfid[0:11]
if len(rfid) == 11:
break
self.move.forward(70)
time.sleep(0.1)
self.move.stop()
time.sleep(1)
return rfid
# tested
# indexes into the sample database with the RFID and returns sample as dict
def takeSample(self, location):
try:
return self.rfid_hash[location]
except KeyError:
raise Exception("unknown location [%s]" % location)
# tested
# uploads a sample dict with a team name (string)
def uploadSample(self, sample, team):
# fetch team ID from nane
r = requests.get(mc_url + '/api/team/' + team)
if r.status_code == 400:
raise Exception(r.text)
team_id = json.loads(r.text)['id']
sample['team'] = str(team_id)
# posts it
r = requests.post(mc_url + '/api/sample', json=sample)
if r.status_code == 400:
return r.text
#.........这里部分代码省略.........
示例6: Motors
# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import connect [as 别名]
class Motors():
def __init__(self):
self.board = Arduino()
self.board.connect()
def enable(self):
self.board.sendCommand(Commands.WRITE_DIGITAL,7,1) # puts motor drive into Phase/Enable mode
def disable(self):
self.board.sendCommand(Commands.WRITE_DIGITAL,7,0)# puts motor drive into IN/IN mode
def forward(self, speed):
self.enable()
commandL = self.speedToCommand(speed)
commandR = self.speedToCommand(speed)
self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,1)
self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,0)
self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,commandR)
self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,commandL)
def getLeft(self):
value = self.board.sendCommand(Commands.READ_LEFT_ENCODER,0,0)
return value
def getRight(self):
value = self.board.sendCommand(Commands.READ_RIGHT_ENCODER,0,0)
return value
def getRightDistance(self):
value = self.board.sendCommand(Commands.READ_RIGHT_DISTANCE,0,0)
return value
def getLeftDistance(self):
value = self.board.sendCommand(Commands.READ_LEFT_DISTANCE,0,0)
return value
def getAtPosition(self):
value = self.board.sendCommand(Commands.AT_POSITION,0,0)
return value
def backward(self, speed):
self.enable()
commandL = self.speedToCommand(speed)
commandR = self.speedToCommand(speed)
self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,commandL)
self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,commandR)
self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,0)
self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,1)
def leftMotor(self, speed, direction):
self.enable()
command = self.speedToCommand(speed)
if(direction==1):
self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,1)
self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,command)
else:
self.board.sendCommand(Commands.WRITE_PWM,LEFT_MOTOR_SPEED,command)
self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_DIRECTION,0)
def rightMotor(self, speed, direction):
self.enable()
command = self.speedToCommand(speed)
if(direction==1):
self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,0)
self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,command)
else:
self.board.sendCommand(Commands.WRITE_PWM,RIGHT_MOTOR_SPEED,command)
self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_DIRECTION,1)
def stop(self):
self.board.sendCommand(Commands.WRITE_DIGITAL,LEFT_MOTOR_SPEED,0)
self.board.sendCommand(Commands.WRITE_DIGITAL,RIGHT_MOTOR_SPEED,0)
#self.position(0)
#self.rotation(0)
def speedToCommand(self,speed):
#speed between 0-100
command = int(speed * 2.55)
if(command > 255):
command = 255
return command
# lower level command to move
def position(self,pos,speed,block):
self.board.sendCommand(Commands.POSITION,DUMMY_PIN,pos)
if block:
there=int(self.getAtPosition())
while int(self.getAtPosition()) == 0:
print("on my way")
sleep(0.2)
#.........这里部分代码省略.........
示例7: Arduino
# 需要导入模块: from arduino import Arduino [as 别名]
# 或者: from arduino.Arduino import connect [as 别名]
import cv2
import os
from arduino import Arduino
arduino = Arduino()
arduino.connect()
cascPath = os.path.dirname(os.path.abspath(__file__)) + '/data/haarcascade_frontalface_alt.xml'
print "hello"
if not os.path.isfile(cascPath):
print "file not exist " + cascPath
exit()
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
video_capture.set(cv2.CAP_PROP_FRAME_COUNT, 10)
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 500)
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 500)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
cv2.flip(frame, 1, frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,