def __init__(self, comPort, pinNumbers): assert len(pinNumbers) > 0, "Parameter pinNumbers has to contain at least one number" assert type(pinNumbers[0]) is int and type(pinNumbers[1]) is int, "pinNumbers must contain integers" self.arduino = Board(comPort, boards.BOARDS["arduino_due"]) if(len(pinNumbers)>1): self.pins = pinNumbers[:2] else: self.pins = pinNumbers self.servos = servoFirmata.servoFirmata(self.arduino, self.pins, [90,90]) time.sleep(2)
class Board(SerializableModel): pk = None port = None pins = None name = None written_pins = set() json_export = ("pk", "port", "pins", "url") def __init__(self, pk, port, layout=DEFAULT_LAYOUT, *args, **kwargs): self.pk = pk self.port = port self._board = FirmataBoard(self.port, layout) self.pins = { "analog": dict(((pin.pin_number, Pin(pk, pin.pin_number, type="analog")) for pin in self._board.analog)), "digital": dict(((pin.pin_number, Pin(pk, pin.pin_number, type="digital")) for pin in self._board.digital)), } [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()) @property def url(self): return "/%s/boards/%s/" % (API_VERSION, self.pk) def disconnect(self): for pin in self.written_pins: pin.write(0) return self._board.exit() def get_pin(self, identifier): a_d = identifier[0] == "a" and "analog" or "digital" pin_nr = int(identifier[1:]) return self.pins[a_d][pin_nr] def firmata_pin(self, firmata_identifier): return self._board.get_pin(firmata_identifier) def release_pin(self, firmata_identifier): bits = firmata_identifier.split(":") a_d = bits[0] == "a" and "analog" or "digital" pin_nr = int(bits[1]) self._board.taken[a_d][pin_nr] = False
def __init__(self, pk, port, layout=DEFAULT_LAYOUT, *args, **kwargs): self.pk = pk self.port = port self._board = FirmataBoard(self.port, layout) self.pins = { "analog": dict(((pin.pin_number, Pin(pk, pin.pin_number, type="analog")) for pin in self._board.analog)), "digital": dict(((pin.pin_number, Pin(pk, pin.pin_number, type="digital")) for pin in self._board.digital)), } [setattr(self, k, v) for k, v in kwargs.items()] super(Board, self).__init__(*args, **kwargs)
class servoFirmataTest: def __init__(self, comPort, pinNumbers): assert len(pinNumbers) > 0, "Parameter pinNumbers has to contain at least one number" assert type(pinNumbers[0]) is int and type(pinNumbers[1]) is int, "pinNumbers must contain integers" self.arduino = Board(comPort, boards.BOARDS["arduino_due"]) if(len(pinNumbers)>1): self.pins = pinNumbers[:2] else: self.pins = pinNumbers self.servos = servoFirmata.servoFirmata(self.arduino, self.pins, [90,90]) time.sleep(2) def execute(self): print("Servo zero to pos 0") self.servos.setPosition(0, 0) time.sleep(3) print("Servo one to pos 0") self.servos.setPosition(1, 0) time.sleep(3) print("Servo zero and one to pos 90") self.servos.setPosition(0, 90) self.servos.setPosition(1, 90) time.sleep(3) print("Test complete") self.arduino.exit() return True
from pyfirmata import Board, util, boards from random import randint import math PORT = '/dev/cu.usbmodem1411' def test_handle(*data): print list(data[::2]) b = Board(PORT, boards.BOARDS['arduino'], 57600) b.add_cmd_handler(0x71, test_handle) random_strings = [] for i in xrange(10): r = map(lambda x: randint(0, 127), xrange(8))] print r s = ''.join(map(lambda x: chr(x), r)) b.send_sysex(0x71, util.str_to_two_byte_iter(s)) print '=================' b.pass_time(2) while b.bytes_available(): b.iterate()
# set the post Comport = "/dev/ttyACM0" #-------------select servo locations------------ servoPos1 = GUI.selectPoint(camera.getNewFrame()) GUI.setCircle(servoPos1, 5, (0, 0, 255)) servoPos2 = GUI.selectPoint(camera.getNewFrame()) GUI.setCircle(servoPos2, 5, (0, 0, 255)) servoPos3 = GUI.selectPoint(camera.getNewFrame()) GUI.setCircle(servoPos3, 5, (0, 0, 255)) print('servo positions set') #----------------------------------------------- # create a Board object arduino = Board(Comport, boards.BOARDS["arduino_due"]) # set initial servo position servoInitPos = 65 # create a servoFirmate object per servo servoFirmata1 = servoFirmata(arduino, [2], [servoInitPos]) servoFirmata2 = servoFirmata(arduino, [3], [servoInitPos]) servoFirmata3 = servoFirmata(arduino, [4], [servoInitPos]) #---------------------------create servo objects---------------------------------------------------------------------- # calculate the avarage of servoPos2 and servoPos3 and substract servoPos1. result is direction of servo1 dirServo1 = [(servoPos2[0] + servoPos3[0]) / 2 - servoPos1[0], (servoPos2[1] + servoPos3[1]) / 2 - servoPos1[1]] # create servo1 object