def subscribe_flock(self): flock_coordinate_subject_list = [self.selfposition_subject] flock_coordinate_subject_list.extend(self.flock_coordinates.values()) self.flockSubscription = Observable.combine_latest(flock_coordinate_subject_list, lambda *positions: self.repulsion_vector(positions)) \ .sample(self.SAMPLE_RATE) \ .subscribe(on_next= lambda v: self.flock_repulsion.on_next(v))
def __init__(self, id, local_setvelocity_publisher, xoffset, yoffset, leader): self.local_setvelocity_publisher = local_setvelocity_publisher self.id = id self.xoffset = xoffset self.yoffset = yoffset self.leader = leader self.started = False self.ros_subscriptions = [] self.flock_coordinates = {} self.leaderposition_subject = Subject() self.selfposition_subject = Subject() self.leadervelocity_subject = Subject() self.selfvelocity_subject = Subject() self.flock_repulsion = Subject() formation_position_attraction = Observable.combine_latest( self.leaderposition_subject, self.selfposition_subject, lambda leaderposition, selfposition: self.formation_position( leaderposition, selfposition)) leaderVelocity = self.leadervelocity_subject \ .map(lambda twist: self.format_velocities(twist)) leaderVelocityDampening = Observable.combine_latest( self.leadervelocity_subject, self.selfvelocity_subject, lambda leadertwist, selftwist: self.velocity_dampening( leadertwist, selftwist)) # self.navigate_subscription = Observable.combine_latest([leaderVelocity, formation_position_attraction, self.flock_repulsion], lambda vectors: self.navigate(vectors)) self.navigate_subscription = Observable.combine_latest([leaderVelocity, leaderVelocityDampening, formation_position_attraction, self.flock_repulsion], lambda *positions: positions) \ .sample(self.SAMPLE_RATE) \ .subscribe(lambda vectors: self.navigate(vectors)) # self.navigate_subscription = self.flock_repulsion \ # .subscribe(lambda vectors: self.navigate([vectors])) self.flockSubscription = Observable.empty().subscribe()
def combine_latest(self, *args): """Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences produces an element. This can be in the form of an argument list of observables or an array. 1 - obs = observable.combine_latest(obs1, obs2, obs3, lambda o1, o2, o3: o1 + o2 + o3) 2 - obs = observable.combine_latest([obs1, obs2, obs3], lambda o1, o2, o3: o1 + o2 + o3) Returns an observable sequence containing the result of combining elements of the sources using the specified result selector function. """ args = list(args) if args and isinstance(args[0], list): args = args[0] args.insert(0, self) return Observable.combine_latest(*args)