def runThymioControl(userFun,eventlist=None): if eventlist is None: eventlist = ["prox","buttons"] eq = EventQueue(eventList) with pythymio.thymio(eventlist,[]) as Thym: # initialize runThymioControl.Thym for use by functions av(), td()... runThymioControl.Thym=Thym # check user_fun() takes no parameters and returns a generator try: userAgent = user_fun() if not isinstance(userAgent,types.GeneratorType): return None except TypeError: raise TypeError('Votre fonction ne doit pas prendre de parametre') # connect event queue with userAgent queueLoopGenerator = eq.queueLoop(userAgent) queueLoopGenerator.next() def dispatch(evtid, evt_name, evt_args): try: queueLoopGenerator.send((evt_name,evt_args)) except StopIteration: Thym.stop() Thym.loop(dispatch)
def runThymioControl(userFun,eventlist=None): eq = EventQueue() if eventlist is None: eventlist = ["prox","buttons"] with pythymio.thymio(eventlist,[]) as Thym: runThymioControl.Thym=Thym queueLoopGenerator = eq.queueLoop(userFun) queueLoopGenerator.next() def dispatch(evtid, evt_name, evt_args): try: queueLoopGenerator.send((evt_name,evt_args)) except StopIteration: Thym.stop() Thym.loop(dispatch)
def runThymioControl(userFun, eventlist=None): eq = EventQueue() if eventlist is None: eventlist = ["prox", "buttons"] with pythymio.thymio(eventlist, []) as Thym: runThymioControl.Thym = Thym queueLoopGenerator = eq.queueLoop(userFun) queueLoopGenerator.next() def dispatch(evtid, evt_name, evt_args): try: queueLoopGenerator.send((evt_name, evt_args)) except StopIteration: Thym.stop() Thym.loop(dispatch)
#!/usr/bin/python # -*- coding: utf-8 -*- import pythymio as pt GREATEST_SUFFIX_MATCH = True def prefix(s, t): return len(s) <= len(t) and s == t[:len(s)] with pt.thymio(["buttons"]) as Thym: state = {} state["delay"] = 100 state["seq"] = "UUDDLRLR" #↑ ↑ ↓ ↓ ← → ← → state["i"] = 0 state["n"] = len(state["seq"]) state["etape"] = "read" state["readseq"] = " " def button2str(t): if t[0] == 1: return "D" if t[1] == 1: return "L" if t[3] == 1: return "U" if t[4] == 1: return "R" return "?"
angle = cap_evitement(460, 100, a=0.1) cap = orientation() + angle for m in orienter(cap): yield m for m in avancer(4): yield m # avancer pendant 1s yield ((0, 0)) #state["navigateur"] = test_cap() #state["navigateur"] = test_polygone(cote = 4) state["navigateur"] = test_evitement() print state["navigateur"].next() with pythymio.thymio(["motor", "prox"], []) as Thym: def progression(evn, evt, data): if evt == "fwd.motor": #toutes les 0.01s state["i"] += 1 # mettre a jour position et orientation left = data[0] / 500.0 right = data[1] / 500.0 delta = right - left state["orientation"] += delta * 0.6 # OK av = (right + left) * 0.04 state["x"] += cosdeg(state["orientation"]) * av state["y"] += sindeg(state["orientation"]) * av if state["i"] == 10: #toutes les 0.1s state["i"] = 0 try:
#!/usr/bin/python # -*- coding: utf-8 -*- import pythymio as pt with pt.thymio(["buttons"], pt.customEvents("circle")) as Thym: state = {} state["delay"] = 100 state["seq"] = "LRLRUUDD" # ← → ← → ↑ ↑ ↓ ↓ state["i"] = 0 state["n"] = len(state["seq"]) state["etape"] = "stop" """ etape, etape * i | V stop -----> read 0 -....- read n ---\ ^ | | | \__________________________________/ """ def play(s): if s == "L": Thym.send_event("circle.left") if s == "R": Thym.send_event("circle.right") if s == "U":
angle = cap_evitement(460, 100, a = 0.1) cap = orientation() + angle for m in orienter(cap): yield m for m in avancer(4): yield m # avancer pendant 1s yield((0, 0)) #state["navigateur"] = test_cap() #state["navigateur"] = test_polygone(cote = 4) state["navigateur"] = test_evitement() print state["navigateur"].next() with pythymio.thymio(["motor", "prox"],[]) as Thym: def progression(evn, evt, data): if evt == "fwd.motor": #toutes les 0.01s state["i"] += 1 # mettre a jour position et orientation left = data[0] / 500.0 right = data[1] / 500.0 delta = right - left state["orientation"] += delta * 0.6 # OK av = (right + left) * 0.04 state["x"] += cosdeg(state["orientation"]) * av state["y"] += sindeg(state["orientation"]) * av if state["i"] == 10: #toutes les 0.1s state["i"] = 0 try: mg, md = state["navigateur"].next()
#!/usr/bin/python # -*- coding: utf-8 -*- import pythymio import random from gardenworld import * init('info2_1') with pythymio.thymio(["acc"],[]) as Thym: state = dict([]) state["time"] = 0 state["delay"] = 10 def dispatch(evtid, evt_name, evt_args): # https://www.thymio.org/en:thymioapi prox freq is 16Hz if evt_name == "fwd.acc": # every 0.0625 sec state["time"] += 0.0625 state["delay"] -= 1 if state["delay"] < 0: if 7 < evt_args[1] < 14: if evt_args[0] > 10: state["delay"] = 20 tg() elif evt_args[0] < -10: state["delay"] = 20 td() elif evt_args[1] > 20 and abs(evt_args[0]) < 8: state["delay"] = 10 av() elif evt_args[1] < 5:
#!/usr/bin/python # -*- coding: utf-8 -*- import pythymio as pt with pt.thymio(["buttons"], pt.customEvents('circle')) as Thym: state = {} state["delay"] = 100 state["seq"] = "LRLRUUDD" # ← → ← → ↑ ↑ ↓ ↓ state["i"] = 0 state["n"] = len(state["seq"]) state["etape"] = "stop" """ etape, etape * i | V stop -----> read 0 -....- read n ---\ ^ | | | \__________________________________/ """ def play(s): if s == "L": Thym.send_event('circle.left') if s == "R": Thym.send_event('circle.right') if s == "U": Thym.send_event('circle.front') if s == "D": Thym.send_event('circle.back')
#!/usr/bin/python # -*- coding: utf-8 -*- import pythymio as pt GREATEST_SUFFIX_MATCH = True def prefix(s, t): return len(s) <= len(t) and s == t[:len(s)] with pt.thymio(["buttons"]) as Thym: state = {} state["delay"] = 100 state["seq"] = "UUDDLRLR" #↑ ↑ ↓ ↓ ← → ← → state["i"] = 0 state["n"] = len(state["seq"]) state["etape"] = "read" state["readseq"] = " " def button2str(t): if t[0] == 1: return "D" if t[1] == 1: return "L" if t[3] == 1: return "U" if t[4] == 1: return "R" return "?" def play(s):
#!/usr/bin/python # -*- coding: utf-8 -*- import pythymio import random def randomstr(s, length): """ return a random string with all char in s of a given length """ res = "" for i in range(length): res += s[random.randint(0, len(s) - 1)] return res with pythymio.thymio(["buttons"]) as Thym: state = dict([]) state["time"] = 0 state["difficulty"] = 3 state["delay"] = 80 state["seq"] = randomstr("CEGB", state["difficulty"]) state["etape"] = "play" state["i"] = 0 def play(N): if N == "B": Thym.send_event('become.yellow') Thym.send_event('circle.back') Thym.send_event('chord.B3') elif N == "C":
#!/usr/bin/python # -*- coding: utf-8 -*- import pythymio with pythymio.thymio(["motor"]) as Thym: state = dict([]) state["time"] = 0 state["left"] = [0] state["right"] = [0] state["prog"] = dict([ (90, (500, 500)), #50 av (140, (0, 0)), (180, (-500, 500)), #108 tg (288, (0, 0)), (350, (500, 500)), #50 av (400, (0,0)), (450, (500, -500)), #108 td (558, (0, 0)), (600, (500, 500)), #50 av (650, (0, 0)), (700, (500, -500)),#108 td (808, (0, 0)), (860, (500, 500)), #50 av (910, (0, 0)), (960, (500, -500)),#108 av (1068, (0, 0)), (1120, (500, 500)),#50 av (1170, (0, 0)) ]) state["end"] = max(state["prog"].keys()) + 10