def off():
    GPIO.output(17, 0)
    #return "The Light is now Off!"
    with Kulka(ADDR) as kulka:
        print "Inside With"
        do_a_square(kulka)
    return "The Light is now Off!"
Esempio n. 2
0
def align_zeros(addrs, kulkas=[]):
    def test_offset(kulka):
        happy = False
        kulka.roll(50, 0)
        time.sleep(1)
        kulka.roll(0, 0)
        offset_est = int(input("Enter offset estimate:"))
        kulka.roll(50, offset_est)
        happy_str = input("Happy with offset?\n")
        if happy_str != "y":
            test_offset(kulka)
        else:
            return offset_est

    addr = addrs.pop()
    with Kulka(addr) as kulka:
        kulkas.append(kulka)
        print("Sphero " + str(len(kulkas)) + " online")
        kulka.set_inactivity_timeout(30)
        kulka.set_rgb(0xFF, 0, 0)
        if len(addrs) > 0:
            align_zeros(addrs, kulkas)
        else:
            offsets = []
            for kulka in kulkas:
                offsets.append(test_offset(kulka))
            return (offsets, kulkas)
Esempio n. 3
0
    def __init__(self, address):
        self._addr = address
        self._kulka = Kulka(address)

        for _ in range(5):
            self.change_ball_color(randint(0, 255), randint(0, 255), randint(0, 255))
            time.sleep(0.1)
Esempio n. 4
0
def main():
    with Kulka(ADDR) as kulka:
        for _ in range(3):
            do_the_dance(kulka)
            time.sleep(0.1)

        print("The end. Sphero goes to bed.")
        kulka.sleep()
Esempio n. 5
0
def main():
    with Kulka(ADDR) as kulka:
        blink_rate = 0.001

        for _ in range(10):
            kulka.set_rgb(255, 255, 255)
            time.sleep(blink_rate)
            kulka.set_rgb(0, 0, 0)
            time.sleep(blink_rate)
Esempio n. 6
0
def func1(labelPredXD):
    #print("HELP")
    with Kulka(ADDR) as kulka:
        while (True):
            #print(labelPredXD.value)
            if (labelPredXD.value == 1):
                print('Do something')
                make_a_circle(kulka, STEPS)
            elif (labelPredXD.value == 0):
                print('Do nothing')
Esempio n. 7
0
def main(addr):
    with Kulka(addr) as kulka:
        print("Connected to Sphero ", addr)
        kulka.set_inactivity_timeout(300)
        for _ in range(6):
            speed = randint(30, 100)
            direction = randint(0, 359)
            print("Rolling")
            print(speed, direction)
            kulka.roll(speed, direction)
            time.sleep(5)
        kulka.sleep()
Esempio n. 8
0
def main():
    with open('mykulka.txt') as file_:
        addr = file_.readline().strip()

    with Kulka(addr) as kulka:
        kulka.set_inactivity_timeout(3600)

        for _ in repeat(None, 600):
            kulka.roll(randint(30, 100), randint(0, 359))
            time.sleep(1)

        kulka.sleep()
Esempio n. 9
0
def main():
    with Kulka(ADDR) as kulka:
        kulka.set_inactivity_timeout(3600)
        blink_rate = 1

        for _ in range(5):
            blink_rate = abs(blink_rate - 0.05)
            kulka.set_rgb(random.randint(0, 255), random.randint(0, 255),
                          random.randint(0, 255))
            time.sleep(blink_rate)
            kulka.set_rgb(0, 0, 0)
            time.sleep(blink_rate)
Esempio n. 10
0
def main():
    addr = '68:86:E7:06:FD:1D'
    with Kulka(addr) as kulka:
        kulka.set_inactivity_timeout(3600)

        for _ in range(10):
            kulka.set_rgb(0, 0xFF, 0)
            time.sleep(0.1)
            kulka.set_rgb(0, 0, 0)
            time.sleep(0.1)
            print(kulka.sequence())


        kulka.sleep()
