Exemplo n.º 1
0
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
Exemplo n.º 2
0
def testWrite(digital_pins, analog_pins):
    board = Arduino(serial_ports()[0])
    iterator = util.Iterator(board)
    iterator.start()
    board.pass_time(1)
    for i in range(
            1, 14
    ):  # 1-13 because there are problems with writing in Digital Pin 0
        pin = board.get_pin(getPin('d', i, 'o'))
        if (i in digital_pins):
            pin.write(1)
        else:
            pin.write(0)

    for pinNo in analog_pins:
        continue
    board.exit()
Exemplo n.º 3
0
"""
Programa pisca.py.

Exemplo executado num ambiente Linux
"""
from pyfirmata import Arduino

PORTA = "/dev/ttyACM0"

uno = Arduino(PORTA)
led = uno.get_pin('d:13:o')


while True:
    led.write(1)
    uno.pass_time(0.5)
    led.write(0)
    uno.pass_time(0.5)
Exemplo n.º 4
0
#!/usr/bin/python
from pyfirmata import Arduino, util

PIN = 12 # Pin 12 is used
DELAY = 2 # A 2 seconds delay

PORT = '/dev/ttyACM0'
board = Arduino(PORT)

while True:
    board.digital[PIN].write(1) # Set the LED pin to 1 (HIGH)
    board.pass_time(DELAY)
    board.digital[PIN].write(0) # Set the LED pin to 0 (LOW)
    board.pass_time(DELAY)
Exemplo n.º 5
0
					board.digital[3].write(1)
					board.digital[4].write(1)
					board.digital[5].write(1)

				else:
					board.digital[2].write(0)
					board.digital[3].write(0)
					board.digital[4].write(0)
					board.digital[5].write(0)

				if datas[3] == 1: # time
					if flag_1f == 1:  # direction
						for i in range(stepPerResolution):
							board.digital[7].write(1)
							board.digital[11].write(1)
							board.pass_time(DELAY)
							board.digital[11].write(0)
							board.pass_time(DELAY)
							flag_1f = 0
				else: 
					if flag_1f == 0:
						for i in range(stepPerResolution):
							board.digital[7].write(0)
							board.digital[11].write(1)
							board.pass_time(DELAY)
							board.digital[11].write(0)
							board.pass_time(DELAY)
							flag_1f = 1

				if datas[4] == 1:   # heat_ctl
					board.digital[12].write(1)
Exemplo n.º 6
0
from pyfirmata import Arduino
from pyfirmata import PWM

board = Arduino("/dev/cu.usbserial-AL008BFQ")

board.digital[13].mode = PWM

for i in range(100):
    print(i)
    board.digital[13].write(i / 100.0)
    board.pass_time(0.05)
Exemplo n.º 7
0
board = Arduino('COM5')
ster = board.get_pin('a:0:p')
gas = board.get_pin('a:1:i')
brake = board.get_pin('a:2:i')
iter = util.Iterator(board)

min_gas, min_brake = calibration.calibrator(board, iter, gas, brake)

interpol_gas = interp1d([min_gas, 1], [0, 16384*2])
interpol_brake = interp1d([min_brake, 1], [0, 16384*2])
interpol_steer = interp1d([0, 1], [0, 16384*2])

j = pyvjoy.VJoyDevice(1)

board.pass_time(0.2)

while True:
    if gas.read() >= min_gas and brake.read() >= min_brake:
    # if gas.read() >= 0.52:
    # if brake.read() >= 0.52:
        gas_val = int(interpol_gas(gas.read()))
        brake_val = int(interpol_brake(brake.read()))
        j.data.wAxisZRot = brake_val
        j.data.wAxisZ = gas_val
        print('Gas: {}, Brake: {}'.format(gas_val, brake_val), end = '\r')
    steer_val = int(interpol_steer(ster.read()))
    j.data.wAxisX = steer_val
    j.update()
    time.sleep(0.05)
