Current code:The output:
Code:
#motion.pyimport tkinter as tkimport mathfrom enum import Enumimport timecommands = ["STOP", "FRWD", "BKWD", "LEFT", "RIHT"]class Commands(Enum): STOP = 0 FRWD = 1 BKWD = 2 LEFT = 3 RIHT = 4class MotionController: def __init__(self): self.width, self.height = 1000, 1000 self.angle_line_startx = self.width / 2 self.angle_line_starty = self.height / 2 self.angle = 0 self.mileage = 0 self.line_length = 20 self.x_factor = self.width / 2 self.y_factor = self.height / 2 self.size_factor = 0.2 self.turn = False self.cmd_pick = 0 self.polygon_points = [] self.root = tk.Tk() self.root.title("Map") self.canvas = tk.Canvas(self.root, width=self.width, height=self.height, bg="black") self.canvas.pack() def arr_pos(self, x, y): start_time = time.time() image_width = 640 * self.size_factor image_height = 480 * self.size_factor x_rot_offset = self.x_factor - image_width / 2 y_rot_offset = self.y_factor - image_height / 2 angle_in_rad_rot = (self.angle + 90) * math.pi / 180 cos_theta = math.cos(angle_in_rad_rot) sin_theta = math.sin(angle_in_rad_rot) x_rot = self.size_factor * (x - self.x_factor) * cos_theta - self.size_factor * (y - self.y_factor) * sin_theta + x_rot_offset y_rot = self.size_factor * (x - self.x_factor) * sin_theta + self.size_factor * (y - self.y_factor) * cos_theta + y_rot_offset angle_in_rad_transform = self.angle * math.pi / 180 cos_fi = math.cos(angle_in_rad_transform) sin_fi = math.sin(angle_in_rad_transform) x_transform_offset = self.mileage * self.size_factor * cos_fi y_transform_offset = self.mileage * self.size_factor * sin_fi x_transformed = x_rot + x_transform_offset y_transformed = y_rot + y_transform_offset end_time = time.time() print(f"arr_pos: {end_time - start_time} ms") return x_transformed, y_transformed def gen_map(self, surrounding, distance, angle, mileage): start_time = time.time() self.angle = angle self.mileage = mileage angle_in_rad = angle * math.pi / 180 cos_theta = math.cos(angle_in_rad) sin_theta = math.sin(angle_in_rad) connection_points = [(0, 0),(0, 0)] angle_line_endx = self.angle_line_startx + self.line_length * cos_theta angle_line_endy = self.angle_line_starty + self.line_length * sin_theta self.canvas.delete("angle_line") self.canvas.create_line(self.angle_line_startx, self.angle_line_starty, angle_line_endx, angle_line_endy, fill="red", tags="angle_line") if mileage % 500 == 0: #just random val to see if mapping works self.turn == True if self.turn == True: x_start_rot, y_start_rot = self.arr_pos(surrounding[0][0], surrounding[0][1]) x_end_rot, y_end_rot = self.arr_pos(surrounding[len(surrounding)-2][0], surrounding[len(surrounding)-2][1]) self.x_factor = (x_start_rot + x_end_rot) / 2 self.y_factor = (y_start_rot + y_end_rot) / 2 self.turn = False self.polygon_points = [self.arr_pos(x, y) for x, y in surrounding] self.canvas.create_polygon(self.polygon_points, outline="white", fill="white", tags="map_polygon") if distance < 30: self.cmd_pick = Commands.STOP.value else: if(angle >= angle + 90): self.cmd_pick = Commands.RIHT.value else: self.cmd_pick = Commands.FRWD.value direction = commands[self.cmd_pick] self.root.update() end_time = time.time() print(f"gen_map: {end_time - start_time} ms") return direction
Code:
gen_map: 1.9618196487426758 msarr_pos: 4.291534423828125e-06 ms...arr_pos: 4.291534423828125e-06 msarr_pos: 4.291534423828125e-06 msarr_pos: 4.5299530029296875e-06 msarr_pos: 4.291534423828125e-06 msgen_map: 2.0169084072113037 ms
Statistics: Posted by mart1nek — Thu Feb 01, 2024 6:11 pm