Esempio n. 11
0
def main():
    with open('mykulka.txt') as file_:
        addr = file_.readline().strip()

    with Kulka(addr) as kulka:
        kulka.set_inactivity_timeout(3600)

        for _ in xrange(10):
            kulka.set_rgb(0xFF, 0, 0)
            time.sleep(0.1)
            kulka.set_rgb(0, 0, 0)
            time.sleep(0.1)

        kulka.sleep()
Esempio n. 12
0
def main():
    ADDR0 = '68:86:E7:07:07:6B'
    ADDR1 = '68:86:E7:06:FD:1D'
    ADDR2 = '68:86:E7:08:0E:DF'

    with Kulka(ADDR0) as kulka0:
        print("Sphero 0 online")
        with Kulka(ADDR1) as kulka1:
            print("Sphero 1 online")
            with Kulka(ADDR2) as kulka2:
                print("Sphero 2 online")
                kulka0.set_inactivity_timeout(30)
                kulka1.set_inactivity_timeout(30)
                kulka2.set_inactivity_timeout(30)

                kulka0.set_rgb(0, 0, 0xFF)
                kulka1.set_rgb(0, 0xFF, 0)
                kulka2.set_rgb(0xFF, 0, 0)
                time.sleep(20)
                kulka0.set_rgb(0xFF, 0xFF, 0xFF)
                kulka1.set_rgb(0xFF, 0xFF, 0xFF)
                kulka2.set_rgb(0xFF, 0xFF, 0xFF)
                time.sleep(20)

                # for i in range(20):
                #     kulka0.roll(15, i*45 % 360)
                #     kulka1.roll(15, i*45 % 360)
                #     kulka2.roll(15, i*45 % 360)
                #     time.sleep(3)
                #     kulka0.roll(0, i*45 % 360)
                #     kulka1.roll(0, i*45 % 360)
                #     kulka2.roll(0, i*45 % 360)

                kulka0.sleep()
                kulka1.sleep()
                kulka2.sleep()
Esempio n. 13
0
def func1(labelPredXD):
  #print("HELP")
  with Kulka(ADDR) as kulka:
    while(True):
      #print(labelPredXD.value)
      if(labelPredXD.value == 3):
        print ('Do something')
        make_it_go_1(kulka)
      elif(labelPredXD.value == 2):
        print ('spread')
        make_it_go_180(kulka)
      elif(labelPredXD.value == 1):
        print ('dance')
        make_it_dance(kulka)
      else:
        print ('Do nothing')
Esempio n. 14
0
def main(addr, limit=10, max_distance=100, speed=50):
    print("Connecting to Sphero at ", addr)
    with Kulka(addrs[i]) as kulka:
        kulka.set_inactivity_timeout(300)
        t0 = time.time()
        t1 = time.time()
        steps = 0
        direction = 0
        while (t1 - t0) < limit * 60:
            print("Step ", steps)
            steps += 1
            roll_until_stuck(kulka, direction)
            time.sleep(0.1)
            direction = randint(0, 359)
            t1 = time.time()
        kulka.close()
Esempio n. 15
0
def main():
    addr = '68:86:E7:08:0E:DF'
    with Kulka(addr) as kulka:
        kulka.set_inactivity_timeout(300)
        kulka.set_streaming()
        for i in range(10):
            kulka.set_rgb(0, 0, 0xFF)
            time.sleep(0.1)
            kulka.set_rgb(0, 0, 0)
            time.sleep(0.1)
            kulka.set_rgb(0xFF, 0, 0)
            # kulka._connection.recv(1024)
            print(kulka._recv_buffer)

            time.sleep(1)

        kulka.sleep()
Esempio n. 16
0
def classifySignal():
    global interpreter
    global input_details
    global output_details

    arrayXD = np.empty((1, 150, 8), dtype="float32")
    super_inp = np.asarray(dataRecollectedPerIteration, dtype="float32")
    arrayXD[0] = super_inp
    interpreter.set_tensor(input_details[0]['index'], arrayXD)
    interpreter.invoke()

    output_data = interpreter.get_tensor(output_details[0]['index'])
    if (np.argmax(output_data) == 1):
        with Kulka(ADDR) as kulka:
            make_a_circle(kulka, STEPS)
    else:
        print("nothing")
