|
| 1 | +""" |
| 2 | +PySquared Ground Station |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +import time |
| 7 | + |
| 8 | +import supervisor |
| 9 | +from pysquared.cdh import CommandDataHandler |
| 10 | +from pysquared.config.config import Config |
| 11 | +from pysquared.hardware.radio.packetizer.packet_manager import PacketManager |
| 12 | +from pysquared.logger import Logger |
| 13 | + |
| 14 | + |
| 15 | +class GroundStation: |
| 16 | + """Ground Station class to manage communication with the satellite.""" |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + logger: Logger, |
| 21 | + config: Config, |
| 22 | + packet_manager: PacketManager, |
| 23 | + cdh: CommandDataHandler, |
| 24 | + ): |
| 25 | + self._log = logger |
| 26 | + self._log.colorized = True |
| 27 | + self._config = config |
| 28 | + self._packet_manager = packet_manager |
| 29 | + self._cdh = cdh |
| 30 | + |
| 31 | + def listen(self): |
| 32 | + """Listen for incoming packets from the satellite.""" |
| 33 | + |
| 34 | + try: |
| 35 | + while True: |
| 36 | + if supervisor.runtime.serial_bytes_available: |
| 37 | + typed = input().strip() |
| 38 | + if typed: |
| 39 | + self.handle_input(typed) |
| 40 | + |
| 41 | + b = self._packet_manager.listen(1) |
| 42 | + if b is not None: |
| 43 | + self._log.info( |
| 44 | + message="Received response", response=b.decode("utf-8") |
| 45 | + ) |
| 46 | + |
| 47 | + except KeyboardInterrupt: |
| 48 | + self._log.debug("Keyboard interrupt received, exiting listen mode.") |
| 49 | + |
| 50 | + def send_receive(self): |
| 51 | + """Send commands to the satellite and wait for responses.""" |
| 52 | + |
| 53 | + try: |
| 54 | + cmd_selection = input( |
| 55 | + """ |
| 56 | + =============================== |
| 57 | + | Select command to send | |
| 58 | + | 1: Reset | |
| 59 | + | 2: Change radio modulation | |
| 60 | + | 3: Send joke | |
| 61 | + =============================== |
| 62 | + """ |
| 63 | + ) |
| 64 | + |
| 65 | + self.handle_input(cmd_selection) |
| 66 | + |
| 67 | + except KeyboardInterrupt: |
| 68 | + self._log.debug("Keyboard interrupt received, exiting send mode.") |
| 69 | + |
| 70 | + def handle_input(self, cmd_selection): |
| 71 | + """ |
| 72 | + Handle user input commands. |
| 73 | +
|
| 74 | + Args: |
| 75 | + cmd_selection: The command selection input by the user. |
| 76 | + """ |
| 77 | + if cmd_selection not in ["1", "2", "3"]: |
| 78 | + self._log.warning("Invalid command selection. Please try again.") |
| 79 | + return |
| 80 | + |
| 81 | + message: dict[str, object] = { |
| 82 | + "name": self._config.cubesat_name, |
| 83 | + "password": self._config.super_secret_code, |
| 84 | + } |
| 85 | + |
| 86 | + if cmd_selection == "1": |
| 87 | + message["command"] = self._cdh.command_reset |
| 88 | + elif cmd_selection == "2": |
| 89 | + message["command"] = self._cdh.command_change_radio_modulation |
| 90 | + modulation = input("Enter new radio modulation [FSK | LoRa]: ") |
| 91 | + message["args"] = [modulation] |
| 92 | + elif cmd_selection == "3": |
| 93 | + message["command"] = self._cdh.command_send_joke |
| 94 | + |
| 95 | + while True: |
| 96 | + # Turn on the radio so that it captures any received packets to buffer |
| 97 | + self._packet_manager.listen(1) |
| 98 | + |
| 99 | + # Send the message |
| 100 | + self._log.info( |
| 101 | + "Sending command", |
| 102 | + cmd=message["command"], |
| 103 | + args=message.get("args", []), |
| 104 | + ) |
| 105 | + self._packet_manager.send(json.dumps(message).encode("utf-8")) |
| 106 | + |
| 107 | + # Listen for ACK response |
| 108 | + b = self._packet_manager.listen(1) |
| 109 | + if b is None: |
| 110 | + self._log.info("No response received, retrying...") |
| 111 | + continue |
| 112 | + |
| 113 | + if b != b"ACK": |
| 114 | + self._log.info( |
| 115 | + "No ACK response received, retrying...", |
| 116 | + response=b.decode("utf-8"), |
| 117 | + ) |
| 118 | + continue |
| 119 | + |
| 120 | + self._log.info("Received ACK") |
| 121 | + |
| 122 | + # Now listen for the actual response |
| 123 | + b = self._packet_manager.listen(1) |
| 124 | + if b is None: |
| 125 | + self._log.info("No response received, retrying...") |
| 126 | + continue |
| 127 | + |
| 128 | + self._log.info("Received response", response=b.decode("utf-8")) |
| 129 | + break |
| 130 | + |
| 131 | + def run(self): |
| 132 | + """Run the ground station interface.""" |
| 133 | + while True: |
| 134 | + print( |
| 135 | + """ |
| 136 | + ============================= |
| 137 | + | | |
| 138 | + | WELCOME! | |
| 139 | + | PROVESKIT Ground Station | |
| 140 | + | | |
| 141 | + ============================= |
| 142 | + | Please Select Your Mode | |
| 143 | + | 'A': Listen | |
| 144 | + | 'B': Send | |
| 145 | + ============================= |
| 146 | + """ |
| 147 | + ) |
| 148 | + |
| 149 | + device_selection = input().lower() |
| 150 | + |
| 151 | + if device_selection not in ["a", "b"]: |
| 152 | + self._log.warning("Invalid Selection. Please try again.") |
| 153 | + continue |
| 154 | + |
| 155 | + if device_selection == "a": |
| 156 | + self.listen() |
| 157 | + elif device_selection == "b": |
| 158 | + self.send_receive() |
| 159 | + |
| 160 | + time.sleep(1) |
0 commit comments