def __init__(self): self.status = "Initializing" logging.info("Initializing motor drivers") # Arduinos must be flashed with Standard Firmata try: self.ard0 = PyMata3(arduino_wait=2, com_port='/dev/ttyACM2') # Lattepanda onboard arduino self.ard1 = PyMata3(arduino_wait=2, com_port='/dev/ttyACM1') # Additional arduino micro except: logging.error("Motor fail") self.status = "FAIL" return # FL self.m0 = 3 self.ard0.servo_config(self.m0, 1000, 2000) # FR self.m1 = 5 self.ard0.servo_config(self.m1, 1000, 2000) # RL self.m2 = 6 self.ard0.servo_config(self.m2, 1000, 2000) # RR self.m3 = 9 self.ard0.servo_config(self.m3, 1000, 2000) # Auger self.m4 = 5 self.ard1.servo_config(self.m4, 1000, 2000) # Slider self.m5 = 3 self.ard1.servo_config(self.m5, 1000, 2000) # Tilt-mining self.m6 = 10 self.ard0.servo_config(self.m6, 1000, 2000) # Deposit bucket self.m7 = 11 self.ard0.servo_config(self.m7, 1000, 2000) self.stop() self.status = "OK" # TODO: real status check (arduinos present, etc)
class Uno: # Initialize PyMata3 and set motor_spd and MOTOR_PIN uno = PyMata3() motor_spd = 255 MOTOR_PIN = 9 dispense_rate = 5 # units in mL/second desired_amount = 0 # default value for desired_amount def __init__(self): # initialize class self.uno.set_pin_mode(self.MOTOR_PIN, Constants.PWM) def set_amount(self, amount): # set value for desired_amount self.desired_amount = amount def pour_amount( self): # begin pouring desired_amount at motor_spd for 5 mL/second self.uno.analog_write(self.MOTOR_PIN, self.motor_spd) time.sleep(self.desired_amount / self.dispense_rate) self.uno.analog_write(self.MOTOR_PIN, 0) def clean_container( self ): # clean container by running pump at full speed for 10 seconds self.uno.analog_write(self.MOTOR_PIN, self.motor_spd) time.sleep(10) self.uno.analog_write(self.MOTOR_PIN, 0) def stop_pour(self): # end pouring, only used during testing functionality self.uno.analog_write(self.MOTOR_PIN, 0)
def __init__(self): """ Instantiate a Board instance """ # create a PyMata instance self.board: PyMata3 = PyMata3(2) # Used to keep track of ultrasonic sensors with the key being the trig pin self.__ultrasonics: Dict[int, UltraSonic] = {} # Setup flag that is switched to True once setup_environment has occurred has self.__setup: bool = False # Acceptable range for values (cm) to be read range to be valid, will get properly set during setup self.__max_distance: int = 200 self.__min_distance: int = 0 # Callback will collect values to __setup_values if __collect_values is set to True self.__collect_values: bool = False self.__setup_values: List = [] self.lock = False self.last_sensor_read = None log.info("Board Successfully Created")
def __init__(self): self.board = PyMata3() self.steps_per_revolution = 32 self.ratio = 64 self.steps = self.steps_per_revolution * self.ratio self.pins = [8, 10, 9, 11] self.setup()
def main(): host = '' port = 7777 server = None board = PyMata3(5) shoulder = Actuator(board, 9) arm = Actuator(board, 10) elbow = Actuator(board, 11, min_angle=-90, max_angle=90, offset=-90) global mech_arm mech_arm = MechanicalArm( [shoulder, arm, elbow], Fabrik( joint_positions=[Vector2(0, 0), Vector2(53, 0), Vector2(100, 0)], link_lengths=[53, 47], tolerance=0.1 ) ) sleep(2) while server is None: try: server = TCPServer((host, port), ConnectionHandler) except OSError: port += 1 continue print("Serving on: {}".format(port)) server.serve_forever() server.server_close()
def __init__(self, address, blink_rate, brightness): # create an instance of PyMata - we are using an Arduino UNO """ @param address: I2C address of the device @param blink_rate: desired blink rate @param brightness: brightness level for the display """ # create a PyMata instance self.firmata = PyMata3() self.board_address = address self.blink_rate = blink_rate self.brightness = brightness self.clear_display_buffer() # configure firmata for i2c on an UNO self.firmata.i2c_config() # turn on oscillator self.oscillator_set(self.OSCILLATOR_ON) # set blink rate self.set_blink_rate(self.blink_rate) # set brightness self.set_brightness(self.brightness)
def arduino_bridge(): """ Main function for arduino bridge :return: """ # noinspection PyShadowingNames parser = argparse.ArgumentParser() parser.add_argument("-b", dest="board_number", default="1", help="Board Number - 1 through 10") parser.add_argument("-p", dest="comport", default="None", help="Arduino COM port - e.g. /dev/ttyACMO or COM3") parser.add_argument('-r', dest='router_ip_address', default='None', help='Router IP Address') args = parser.parse_args() if args.comport == "None": pymata_board = PyMata3() else: pymata_board = PyMata3(com_port=args.comport) board_num = args.board_number router_ip_address = args.router_ip_address abridge = ArduinoBridge(pymata_board, board_num, router_ip_address) # while True: abridge.run_arduino_bridge() # signal handler function called when Control-C occurs # noinspection PyShadowingNames,PyUnusedLocal,PyUnusedLocal def signal_handler(signal, frame): print("Control-C detected. See you soon.") abridge.clean_up() sys.exit(0) # listen for SIGINT signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler)
def reset(self) -> None: from pymata_aio.pymata3 import PyMata3 board = PyMata3() try: board.shutdown() except SystemExit: pass
def __init__(self, servo_pin = 6, esc_pin = 5): from pymata_aio.pymata3 import PyMata3 self.board = PyMata3() self.board.sleep(0.015) self.servo_pin = servo_pin self.esc_pin = esc_pin self.board.servo_config(servo_pin) self.board.servo_config(esc_pin)
def __init__(self, com_port=None, arduino_wait=5): self.board = PyMata3(com_port=com_port, arduino_wait=arduino_wait) self.LOW = LOW self.HI = HI self.IN = IN self.OUT = OUT self.BRD = BRD self.BCM = BCM pass
def initBoard(self): self.board = PyMata3( log_output=True, arduino_wait=5) # initializing X stepper with device number 0 self.board.get_pin_state(2) self.board.accelstepper_config( 0, 1, 1, 0, [2,5]) self.board.get_pin_state(2) # initializing Y stepper with device number 1 self.board.accelstepper_config( 1, 1, 1, 0, [3,6]) # initializing Z stepper with device number 2 self.board.accelstepper_config( 2, 1, 1, 0, [4,7])
def __init__(self): self.board = PyMata3(com_port=self.portArduino()) self.TRLIGHT_RED = 8 self.TRLIGHT_YELLOW = 7 self.TRLIGHT_GREEN = 2 self.RGB_RED = 5 self.RGB_GREEN = 3 self.RGB_BLUE = 6 self.BOARD_LED = 13 self.PWM_LED = 9 self.ON = 1 self.OFF = 0
def __init__(self, port='', pwmMax=100): board = None try: #Board init if len(port) > 0: board = PyMata3(com_port=port) else: board = PyMata3() print("Firmware version {0}".format(board.get_firmware_version())) except Exception as excObj: print("Something is wrong: {0}".format(excObj)) raise excObj self.__board = board #Led init self.leds = { 1: Led(board, 10, False, Constants.PWM, pwmMax), 2: Led(board, 9, False, Constants.PWM, pwmMax), 3: Led(board, 5, False, Constants.PWM, pwmMax), 4: Led(board, 4, False, Constants.OUTPUT, 1), 5: Led(board, 8, True, Constants.OUTPUT, 1) } #Buzzer init #Keyboard init self.keys = { 1: Key(board, 13), 2: Key(board, 12), 3: Key(board, 7), 4: Key(board, 8) } #Sensors init self.SensorLight = AnalogSensor(board, 0) self.SensorTemperature = AnalogSensor(board, 1) self.SensorAlcohol = AnalogSensor(board, 2) self.SensorAxisX = AnalogSensor(board, 3) self.SensorAxisY = AnalogSensor(board, 4) self.SensorAxisZ = AnalogSensor(board, 5)
def __init__(self, name): self.name = name self.board = PyMata3() self.cur_loc = {'x': 0, 'y': 0, 'z': -140} self.num_steps = 5 self.s1 = Servo("{} S1".format(self.name), self.board, SERVO_PIN1) self.s2 = Servo("{} S2".format(self.name), self.board, SERVO_PIN2) self.s3 = Servo("{} S3".format(self.name), self.board, SERVO_PIN3) self.move_to(0, 0, -140) self.s_power = Servo("{} PW".format(self.name), self.board, SERVO_POWER) self.script = list() self.script.append([list(self.cur_loc.values()), None])
def testConnection(self): ''' Test connection to the board as well as Firmware is loaded https://github.com/MrYsLab/pymata-aio/issues/63#issuecomment-377400310 ''' try: board = PyMata3() # Board not plugged in - correct Firmware/sketch or not except TypeError: assert False # incorrect Firmware/sketch loaded except asyncio.futures.CancelledError: assert False
def main(): bd = PyMata3() bd.set_pin_mode(2, Constants.OUTPUT) for i in range(100): bd.digital_write(2, 1) print(' 1') time.sleep(3) bd.digital_write(2, 0) print('0 ') time.sleep(2) bd.shutdown()
def check_for_firmata(port_list): boards = {} for port in port_list: temp_board = PyMata3(com_port=port) temp_fw_string = temp_board.get_firmware_version() if "TestRig-FW.ino" in temp_fw_string: boards["MASTER"] = temp_board elif "DUT-FW.ino" in temp_fw_string: boards["DUT"] = temp_board return boards
def __init__(self, router_ip_address=None, subscriber_port='43125', publisher_port='43124'): """ This method instantiates a PyMata3 instance. It sets pin 9 as a digital output and analog pin 2 as sn input. A callback method is associated with the analog input pin to report the current value. :return: """ super().__init__(router_ip_address, subscriber_port, publisher_port) self.board = PyMata3(3) self.board.set_pin_mode(self.BLUE_LED, Constants.OUTPUT) self.board.set_pin_mode(self.POTENTIOMETER, Constants.ANALOG, self.analog_callback)
def __init__(self): # motor pins such as MX_pins = [EN,IN1,IN2] self.M1_pins = [6, 7, 4] self.M2_pins = [5, 3, 2] self.M3_pins = [11, 13, 12] self.M4_pins = [10, 9, 8] self.data_pin = 14 self.load_pin = 15 self.clock_pin = 16 self.analog_in_x_pin = 4 self.analog_in_y_pin = 5 # instantiate the pymata_core API self.uno_board = PyMata3(2)
def read(self): self.board = PyMata3() if not self.board else self.board self.digi = [DigitalOut(self.board, i) for i in [int(i) for i in self.d_out.split(',')]] while True: try: results = {} for pin in [AnalogIn(self.board, i, Constants.ANALOG) for i in [int(i) for i in self.a_in.split(',')]]: results[pin.pin_id] = pin.value # print(f'{str(pin.pin_id):>2}:{str(pin.value)}:<4 ', end='') # print('') self.board.sleep(self.sleep) yield results # {0: 0-1023, 1: 0-1023} except (KeyboardInterrupt, SystemExit): self.board.shutdown()
def pin_6_pwm_128(): """ Set digital pin 6 as a PWM output and set its output value to 128 @return: """ # instantiate the pymata_core API board = PyMata3() # set the pin mode board.set_pin_mode(6, Constants.PWM) board.analog_write(6, 128) # wait for 3 seconds to see the LED lit board.sleep(3) # reset the board and exit board.shutdown()
def pinsBoardInit(self): # Uncoment: In case of problems specify port explicitly # self.BOARD_COMPORT = "COM4" # self.board = PyMata3(com_port=self.BOARD_COMPORT) self.board = PyMata3(arduino_wait = 5) # TODO uncomment to configure i2c communications self.board.i2c_config() self.board.set_pin_mode(Consts.AR_NANO_PINS['board led'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['big pump On/Off'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['small pump on/Off'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['outlet valve to big pump'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['outlet valve to small pump'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['inlet valve'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['exhaust valve for small pump'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['trap valve, extra outlet valve to small pump'], Constants.OUTPUT) self.board.set_pin_mode(Consts.AR_NANO_PINS['gas1 real flow, analog_read pin'], Constants.ANALOG) self.board.set_pin_mode(Consts.AR_NANO_PINS['gas2 real flow, analog_read pin'], Constants.ANALOG) self.board.set_pin_mode(Consts.AR_NANO_PINS['pressure value, analog_read pin'], Constants.ANALOG) self.board.set_pin_mode(Consts.AR_NANO_PINS['gas1 real flow setter, pwm pin'], Constants.PWM) self.board.set_pin_mode(Consts.AR_NANO_PINS['gas2 real flow setter, pwm pin'], Constants.PWM) self.initRelayPins()
def __init__(self, port): self.port = port self.state = { "left_analog_x": 0, "left_analog_y": 0, "right_analog_x": 0, "right_analog_y": 0, "l2_analog": 0, "r2_analog": 0, "battery": 0 } self.a = PyMata3(com_port=self.port) self.a.set_pin_mode(self.BRAKE_A, Constants.OUTPUT) self.a.set_pin_mode(self.DIR_A, Constants.OUTPUT) self.a.set_pin_mode(self.PWM_A, Constants.PWM) self.a.set_pin_mode(self.BRAKE_B, Constants.OUTPUT) self.a.set_pin_mode(self.DIR_B, Constants.OUTPUT) self.a.set_pin_mode(self.PWM_B, Constants.PWM) thread = Thread(target=self.loop, name='Robot') thread.start()
* the Arduino community. This code is completely free for any use. * * 18 Feb 2015 B. Huang ***********************************************************************/""" from pymata_aio.pymata3 import PyMata3 from pymata_aio.constants import Constants from library.redbot import RedBotMotors, RedBotSensor COM_PORT = None # Use automatic com port detection (the default) #COM_PORT = "COM5" # Manually specify the com port (optional) # This line "includes" the RedBot library into your sketch. # Provides special objects, methods, and functions for the RedBot. board = PyMata3(com_port=COM_PORT) left = RedBotSensor(board, 3) # pin number assignments for each IR sensor center = RedBotSensor(board, 6) right = RedBotSensor(board, 7) # constants that are used in the code. LINETHRESHOLD is the level to detect # if the sensor is on the line or not. If the sensor value is greater than this # the sensor is above a DARK line. # # SPEED sets the nominal speed LINE_THRESHOLD = 800 SPEED = 150 # sets the nominal speed. Set to any number 0-255 motors = RedBotMotors(board)
#!/usr/bin/env python3 """Example script using acnet.py and Firmata""" from pymata_aio.pymata3 import PyMata3 from pymata_aio.constants import Constants from acnet import Device ARDUINO = PyMata3() REMOTE = Device('Z:REMOTE') ARDUINO.read_pin = 2 ON = 1023 OFF = 0 ARDUINO.set_pin_mode(ARDUINO.read_pin, Constants.ANALOG) def rawToVolts(raw): return raw / 1023 * 5 while True: ARDUINO.sleep(1) readingVolts = rawToVolts(ARDUINO.analog_read(ARDUINO.read_pin)) print(readingVolts) REMOTE.set_setting(readingVolts)
# Import Modules from pymata_aio.pymata3 import PyMata3 from pymata_aio.constants import Constants from time import sleep #open socket for control board - these are from Matt's program, so I assume these will connect to the board. cboard = PyMata3(ip_address = '192.168.0.177', ip_port=3030, ip_handshake='') #the ip address is set by Matt's router I believe. #I assume the port is part of his hardware. What is the handshake? as in why is it ' '? cboard.i2c_config(0) #0 is the delay? #arduino = PyMata3() #arduino.i2c_config() # Models - What is the purpose for these? As far as I see below, it has something to do with the different sensors and how they calculate pressure. MODEL_02BA = 0 #This is our 2 Bar pressure sensor #MODEL_30BA = 1 #30 Bar pressure sensor # Oversampling options - I don't care as long as it works... OSR_256 = 0 OSR_512 = 1 OSR_1024 = 2 OSR_2048 = 3 OSR_4096 = 4 OSR_8192 = 5 # kg/m^3 convenience DENSITY_FRESHWATER = 997 DENSITY_SALTWATER = 1029 # Conversion factors (from native unit, mbar) UNITS_Pa = 100.0 UNITS_hPa = 1.0
def __init__(self, pin): self.board = PyMata3() self.pin = pin self.setup()
from pymata_aio.pymata3 import PyMata3 from pymata_aio.constants import Constants from time import sleep #arduino = PyMata3() # Why don't we do this? I assume we use the open socket stuff... #arduino.i2c_config() # or this? #open socket for control board - this is from Matt board = PyMata3(ip_address = '192.168.0.177', ip_port=3030, ip_handshake='') class TSYS01(object): # What is the purpose of making a "class?" # Registers - Why do these start with underscore? _TSYS01_ADDR = 0x77 _TSYS01_RESET = 0x1E _TSYS01_READ = 0x00 _TSYS01_PROM_READ = 0xA0 _TSYS01_CONVERT = 0x48 def __init__(self, model=MODEL_30BA): self._model = model try: self._board = board except: self._board = None self._temperature = 0 def init(self): if board is None: "No board!"
#!/usr/bin/python """ Pymata-aio port of the Arduino Servo --> Sweep Example """ from pymata_aio.pymata3 import PyMata3 board = PyMata3() SERVO_PIN = 11 def setup(): board.servo_config(SERVO_PIN) def loop(): print("Servo up") # The range of motion for some servos isn't all the way from 0 degrees to 180 degrees, change as needed. for pos in range( 0, 180 ): # Start=0 degrees, Finish=180 degree, (Increment=1 degree which is the default) board.analog_write(SERVO_PIN, pos) board.sleep(0.015) print("Servo down") for pos in range( 180, 0, -1 ): # Start=180 degrees, Finish=0 degrees, Increment=-1 degrees (moving down) board.analog_write(SERVO_PIN, pos) board.sleep(0.015)
1. Just plug the servos into servo ports on the Arduino 3, 9, or 10 (NOT 11!). This demo uses 3 and 10. 2. Plug the servos in on the Pixy board as recommended here http://cmucam.org/projects/cmucam5/wiki/Assembling_pantilt_Mechanism This code assumes you have connected the servos connected to the RedBot board NOT to the Pixy board. """ from pymata_aio.pymata3 import PyMata3 WIFLY_IP_ADDRESS = None # Leave set as None if not using WiFly WIFLY_IP_ADDRESS = "10.0.1.19" # If using a WiFly on the RedBot, set the ip address here. #WIFLY_IP_ADDRESS = "r01.wlan.rose-hulman.edu" # If your WiFi network allows it, you can use the device hostname instead. if WIFLY_IP_ADDRESS: # arduino_wait is a timer parameter to allow for the arduino to reboot when the connection is made which is NA for WiFly. board = PyMata3(arduino_wait=0, sleep_tune=0.0, ip_address=WIFLY_IP_ADDRESS) else: # Use a USB cable to RedBot or an XBee connection instead of WiFly. COM_PORT = None # Use None for automatic com port detection, or set if needed i.e. "COM7" board = PyMata3(sleep_tune=0.0, com_port=COM_PORT) # Servo connection locations on the RedBot board. PIN_PAN_SERVO = 3 PIN_TILT_SERVO = 10 def print_pixy_blocks(blocks): """ Prints the Pixy blocks data.""" print("Detected " + str(len(blocks)) + " Pixy blocks:") for block_index in range(len(blocks)):