Esempio n. 17
0
def main(addr, limit = 1, max_distance = 100, speed = 50):
	print("Connecting to Sphero at ", addr)
	with Kulka(addr) as kulka:
		kulka.set_inactivity_timeout(300)
		t0 = time.time()
		t1 = time.time()
		print("Moving to random position")
		for _ in range(3):
			direction = randint(0, 359)
			kulka.roll(speed, direction)
		while ((t1 - t0) < limit * 60):
			time.sleep(3)
			distance = distance_from_point(kulka)
			# print(steps, distance)
			if distance > 100: 
				print("outside range, rolling home")
				taxicab_homing(kulka)
				break
			kulka.roll(speed, direction)	
			t1 = time.time()
	kulka.close()
Esempio n. 18
0
def main():
    addr = '68:86:E7:06:FD:1D'
    with Kulka(addr) as kulka:
        kulka.set_inactivity_timeout(300)
        for i in range(5):
            kulka.set_rgb(0, 0, 0xFF)
            time.sleep(0.1)
            kulka.set_rgb(0, 0, 0)
            time.sleep(0.1)
            kulka.set_rgb(0xFF, 0, 0)
            direction = input('What direction?\n')
            if int(direction) == -1:
                break
            speed = input('What speed?\n')
            kulka.roll(int(speed), int(direction))
            for i in range(10):
                kulka.read_locator()
                data = kulka.data_poll()
                # print("Raw output:\n", data[2])
                output = parse_locator(data)
                print(output)
        kulka.sleep()
Esempio n. 19
0
from kulka import Kulka
import time

sphero = Kulka('68:86:E7:09:23:79')

count = 0

while (count < 5):
    sphero.set_rgb(0xFF, 0, 0)
    sphero.roll(35, 0)
    time.sleep(10)
    sphero.roll(35, 180)
    time.sleep(10)
    count += 1
Esempio n. 20
0

"""
Cześć! To Twój pierwszy program Sphero. Uruchom go, wywołując w
konsoli następującą komendę:

    $ python 1_hello_sphero.py

I zaobseruj co się dzieje z Kuleczką.
"""


ADDR = os.environ["SPHERO"]


with Kulka(ADDR) as kulka:
    # nadaj Sphero kolor
    kulka.set_rgb(255, 0, 0)
    print("czerwono!")
    time.sleep(5)

    kulka.set_rgb(0, 255, 0)
    print("zielono!")
    time.sleep(5)

    kulka.set_rgb(0, 0, 255)
    print("niebiesko!")
    time.sleep(5)

    print("koniec.")
Esempio n. 21
0
#sudo pip install kulka

import time
from kulka import Kulka
from random import randint

with Kulka('00:06:66:47:37:C7') as kulka:
	while True:
		kulka.roll(60 , randint(0, 359))
		time.sleep(.5)		

