def test_json(self): """ Vérifie le format json du robot """ print("Testing json format...") r = RobotMotorise() r.save("robotmotorise.json") r2 = RobotMotorise.load("robotmotorise.json") self.assertIsInstance(r2, RobotMotorise) print("Done")
def setUp(self): self.distance = 1 self.strat = DeplacementDroit( robot=RobotMotorise(forme=Pave(1, 1, 1, Point(3, 3, 0))), distance_max=self.distance) self.arene = Arene() self.arene.add(self.strat.robot)
def hook(dct): res = RobotMotorise.hook(dct) if res is not None: return res elif dct["__class__"] == RobotTarget.__name__: return RobotTarget(dct["forme"], dct["rg"], dct["rd"], dct["direction"], dct["tete"], dct["balise"])
def hook(dct): res = RobotMotorise.hook(dct) if res is not None: return res elif dct["__class__"] == RobotTarget.__name__: return RobotTarget.hook(dct) elif dct["__class__"] == Strategie.__name__: return Strategie(dct["robot"])
def __init__(self, forme=Pave(1, 1, 1), rg=Roue(0.25), rd=Roue(0.25), direction=Vecteur(1, 1, 0).norm(), tete: Tete = Tete(), balise=None): """ :param balise: Contient les couleurs à afficher """ RobotMotorise.__init__(self, forme=forme, rg=rg, rd=rd, direction=direction, tete=tete) self.balise = balise
def setUp(self): v = Vecteur(1, 1, 0).norm() p0 = Point(1, 1, 1) self.strat = StrategieVision( RobotMotorise(pave=Pave(1, 1, 1, p0.clone()), direction=v.clone()), AreneFermee(3, 3, 3)) self.target = RobotTarget(pave=Pave(1, 1, 1, p0.clone() + v * 2), direction=v.clone()) self.target.rotate_all_around(self.target.centre, -pi / 4) self.strat.arene.add(self.target)
def setUp(self): v = Vecteur(1, 1, 0).norm() p0 = Point(1, 1, 1) self.strat = TournerVersBalise(robot=RobotMotorise( forme=Pave(1, 1, 1, p0.clone()), direction=v.clone()), arene=AreneFermee(3, 3, 3)) self.target = RobotTarget(forme=Pave(1, 1, 1, (p0.clone() + v * 3)), direction=v.clone()) self.strat.robot.rotate_all_around(self.strat.robot.centre, 30 * pi / 180) self.strat.robot.update() self.strat.arene.add(self.target)
def test_target_circle_trajectectory(self): self.strat = DroitVersBaliseVision(RobotMotorise(forme=Pave(1,1,1,self.p0.clone()), direction=self.v2.clone()), AreneFermee(10,10,10)) self.strat2 = DeplacementCercle(robot=self.target,vitesse=30, diametre=3, angle_max=360) td = Thread(target=self.strat.start_3D) sim = Simulation(strategies=[self.strat2, self.strat], tmax=15, tic=1,tic_display=[self.strat], final_actions=[self.strat.stop_3D]) td.start() while not self.strat.robot.tete.sensors["cam"].is_set: pass sim.start() while not sim.stop: pass self.assertNotEqual(self.strat.last_detected, None)
def setUp(self): c = Point(2, 2, 1) v = Vecteur(1, 0, 0) dist = 3.0 self.arene = Arene() self.strat = DeplacementDroitAmeliore(robot=RobotMotorise( forme=Pave(1, 1, 1, c.clone()), direction=v.clone()), arene=self.arene) self.v = self.strat.robot.direction * dist self.p = Pave(0.5, 0.5, 0.5, self.strat.robot.centre + self.v) self.arene.add(self.p) self.arene.add(self.strat.robot)
def setUp(self): self.v2 = Vecteur(1,0,0) self.p0 = Point(1,1,0.5) self.strat = DroitVersBaliseVision(RobotMotorise(forme=Pave(1,1,1,self.p0.clone()), direction=self.v2.clone()), AreneFermee(5,5,5)) self.target = RobotTarget(forme=Pave(1,1,1, self.p0.clone()+self.v2*3), direction=self.v2.clone()) self.strat.arene.add(self.target)
class TestRobotMotorise(unittest.TestCase): def setUp(self): self.r = RobotMotorise(pave=Pave(1, 1, 0, centre=Point(5, 5, 0)), direction=Vecteur(1, 0, 0)) def test_mecanism(self): """ Vérifie que le robot avance correctement """ self.r.set_wheels_rotation(1, 30) self.r.set_wheels_rotation(2, 30) self.p0 = self.r.centre.clone() self.n = int(1 / PAS_TEMPS) * 2 print("Testing distance travelled...") for i in range(1, self.n): self.r.update() t_tot = self.n * PAS_TEMPS print("Time passed: ", t_tot, " s") d_th = (self.p0 - self.r.centre).to_vect().get_mag() d_calc = self.r.get_wheels_angles(1) * self.r.rd.diametre * (pi / 180) print("Error distance: ", abs(d_th - d_calc), " m") print("Done") self.assertAlmostEqual(d_th, d_calc) def test_json(self): """ Vérifie le format json du robot """ print("Testing json format...") r = RobotMotorise() r.save("robotmotorise.json") r2 = RobotMotorise.load("robotmotorise.json") self.assertIsInstance(r2, RobotMotorise) print("Done") def test_tete(self): """ Vérifie la liaison entre la tête et le robot """ print("Testing movement dependencies...") d = (random.random() * 0.5 + 0.5) * random.randint(5, 10) angle = random.random() * 2 * pi self.assertEqual(self.r.centre, self.r.tete.centre) self.r.move(Vecteur(1, 0, 0) * d) self.r.rotate_all_around(self.r.centre, pi / 4) self.r.tete.dir_rel = Vecteur(1, 0, 0) self.r.update() self.assertEqual(self.r.centre, self.r.tete.centre) self.assertEqual(self.r.direction, self.r.tete.direction) print("Done")
def setUp(self): self.angle = rm.random() * rm.choice([-1, 1]) * pi self.strat = Tourner(robot=RobotMotorise(Pave(1,1,1,Point(2,2,0))), angle_max=(self.angle * 180 / pi)) self.arene = Arene() self.arene.add(self.strat.robot)
return DroitVersBalise(**dct) @staticmethod def load(filename): """ Permet de charger un objet DroitVersbalise depuis un fichier au format json adapté :param filename: Nom du fichier """ with open(filename, 'r', encoding='utf-8') as f: return json.load(f, object_hook=DroitVersBalise.hook) class DroitVersBaliseVision(DroitVersBalise, StrategieVision): def __init__(self, robot, arene): DroitVersBalise.__init__(self, robot=robot, arene=arene) StrategieVision.__init__(self, robot=robot, arene=arene) def update(self): DroitVersBalise.update(self) if __name__=='__main__': st = DroitVersBalise(robot=RobotMotorise(), arene=Arene()) st.save("droitVersBalise.json") st2 = DroitVersBalise.load("droitVersBalise.json") print(st2.clone())
pass @staticmethod def hook(dct): res = StrategieVision.hook(dct) if res is not None: return res elif dct["__class__"] == TournerVersBalise.__name__: return TournerVersBalise(**dct) @staticmethod def load(filename): """ Permet de charger un objet TournerVersBalise depuis un fichier au format json adapté :param filename: Nom du fichier """ with open(filename, 'r', encoding='utf-8') as f: return json.load(f, object_hook=TournerVersBalise.hook) if __name__ == '__main__': from gl_lib.sim.robot import RobotMotorise from gl_lib.sim.geometry import Arene, signe st = TournerVersBalise(robot=RobotMotorise(), arene=Arene()) st.save("TournerVersBalise.json") st2 = TournerVersBalise.load("TournerVersBalise.json") print(st2.clone())
def setUp(self): self.robot = RobotMotorise()
:param filename: Nom du fichier """ with open(filename, 'r', encoding='utf-8') as f: return json.load(f, object_hook=DeplacementCercle.hook) if __name__ == '__main__': from gl_lib.sim import Simulation from gl_lib.sim.display.d2.gui import AppAreneThread from gl_lib.sim.robot import * from gl_lib.sim.robot.sensor import Accelerometre from gl_lib.sim.geometry import * a = AreneRobot() r = RobotMotorise( forme=Pave(centre=Point(3.7, 6.7, 0), width=1, height=1, length=1)) r.tete.sensors["cam"].arene = AreneFermee(3, 3, 3) td = Thread(target=r.tete.sensors["cam"].run) sim = Simulation( strategies=[DeplacementCercle(robot=r, angle_max=360, diametre=1)]) a.add(r) app = AppAreneThread(a) app.start() min_x, min_y = sim.strategie.robot.centre.x, sim.strategie.robot.centre.y max_x, max_y = sim.strategie.robot.centre.x, sim.strategie.robot.centre.y dps_wheels = sim.strategie.robot.get_wheels_rotations(3) # td.start() sim.start() # print(sim.stop, sim.strategie.stop()) while not sim.stop: sleep(1)
def setUp(self): self.r = RobotMotorise(pave=Pave(1, 1, 0, centre=Point(5, 5, 0)), direction=Vecteur(1, 0, 0))
self.robot.tete.sensors["cam"].picture() self.robot.tete.sensors["cam"].print_picture(filename) @staticmethod def hook(dct): res = AreneRobot.hook(dct) if res is not None: return res elif dct["__class__"] == StrategieVision.__name__: return StrategieVision(dct["robot"], dct["arene"]) @staticmethod def load(filename): """ Permet de charger un objet StrategieVision depuis un fichier au format json adapté :param filename: Nom du fichier """ with open(filename, 'r', encoding='utf-8') as f: return json.load(f, object_hook=StrategieVision.hook) if __name__ == '__main__': st = StrategieVision(RobotMotorise(), AreneRobot()) st.save("strategie_vision.json") print(st) st2 = StrategieVision.load("strategie_vision.json")
def __dict__(self): dct = OrderedDict() dct["__class__"] = Strategie.__name__ dct["robot"] = self.robot.__dict__() return dct @staticmethod def hook(dct): res = RobotMotorise.hook(dct) if res is not None: return res elif dct["__class__"] == RobotTarget.__name__: return RobotTarget.hook(dct) elif dct["__class__"] == Strategie.__name__: return Strategie(dct["robot"]) @staticmethod def load(filename): """ Permet de charger une Strategie à partir d'un fichier json """ with open(filename, 'r', encoding='utf-8') as f: return json.load(f, object_hook=Strategie.hook) if __name__ == '__main__': s = Strategie(RobotMotorise()) s.save("strategie.json") s2 = Strategie.load("strategie.json") print(s2)
def __init__(self, arene): Thread.__init__(self) self.arene = arene self.window = None def run(self): self.window = AppArene(self.arene) self.window.mainloop() def stop(self): self.window.quit() if __name__ == '__main__': from gl_lib.sim.robot.strategy.deplacement import DeplacementDroitAmeliore from gl_lib.sim.robot import RobotMotorise from gl_lib.sim import Simulation from gl_lib.sim.geometry import Vecteur a = Arene() strat = DeplacementDroitAmeliore( robot=RobotMotorise(direction=Vecteur(1, 1, 0).norm()), distance_max=1, arene=a) a.add(strat.robot) sim = Simulation(strategies=[strat]) app = AppAreneThread(strat.arene) app.start() sim.start()
def test_json(self): dda = DeplacementDroit(robot=RobotMotorise()) d = dda.__dict__() dda.save("deplacement_droit.json") dda2 = DeplacementDroit.load("deplacement_droit.json")
print(s) self.cpt += 1 self.stop = b if self.tmax is not None: if self.cpt > self.tmax: self.stop = True break sleep(dt) # On sort de la simulation self.time_end = self.cpt * PAS_TEMPS print("End simalution ({} s)".format(self.time_end)) if self.final_actions is not None and len(self.final_actions) > 0: print("Closing...") # Exécute les actions de fin for f in self.final_actions: f() if __name__ == '__main__': from gl_lib.sim.robot import RobotMotorise strat = DeplacementCercle(robot=RobotMotorise()) sim = Simulation(strategies=[strat], final_actions=[strat.update]) sim.save("simulation.json") sim2 = Simulation.load("simulation.json") print(repr(sim2.clone()))