|
| 1 | +# |
| 2 | +# (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved. |
| 3 | +# |
| 4 | +# RTI grants Licensee a license to use, modify, compile, and create derivative |
| 5 | +# works of the Software solely for use with RTI products. The Software is |
| 6 | +# provided "as is", with no warranty of any type, including any warranty for |
| 7 | +# fitness for any purpose. RTI is under no obligation to maintain or support |
| 8 | +# the Software. RTI shall not be liable for any incidental or consequential |
| 9 | +# damages arising out of the use or inability to use the software. |
| 10 | +# |
| 11 | + |
| 12 | +import random |
| 13 | +import time |
| 14 | +import typing |
| 15 | + |
| 16 | +import rti.connextdds as dds |
| 17 | + |
| 18 | +from VehicleModeling import Coord, VehicleMetrics, VehicleTransit |
| 19 | + |
| 20 | + |
| 21 | +def new_route( |
| 22 | + n: int = 5, |
| 23 | + start: typing.Optional[Coord] = None, |
| 24 | + end: typing.Optional[Coord] = None, |
| 25 | +): |
| 26 | + def new_random_coord(): |
| 27 | + return Coord( |
| 28 | + (0.5 - random.random()) * 100, |
| 29 | + (0.5 - random.random()) * 100, |
| 30 | + ) |
| 31 | + |
| 32 | + start = start or new_random_coord() |
| 33 | + intermediate = (new_random_coord() for _ in range(n)) |
| 34 | + end = end or new_random_coord() |
| 35 | + |
| 36 | + return [start, *intermediate, end] |
| 37 | + |
| 38 | + |
| 39 | +class PublisherSimulation: |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + metrics_writer: "dds.DataWriter", |
| 43 | + transit_writer: "dds.DataWriter", |
| 44 | + ): |
| 45 | + self._metrics_writer = metrics_writer |
| 46 | + self._transit_writer = transit_writer |
| 47 | + self._vehicle_vin: str = "".join( |
| 48 | + random.choices("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", k=17) |
| 49 | + ) |
| 50 | + self._vehicle_fuel = 100.0 |
| 51 | + self._vehicle_route = new_route() |
| 52 | + self._vehicle_position = self._vehicle_route.pop(0) |
| 53 | + |
| 54 | + def __repr__(self): |
| 55 | + return ( |
| 56 | + f"Simulation(" |
| 57 | + f"{self._metrics_writer=}, " |
| 58 | + f"{self._transit_writer=}, " |
| 59 | + f"{self._vehicle_vin=}, " |
| 60 | + f"{self._vehicle_fuel=}, " |
| 61 | + f"{self._vehicle_route=}, " |
| 62 | + f"{self._vehicle_position=})" |
| 63 | + ) |
| 64 | + |
| 65 | + @property |
| 66 | + def has_ended(self): |
| 67 | + return self._is_out_of_fuel |
| 68 | + |
| 69 | + @property |
| 70 | + def _is_out_of_fuel(self): |
| 71 | + return self._vehicle_fuel <= 0.0 |
| 72 | + |
| 73 | + @property |
| 74 | + def _is_on_standby(self): |
| 75 | + return not self._vehicle_route |
| 76 | + |
| 77 | + def run(self): |
| 78 | + while not self.has_ended: |
| 79 | + self._metrics_writer.write( |
| 80 | + VehicleMetrics( |
| 81 | + self._vehicle_vin, |
| 82 | + self._vehicle_fuel, |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + self._transit_writer.write( |
| 87 | + VehicleTransit( |
| 88 | + self._vehicle_vin, |
| 89 | + current_route=self._vehicle_route, |
| 90 | + current_position=self._vehicle_position, |
| 91 | + ) |
| 92 | + ) |
| 93 | + |
| 94 | + time.sleep(1) |
| 95 | + |
| 96 | + if self._is_on_standby: |
| 97 | + print( |
| 98 | + f"Vehicle '{self._vehicle_vin}' has reached its destination, now moving to a new location..." |
| 99 | + ) |
| 100 | + self._vehicle_route = new_route(start=self._vehicle_position) |
| 101 | + |
| 102 | + self._vehicle_position = self._vehicle_route.pop(0) |
| 103 | + self._vehicle_fuel -= 10 * random.random() |
| 104 | + |
| 105 | + if self._is_out_of_fuel: |
| 106 | + self._vehicle_fuel = 0.0 |
| 107 | + |
| 108 | + print(f"Vehicle '{self._vehicle_vin}' ran out of fuel!") |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + dds.DomainParticipant.register_idl_type(VehicleMetrics, "VehicleMetrics") |
| 113 | + dds.DomainParticipant.register_idl_type(VehicleTransit, "VehicleTransit") |
| 114 | + |
| 115 | + qos_provider = dds.QosProvider("../VehicleModeling.xml") |
| 116 | + |
| 117 | + with qos_provider.create_participant_from_config( |
| 118 | + "ParticipantLibrary::PublisherApp" |
| 119 | + ) as participant: |
| 120 | + metrics_writer = dds.DataWriter( |
| 121 | + participant.find_datawriter("Publisher::MetricsWriter") |
| 122 | + ) |
| 123 | + transit_writer = dds.DataWriter( |
| 124 | + participant.find_datawriter("Publisher::TransitWriter") |
| 125 | + ) |
| 126 | + |
| 127 | + simulation = PublisherSimulation( |
| 128 | + metrics_writer=metrics_writer, transit_writer=transit_writer |
| 129 | + ) |
| 130 | + print(f"Running simulation: {simulation=}") |
| 131 | + simulation.run() |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + try: |
| 136 | + main() |
| 137 | + except KeyboardInterrupt: |
| 138 | + pass |
0 commit comments