def constrain_creatures(self, x_min: int = 0, y_min: int = 0, x_max: int = SIMULATION_WIDTH, y_max: int = SIMULATION_HEIGHT) -> None: """ Makes sure all creatures stay within given borders (default is screen size). """ for creature_info in self.population.values(): creature_info.x = clamp(creature_info.x, x_min, x_max) creature_info.y = clamp(creature_info.y, y_min, y_max)
def _anneal_embedding(self, step): warm_up = self.hyperparams["embedding_hyperparams"]["warm_up"] annealing_type = self.hyperparams["embedding_hyperparams"]["annealing_type"] if annealing_type == "linear": return min(step / warm_up, 1.) elif annealing_type == "piecewise_linear": return clamp(torch.sigmoid(torch.tensor(step-warm_up).float()).item() * ((step-warm_up)/warm_up), 0., 1.) elif annealing_type == "sigmoid": slope = self.hyperparams["embedding_hyperparams"]["sigmoid_slope"] return torch.sigmoid(torch.tensor(slope * (step-warm_up))).item()
def restrictAngles(targetAngles): # Restrict movement targetAngles[1] = functions.clamp(targetAngles[1], -180, 180) targetAngles[2] = functions.clamp(targetAngles[2], -180, 180) targetAngles[3] = functions.clamp(targetAngles[3], -100, 100) targetAngles[4] = functions.clamp(targetAngles[4], -100, 100) targetAngles[5] = functions.clamp(targetAngles[5], -100, 100) targetAngles[6] = functions.clamp(targetAngles[6], -100, 100) return targetAngles
def update(self): # -- Position Management -- self.pos.x += self.vel.x self.rect.x = self.pos.x self.super_jump = False self.collided = pygame.sprite.spritecollide(self, public.blocks, False) for block in self.collided: if block.color != public.bg_type: if self.vel.x > 0 and functions.block_check(block, 4): self.rect.right = block.rect.left elif self.vel.x < 0 and functions.block_check(block, 4): self.rect.left = block.rect.right if functions.block_check(block, 2): self.accelerating = False self.pos.x = self.rect.x if block.type in ['Pit', 'KillBlock'] and not self.died: self.died = True self.anim_ticks = 0 self.anim_index = 0 dictionaries.MEDIA['died'].play() elif block.type == 'Exit' and not self.died: public.level += 1 if public.level == public.level_max: pass elif public.level != public.level_max: functions.generate_level(True) dictionaries.MEDIA['finish'].play() self.kill() elif block.type == 'Breakable': if not block.dead and not block.recovering: dictionaries.MEDIA['crumble'].play() block.broken = True elif block.type == 'Jumpad' and not (self.died and self.super_jump): if not self.super_jump: dictionaries.MEDIA['jumpad'].play() self.super_jump = True self.vel.y = -4.5 self.on_ground = False elif block.type == 'RGBSphere': dictionaries.MEDIA['collect'].play() block.rect.y -= 10 self.won = True self.pos.y += self.vel.y self.rect.y = self.pos.y self.super_jump = False self.collided = pygame.sprite.spritecollide(self, public.blocks, False) for block in self.collided: if block.color != public.bg_type: if self.vel.y > 0 and functions.block_check(block, 4): self.rect.bottom = block.rect.top self.on_ground = True self.vel.y = 0 elif self.vel.y < 0 and functions.block_check(block, 4): self.rect.top = block.rect.bottom self.on_ground = True self.vel.y = 0 if functions.block_check(block, 2): self.super_jump = False self.jumping = False self.pos.y = self.rect.y if block.type in ['Pit', 'KillBlock'] and not self.died: self.died = True self.anim_ticks = 0 self.anim_index = 0 dictionaries.MEDIA['died'].play() elif block.type == 'Pit' and not self.died: self.died = True self.anim_ticks = 0 self.anim_index = 0 dictionaries.MEDIA['died'].play() elif block.type == 'Breakable': if not block.dead and not block.recovering: dictionaries.MEDIA['crumble'].play() block.broken = True if public.wrapping: if self.rect.right >= public.SWIDTH + 10: self.rect.left = 1 self.pos.x = self.rect.left elif self.rect.right <= 0: self.rect.right = public.SWIDTH - 1 self.pos.x = self.rect.left elif not public.wrapping: if self.rect.right >= public.SWIDTH: self.rect.right = public.SWIDTH self.accelerating = False self.pos.x = self.rect.left elif self.rect.left <= 0: self.rect.left = 0 self.accelerating = False self.pos.x = self.rect.left if self.rect.top >= public.SHEIGHT and not self.died: self.died = True self.anim_ticks = 0 self.anim_index = 0 dictionaries.MEDIA['died'].play() self.vel.y += public.GRAVITY if self.vel.y < -0.5 or self.vel.y > 0.5 and not self.jumping: self.on_ground = False if public.level == 0: public.player.vel.x = functions.clamp(public.player.vel.x, -5.0, 5.0) else: public.player.vel.x = functions.clamp(public.player.vel.x, -10.0, 10.0) public.player.pos.x += public.player.vel.x public.player.vel.x *= 0.925 # -- Animation -- self.anim_ticks += 1 if self.anim_ticks == self.anim_caps[self.anim_type]: self.anim_index = (self.anim_index + 1) % 4 self.anim_ticks = 0 else: if self.anim_ticks > self.anim_caps[self.anim_type]: self.anim_ticks = 0 self.anim_type = functions.anim_check(self) if self.died and self.anim_index == 3: if public.level == 0: raise Exception( '@!^& // Sometimes its better to let secrets be secrets. // %$#&' ) functions.generate_level(False) self.image = dictionaries.IMAGES[self.anim_type] self.invert = dictionaries.I_IMAGES[self.anim_type] if self.flip_cooldown > 0: self.flip_cooldown -= public.dt
def clampMotorSpeeds(motorSpeeds): minSpeed = -100.0 maxSpeed = 100.0 for i in range(len(motorSpeeds)): motorSpeeds[i] = functions.clamp(motorSpeeds[i], minSpeed, maxSpeed) return motorSpeeds
def update(self): # -- Position Management -- self.pos.x += self.vel.x self.rect.x = self.pos.x self.super_jump = False self.collided = pygame.sprite.spritecollide(self, public.blocks, False) for block in self.collided: if block.color != public.bg_type: if self.vel.x > 0 and functions.block_check(block, 3): self.rect.right = block.rect.left elif self.vel.x < 0 and functions.block_check(block, 3): self.rect.left = block.rect.right if functions.block_check(block, 2): self.accelerating = False self.pos.x = self.rect.x if block.type == 'Pit' and not self.died: self.died = True self.anim_ticks = 0 self.anim_index = 0 dictionaries.MEDIA['died'].play() elif block.type == 'Exit' and not self.died: public.level += 1 functions.generate_level(True) dictionaries.MEDIA['finish'].play() if public.level == 21: dictionaries.MEDIA['greetings'].stop() dictionaries.MEDIA['deathly'].play(-1) self.kill() elif block.type == 'Breakable': dictionaries.MEDIA['crumble'].play() block.broken = True elif block.type == 'Jumpad' and not (self.died and self.super_jump): if self.flipped and block.direction == 'D': self.vel.y = 4.5 elif not self.flipped and block.direction == 'U': self.vel.y = -4.5 self.on_ground = False self.super_jump = True elif block.type == 'Flipad' and not self.died: if block.direction == 'D' and self.flipped: dictionaries.MEDIA['flipad'].play() self.flipped = False self.vel.y = 2 elif block.direction == 'U' and not self.flipped: dictionaries.MEDIA['flipad'].play() self.flipped = True self.vel.y = -2 elif block.type == 'RGBSphere': dictionaries.MEDIA['collect'].play() block.rect.y -= 10 self.won = True self.pos.y += self.vel.y self.rect.y = self.pos.y self.super_jump = False self.collided = pygame.sprite.spritecollide(self, public.blocks, False) for block in self.collided: if block.color != public.bg_type: if self.vel.y > 0 and functions.block_check(block, 3): self.rect.bottom = block.rect.top self.on_ground = True self.vel.y = 0 elif self.vel.y < 0 and functions.block_check(block, 3): self.rect.top = block.rect.bottom self.on_ground = True self.vel.y = 0 if functions.block_check(block, 2): self.super_jump = False self.jumping = False self.pos.y = self.rect.y if block.type == 'Pit' and not self.died: self.died = True self.anim_ticks = 0 self.anim_index = 0 dictionaries.MEDIA['died'].play() elif block.type == 'Breakable': dictionaries.MEDIA['crumble'].play() block.broken = True elif block.type == 'Jumpad' and not (self.died and self.super_jump): if self.flipped and block.direction == 'D': self.vel.y = 4.5 dictionaries.MEDIA['jumpad'].play() elif not self.flipped and block.direction == 'U': self.vel.y = -4.5 dictionaries.MEDIA['jumpad'].play() self.on_ground = False self.super_jump = True elif block.type == 'Flipad' and not self.died: if block.direction == 'D' and self.flipped: dictionaries.MEDIA['flipad'].play() self.flipped = False self.vel.y = 2 elif block.direction == 'U' and not self.flipped: dictionaries.MEDIA['flipad'].play() self.flipped = True self.vel.y = -2 elif block.type == 'RGBSphere': dictionaries.MEDIA['collect'].play() block.rect.y -= 10 self.won = True if public.wrapping: if self.rect.right >= public.SWIDTH + 10: self.rect.left = 1 self.pos.x = self.rect.left elif self.rect.right <= 0: self.rect.right = public.SWIDTH - 1 self.pos.x = self.rect.left elif not public.wrapping: if self.rect.right >= public.SWIDTH: self.rect.right = public.SWIDTH self.accelerating = False self.pos.x = self.rect.left elif self.rect.left <= 0: self.rect.left = 0 self.accelerating = False self.pos.x = self.rect.left if self.rect.top <= -10 and not self.died: self.died = True dictionaries.MEDIA['died'].play() if self.flipped: self.vel.y -= public.GRAVITY elif not self.flipped: self.vel.y += public.GRAVITY if self.vel.y < -0.5 or self.vel.y > 0.5 and not self.jumping: self.on_ground = False public.player.vel.x = functions.clamp(public.player.vel.x, -10.0, 10.0) public.player.pos.x += public.player.vel.x public.player.vel.x *= 0.925 # -- Animation -- self.anim_ticks += 1 if self.anim_ticks == self.anim_caps[self.anim_type]: self.anim_index = (self.anim_index + 1) % 4 self.anim_ticks = 0 else: if self.anim_ticks > self.anim_caps[self.anim_type]: self.anim_ticks = 0 self.anim_type = functions.anim_check(self) if self.died and self.anim_index == 3: functions.generate_level(False) self.image = dictionaries.IMAGES[self.anim_type] self.invert = dictionaries.I_IMAGES[self.anim_type] if self.flip_cooldown > 0: self.flip_cooldown -= public.dt
# Libraries import functions as fun import numpy as np from constants import earth_radius, irradiance, mie_coeff, num_wavelengths, ozone_coeff, rayleigh_coeff, rayleigh_scale, mie_scale from math import ceil, cos, exp, pi, radians, sin, sqrt, dist from properties import air_density, altitude, dust_density, ozone_density, steps, steps_light, sun_lat # Definitions cam_altitude = 1000 * fun.clamp(altitude, 0.001, 59.999) cam_pos = np.array([0, 0, earth_radius + cam_altitude]) sun_dir = fun.geographical_to_direction(radians(sun_lat), 0) coefficients = np.array([rayleigh_coeff, 1.11 * mie_coeff, ozone_coeff], dtype=np.object) density_multipliers = np.array([air_density, dust_density, ozone_density]) def ray_optical_depth(ray_origin, ray_dir): # optical depth along a ray through the atmosphere ray_origin = np.array(ray_origin) ray_dir = np.array(ray_dir) ray_end = fun.atmosphere_intersection(ray_origin, ray_dir) ray = ray_origin - ray_end ray_length = np.sqrt(ray.dot(ray)) # step along the ray in segments and accumulate the optical depth along each segment segment_length = ray_length / steps_light segment = ray_dir * segment_length # the density of each segment is evaluated at its middle P = ray_origin + 0.5 * segment # list of all P values in steps Ps = np.outer(np.arange(steps_light), segment) + P
def main(): # Flush output for file logging sys.stdout.flush() # Init motor angle sensors via I2C magneticSensors = sensors.AMS() magneticSensors.connect(1) # Init motors via USB motorsModule.initMotors() motors = [0.0] * 9 # Motor outputs 1 to 8, ignore 0 # Motor speeds speed_left = 0.0 speed_right = 0.0 # Remote control old_remote_x = 0.0 old_remote_y = 0.0 # Wait on other threads to start up time.sleep(0.5) # Send OSC commands # ip = "192.168.4.1" # port = 2222 # client = udp_client.SimpleUDPClient(ip, port) # Loop while not keys or not keys.esc_key_pressed: # Read current accelerometer value to see how far forward we're leaning pitch = motorsModule.readIMU('ax') # Read battery level voltage = 0 #motorsModule.readBatteryVoltage() # Read current angles of motors currentAngles = functions.readCurrentAngles(magneticSensors) # Calculate difference in mouse x position try: diff_x = remote.x - old_remote_x diff_y = remote.y - old_remote_y old_remote_x = remote.x old_remote_y = remote.y except NameError: pass FORWARD_SPEED = 2.0 BACKWARD_SPEED = 2.0 TURNING_SPEED = 4.0 MOVEMENT_SPEED = 4.0 # Remote try: # Change motor speeds for turning left right speed_left -= diff_x * TURNING_SPEED / 100.0 speed_right += diff_x * TURNING_SPEED / 100.0 # Remote keyboard commands if remote.up: speed_left = speed_left - FORWARD_SPEED speed_right = speed_right - FORWARD_SPEED if remote.down: speed_left = speed_left + BACKWARD_SPEED speed_right = speed_right + BACKWARD_SPEED if remote.left: speed_left += 20.0 speed_right -= 20.0 if remote.right: speed_left -= 20.0 speed_right += 20.0 # Go forward backward on mouse y speed_left += diff_y * MOVEMENT_SPEED / 100.0 speed_right += diff_y * MOVEMENT_SPEED / 100.0 # Go foward backward on clicks if remote.left_mouse_down: speed_left = speed_left - FORWARD_SPEED speed_right = speed_right - FORWARD_SPEED if remote.right_mouse_down: speed_left = speed_left + BACKWARD_SPEED speed_right = speed_right + BACKWARD_SPEED except NameError: pass # Let's Robot controller try: if letsrobot_controller.forward: speed_left = speed_left - FORWARD_SPEED speed_right = speed_right - FORWARD_SPEED if letsrobot_controller.backward: speed_left = speed_left + BACKWARD_SPEED speed_right = speed_right + BACKWARD_SPEED if letsrobot_controller.left: speed_left += 1.0 * TURNING_SPEED / 5.0 speed_right -= 1.0 * TURNING_SPEED / 5.0 if letsrobot_controller.right: speed_left -= 1.0 * TURNING_SPEED / 5.0 speed_right += 1.0 * TURNING_SPEED / 5.0 except NameError: pass # Clamp speed_left = functions.clamp(speed_left, -100, 100) speed_right = functions.clamp(speed_right, -100, 100) # Send to BROBOT # client.send_message("/1/fader1", 0.5 + speed_left/400.0 + speed_right/400.0) # client.send_message("/1/fader2", 0.5 + speed_left/500.0 - speed_right/500.0) # Balance targetAngles = [None, 0, 0] targetAngles[1] = -pitch - 22 - speed_left / 2 targetAngles[2] = -pitch - 22 - speed_right / 2 # Run movement controller to see how fast we should set our motor speeds movement = walk.calculateMovement(currentAngles, targetAngles) # Send motor commands motors[1] = 0 #movement[1] # Right motor motors[2] = 0 #movement[2] # Left motor motors[1] = speed_right / 5 # Right motor motors[2] = speed_left / 5 # Left motor motorsModule.sendMotorCommands(motors, simulator, False, True) # Display balance, angles, target angles and speeds # functions.display( "Pitch: %3d. Right, Left: Hips: %3d, %3d, Targets: %3d, %3d, Speeds: %3d, %3d. %3d %3d" # % (pitch, currentAngles[1], currentAngles[2], targetAngles[1], targetAngles[2], motors[1], motors[2], speed_left, speed_right ) ) #functions.display( "Pitch: %3d. Right, Left: Knees: %3d, %3d, Targets: %3d, %3d, Speeds: %3d, %3d" # % (pitch, 0, 0, targetAngles[3], targetAngles[4], motors[3], motors[4] ) ) # functions.display( "Pitch: %3d. Right, Left: Feet: %3d, %3d, Targets: %3d, %3d, Speeds: %3d, %3d" # % (pitch, 0, 0, targetAngles[5], targetAngles[6], motors[5], motors[6] ) ) # Slow down speed_left = speed_left * 0.94 speed_right = speed_right * 0.94 # Stop motors motorsModule.stopMotors()