本文整理汇总了Python中pyfirmata.Arduino类的典型用法代码示例。如果您正苦于以下问题:Python Arduino类的具体用法?Python Arduino怎么用?Python Arduino使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Arduino类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
class LightsController:
def __init__(self):
self.board = Arduino("/dev/ttyACM0")
self.lightPin = self.board.get_pin("d:3:p")
def Off(self):
self.lightPin.write(0)
sleep(1)
def On(self):
self.lightPin.write(1)
sleep(1)
def Fade(self):
for pwm in arange(0.02, 0.1, 0.004):
self.lightPin.write(pwm)
sleep(0.075)
for pwm in arange(0.1, 0.8, 0.01):
self.lightPin.write(pwm)
sleep(0.075)
for pwm in arange(0.8, 0.1, -0.01):
self.lightPin.write(pwm)
sleep(0.075)
for pwm in arange(0.1, 0.02, -0.004):
self.lightPin.write(pwm)
sleep(0.075)
def Blink(self):
self.lightPin.write(1)
sleep(1)
self.lightPin.write(0)
sleep(1)
示例2: main
def main():
board = Arduino("/dev/ttyACM0")
board.add_cmd_handler(pyfirmata.pyfirmata.STRING_DATA, on_string_received)
iter = util.Iterator(board)
iter.start()
write_loop(board)
示例3: __init__
def __init__(self, port):
try:
self.board = Arduino(port)
except OSError as e:
raise Exception("Arduino not found on: {0}".format(port))
self._setup()
示例4: ArdUno
class ArdUno():
def __init__(self):
self.port = 'COM21'
self.board = Arduino(self.port)
self.anticrash()
self.readenable()
self.board.digital[8].write(1)
def anticrash(self):
""" Iterator evita que se crashee el puerto (no permite que se sobrecargue de trafico) """
it = util.Iterator(self.board)
it.start() #corremos el iterator
def readenable(self):
self.board.analog[0].enable_reporting()
def getpin(self,numchannel):
ch=str(numchannel)
pin='a:' + ch + ':i'
R=self.board.get_pin(pin)
return(R)
def chdef(self,namechannel,numchannel):
ch=str(numchannel)
R='A' + ch + ' = h.getpin(' + ch + ')'
return(R)
def voltread(self,namechannel,numchannel):
ch=str(numchannel)
A=namechannel+ ch +'.read()'
return(str(A))
示例5: __init__
def __init__(self, config, SS):
from pyfirmata import Arduino, util, SERVO
self.SS = SS
self.board = Arduino(config['board'])
self.iterator = util.Iterator(self.board)
self.iterator.daemon = True
self.iterator.start()
self.fwheel = self.board.get_pin('d:11:p')
self.fwheel.mode = SERVO
self.rwheel = self.board.get_pin('d:10:p')
self.rwheel.mode = SERVO
self.awheel = self.board.get_pin('d:3:p')
self.awheel.mode = SERVO
self.awheel.write(self.awheel_stop)
self.state = 'start'
self.controlobjects = [
ControlObject(9, self, [self.fire_red]),
ControlObject(8, self, [self.fire_orange]),
ControlObject(12, self, [self.fire_yellow]),
ControlObject(13, self, [self.fire_green])
]
self.colormatch_measurement = M.Measurement.objects(method="closestcolorml")[0]
self.timesince_measurement = M.Measurement.objects(method="timesince_manual")[0]
self.deliveredcolor_measurement = M.Measurement.objects(method="closestcolor_manual")[0]
self.region_inspection = M.Inspection.objects[0]
self.SS = SS
if not "debug" in config:
self.cw = ControlWatcher()
self.cw.control = self
self.cw.daemon = True
self.cw.start()
示例6: ArduinoConnection
class ArduinoConnection(Thread):
def __init__(self):
Thread.__init__(self)
self.SAMPLING_INTERVAL = 0.100
self.MEAN_INTERVAL = 5
self.MEAN_SAMPLES_NUMBER = round(self.MEAN_INTERVAL/self.SAMPLING_INTERVAL)
PORT = '/dev/ttyACM0'
self.board = Arduino(PORT)
it = util.Iterator(self.board)
it.start()
self.analog_pin_value_arr = [self.board.get_pin('a:0:i'), self.board.get_pin('a:1:i'), self.board.get_pin('a:2:i'), self.board.get_pin('a:3:i'), self.board.get_pin('a:4:i'), self.board.get_pin('a:5:i')]
for i in range(len(self.analog_pin_value_arr)):
self.analog_pin_value_arr[i].enable_reporting()
self.mean_analog_valuea_arr = [0.0] * 6
self.mean_analog_valuea_assigned_arr = [0.0] * 6
def run(self):
#s= ''
sample_number = 0
while True:
while (sample_number < self.MEAN_SAMPLES_NUMBER):
# time.sleep(DELAY)
self.board.pass_time(self.SAMPLING_INTERVAL)
for i in range(len(self.mean_analog_valuea_arr)):
self.mean_analog_valuea_arr[i] = self.mean_analog_valuea_arr [i] + self.analog_pin_value_arr[i].read()
sample_number = sample_number + 1
for i in range(len(self.mean_analog_valuea_arr)):
self.mean_analog_valuea_arr[i] = self.mean_analog_valuea_arr[i] / self.MEAN_SAMPLES_NUMBER
#s = s + str(self.mean_analog_valuea_arr[i]) + ' '
self.mean_analog_valuea_assigned_arr = self.mean_analog_valuea_arr
#print s
#s = ''
sample_number = 0
self.mean_analog_valuea_arr = [0.0] * 6
def getMeanAnalogArduinoValueArray(self):
return self.mean_analog_valuea_assigned_arr
示例7: __init__
def __init__(self, pk, port, *args, **kwargs):
self.pk = pk
self.port = port
self._board = Arduino(self.port)
self.pins = dict(((i, Pin(pk, i)) for i in range(14)))
[setattr(self, k, v) for k, v in kwargs.items()]
super(Board, self).__init__(*args, **kwargs)
示例8: __init__
class Hinako:
def __init__(self, port, b_pin_id, w_pin_id, h_pin_id):
self.board = Arduino(port)
'''
d: digital output
n: number PWM pin
s: servo control
'''
self.b_pin = self.board.get_pin(b_pin_id)
self.w_pin = self.board.get_pin(w_pin_id)
self.h_pin = self.board.get_pin(h_pin_id)
self.b = 0
self.w = 0
self.h = 0
def _move_servo(self, pin, begin_val, end_val, ds=0.1):
step = 1 if begin_val < end_val else -1
print '%d -> %d' % (begin_val, end_val)
print step
for i in range(begin_val, end_val, step):
print i
pin.write(i)
time.sleep(ds)
def set_bust(self, size_cm, ds=0.1):
print "bust: %d cm" % size_cm
val = int(round(map_value(size_cm, 70, 100, 65, 0)))
self._move_servo(self.b_pin, self.w, val, ds=ds)
self.w = val
def set_waist(self, val):
'''
dc motor
self.w_pin
'''
def set_hip(self, val):
self._move_servo(self.h_pin, self.h, val)
self.h = val
示例9: __init__
def __init__(self, port, claw=False):
self.uarm_status = 0
# self.pin2_status = 0
self.coord = {}
self.g_interpol_val_arr = {}
self.angle = {}
self.claw = claw
self.uarm = Arduino(port)
self.assignServos()
self.uarmDetach()
示例10: __init__
def __init__(self, app, parent=None):
super(Form, self).__init__(parent)
# Pyfirmata code
self.potPin = 0
self.servoPin = 9
self.ledPin = 13
self.board = Arduino("/dev/ttyACM0")
iterator = util.Iterator(self.board)
iterator.start()
self.board.analog[self.potPin].enable_reporting()
# self.board.digital[self.servoPin].mode = SERVO
# PyQT Code
servoLabel = QLabel("Servo")
self.servoDial = QDial()
self.servoDial.setMinimum(0)
self.servoDial.setMaximum(180)
self.servoPosition = 0
potLabel = QLabel("Potenciometro")
self.potSlider = QSlider()
self.potSlider.setMinimum(0)
self.potSlider.setMaximum(1000)
ledLabel = QLabel("Led 13")
self.ledButton = QPushButton("Light")
grid = QGridLayout()
grid.addWidget(servoLabel, 0, 0)
grid.addWidget(potLabel, 0, 1)
grid.addWidget(ledLabel, 0, 2)
grid.addWidget(self.servoDial, 1, 0)
grid.addWidget(self.potSlider, 1, 1)
grid.addWidget(self.ledButton, 1, 2)
self.setLayout(grid)
self.connect(self.ledButton, SIGNAL("pressed()"), self.ledOn)
self.connect(self.ledButton, SIGNAL("released()"), self.ledOff)
# self.connect(self.servoDial,
# SIGNAL("valueChanged(int)"), self.moveServo)
self.connect(app, SIGNAL("lastWindowClosed()"), self.cleanup)
self.timer = QTimer()
self.timer.setInterval(500)
self.connect(self.timer, SIGNAL("timeout()"), self.readPot)
self.setWindowTitle("Arduino")
self.timer.start()
示例11: ArduinoConnection
class ArduinoConnection(ConnectionBase):
def __init__(self, connection_type, port_name):
super().__init__()
self.connectionStatus = ConnectionStatus.Closed
self.connectionType = connection_type
self.portName = port_name
self._iterator = None
def open_connection(self):
if self.connectionStatus == ConnectionStatus.Open:
print(self.connectionStatus)
return None
try:
if self.board is None:
print("Initializing board object specified connected to port {} ...".format(self.portName))
self.board = Arduino(self.portName)
print("Board object created!")
if self._iterator is None:
print("Initializing iterator object to prevent overflow serial buffer ...")
self._iterator = util.Iterator(self.board)
print("Iterator object created!")
self._iterator.start()
self.connectionStatus = ConnectionStatus.Open
print("{}, {} , {} ".format(self.connectionStatus, self.connectionType, self.portName))
except serial.SerialException as e:
print(e.args[0])
if self.board is not None:
self.board.exit()
raise SystemExit
def close_connection(self):
if self.connectionStatus == ConnectionStatus.Closed:
print("Connection is already closed!")
else:
self.board.exit()
self.board = None
self._iterator = None
self.connectionStatus = ConnectionStatus.Closed
print(self.connectionStatus)
示例12: Board
class Board(SerializableModel):
pk = None
port = None
pins = None
name = None
written_pins = set()
json_export = ('pk', 'port', 'pins')
def __init__(self, pk, port, *args, **kwargs):
self.pk = pk
self.port = port
self._board = Arduino(self.port)
self.pins = dict(((i, Pin(pk, i)) for i in range(14)))
[setattr(self, k, v) for k, v in kwargs.items()]
super(Board, self).__init__(*args, **kwargs)
def __del__(self):
try:
self.disconnect()
except:
print(traceback.format_exc())
def disconnect(self):
for pin in self.written_pins:
pin.write(0)
return self._board.exit()
def firmata_pin(self, identifier):
return self._board.get_pin(identifier)
def release_pin(self, identifier):
bits = identifier.split(':')
a_d = bits[0] == 'a' and 'analog' or 'digital'
pin_nr = int(bits[1])
self._board.taken[a_d][pin_nr] = False
示例13: main
def main():
parser = ArgumentParser(
description='Bridge connecting Arduino and hat webapp')
parser.add_argument('-s', '--server', default='127.0.0.1:3000',
help='Server the webapp is running on')
parser.add_argument('device',
help='Path to Arduino device, e.g., /dev/tty/ACM0')
args = parser.parse_args()
url = ddp.ServerUrl(args.server)
board = None
hat_controller = None
try:
print 'Connecting to Arduino board...'
board = Arduino(args.device)
print 'Connecting to DDP server...'
hat_controller = HatController(url, board)
hat_controller.connect()
wait_for_user_to_exit()
finally:
if hat_controller is not None:
try:
hat_controller.disconnect()
except:
print (
'An error occurred while disconnecting from the DDP '
'server.'
)
if board is not None:
try:
board.exit()
except:
print 'An error occurred while exiting from the Arduino board.'
示例14: main
def main():
board = Arduino('/dev/ttyUSB0')
#starting values
multiplier = 400.0
cam = Camera()
js = JpegStreamer()
analog_4 = board.get_pin('a:1:i')
analog_5 = board.get_pin('a:2:i')
button_13 = board.get_pin('d:13:i')
it = util.Iterator(board)
it.start()
while (1):
#print str(analog_5.read()) + "\t\t" + str(analog_4.read())
t1 = analog_5.read()
t2 = analog_4.read()
b13 = button_13.read()
if not t1:
t1 = 50
else:
t1 *= multiplier
if not t2:
t2 = 100
else:
t2 *= multiplier
print "t1 " + str(t1) + ", t2 " + str(t2) + ", b13 " + str(b13)
cam.getImage().flipHorizontal().edges(int(t1), int(t2)).invert().smooth().save(js.framebuffer)
time.sleep(0.01)
示例15: onActivated
def onActivated(self, ec_id):
print "onActivated Begin"
pinNumber = [3, 5, 6, 7, 11]
self.pin = [None, None, None, None, None]
try:
self.board = Arduino(self._com_port[0])
for i in range(5):
self.board.servo_config(pinNumber[i], angle=90)
# Caution: Don't use board.get_pin('d:*:s') as it calls servo_config method with angle=0, which damages your servo.
self.pin[i] = self.board.digital[pinNumber[i]]
print "onActivated End"
except Exception as e:
print "some errors %s" % str(e)
self.pin[0].write(80)
return RTC.RTC_OK