Best Python code snippet using hypothesis
robot_v4.py
Source:robot_v4.py
...53 54 value = helper.determine_team(OWN_ID)55 if helper.determine_team(OWN_ID) == c.VALUE_TYPE_COM:56 print "I'm in com_mode now!"57 helper.set_element(flags, 'robot_type_com', True)58 elif helper.determine_team(OWN_ID) == c.VALUE_TYPE_AUTO:59 print "I'm in auto_mode now!"60 helper.set_element(flags, 'robot_type_com', False)61 local_prev_value = helper.determine_team(OWN_ID)62 # TODO: IDLE-STATE?63 prev_state = state64 state = 'IDLE'65 if state == 'IDLE':66 if prev_state != 'IDLE' or helper.get_element(flags, 'master_set_type'):67 helper.set_element(flags, 'master_set_type', False)68 if helper.get_element(flags, 'robot_type_com'):69 pi2go.setAllLEDs(c.LED_ON, c.LED_OFF, c.LED_ON)70 else: 71 pi2go.setAllLEDs(c.LED_OFF, c.LED_ON, c.LED_ON)72 pi2go.stop()73 if helper.check_time_limit(times, 'prev_get_switch', c.WAIT_SWITCH):74 # Pressed = 1, Released = 075 button = pi2go.getSwitch()76 if not button:77 helper.set_element(flags, 'button_release', True)78 if button and helper.get_element(flags, 'button_release'):79 helper.set_element(flags, 'button_release', False)80 prev_mode = ''81 prev_state = state82 state = 'RUNNING'83 helper.set_element(flags, 'master_set_LED', True)84 helper.set_element(flags, 'set_motor', True)85 # change to sensor-based or master_idle type 86 data = 'new_round'87 while data != '':88 data, addr = com.receive_message(sock) 89 if data != '':90 sender_ID = com.get_id_from_ip(addr[0])91 if sender_ID == OWN_ID:92 # print 'OWN: ' , sender_ID, ' : ' , data93 continue94 if c.TEAM_END >= sender_ID >= c.TEAM_START:95 # print 'ROBOT: ', sender_ID, ' : ' , data96 if data == 'PROBLEM':97 warning[sender_ID-c.TEAM_START] = False98 elif data == 'RELEASE':99 warning[sender_ID-c.TEAM_START] = True100 else:101 command, value = com.string_to_command(data)102 print 'MASTER:', sender_ID, ' : ', data103 try:104 if command == c.COMMAND_SPEED:105 helper.set_element(flags, 'master_set_speed', True)106 prev_SPEED_RUN = SPEED_RUN107 if value == '+':108 SPEED_RUN += 5109 elif value == '-':110 SPEED_RUN -= 5111 else:112 SPEED_RUN = value113 print 'Set SPEED_RUN from ' + str(prev_SPEED_RUN) + ' to ' + str(SPEED_RUN)114 elif command == c.COMMAND_DIST:115 prev_DIST_MIN = DIST_MIN116 DIST_MIN = value117 print 'Set DIST_MIN from ' + str(prev_DIST_MIN) + ' to ' + str(DIST_MIN)118 elif command == c.COMMAND_BLINK:119 helper.blink('white')120 helper.set_element(flags, 'master_set_LED', True)121 elif command == c.COMMAND_RESET: 122 SPEED_RUN = c.SPEED_RUN123 SPEED_WARN = round(float(SPEED_RUN)/3, 0)124 SPEED_CONTROL_MAX = SPEED_RUN125 SPEED_CONTROL_MIN = SPEED_WARN126 DIST_MIN = c.DIST_MIN127 helper.set_element(times, 'prev_get_dist', 0)128 helper.set_element(flags, 'master_set_LED', True)129 helper.set_element(flags, 'master_set_speed', True)130 helper.set_element(flags, 'set_motor', True)131 warning = [True] * len(warning)132 print "Reset major values"133 elif command == c.COMMAND_STATE:134 helper.set_element(flags, 'set_motor', True)135 helper.set_element(flags, 'master_set_LED', True)136 local_prev_state = state137 if value == c.VALUE_STATE_RUNNING:138 state = 'RUNNING'139 elif value == c.VALUE_STATE_IDLE:140 state = 'IDLE'141 print 'Going from state ' + local_prev_state + ' to state ' + state142 elif command == c.COMMAND_TYPE:143 helper.set_element(flags, 'master_set_type', True)144 helper.set_element(flags, 'master_set_LED', True)145 if value == c.VALUE_TYPE_ORIGINAL:146 if helper.determine_team(OWN_ID) == c.VALUE_TYPE_COM:147 helper.set_element(flags, 'robot_type_com', True)148 else:149 helper.set_element(flags, 'robot_type_com', False)150 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS,151 c.WAIT_SEND)152 elif value == c.VALUE_TYPE_COM:153 helper.set_element(flags, 'robot_type_com', True)154 elif value == c.VALUE_TYPE_AUTO:155 helper.set_element(flags, 'robot_type_com', False)156 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)157 print "MASTER: Changing from type " + local_prev_value + " to type " + value158 local_prev_value = value159 except:160 print "Error interpreting message from master! Continuing anyway"161 elif state == 'RUNNING':162 # Distance163 if mode == 'STOP':164 interval = c.WAIT_DIST_STOP165 else:166 interval = c.WAIT_DIST167 if helper.check_time_limit(times, 'prev_get_dist', c.WAIT_DIST):168 time_between = time.time() - last_meas_time169 last_meas_time = time.time() 170 new_dist = pi2go.getDistance()171 if new_dist > 1:172 prev_distance = distance173 distance = new_dist174 print 'dt:', time_between, distance175 176 # Obstacle = 1, No Obstacle = 0177 irCentre = pi2go.irCentre()178 # Obstacle Analysis179 prev_distance_level = distance_level180 if mode == 'STOP':181 if irCentre or (distance < DIST_MIN and prev_distance < DIST_MIN):182 distance_level = 0183 elif distance > c.DIST_MAX and prev_distance > c.DIST_MAX:184 distance_level = 2185 elif DIST_MIN <= distance <= c.DIST_MAX and DIST_MIN <= prev_distance <= c.DIST_MAX:186 distance_level = 1187 else:188 if irCentre or distance < DIST_MIN:189 distance_level = 0190 elif distance > c.DIST_MAX:191 distance_level = 2192 else:193 distance_level = 1194 195 # Receive196 data = 'new_round'197 while data != '':198 data, addr = com.receive_message(sock) 199 if data != '':200 sender_ID = com.get_id_from_ip(addr[0])201 if sender_ID == OWN_ID:202 # print 'OWN: ' , sender_ID, ' : ' , data203 continue204 if c.TEAM_END >= sender_ID >= c.TEAM_START:205 # print 'ROBOT: ', sender_ID, ' : ' , data206 if data == 'PROBLEM':207 warning[sender_ID-c.TEAM_START] = False208 elif data == 'RELEASE':209 warning[sender_ID-c.TEAM_START] = True210 else:211 try:212 # print 'MASTER:' , sender_ID , ' : ' , data213 command, value = com.string_to_command(data)214 if command == c.COMMAND_SPEED:215 helper.set_element(flags, 'master_set_speed', True)216 prev_SPEED_RUN = SPEED_RUN217 if value == '+':218 SPEED_RUN += 5219 elif value == '-':220 SPEED_RUN -= 5221 else:222 SPEED_RUN = value223 print 'MASTER: Set SPEED_RUN from ' + str(prev_SPEED_RUN) + ' to ' + str(SPEED_RUN)224 elif command == c.COMMAND_DIST:225 prev_DIST_MIN = DIST_MIN226 DIST_MIN = value227 print 'MASTER: Set DIST_MIN from ' + str(prev_DIST_MIN) + ' to ' + str(DIST_MIN)228 elif command == c.COMMAND_BLINK:229 helper.blink('white')230 helper.set_element(flags, 'master_set_LED', True)231 elif command == c.COMMAND_RESET: 232 SPEED_RUN = c.SPEED_RUN233 SPEED_WARN = round(float(SPEED_RUN)/3, 0)234 SPEED_CONTROL_MAX = SPEED_RUN235 SPEED_CONTROL_MIN = SPEED_WARN236 DIST_MIN = c.DIST_MIN237 helper.set_element(times, 'prev_get_dist', 0)238 helper.set_element(flags, 'master_set_LED', True)239 helper.set_element(flags, 'master_set_speed', True)240 helper.set_element(flags, 'set_motor', True)241 warning = [True] * len(warning)242 print 'MASTER: Reset major values'243 elif command == c.COMMAND_STATE:244 helper.set_element(flags, 'master_set_state', True)245 if value == c.VALUE_STATE_RUNNING:246 next_state = 'RUNNING'247 elif value == c.VALUE_STATE_IDLE:248 next_state = 'IDLE'249 print 'MASTER: Going from state ' + state + ' to state ' + next_state250 # elif command == c.COMMAND_TYPE and value != c.VALUE_TYPE_COM:251 elif command == c.COMMAND_TYPE:252 helper.set_element(flags, 'master_set_type', True)253 helper.set_element(flags, 'master_set_LED', True)254 local_prev_value = value255 if value == c.VALUE_TYPE_ORIGINAL:256 if helper.determine_team(OWN_ID) == c.VALUE_TYPE_COM:257 helper.set_element(flags, 'robot_type_com', True)258 else:259 helper.set_element(flags, 'robot_type_com', False)260 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS,261 c.WAIT_SEND)262 elif value == c.VALUE_TYPE_COM:263 helper.set_element(flags, 'robot_type_com', True)264 elif value == c.VALUE_TYPE_AUTO:265 helper.set_element(flags, 'robot_type_com', False)266 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)267 268 print "MASTER: Changing from type " + local_prev_value + " to type " + value269 except:270 print "Error interpreting message from master! Continuing anyway"271 272 # Analyse --> Calculate MODE273 if prev_state == 'RUNNING': 274 prev_mode = mode275 276 # Find Mode277 if helper.get_element(flags, 'robot_type_com'):278 if distance_level == 0:279 mode = 'STOP'280 elif distance_level == 1 and all(warning):281 mode = 'SLOW'282 elif distance_level == 2 and all(warning):283 mode = 'RUN'284 elif distance_level != 0 and not all(warning):285 mode = 'WARN'286 else:287 if distance_level == 0:288 mode = 'STOP'289 elif distance_level == 1:290 mode = 'SLOW'291 elif distance_level == 2:292 mode = 'RUN'293 # Set own Warning-Flag 294 if mode != prev_mode: 295 if mode == 'STOP':296 warning[OWN_ID-c.TEAM_START] = False297 else:298 warning[OWN_ID-c.TEAM_START] = True299 # LEDs 300 if mode != prev_mode or helper.get_element(flags, 'master_set_LED'):301 if helper.get_element(flags, 'master_set_LED'):302 helper.set_element(flags, 'master_set_LED', False)303 if mode == 'RUN':304 pi2go.setAllLEDs(c.LED_OFF, c.LED_ON, c.LED_OFF)305 elif mode == 'SLOW':306 # pi2go.setAllLEDs(c.LED_OFF, c.LED_OFF, c.LED_ON) #TODO: test307 # pi2go.setAllLEDs(c.LED_ON, c.LED_ON, c.LED_OFF)308 pi2go.setAllLEDs(c.LED_OFF, c.LED_ON, c.LED_OFF) # TODO: presentation309 # elif mode == 'WARN':310 # pi2go.setAllLEDs(c.LED_ON, c.LED_ON, c.LED_OFF)311 elif mode == 'STOP':312 pi2go.setAllLEDs(c.LED_ON, c.LED_OFF, c.LED_OFF)313 # Blinking-Mode314 if mode == 'WARN' and helper.get_element(flags, 'robot_type_com'):315 if helper.check_time_limit(times, 'prev_set_LED', c.WAIT_LED):316 if helper.get_element(flags, 'status_warn_LED'):317 pi2go.setAllLEDs(c.LED_OFF, c.LED_OFF, c.LED_OFF)318 helper.set_element(flags, 'status_warn_LED', False)319 else:320 pi2go.setAllLEDs(c.LED_ON, c.LED_ON, c.LED_OFF)321 helper.set_element(flags, 'status_warn_LED', True)322 323 # Calculate new speed324 if mode == 'RUN':325 if prev_mode != 'RUN' or helper.get_element(flags, 'master_set_speed'):326 helper.set_element(flags, 'master_set_speed', False)327 helper.set_element(times, 'new_in_RUN', time.time())328 speed_new_in_RUN = speed329 dspeed = SPEED_RUN-speed_new_in_RUN330 331 if speed != SPEED_RUN:332 dt = time.time()-helper.get_element(times, 'new_in_RUN')333 new_value = round(speed_new_in_RUN + dspeed*(dt/c.TIME_TO_SPEED_UP), 1)334 335 if new_value > SPEED_RUN:336 new_value = SPEED_RUN337 338 if new_value != speed:339 speed = new_value340 helper.set_element(flags, 'set_motor', True)341 # Blocking Avoidance342 elif mode == 'SLOW':343 # linear344 gradient = float(SPEED_CONTROL_MAX - SPEED_CONTROL_MIN)/float(c.DIST_MAX-DIST_MIN)345 error = c.DIST_MAX - distance346 new_value = round(SPEED_RUN - error * gradient, 1)347 348 if new_value < SPEED_CONTROL_MIN:349 new_value = SPEED_CONTROL_MIN350 elif new_value > SPEED_CONTROL_MAX:351 new_value = SPEED_CONTROL_MAX352 353 if new_value != speed:354 speed = new_value355 helper.set_element(flags, 'set_motor', True)356 # Slow-Down in Warning-Mode 357 elif mode == 'WARN' and helper.get_element(flags, 'robot_type_com'):358 if prev_mode != 'WARN':359 helper.set_element(times, 'get_warning', time.time())360 speed_get_warning = speed361 362 if prev_distance_level == 0 and distance_level != 0:363 speed_get_warning = 50364 helper.set_element(times, 'get_warning', time.time())365 366 new_value = round(speed_get_warning * (1 - (time.time()-helper.get_element(times, 'get_warning')) /367 c.TIME_TO_SLOW_DOWN), 1)368 369 # ###############################changed from SPEED_WARN to c.SPEED_STOP370 if new_value < c.SPEED_STOP:371 new_value = c.SPEED_STOP372 #########################373 374 if new_value != speed:375 speed = new_value376 helper.set_element(flags, 'set_motor', True)377 378 elif mode == 'STOP':379 if prev_mode != 'STOP':380 speed = c.SPEED_STOP381 helper.set_element(flags, 'set_motor', True)382 # Motor383 if helper.get_element(flags, 'set_motor'):384 if speed > c.SPEED_LIMIT_MAX:385 speed = c.SPEED_LIMIT_MAX386 elif speed < c.SPEED_LIMIT_MIN:387 speed = c.SPEED_LIMIT_MIN 388 if mode == 'SLOW' or mode == 'WARN':389 if helper.check_time_limit(times, 'prev_set_motor', c.WAIT_MOTOR):390 pi2go.go(speed, speed)391 helper.set_element(flags, 'set_motor', False)392 else:393 pi2go.go(speed, speed)394 helper.set_element(flags, 'set_motor', False)395 396 # Send397 if mode != prev_mode and helper.get_element(flags, 'robot_type_com'): 398 if prev_mode == 'STOP':399 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)400 elif mode == 'STOP':401 com.send_x_broadcast_messages(c.PORT, "PROBLEM", c.SENDING_ATTEMPTS, c.WAIT_SEND)402 # Next State 403 prev_state = state404 if helper.get_element(flags, 'master_set_state'):405 helper.set_element(flags, 'master_set_state', False)406 state = next_state407 408 # Button409 if helper.check_time_limit(times, 'prev_get_switch', c.WAIT_SWITCH):410 # Pressed = 1, Released = 0411 button = pi2go.getSwitch()412 if not button:413 helper.set_element(flags, 'button_release', True)414 if button and helper.get_element(flags, 'button_release'):415 helper.set_element(flags, 'button_release', False)416 prev_state = state417 state = 'IDLE'418 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)419except KeyboardInterrupt:420 print 'KEYBOARD'421finally:422 pi2go.stop()423 pi2go.cleanup()424 sock.close()...
robot_v3.py
Source:robot_v3.py
...52 53 value = helper.determine_team(OWN_ID)54 if helper.determine_team(OWN_ID) == c.VALUE_TYPE_COM:55 print "I'm in com_mode now!"56 helper.set_element(flags, 'robot_type_com', True)57 elif helper.determine_team(OWN_ID) == c.VALUE_TYPE_AUTO:58 print "I'm in auto_mode now!"59 helper.set_element(flags, 'robot_type_com', False)60 local_prev_value = helper.determine_team(OWN_ID)61 # TODO: IDLE-STATE?62 prev_state = state63 state = 'IDLE'64 if state == 'IDLE':65 if prev_state != 'IDLE' or helper.get_element(flags, 'master_set_type'):66 helper.set_element(flags, 'master_set_type', False)67 if helper.get_element(flags, 'robot_type_com'):68 pi2go.setAllLEDs(c.LED_ON, c.LED_OFF, c.LED_ON)69 else: 70 pi2go.setAllLEDs(c.LED_OFF, c.LED_ON, c.LED_ON)71 pi2go.stop()72 if helper.check_time_limit(times, 'prev_get_switch', c.WAIT_SWITCH):73 # Pressed = 1, Released = 074 button = pi2go.getSwitch()75 if not button:76 helper.set_element(flags, 'button_release', True)77 if button and helper.get_element(flags, 'button_release'):78 helper.set_element(flags, 'button_release', False)79 prev_mode = ''80 prev_state = state81 state = 'RUNNING'82 helper.set_element(flags, 'master_set_LED', True)83 helper.set_element(flags, 'set_motor', True)84 # change to sensor-based or master_idle type 85 data = 'new_round'86 while data != '':87 data, addr = com.receive_message(sock) 88 if data != '':89 sender_ID = com.get_id_from_ip(addr[0])90 if sender_ID == OWN_ID:91 # print 'OWN: ' , sender_ID, ' : ' , data92 continue93 if c.TEAM_END >= sender_ID >= c.TEAM_START:94 # print 'ROBOT: ', sender_ID, ' : ' , data95 if data == 'PROBLEM':96 warning[sender_ID-c.TEAM_START] = False97 elif data == 'RELEASE':98 warning[sender_ID-c.TEAM_START] = True99 else:100 command, value = com.string_to_command(data)101 #102 print 'MASTER:', sender_ID, ' : ', data103 try:104 if command == c.COMMAND_SPEED:105 helper.set_element(flags, 'master_set_speed', True)106 prev_SPEED_RUN = SPEED_RUN107 if value == '+':108 SPEED_RUN += 5109 elif value == '-':110 SPEED_RUN -= 5111 else:112 SPEED_RUN = value113 print 'Set SPEED_RUN from ' + str(prev_SPEED_RUN) + ' to ' + str(SPEED_RUN)114 elif command == c.COMMAND_DIST:115 prev_DIST_MIN = DIST_MIN116 DIST_MIN = value117 print 'Set DIST_MIN from ' + str(prev_DIST_MIN) + ' to ' + str(DIST_MIN)118 elif command == c.COMMAND_BLINK:119 helper.blink('white')120 helper.set_element(flags, 'master_set_LED', True)121 elif command == c.COMMAND_RESET: 122 SPEED_RUN = c.SPEED_RUN123 SPEED_WARN = round(float(SPEED_RUN)/3, 0)124 SPEED_CONTROL_MAX = SPEED_RUN125 SPEED_CONTROL_MIN = SPEED_WARN126 DIST_MIN = c.DIST_MIN127 helper.set_element(times, 'prev_get_dist', 0)128 helper.set_element(flags, 'master_set_LED', True)129 helper.set_element(flags, 'master_set_speed', True)130 helper.set_element(flags, 'set_motor', True)131 warning = [True] * len(warning)132 print "Reset major values"133 elif command == c.COMMAND_STATE:134 helper.set_element(flags, 'set_motor', True)135 helper.set_element(flags, 'master_set_LED', True)136 local_prev_state = state137 if value == c.VALUE_STATE_RUNNING:138 state = 'RUNNING'139 elif value == c.VALUE_STATE_IDLE:140 state = 'IDLE'141 print 'Going from state ' + local_prev_state + ' to state ' + state142 elif command == c.COMMAND_TYPE:143 helper.set_element(flags, 'master_set_type', True)144 helper.set_element(flags, 'master_set_LED', True)145 if value == c.VALUE_TYPE_ORIGINAL:146 if helper.determine_team(OWN_ID) == c.VALUE_TYPE_COM:147 helper.set_element(flags, 'robot_type_com', True)148 else:149 helper.set_element(flags, 'robot_type_com', False)150 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS,151 c.WAIT_SEND)152 elif value == c.VALUE_TYPE_COM:153 helper.set_element(flags, 'robot_type_com', True)154 elif value == c.VALUE_TYPE_AUTO:155 helper.set_element(flags, 'robot_type_com', False)156 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)157 print "MASTER: Changing from type " + local_prev_value + " to type " + value158 local_prev_value = value159 except:160 print "Error interpreting message from master! Continuing anyway"161 elif state == 'RUNNING':162 # Distance 163 if helper.check_time_limit(times, 'prev_get_dist', c.WAIT_DIST):164 time_between = time.time() - last_meas_time165 last_meas_time = time.time() 166 new_dist = pi2go.getDistance()167 if new_dist > 1:168 prev_distance = distance169 distance = new_dist170 print 'dt:', time_between, distance171 172 # Obstacle = 1, No Obstacle = 0173 irCentre = pi2go.irCentre()174 # Obstacle Analysis175 prev_distance_level = distance_level176 if mode == 'STOP':177 if irCentre or (distance < DIST_MIN and prev_distance < DIST_MIN):178 distance_level = 0179 elif distance > c.DIST_MAX and prev_distance > c.DIST_MAX:180 distance_level = 2181 elif DIST_MIN <= distance <= c.DIST_MAX and DIST_MIN <= prev_distance <= c.DIST_MAX:182 distance_level = 1183 else:184 if irCentre or distance < DIST_MIN:185 distance_level = 0186 elif distance > c.DIST_MAX:187 distance_level = 2188 else:189 distance_level = 1190 # Receive191 data = 'new_round'192 while data != '':193 data, addr = com.receive_message(sock) 194 if data != '':195 sender_ID = com.get_id_from_ip(addr[0])196 if sender_ID == OWN_ID:197 # print 'OWN: ' , sender_ID, ' : ' , data198 continue199 if c.TEAM_END >= sender_ID >= c.TEAM_START:200 # print 'ROBOT: ', sender_ID, ' : ' , data201 if data == 'PROBLEM':202 warning[sender_ID-c.TEAM_START] = False203 elif data == 'RELEASE':204 warning[sender_ID-c.TEAM_START] = True205 else:206 try:207 # print 'MASTER:' , sender_ID , ' : ' , data208 command, value = com.string_to_command(data)209 if command == c.COMMAND_SPEED:210 helper.set_element(flags, 'master_set_speed', True)211 prev_SPEED_RUN = SPEED_RUN212 if value == '+':213 SPEED_RUN += 5214 elif value == '-':215 SPEED_RUN -= 5216 else:217 SPEED_RUN = value218 print 'MASTER: Set SPEED_RUN from ' + str(prev_SPEED_RUN) + ' to ' + str(SPEED_RUN)219 elif command == c.COMMAND_DIST:220 prev_DIST_MIN = DIST_MIN221 DIST_MIN = value222 print 'MASTER: Set DIST_MIN from ' + str(prev_DIST_MIN) + ' to ' + str(DIST_MIN)223 elif command == c.COMMAND_BLINK:224 helper.blink('white')225 helper.set_element(flags, 'master_set_LED', True)226 elif command == c.COMMAND_RESET: 227 SPEED_RUN = c.SPEED_RUN228 SPEED_WARN = round(float(SPEED_RUN)/3, 0)229 SPEED_CONTROL_MAX = SPEED_RUN230 SPEED_CONTROL_MIN = SPEED_WARN231 DIST_MIN = c.DIST_MIN232 helper.set_element(times, 'prev_get_dist', 0)233 helper.set_element(flags, 'master_set_LED', True)234 helper.set_element(flags, 'master_set_speed', True)235 helper.set_element(flags, 'set_motor', True)236 warning = [True] * len(warning)237 print 'MASTER: Reset major values'238 elif command == c.COMMAND_STATE:239 helper.set_element(flags, 'master_set_state', True)240 if value == c.VALUE_STATE_RUNNING:241 next_state = 'RUNNING'242 elif value == c.VALUE_STATE_IDLE:243 next_state = 'IDLE'244 print 'MASTER: Going from state ' + state + ' to state ' + next_state245 # elif command == c.COMMAND_TYPE and value != c.VALUE_TYPE_COM:246 elif command == c.COMMAND_TYPE:247 helper.set_element(flags, 'master_set_type', True)248 helper.set_element(flags, 'master_set_LED', True)249 local_prev_value = value250 if value == c.VALUE_TYPE_ORIGINAL:251 if helper.determine_team(OWN_ID) == c.VALUE_TYPE_COM:252 helper.set_element(flags, 'robot_type_com', True)253 else:254 helper.set_element(flags, 'robot_type_com', False)255 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS,256 c.WAIT_SEND)257 elif value == c.VALUE_TYPE_COM:258 helper.set_element(flags, 'robot_type_com', True)259 elif value == c.VALUE_TYPE_AUTO:260 helper.set_element(flags, 'robot_type_com', False)261 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)262 263 print "MASTER: Changing from type " + local_prev_value + " to type " + value264 except:265 print "Error interpreting message from master! Continuing anyway"266 267 # Analyse --> Calculate MODE268 if prev_state == 'RUNNING': 269 prev_mode = mode270 271 # Find Mode272 if helper.get_element(flags, 'robot_type_com'):273 if distance_level == 0:274 mode = 'STOP'275 elif distance_level == 1 and all(warning):276 mode = 'SLOW'277 elif distance_level == 2 and all(warning):278 mode = 'RUN'279 elif distance_level != 0 and not all(warning):280 mode = 'WARN'281 else:282 if distance_level == 0:283 mode = 'STOP'284 elif distance_level == 1:285 mode = 'SLOW'286 elif distance_level == 2:287 mode = 'RUN'288 # Set own Warning-Flag 289 if mode != prev_mode: 290 if mode == 'STOP':291 warning[OWN_ID-c.TEAM_START] = False292 else:293 warning[OWN_ID-c.TEAM_START] = True294 # LEDs 295 if mode != prev_mode or helper.get_element(flags, 'master_set_LED'):296 if helper.get_element(flags, 'master_set_LED'):297 helper.set_element(flags, 'master_set_LED', False)298 if mode == 'RUN':299 pi2go.setAllLEDs(c.LED_OFF, c.LED_ON, c.LED_OFF)300 elif mode == 'SLOW':301 # pi2go.setAllLEDs(c.LED_OFF, c.LED_OFF, c.LED_ON) #TODO: test302 # pi2go.setAllLEDs(c.LED_ON, c.LED_ON, c.LED_OFF)303 pi2go.setAllLEDs(c.LED_OFF, c.LED_ON, c.LED_OFF) # TODO: presentation304 # elif mode == 'WARN':305 # pi2go.setAllLEDs(c.LED_ON, c.LED_ON, c.LED_OFF)306 elif mode == 'STOP':307 pi2go.setAllLEDs(c.LED_ON, c.LED_OFF, c.LED_OFF)308 # Blinking-Mode309 if mode == 'WARN' and helper.get_element(flags, 'robot_type_com'):310 if helper.check_time_limit(times, 'prev_set_LED', c.WAIT_LED):311 if helper.get_element(flags, 'status_warn_LED'):312 pi2go.setAllLEDs(c.LED_OFF, c.LED_OFF, c.LED_OFF)313 helper.set_element(flags, 'status_warn_LED', False)314 else:315 pi2go.setAllLEDs(c.LED_ON, c.LED_ON, c.LED_OFF)316 helper.set_element(flags, 'status_warn_LED', True)317 # Calculate new speed318 if mode == 'RUN':319 if prev_mode != 'RUN' or helper.get_element(flags, 'master_set_speed'):320 helper.set_element(flags, 'master_set_speed', False)321 helper.set_element(times, 'new_in_RUN', time.time())322 speed_new_in_RUN = speed323 dspeed = SPEED_RUN-speed_new_in_RUN324 325 if speed != SPEED_RUN:326 dt = time.time()-helper.get_element(times, 'new_in_RUN')327 new_value = round(speed_new_in_RUN + dspeed*(dt/c.TIME_TO_SPEED_UP), 1)328 329 if new_value > SPEED_RUN:330 new_value = SPEED_RUN331 332 if new_value != speed:333 speed = new_value334 helper.set_element(flags, 'set_motor', True)335 # Blocking Avoidance336 elif mode == 'SLOW':337 # linear338 gradient = float(SPEED_CONTROL_MAX - SPEED_CONTROL_MIN)/float(c.DIST_MAX-DIST_MIN)339 error = c.DIST_MAX - distance340 new_value = round(SPEED_RUN - error * gradient, 1)341 342 if new_value < SPEED_CONTROL_MIN:343 new_value = SPEED_CONTROL_MIN344 elif new_value > SPEED_CONTROL_MAX:345 new_value = SPEED_CONTROL_MAX346 347 if new_value != speed:348 speed = new_value349 helper.set_element(flags, 'set_motor', True)350 # Slow-Down in Warning-Mode 351 elif mode == 'WARN' and helper.get_element(flags, 'robot_type_com'):352 if prev_mode != 'WARN':353 helper.set_element(times, 'get_warning', time.time())354 speed_get_warning = speed355 356 if prev_distance_level == 0 and distance_level != 0:357 speed_get_warning = 50358 helper.set_element(times, 'get_warning', time.time())359 360 new_value = round(speed_get_warning * (1 - (time.time()-helper.get_element(times, 'get_warning'))361 / c.TIME_TO_SLOW_DOWN), 1)362 363 # ###############################changed from SPEED_WARN to c.SPEED_STOP364 if new_value < c.SPEED_STOP:365 new_value = c.SPEED_STOP366 #########################367 368 if new_value != speed:369 speed = new_value370 helper.set_element(flags, 'set_motor', True)371 372 elif mode == 'STOP':373 if prev_mode != 'STOP':374 speed = c.SPEED_STOP375 helper.set_element(flags, 'set_motor', True)376 377 # Motor378 if helper.get_element(flags, 'set_motor'):379 if speed > c.SPEED_LIMIT_MAX:380 speed = c.SPEED_LIMIT_MAX381 elif speed < c.SPEED_LIMIT_MIN:382 speed = c.SPEED_LIMIT_MIN 383 if mode == 'SLOW' or mode == 'WARN':384 if helper.check_time_limit(times, 'prev_set_motor', c.WAIT_MOTOR):385 pi2go.go(speed, speed)386 helper.set_element(flags, 'set_motor', False)387 else:388 pi2go.go(speed, speed)389 helper.set_element(flags, 'set_motor', False)390 # Send391 if mode != prev_mode and helper.get_element(flags, 'robot_type_com'): 392 if prev_mode == 'STOP':393 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)394 elif mode == 'STOP':395 com.send_x_broadcast_messages(c.PORT, "PROBLEM", c.SENDING_ATTEMPTS, c.WAIT_SEND)396 # Next State 397 prev_state = state398 if helper.get_element(flags, 'master_set_state'):399 helper.set_element(flags, 'master_set_state', False)400 state = next_state401 402 # Button403 if helper.check_time_limit(times, 'prev_get_switch', c.WAIT_SWITCH):404 # Pressed = 1, Released = 0405 button = pi2go.getSwitch()406 if not button:407 helper.set_element(flags, 'button_release', True)408 if button and helper.get_element(flags, 'button_release'):409 helper.set_element(flags, 'button_release', False)410 prev_state = state411 state = 'IDLE'412 com.send_x_broadcast_messages(c.PORT, "RELEASE", c.SENDING_ATTEMPTS, c.WAIT_SEND)413 414except KeyboardInterrupt:415 print 'KEYBOARD'416finally:417 pi2go.stop()418 pi2go.cleanup()419 sock.close()...
Garabedian_to_RBC_ZBS
Source:Garabedian_to_RBC_ZBS
...4n_max = 35m_max = 26nfp = 57Delta = np.zeros((n_max*2+1, m_max+1))8def set_element(arr, n, m, value):9 arr[n + n_max, m] = value10# Optimum for the 7 DoF problem:11set_element(Delta,-1, 0,-1.498476261706249E-002)12set_element(Delta, 0, 0, 1.0)13set_element(Delta, 1, 0, 1.331244953825667E-002)14set_element(Delta,-1, 1,-9.491502726031747E-002)15set_element(Delta, 0, 1, 10.0)16set_element(Delta, 1, 1, 1.111342639510550E-003)17set_element(Delta,-1, 2, 0.310930057294306)18set_element(Delta, 0, 2,-5.604048985502819E-003)19set_element(Delta, 1, 2,-7.089557472553538E-004)20set_element(Delta,-1, 0,-0.5) # x(1)21set_element(Delta, 1, 2,-0.447368421052632) # x(7)22#set_element(Delta,-1, 0,0.5) # x(1)23#set_element(Delta, 1, 2,0.5) # x(7)24figure_size = (7,7)25##############################################326# End of input parameters27##############################################328print("Here comes the Delta array:")29print(Delta)30# Convert from the Garabedian Delta coefficients to31# VMEC's RBC and ZBS coefficients:32def get_element(arr, n, m):33 return arr[n + n_max, m]34# LIBSTELL/Optimization/unique_boundary_PG.f uses the next few lines to scale everything:35rnorm = get_element(Delta,0,0) / get_element(Delta,0,1)36r00 = get_element(Delta,0,0)37set_element(Delta,0,0,1.0)38RBC = np.zeros((n_max*2+1, m_max+1))39ZBS = np.zeros((n_max*2+1, m_max+1))40for m in range(0, m_max+1):41 n_start = -n_max42 if m==0:43 n_start = 144 for n in range(n_start, n_max+1):45 m_index = 1 - m46 term1 = 047 term2 = 048 if (m_index >= 0) and (m_index <= m_max):49 term1 = get_element(Delta,-n,m_index)50 m_index = 1 + m51 if (m_index >= 0) and (m_index <= m_max):52 term2 = get_element(Delta,n,m_index)53 # Eq (14) and (17) in https://terpconnect.umd.edu/~mattland/plasmanotes/toroidal_surface_parameterizations.pdf54 set_element(RBC, n, m, term1 + term2)55 set_element(ZBS, n, m, term1 - term2)56# Eq (12) in toroidal_surface_parameterizations.pdf57#set_element(RBC, 0, 0, get_element(Delta,0,1))58# Scaling from LIBSTELL/Optimization/unique_boundary_PG.f:59RBC *= rnorm60ZBS *= rnorm61set_element(RBC, 0, 0, r00)62print("Here comes the corresponding VMEC input data:")63for m in range(0, m_max+1):64 for n in range(-n_max, n_max+1):65 if get_element(RBC, n, m) != 0 or get_element(ZBS, n, m) != 0:66 print("RBC({:3d},{:3d}) = {:24.15E} ZBS({:3d},{:3d}) = {:24.15E}".format(n,m,get_element(RBC,n,m),n,m,get_element(ZBS,n,m)))67##################################################68# Plot the resulting surface69##################################################70import matplotlib.pyplot as plt71from mpl_toolkits.mplot3d import Axes3D72ntheta = 20073nphi = 474theta = np.linspace(0,2*np.pi,num=ntheta)75phi = np.linspace(0,2*np.pi/nfp,num=nphi,endpoint=False)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!