def replay_experiment(experiment, experiment_id, lat_server, ground_truth_id_offset): estimates = experiment.estimates(options.experiment_id) last_timestamp = None for tag_id, x, y, timestamp in estimates: tags = {tag_id: (x, y)} # The dictionary we will send as updates. # Wait before sending, if necessary if last_timestamp: wait_time = timestamp - last_timestamp print "sleeping for %.2f seconds" % wait_time time.sleep(wait_time) clock.set_time(timestamp) last_timestamp = timestamp # Add the ground truth to the update, maybe. if ground_truth_id_offset: ground_truth_tag_id = tag_id + ground_truth_id_offset location = experiment.ground_truth(tag_id) if location: tags[ground_truth_tag_id] = location lat_server.send_tag_updates(tags)
def push_updates(self): "Send distance readings to all the clients, update self.next_update_time." now = clock.get_time() updates = "" reading = self.readings[self.next_reading] while reading["timestamp"] < now: distance_reading = Reading(distance=reading["distance"], tag_id=reading["tag_id"], anchor_id=reading["anchor_id"], error_code=0) updates += "%s%s" % (str(distance_reading), LocationServer.line_separator) self.next_reading += 1 if self.next_reading >= len(self.readings): if self.loop: # Set the next reading to the first, and set the time three seconds before the first reading. self.next_reading = 0 first_timestamp = self.readings[0]["timestamp"] clock.set_time(first_timestamp - 3.0) self.next_update_time = first_timestamp break reading = self.readings[self.next_reading] if updates: logging.info("Pushing updates of length %d" % len(updates)) logging.debug("Updates:\n%s" % updates) for client in self.clients: client.send(updates)
def run_locmod(experiment, locmod, config): "Run the locmod for a particular configuration against the distance readings to generate a new set of estimates." last_anchor_id = 0 logging.info("Running location module: %s" % (config.locmod_filename)) experiment.register_configuration(config.filename, config.text, config.locmod_filename, config.locmod_text) sql = "SELECT anchor_id, tag_id, distance, timestamp FROM distance_reading ORDER BY timestamp" for anchor_id, tag_id, distance, timestamp in experiment.query(sql): # Skip if the reading is not relavent to this configuration if not anchor_id in config.anchors: logging.debug("Ignoring reading from unknown anchor: %d" % anchor_id) continue if not tag_id in config.tag_ids: logging.debug("Ignoring reading from unknown tag: %d" % tag_id) continue # Anchor IDs come in order, so if we roll back to an earlier one it means we have all the reading for this update, and it's time to do the location update. if anchor_id < last_anchor_id: update = locmod.update_locations([tag_id]) if update.has_key(tag_id): location = update[tag_id] x, y = location ground_truth = experiment.ground_truth(tag_id) if ground_truth: gx, gy = ground_truth error = math.hypot(x - gx, y - gy) ground_truth_id = experiment.ground_truth_id(tag_id) sql = "INSERT INTO estimate(tag_id, x, y, timestamp, ground_truth_id, error, configuration_id) VALUES (?, ?, ?, ?, ?, ?, ?)" experiment.cursor.execute(sql, (tag_id, x, y, clock.get_time(), ground_truth_id, error, experiment.configuration_id)) logging.debug("Inserted estimate: (%06.2f, %06.2f) error %05.2fm from (%06.2f, %06.2f)" % (x, y, error, gx, gy)) else: logging.debug("Not adding estimate, location not known.") clock.set_time(timestamp) locmod.add_reading(anchor_id, tag_id, distance) last_anchor_id = anchor_id experiment.connection.commit()
def advance_time(self, interval): now = clock.get_time() should_be = now + interval clock.set_time(should_be) print "Error: %.2f" % (clock.get_time() - should_be)