def game_loop_Simon():
    global pause, pattern, index, waitinginput, play
    clock = pygame.time.Clock()
    play = True
    index = 0
    pattern = []
    waitinginput = False
    score = 0
    clicked = None
    gameDisplay.fill(black)
    #drawbutton_Simon()
    displayscore(score)
    pygame.display.update()

    with Kulka(ADDR) as kulka:  #Conecta
        while play:
            #gameDisplay.fill(black)
            #drawbutton_Simon()
            #displayscore(score)
            #pygame.display.update()
            clock.tick(60)
            clicked = None
            for event in pygame.event.get():
                if (event.type == pygame.QUIT):
                    quitgame()
                if (event.type == pygame.KEYDOWN):  #Entrada do teclado
                    if (event.key == pygame.K_q):
                        clicked = YELLOW
                    elif (event.key == pygame.K_w):
                        clicked = BLUE
                    elif (event.key == pygame.K_a):
                        clicked = RED
                    elif (event.key == pygame.K_s):
                        clicked = GREEN
                    if (event.key == pygame.K_p):
                        pause = True
                        paused()

            if not waitinginput:
                gameDisplay.fill(black)
                #drawbutton_Simon()
                displayscore(score)
                pygame.display.update()
                #pygame.display.update()
                pygame.time.wait(1000)
                pattern.append(random.choice((YELLOW, BLUE, RED, GREEN)))
                #origsurf = gameDisplay.copy()
                for button in pattern:
                    #print (button)
                    kulka.set_rgb(button[0], button[1], button[2])  #Sphero cor
                    pygame.time.wait(300)
                    kulka.set_rgb(0, 0, 0)
                    #drawbutton_Simon()
                    #pygame.display.update()
                    pygame.time.wait(500)
                    #gameDisplay.blit(origsurf,(0,0))

                waitinginput = True
                drawbutton_Simon()
                pygame.display.update()

            else:
                if clicked and clicked == pattern[index]:
                    drawsibutton_Simon(clicked)
                    pygame.display.update()
                    drawbutton_Simon()
                    pygame.display.update()
                    pygame.time.wait(200)
                    index += 1
                    if index == len(pattern):
                        score += 1
                        waitinginput = False
                        index = 0
                        gameDisplay.fill(black)
                        drawbutton_Simon()
                        displayscore(score)
                        pygame.display.update()
                elif clicked and clicked != pattern[index]:
                    crash(score)
def game_loop_Circle():
    global pause, pattern, index, waitinginput, play
    clock = pygame.time.Clock()
    play = True
    steps = 5
    stepsANT = steps
    direction = 1
    directionANT = direction
    waitinginput = False
    color = None
    colorANT = None
    score = 0
    #	gameDisplay.fill(black)
    #	displayscore(score)
    #	pygame.display.update()
    clock.tick(60)

    with Kulka(ADDR) as kulka:
        while play:
            clicked = None
            displayscore(score)
            clock.tick(60)

            for event in pygame.event.get(
            ):  #leitura do teclado para colher a resposta
                if (event.type == pygame.QUIT):
                    quitgame()
                if (event.type == pygame.KEYDOWN):
                    if (event.key == pygame.K_q):
                        clicked = "YES"
                        resUser = "******"
                    elif (event.key == pygame.K_w):
                        clicked = "NO"
                        resUser = "******"
                    if (event.key == pygame.K_p):
                        pause = True
                        paused()
            if not waitinginput:  #chamar funcao do circle
                pygame.time.wait(1000)
                Color = random.choice((YELLOW, BLUE, RED, GREEN))
                direction = random.choice((1, -1))
                steps = random.choice((5, 10, 15, 20))
                make_a_circle(kulka, steps, direction, Color)
                question = random.choice(
                    ("A cor e a mesma que a anterior?", "A cor era amarelo?",
                     "A cor era verde?", "A cor era vermelho?",
                     "A cor era azul?", "A direcao foi a mesma?",
                     "A bola foi para a esquerda?",
                     "A bola foi para a direita?",
                     "O tamanho era maior que o anterior?",
                     "O tamanho era o mesmo anterior?",
                     "O tamanho era menor que o anterior?"
                     ))  #Perguntas possiveis

                if question == "A cor e a mesma que a anterior?":
                    if color == colorANT:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "A cor era amarelo?":
                    if color == YELLOW:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "A cor era verde?":
                    if color == GREEN:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "A cor era vermelho?":
                    if color == RED:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "A cor era azul?":
                    if color == BLUE:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "A direcao foi a mesma?":
                    if direction == directionANT:
                        res = "YES"
                    else:
                        res = "NO"

                if question == "A bola foi para a direita?":
                    if direction == 1:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "A bola foi para a esquerda?":
                    if direction == -1:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "O tamanho era o mesmo anterior?":
                    if steps == stepsANT:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "O tamanho era menor que o anterior?":
                    if steps < stepsANT:
                        res = "YES"
                    else:
                        res = "NO"
                if question == "O tamanho era maior que o anterior?":
                    if steps > stepsANT:
                        res = "YES"
                    else:
                        res = "NO"
                #Faz a tela de resposta
                gameDisplay.fill(bgcolor)
                fontText = pygame.font.SysFont("cooperblack", 25)
                TextSurf, TextRect = text_objects(question, fontText)
                TextRect.center = (400, 100)
                gameDisplay.blit(TextSurf, TextRect)
                button("SIM", 150, 450, 100, 30, GREEN, bright_green, None)
                button("NAO", 550, 450, 100, 30, RED, bright_red, None)
                pygame.display.update()
                waitinginput = True
            else:
                if clicked and res == resUser:  #Certa a resposta
                    #drawsibutton_Simon(clicked)
                    #pygame.display.update()
                    pygame.time.wait(200)
                    score += 1
                    waitinginput = False
                    clicked = None
                    displayscore(score)
                    button("ACERTOU", 150, 450, 100, 30, GREEN, bright_green,
                           None)
                    pygame.display.update()
                    colorANT = color
                    directionANT = direction
                    stepsANT = steps
                    pygame.time.wait(500)
                    displayscore(score)
                    gameDisplay.fill(black)
                    pygame.display.update()
                elif clicked and res != resUser:
                    crash(score)  #errou