Exemplo n.º 8
0
Relay.write(1)

f = function()
while True:
    f.camera()
    readPC = f.mask()
        
    print('1번')
    print(readPC)
  
    if readPC == 1:
        Relay.write(0) # 문열림 NO를 연결 전기 흐름
        print('2번')
        print(readPC)
    else:
        Relay.write(1) # 문닫힘 NO 평상시 상태 전기 흐름X
        print('3번')
        print(readPC)
    
    serial.pass_time(10)  # 10초간 상태 유지
    print('RMX')
    print(readPC)
    Relay.write(1) # 문닫힘


# In[ ]:




Exemplo n.º 9
0
from getSerialPort import serial_ports
try:
    from pyfirmata import Arduino, util
except:
    import os
    os.system('pip install pyfirmata')
    from pyfirmata import Arduino, util

board = Arduino(serial_ports()[0])

iterator = util.Iterator(board)
iterator.start()
board.pass_time(1)
"""led = board.get_pin('d:12:o')
for i in range(5):
    led.write(1)
    board.pass_time(1)
    led.write(0)
    board.pass_time(1)"""


def getPin(pinType, pinNo, pinMode):
    return (pinType + ':' + str(pinNo) + ':' + pinMode)


def testWrite(digital_pins, analog_pins):
    board = Arduino(serial_ports()[0])
    iterator = util.Iterator(board)
    iterator.start()
    board.pass_time(1)
    for i in range(
Exemplo n.º 10
0
def main():
    print('Hello there, please record your command'
          ' immediately after you see start')

    board = Arduino('/dev/ttyUSB0')
    RightLeftServoPin = board.get_pin('d:5:s')
    ForwardBackwardServoLPin = board.get_pin('d:6:s')
    UpDownServoPin = board.get_pin('d:7:s')
    # gripServoPin = board.get_pin('d:8:p')
    sleep(3)

    iterSer = util.Iterator(board)
    iterSer.start()

    angle = 90
    change = 45

    while True:
        command = imp()
        if command == "left":
            print('Your command is : {} \n Turning Left'.format("Left"))
            RightLeftServoPin.write(angle - change)
            board.pass_time(2)
        elif command == "right":
            print('Your command is : {} \n Turning Right'.format("Right"))
            RightLeftServoPin.write(angle + change)
            board.pass_time(2)
        elif command == "up":
            print('Your command is : {} \n Going Up'.format("Up"))
            UpDownServoPin.write(angle + change)
            board.pass_time(2)
        elif command == "down":
            print('Your command is : {} \n Going Down'.format("Down"))
            UpDownServoPin.write(angle - change)
            board.pass_time(2)
        elif command == "forward":
            print('Your command is : {} \n Going forward'.format("forward"))
            ForwardBackwardServoLPin.write(angle + change)
            board.pass_time(2)
        elif command == "backward":
            print('Your command is : {} \n Going backward'.format("backward"))
            ForwardBackwardServoLPin.write(angle - change)
            board.pass_time(2)
        elif command == "off":
            print('Your command is : {} \n Turning off '.format("off"))
            break

    board.exit()
Exemplo n.º 11
0
#Input
customer_order = ""
while customer_order not in menu.keys():
    customer_order = input(
        f"Select the drink type: ({', '.join(menu.keys())}): ").strip()

price = menu[customer_order]

quantity = ""
while not quantity.isdigit():
    quantity = input("Select the quantity of drinks: ").strip()
quantity = int(quantity)

if quantity % 2 == 0:
    green_led.write(1)
    board.pass_time(3)
    green_led.write(0)
else:
    red_led.write(1)
    board.pass_time(3)
    red_led.write(0)

membership_values = {"Y": True, "N": False}
membership_input = ""
while membership_input not in membership_values.keys():
    membership_input = input("Are you a member (Y/N): ").strip()
membership = membership_values[membership_input]

#Calculation
initial_amount = price * quantity
purchase_discount = getPercent(initial_amount,