Skip to content

Commit

Permalink
seek engament/disengament in qlogs
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Dec 16, 2021
1 parent 53ded45 commit e1b4ace
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
5 changes: 2 additions & 3 deletions selfdrive/ui/replay/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ void keyboardThread(Replay *replay_) {
} else if (c == 'G') {
replay_->seekTo(0, true);
} else if (c == 'e') {
replay_->nextEngagement();
replay_->seekToFlag(FindFlag::nextEngagement);
} else if (c == 'd') {
replay_->nextDisengagement();
}
replay_->seekToFlag(FindFlag::nextDisEngagement);
} else if (c == ' ') {
replay_->pause(!replay_->isPaused());
}
Expand Down
48 changes: 40 additions & 8 deletions selfdrive/ui/replay/replay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Replay::Replay(QString route, QStringList allow, QStringList block, SubMaster *s
events_ = std::make_unique<std::vector<Event *>>();
new_events_ = std::make_unique<std::vector<Event *>>();

qRegisterMetaType<FindFlag>("FindFlag");
connect(this, &Replay::seekTo, this, &Replay::doSeek);
connect(this, &Replay::seekToFlag, this, &Replay::doSeekToFlag);
connect(this, &Replay::segmentChanged, this, &Replay::queueSegment);
}

Expand Down Expand Up @@ -111,20 +113,50 @@ void Replay::doSeek(int seconds, bool relative) {
queueSegment();
}

void Replay::nextEngagement() {
// only search in the loaded segments.
auto find_engaged = [](const Segment *seg, uint64_t tm) {
std::find()
};
void Replay::doSeekToFlag(FindFlag flag) {
if (flag == FindFlag::nextEngagement) {
qInfo() << "seeking to next engagement...";
} else {
qInfo() << "seeking to next disengagement...";
}

updateEvents([&]() {
for (auto &[n, seg] : segments_) {
if (n >= currentSeconds() / 60 ) {
auto next = find(flag);
if (next) {
cur_mono_time_ = *next;
current_segment_ = currentSeconds() / 60;
qInfo() << "seeked";
return isSegmentMerged(current_segment_);
}
qWarning() << "seeking failed";
return true;
});

queueSegment();
}

std::optional<uint64_t> Replay::find(FindFlag flag) {
for (auto &[n, seg] : segments_) {
if (n < current_segment_) continue;

LogReader log;
if (log.load(route_->at(n).qlog.toStdString(), nullptr, true, 0, 3)) {
for (auto evt : log.events) {
if (evt->mono_time > cur_mono_time_) {
if (flag == FindFlag::nextEngagement) {
if (evt->which == cereal::Event::Which::CONTROLS_STATE && evt->event.getControlsState().getEnabled()) {
return evt->mono_time - 2 * 1e9;
}
} else if (flag == FindFlag::nextDisEngagement) {
if (evt->which == cereal::Event::Which::CONTROLS_STATE && !evt->event.getControlsState().getEnabled()) {
return evt->mono_time - 2 * 1e9;
}
}
}
}
}
}
// controls_state.getEnabled()
return std::nullopt;
}

void Replay::nextDisengagement() {
Expand Down
8 changes: 8 additions & 0 deletions selfdrive/ui/replay/replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ enum REPLAY_FLAGS {
REPLAY_FLAG_NO_CUDA = 0x0100,
};

enum class FindFlag {
nextEngagement,
nextDisEngagement
};

class Replay : public QObject {
Q_OBJECT

Expand All @@ -37,14 +42,17 @@ class Replay : public QObject {
signals:
void segmentChanged();
void seekTo(int seconds, bool relative);
void seekToFlag(FindFlag flag);

protected slots:
void queueSegment();
void doSeek(int seconds, bool relative);
void doSeekToFlag(FindFlag flag);
void segmentLoadFinished(bool sucess);

protected:
typedef std::map<int, std::unique_ptr<Segment>> SegmentMap;
std::optional<uint64_t> find(FindFlag flag);
void startStream(const Segment *cur_segment);
void stream();
void setCurrentSegment(int n);
Expand Down

0 comments on commit e1b4ace

Please sign in to comment.