Esempio n. 24
0
    for layer in l:
        model.add(Dense(layer['size'], activation=layer['activation']))
    # the output layer
    model.add(Dense(num_outputs, activation='tanh'))
    model.compile(optimizer=optimizer, loss="mean_squared_error")
    return model


if __name__ == "__main__":
    addrs = ['68:86:E7:06:FD:1D', '68:86:E7:07:07:6B', '68:86:E7:08:0E:DF']
    while True:
        i = int(input("Enter Sphero #: "))
        n = int(input("Enter number of steps: "))
        epi = int(input("Enter number of episodes: "))
        model = baseline_model()
        with Kulka(addrs[i]) as kulka:
            for _ in range(epi):
                if os.path.isfile("sphero_model.h5") == True:
                    print("Loading existing model")
                    model.load_weights("sphero_model.h5")
                output = play_game(kulka=kulka, model=model, steps=n)
                i = 0
                model.save("sphero_model.h5")
                model_filestr = "models/autosaved_model_" + str(i) + ".h5"
                data_filestr = "episodes/autosaved_data_" + str(i) + ".p"
                while (os.path.isfile(model_filestr) == True):
                    i += 1
                    model_filestr = "models/autosaved_model_" + str(i) + ".h5"
                    data_filestr = "episodes/autosaved_data_" + str(i) + ".p"
                if model != None:
                    replay_loss = model.train_on_batch(output[0], output[1])
Esempio n. 25
0
class Codesphere(object):

    def __init__(self, address):
        self._addr = address
        self._kulka = Kulka(address)

        for _ in range(5):
            self.change_ball_color(randint(0, 255), randint(0, 255), randint(0, 255))
            time.sleep(0.1)


    def __enter__(self):
        return self


    def __exit__(self, type_, value, traceback):
        self._kulka.close()


    def set_inactivity_timeout(self, timeout):
        return self._kulka.set_inactivity_timeout(timeout)


    def change_ball_color(self, red, green, blue):
        return self._kulka.set_rgb(red, green, blue)


    def change_tail_brightness(self, bright):
        return self._kulka.set_back_led(bright)


    def set_heading(self, heading):
        return self._kulka.set_heading(heading)

    def roll(self, heading, wait):
        self._kulka.roll(175, heading, 1)
        time.sleep(wait + 0.5)
        return


    def roll_for_one_second(self, heading):
        self._kulka.roll(175, heading, 1)
        time.sleep(1 + 0.5)
        return

    def stop(self):
        self._kulka.roll(0, 0, 1)
        time.sleep(0.5)
        return

    def sleep(self, wakeup=0, macro=0, orb_basic=0):
        return self._kulka.sleep(wakeup, macro, orb_basic)
Esempio n. 26
0
def main():
    with open('mykulka.txt') as file_:
        addr = file_.readline().strip()

    with Kulka(addr) as kulka:
        kulka.sleep()
Esempio n. 27
0
def main():
    with Kulka(ADDR) as kulka:
        make_a_circle(kulka, STEPS)