|
| 1 | +from io import TextIOWrapper |
| 2 | +from time import sleep |
| 3 | +from math import tan, atan, degrees |
| 4 | +from re import compile |
| 5 | + |
| 6 | +from click_extra import echo |
| 7 | +from geocompy.data import Angle, Coordinate |
| 8 | +from geocompy.geo import GeoCom |
| 9 | +from geocompy.communication import open_serial |
| 10 | + |
| 11 | +from ..calculations import adjust_uniform_single |
| 12 | +from ..utils import echo_green |
| 13 | + |
| 14 | + |
| 15 | +_LINE = compile(r"^\d+(?:\.\d+)?(?:,\-?\d+\.\d+){2}$") |
| 16 | + |
| 17 | + |
| 18 | +def run_measure( |
| 19 | + tps: GeoCom, |
| 20 | + output: TextIOWrapper | None = None, |
| 21 | + positions: int = 1, |
| 22 | + zero: bool = False, |
| 23 | + cycles: int = 1 |
| 24 | +) -> None: |
| 25 | + turn = 360 // positions |
| 26 | + v = Angle(90, 'deg') |
| 27 | + start = 0 |
| 28 | + |
| 29 | + if not zero: |
| 30 | + angles = tps.tmc.get_angle() |
| 31 | + if angles.params is not None: |
| 32 | + start = round(angles.params[0].asunit('deg')) |
| 33 | + |
| 34 | + echo( |
| 35 | + "hz_deg,cross_sec,length_sec", |
| 36 | + output |
| 37 | + ) |
| 38 | + |
| 39 | + for a in range(start, start + cycles * 360, turn): |
| 40 | + hz = Angle(a, 'deg').normalized() |
| 41 | + tps.aut.turn_to(hz, v) |
| 42 | + |
| 43 | + sleep(1) |
| 44 | + fullangles = tps.tmc.get_angle_inclination('MEASURE') |
| 45 | + if fullangles.params is None: |
| 46 | + continue |
| 47 | + |
| 48 | + az = fullangles.params[0] |
| 49 | + cross = fullangles.params[4] |
| 50 | + length = fullangles.params[5] |
| 51 | + |
| 52 | + echo( |
| 53 | + f"{az.asunit('deg'):.4f}," |
| 54 | + f"{cross.asunit('deg') * 3600:.2f}," |
| 55 | + f"{length.asunit('deg') * 3600:.2f}", |
| 56 | + output |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def main_measure( |
| 61 | + port: str, |
| 62 | + baud: int = 9600, |
| 63 | + timeout: int = 15, |
| 64 | + retry: int = 1, |
| 65 | + sync_after_timeout: bool = False, |
| 66 | + output: TextIOWrapper | None = None, |
| 67 | + positions: int = 1, |
| 68 | + zero: bool = False, |
| 69 | + cycles: int = 1 |
| 70 | +) -> None: |
| 71 | + with open_serial( |
| 72 | + port, |
| 73 | + retry=retry, |
| 74 | + sync_after_timeout=sync_after_timeout, |
| 75 | + speed=baud, |
| 76 | + timeout=timeout |
| 77 | + ) as com: |
| 78 | + tps = GeoCom(com) |
| 79 | + run_measure(tps, output, positions, zero, cycles) |
| 80 | + |
| 81 | + |
| 82 | +def main_merge( |
| 83 | + inputs: list[TextIOWrapper], |
| 84 | + output: TextIOWrapper |
| 85 | +) -> None: |
| 86 | + echo("hz_deg,cross_sec,length_sec", output) |
| 87 | + for item in inputs: |
| 88 | + for line in item: |
| 89 | + if not _LINE.match(line.strip()): |
| 90 | + continue |
| 91 | + |
| 92 | + echo(line, output, False) |
| 93 | + |
| 94 | + echo_green(f"Merged measurements from {len(inputs)} files.") |
| 95 | + |
| 96 | + |
| 97 | +def main_calc( |
| 98 | + input: TextIOWrapper, |
| 99 | + output: TextIOWrapper | None = None |
| 100 | +) -> None: |
| 101 | + points: list[Coordinate] = [] |
| 102 | + |
| 103 | + for line in input: |
| 104 | + if not _LINE.match(line.strip()): |
| 105 | + continue |
| 106 | + |
| 107 | + fields = line.strip().split(",") |
| 108 | + azimut = Angle(float(fields[0]), 'deg') |
| 109 | + cross = Angle(float(fields[1]) / 3600, 'deg') |
| 110 | + length = Angle(float(fields[2]) / 3600, 'deg') |
| 111 | + |
| 112 | + coord = Coordinate(tan(cross), tan(length), 1) |
| 113 | + bearing, inclination, s = coord.to_polar() |
| 114 | + |
| 115 | + points.append( |
| 116 | + Coordinate.from_polar( |
| 117 | + (bearing + azimut).normalized(), |
| 118 | + inclination, |
| 119 | + s |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + x, x_dev = adjust_uniform_single([p.x for p in points]) |
| 124 | + y, y_dev = adjust_uniform_single([p.y for p in points]) |
| 125 | + |
| 126 | + inc_x = degrees(atan(x)) * 3600 |
| 127 | + inc_y = degrees(atan(y)) * 3600 |
| 128 | + inc_x_dev = degrees(atan(x_dev)) * 3600 |
| 129 | + inc_y_dev = degrees(atan(y_dev)) * 3600 |
| 130 | + |
| 131 | + direction, inc, _ = Coordinate(x, y, 1).to_polar() |
| 132 | + |
| 133 | + if output is None: |
| 134 | + echo(f"""Axis aligned: |
| 135 | + inclination X: {inc_x:.1f}" +/- {inc_x_dev:.1f}" |
| 136 | + inclination Y: {inc_y:.1f}" +/- {inc_y_dev:.1f}" |
| 137 | +Polar: |
| 138 | + direction: {direction.asunit('deg'):.4f}° |
| 139 | + inclination: {inc.asunit('deg') * 3600:.1f}\"""" |
| 140 | + ) |
| 141 | + return |
| 142 | + |
| 143 | + echo( |
| 144 | + "inc_x_sec,inc_x_dev_sec,inc_y_sec,inc_y_dev_sec,dir_deg,inc_sec", |
| 145 | + output |
| 146 | + ) |
| 147 | + |
| 148 | + echo( |
| 149 | + ( |
| 150 | + f"{inc_x:.1f},{inc_x_dev:.1f},{inc_y:.1f},{inc_y_dev:.1f}," |
| 151 | + f"{direction.asunit('deg'):.4f},{inc.asunit('deg') * 3600:.1f}" |
| 152 | + ), |
| 153 | + output |
| 154 | + ) |
0 commit comments