71 lines
2.2 KiB
Python
71 lines
2.2 KiB
Python
"""Alle gemessenen Koordinaten der Quelle und der Sonne haben den Ursprung in der linken unteren Ecke des Clusters in einem rechtshaendigen flachen System."""
|
|
|
|
from objects.generic import Source, Target
|
|
|
|
import math
|
|
import objects.motor as motor
|
|
import numpy as np
|
|
from objects.calculator import get_angles
|
|
|
|
|
|
class Mirror:
|
|
def __init__(self, world, cluster_x=0, cluster_y=0):
|
|
self.world: World = world
|
|
self.cluster_x = cluster_x
|
|
self.cluster_y = cluster_y
|
|
|
|
# Store the motors
|
|
self.theta = motor.Motor(self.world.board)
|
|
self.phi = motor.Motor(self.world.board)
|
|
|
|
# Position in un-tilted coordinate system
|
|
self.pos = np.array(
|
|
[cluster_x * self.world.grid_size, cluster_y * self.world.grid_size, 0.0]
|
|
)
|
|
|
|
def get_pos_rotated(self):
|
|
return self.world.rotate_point_y(self.pos)
|
|
|
|
def set_angle_from_source_target(self, source: Source, target: Target):
|
|
"Set the angles of a mirror from global source and target vectors."
|
|
|
|
rot_pos = self.get_pos_rotated()
|
|
rel_source = source.pos - rot_pos
|
|
rel_target = target.pos - rot_pos
|
|
|
|
phi, theta = get_angles(rel_source, rel_target)
|
|
|
|
# Update the angles based on the normals in rotated positions
|
|
self.phi.set_angle(phi)
|
|
self.theta.set_angle(theta)
|
|
|
|
def get_angles(self):
|
|
return self.phi.angle, self.theta.angle
|
|
|
|
|
|
class World:
|
|
def __init__(self, board, tilt_deg=0.0):
|
|
self.board = board
|
|
|
|
self.grid_size = 10 # In cm
|
|
self.tilt_deg = tilt_deg # Tilt of the grid system around y-axis
|
|
self.mirrors: list[Mirror] = []
|
|
|
|
def add_mirror(self, mirror):
|
|
self.mirrors.append(mirror)
|
|
|
|
def update_mirrors_from_source_target(self, source: Source, target: Target):
|
|
for mirror in self.mirrors:
|
|
mirror.set_angle_from_source_target(source, target)
|
|
|
|
def rotate_point_y(self, point):
|
|
"""Rotate a point around the y-axis by the world's tilt angle."""
|
|
x, y, z = point
|
|
theta = math.radians(self.tilt_deg)
|
|
cos_t = math.cos(theta)
|
|
sin_t = math.sin(theta)
|
|
x_rot = x * cos_t + z * sin_t
|
|
y_rot = y
|
|
z_rot = -x * sin_t + z * cos_t
|
|
return np.array([x_rot, y_rot, z_rot])
|