@@ -55,7 +55,7 @@ def __init__(self, *args, **kwargs):
5555 bot_id_param_name : str = self .get_parameter ('bot_id_param_name' ).value
5656 # Get the parameters from the blackboard
5757 params = get_parameters_from_other_node (self , param_blackboard_name , [
58- team_id_param_name ,
58+ team_id_param_name ,
5959 bot_id_param_name ])
6060 # Set the parameters
6161 self .team_number = params [team_id_param_name ]
@@ -69,13 +69,13 @@ def __init__(self, *args, **kwargs):
6969 #The publisher for the diagnostics
7070 self .diagnostic_pub = self .create_publisher (DiagnosticArray , "diagnostics" , 1 )
7171
72- # The time in seconds after which we assume the game controller is lost
72+ # The time in seconds after which we assume the game controller is lost
7373 # and we tell the robot to move
7474 self .game_controller_lost_time = 5
7575
7676 # The address listening on and the port for sending back the robots meta data
7777 self .addr = (
78- self .get_parameter ('listen_host' ).value ,
78+ self .get_parameter ('listen_host' ).value ,
7979 self .get_parameter ('listen_port' ).value
8080 )
8181 self .answer_port = self .get_parameter ('answer_port' ).value
@@ -99,18 +99,15 @@ def receive_forever(self):
9999 while rclpy .ok ():
100100 # Try to receive a package
101101 self .receive_and_answer_once ()
102- # Check if we didn't receive a package for a long time and if so
103- # call the fallback behavior
104- if self .get_time_since_last_package () > Duration (seconds = self .game_controller_lost_time ):
105- self .publish_diagnostics (False )
106- else :
107- self .publish_diagnostics (True )
102+ # Check if we didn't receive a package for a long time for publishing diagnostics
103+ received_message_lately = self .get_time_since_last_package () < Duration (seconds = self .game_controller_lost_time )
104+ self .publish_diagnostics (received_message_lately )
108105
109106
110107 def receive_and_answer_once (self ):
111108 """ Receives a package, interprets it and sends an answer. """
112109 try :
113- # Receive the package
110+ # Receive the package
114111 data , peer = self .socket .recvfrom (GameStateStruct .sizeof ())
115112
116113 # Parse the package based on the GameStateStruct
@@ -136,26 +133,25 @@ def receive_and_answer_once(self):
136133 self .get_logger ().warn (f"Error while sending keep-alive: { str (e )} " )
137134
138135 def publish_diagnostics (self , received_message_lately : bool ):
139- """
136+ """
140137 This publishes a Diagnostics Array.
141138 """
142- self .get_logger ().info ("No GameController message received" , throttle_duration_sec = 5 )
143-
144- #initialize DiagnsticArray message
139+ # initialize DiagnsticArray message
145140 diag_array = DiagnosticArray ()
146141
147- #configure DiagnosticStatus message
148- diag = DiagnosticStatus (name = "Game Controller" , hardware_id = "Game Controller" )
142+ # configure DiagnosticStatus message
143+ diag = DiagnosticStatus (name = "Game Controller" , hardware_id = "Game Controller" )
149144 if not received_message_lately :
150- diag .message = "Lost connection to game controller for " + str (int (self .get_time_since_last_package ().nanoseconds / 1e9 )) + " sec"
145+ self .get_logger ().info ("No GameController message received" , throttle_duration_sec = 5 )
146+ diag .message = "Lost connection to game controller for " + str (int (self .get_time_since_last_package ().nanoseconds / 1e9 )) + " sec"
151147 diag .level = DiagnosticStatus .WARN
152148 else :
153149 diag .message = "Connected"
154- diag .level = DiagnosticStatus .OK
150+ diag .level = DiagnosticStatus .OK
151+
152+ diag_array .status .append (diag )
155153
156- diag_array .status .append (diag )
157-
158- #add timestamp to header and publish DiagnosticArray
154+ # add timestamp to header and publish DiagnosticArray
159155 diag_array .header .stamp = self .get_clock ().now ().to_msg ()
160156 self .diagnostic_pub .publish (diag_array )
161157
@@ -176,22 +172,22 @@ def answer_to_gamecontroller(self, peer):
176172
177173 def build_game_state_msg (self , state ) -> GameState :
178174 """ Builds a GameState message from the game state """
179-
175+
180176 # Get the team objects sorted into own and rival team
181177 own_team = GameStateReceiver .select_team_by (
182- lambda team : team .team_number == self .team_number ,
178+ lambda team : team .team_number == self .team_number ,
183179 state .teams )
184180 rival_team = GameStateReceiver .select_team_by (
185- lambda team : team .team_number != self .team_number ,
181+ lambda team : team .team_number != self .team_number ,
186182 state .teams )
187183
188184 # Add some assertions to make sure everything is fine
189185 assert not (own_team is None or rival_team is None ), \
190186 f'Team { self .team_number } not playing, only { state .teams [0 ].team_number } and { state .teams [1 ].team_number } '
191-
187+
192188 assert self .player_number <= len (own_team .players ), \
193189 f'Robot { self .player_number } not playing'
194-
190+
195191 this_robot = own_team .players [self .player_number - 1 ]
196192
197193 return GameState (
0 commit comments