def set_camera_angle(self, vertical, horizontal): self.vertical_angle = utilities.bound(MIN_V_ANGLE, MAX_V_ANGLE, vertical) self.horizontal_angle = utilities.bound(MIN_H_ANGLE, MAX_H_ANGLE, horizontal) command = utilities.get_command(SERVO, self.vertical_angle, self.horizontal_angle) self.ser.write(command)
def setJoints(self, theta_desired): self.theta_ideal = np.array(theta_desired) self.EE_ideal = util.forward(self.theta_ideal, self.length) # Correct for servo inaccuracies self.theta_real = self.theta_ideal + self.correction # Converts theta from "Matlab-space" to "servo-ready-space" self.theta_real[0] = util.bound(self.theta_real[0], -90, 90, 0, 180) self.theta_real[1] = util.bound(self.theta_real[1], -90, 90, 0, 180) self.theta_real[2] = util.bound(180 - self.theta_real[2], 0, 180, 0, 180) # Send commands to corresponding servo hat and pin for i in range(3): self.servo_hat.servo[self.pins[i]].angle = self.theta_real[i]
def draw(self, screen): t = (time.time() - self.shootTime) / self.LASER_TIME t = utilities.bound(0, t, 1) thickness = round((1 - t) * 3) color = colors.RED pygame.draw.line(screen, color, self.rect.topleft, (geo.Vector2D(*self.pos()) + self.v).tuple(), thickness)
def update(self): self.pos() # powerup logic if self.powerActive: timeSpentActivated = time.time() - self.lastPowerupTime self.power.timeLeft = self.power.startTimeLeft - timeSpentActivated if self.power.timeLeft <= 0: self.deactivatePower() self.removePower() if self.hasPower() and self.power.timeLeft <= 0: self.removePower() # speed logic if self.powerActive \ and (self.hasPower(PowerupType.SPEED_BOOST) or self.hasPower(PowerupType.CONTROLLED_BOOST)): if not self.slowed: self.MAX_FWD_SPEED = self.BOOST_FWD_SPEED self.MAX_REV_SPEED = self.BOOST_REV_SPEED else: self.MAX_FWD_SPEED = self.DEFAULT_MAX_FWD_SPEED self.MAX_REV_SPEED = self.DEFAULT_MAX_REV_SPEED elif self.powerActive and self.hasPower(PowerupType.RANDOMIZER): if not self.slowed: # randomize speed according to a standard normal self.MAX_FWD_SPEED = utilities.bound(self.MIN_RANDOM_SPEED, self.rng.standard_normal() * self.RANDOM_SPREAD + self.MAX_FWD_SPEED, self.MAX_RANDOM_SPEED) self.MAX_REV_SPEED = self.BOOST_REV_SPEED else: self.MAX_FWD_SPEED = self.SLOWED_FWD_SPEED self.MAX_REV_SPEED = self.SLOWED_REV_SPEED elif self.powerActive and self.hasPower(PowerupType.POWER_WHEELS): self.MAX_FWD_SPEED = self.DEFAULT_MAX_FWD_SPEED self.MAX_REV_SPEED = self.DEFAULT_MAX_REV_SPEED else: if self.slowed or (self.powerActive and self.hasPower(PowerupType.SLOWDOWN)): self.MAX_FWD_SPEED = self.SLOWED_FWD_SPEED self.MAX_REV_SPEED = self.SLOWED_REV_SPEED else: self.MAX_FWD_SPEED = self.DEFAULT_MAX_FWD_SPEED self.MAX_REV_SPEED = self.DEFAULT_MAX_REV_SPEED if self.powerActive and self.hasPower(): self.trail.append([self.rect.center[0], self.rect.center[1]]) while (len(self.trail) > self.TRAIL_LENGTH): self.trail.pop(0) else: self.trail = [] # driving logic self.speed = max(-self.MAX_REV_SPEED, min(self.maxSpeed, self.speed + self.acceleration)) self.v = geo.Vector2D.create_from_angle(-np.radians((self.angle)), self.speed) # angle in radians self.rect.move_ip(*self.v)
def generateWalls(self): info = pygame.display.Info() screenWidth, screenHeight = info.current_w, info.current_h # number of walls N = int(np.ceil(screenWidth / copter.Wall.WIDTH)) + 3 # generate walls gap_height = self.GAP_FRACTION * screenHeight gap_pos = self.rng.random() \ * (screenHeight * (1 - 2 * self.GAP_CLEARANCE - self.GAP_FRACTION)) \ + screenHeight * (self.GAP_CLEARANCE + 0.5 * self.GAP_FRACTION) self.gap_lastheight = gap_height self.gap_heights = np.ones(N) * gap_height self.gap_pos = np.zeros(N) self.gap_pos[0] = gap_pos gap_roof = round(gap_pos - gap_height / 2) gap_floor = round(gap_pos + gap_height / 2) for i in range(1, N): gap_pos += self.FLUCTUATION * self.rng.standard_normal() gap_pos = utilities.bound( gap_height / 2 + self.GAP_CLEARANCE * screenHeight, gap_pos, (1 - self.GAP_CLEARANCE) * screenHeight - gap_height / 2) self.gap_pos[i] = gap_pos for i in range(N - 1): last_gap_roof = gap_roof last_gap_floor = gap_floor gap_roof = round(self.gap_pos[i] - self.gap_heights[i] / 2) gap_floor = round(self.gap_pos[i] + self.gap_heights[i] / 2) next_gap_roof = round(self.gap_pos[i + 1] - self.gap_heights[i + 1] / 2) next_gap_floor = round(self.gap_pos[i + 1] + self.gap_heights[i + 1] / 2) NW, NE, SW, SE = 0, 0,\ round((last_gap_roof + gap_roof) / 2), round((gap_roof + next_gap_roof) / 2) top = copter.Wall(NW, NE, SE, SW) NW, NE, SW, SE = round((last_gap_floor + gap_floor) / 2),\ round((gap_floor + next_gap_floor) / 2),\ screenHeight, screenHeight bottom = copter.Wall(NW, NE, SE, SW) x = i * copter.Wall.WIDTH top.rect.x = x bottom.rect.x = x if abs(x - self.copter.rect.x) < copter.Wall.WIDTH: self.copterIndex = i self.walls.add(top) self.walls.add(bottom)
def generateWall(self, top=True): info = pygame.display.Info() screenWidth, screenHeight = info.current_w, info.current_h if top: if (time.time() - self.lastnarrow) >= self.NARROWING_INTERVAL: self.gap_lastheight = max(0.95 * self.gap_lastheight, 3 * self.copter.rect.height) self.gap_heights[0] = self.gap_lastheight self.lastnarrow = time.time() if (time.time() - self.lastfluct) >= self.FLUCTUATION_INTERVAL: self.FLUCTUATION = min(self.FLUCTUATION + 1, self.MAX_FLUCTUATION) self.lastfluct = time.time() self.gap_pos[0] = self.gap_pos[ -1] + self.FLUCTUATION * self.rng.standard_normal() self.gap_pos[0] = utilities.bound( self.gap_lastheight / 2 + self.GAP_CLEARANCE * screenHeight, self.gap_pos[0], (1 - self.GAP_CLEARANCE) * screenHeight - self.gap_lastheight / 2) self.gap_heights[0] = self.gap_lastheight # roll gap arrays self.gap_pos = np.roll(self.gap_pos, -1) self.gap_heights = np.roll(self.gap_heights, -1) def roof(index): return round(self.gap_pos[index] - self.gap_heights[index] / 2) def floor(index): return round(self.gap_pos[index] + self.gap_heights[index] / 2) last_gap_roof = roof(-3) last_gap_floor = floor(-3) gap_roof = roof(-2) gap_floor = floor(-2) next_gap_roof = roof(-1) next_gap_floor = floor(-1) if top: NW, NE, SW, SE = 0, 0,\ round((last_gap_roof + gap_roof) / 2),\ round((gap_roof + next_gap_roof) / 2) else: NW, NE, SW, SE = round((last_gap_floor + gap_floor) / 2),\ round((gap_floor + next_gap_floor) / 2),\ screenHeight, screenHeight new = copter.Wall(NW, NE, SE, SW) return new
def update(self): T = time.time() - self.lastLoop color = np.array(self.color) if (T > self.loopTime): if self.switch: newType = PowerupType(int(self.rng.random() * PowerupType.NUMBER_POWERUPS.value)) self.switchTo(newType) self.loopTime = utilities.bound(self.MIN_LOOP_TIME, self.loopTime + self.rng.standard_normal() * self.loopSpread, self.MAX_LOOP_TIME) self.lastLoop = time.time() else: t = T / self.loopTime # find the shade of the color using a linear seesaw color = utilities.seesaw(0.7 * color, color, t) self.image.fill(color)
def fly(self, roof, ground): self.y = utilities.bound(roof + self.CLEARANCE, self.y + np.random.normal(), ground - self.rect.height - self.CLEARANCE) self.rect.y = int(self.y)
def set_horizontal_angle(self, angle): self.horizontal_angle = utilities.bound(MIN_H_ANGLE, MAX_H_ANGLE, angle) command = utilities.get_command(SERVO, self.vertical_angle, self.horizontal_angle) self.ser.write(command)