From a8efe38aaf3916aebd956d73831b95292a9afa23 Mon Sep 17 00:00:00 2001 From: moscowsky Date: Thu, 25 Jun 2026 14:57:13 +0300 Subject: [PATCH] dump some params --- robot_controller.py | 78 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/robot_controller.py b/robot_controller.py index c2763ba..24a79cf 100644 --- a/robot_controller.py +++ b/robot_controller.py @@ -1,9 +1,12 @@ import copy import time +import yaml +from datetime import datetime + class RobotController(object): - def __init__(self, fire_ext_max_capacity = 5, collision_damage = 1, k_sof = 1, k_cas = 1, k_asset = 1, k_hp = 1, fall_damage = 10): + def __init__(self, fire_ext_max_capacity = 5, collision_damage = 1, k_sof = 1, k_cas = 1, k_asset = 1, k_hp = 1, fall_damage = 10, k_charge = 1): # STATE VARIABLES self._fire_ext_max_capacity = fire_ext_max_capacity @@ -12,9 +15,12 @@ class RobotController(object): self._start_hp = 100 self._hit_points = self._start_hp self._collision_damage = collision_damage - self._charge_points = 100 + self._start_charge = 100 + self._charge_points = self._start_charge self._fall_damage = fall_damage + self._start_time = None + self._collisions = [] self._exted_sofs = [] self._found_casualty = [] @@ -25,17 +31,29 @@ class RobotController(object): self._k_cas = k_cas self._k_asset = k_asset self._k_hp = k_hp + self._k_charge = k_charge + + def _check_time_init(self): + if not self._start_time is None: + return + self._start_time = time.time() + def _ts(self): + return time.time() - self._start_time ''' CONTROL ''' # speeds in [-1, 1] extra stuff will be cut def send_speed_cmd(self, v, w): + self._check_time_init() + if self._charge_points == 0: + return 0, 0 v = min(max(-1, v), 1) w = min(max(-1, w), 1) return v, w def send_fire_ext_burst_cmd(self): + self._check_time_init() if self._fire_ext_capacity > 0: self._fire_ext_capacity -= 1 return True @@ -43,47 +61,75 @@ class RobotController(object): # poses in [-1, 1] extra stuff will be cut def send_fire_ext_pose_cmd(self, horisontal_pose, vertical_pose): + self._check_time_init() horisontal_pose = min(max(-1, horisontal_pose), 1) vertical_pose = min(max(-1, vertical_pose), 1) return horisontal_pose, vertical_pose def send_pick_asset_cmd(self): + self._check_time_init() raise NotImplemented("") def get_score(self, time_stamp = None): - score = self._k_sof * len(self._exted_sofs) + self._k_cas * len(self._found_casualty) + self._k_asset * len(self._found_assets) + self._k_hp * self._hit_points + score = self._k_sof * len(self._exted_sofs) + self._k_cas * len(self._found_casualty) + self._k_asset * len(self._found_assets) + self._k_hp * (self._hit_points/self._start_hp) + self._k_charge * (self._charge_points / self._start_charge) return score def get_str_status(self): - status = f"{type(self).__name__}\n - Total score {self.get_score()}\n - Hit points: {self._hit_points}/{self._start_hp}\n - Extingushed sources of fire {len(self._exted_sofs)}\n - Fire extinguiher capacity {self._fire_ext_capacity}/{self._fire_ext_max_capacity}\n - Collisions {len(self._collisions)}\n - Falls {len(self._falls)}" + status = f"{type(self).__name__}\n - Total score {self.get_score()}\n - Hit points: {self._hit_points}/{self._start_hp}\n - Charge points {round(self._charge_points, 2)}\{self._start_charge}\n - Extingushed sources of fire {len(self._exted_sofs)}\n - Fire extinguiher capacity {self._fire_ext_capacity}/{self._fire_ext_max_capacity}\n - Collisions {len(self._collisions)}\n - Falls {len(self._falls)}" return status def send_lights_cmd(self): - pass + self._check_time_init() + def send_fall_reset_cmd(self): - pass + self._check_time_init() + + + def dump_stats(self, save_path = '/tmp'): + date = datetime.now().strftime("%d_%H:%M:%S") + stats = {"total_time": float(self._ts()), + "date": date, + "score": float(self.get_score()), + "status": self.get_str_status(), + "class": type(self).__name__, + "k_params": {k: v for k, v in vars(self).items() if k.startswith('_k_') and not callable(v)}, + "exted_sofs": self._exted_sofs, + "collisions": self._collisions, + "falls": self._falls + } + + path = save_path + f"/sirius_{date}.yaml" + with open(path, "w") as file: + yaml.dump(stats, file, default_flow_style=False, sort_keys=False) + return path + return "" ''' EVENTS ''' - def _register_collision(self, time_stamp): - self._collisions.append(time_stamp) + def _register_collision(self): + self._collisions.append(self._ts()) self._hit_points = max(0, self._hit_points - self._collision_damage) - def _register_exted_sof(self, time_stamp, sof_params = {}): - self._exted_sofs.append((time_stamp, + def _register_exted_sof(self, sof_params = {}): + self._exted_sofs.append((self._ts(), copy.deepcopy(sof_params))) - def _register_found_casualty(self, time_stamp, casualty_params = {}): - self._found_casualty.append((time_stamp, + def _register_found_casualty(self, casualty_params = {}): + self._found_casualty.append((self._ts(), copy.deepcopy(casualty_params))) - def _register_found_assest(self, time_stamp, asset_params = {}): - self._found_assets.append((time_stamp, + def _register_found_assest(self, asset_params = {}): + self._found_assets.append((self._ts(), copy.deepcopy(asset_params))) - def _register_fall(self, time_stamp): - self._falls.append(time_stamp) + def _register_fall(self): + self._falls.append(self._ts()) + + def _decrease_charge(self, speed, dt): + new_value = self._charge_points - speed * dt + self._charge_points = max(0, new_value) +