Skip to content

Commit 180f6fa

Browse files
authored
Merge pull request #129 from strykeforce/log-lights
Increment frame log seq number when lights toggled
2 parents a3141e6 + 5dc5dc0 commit 180f6fa

File tree

13 files changed

+124
-68
lines changed

13 files changed

+124
-68
lines changed

daemon/.idea/cmake.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

daemon/CPPLINT.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
set noparent
2-
filter=-build/include_order,-build/include_subdir,-runtime/references,-build/c++11,-whitespace/indent
2+
filter=-build/include_order,-build/include_subdir,-runtime/references,-build/c++11,-whitespace/indent,-readability/nolint

daemon/src/hardware/camera.cpp

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,26 @@ class On : public Camera<inum> {
3434
std::rethrow_exception(std::current_exception());
3535
}
3636
});
37-
spdlog::info("Camera<{}{}> on", DEADEYE_UNIT, inum);
37+
base::pipeline_runner_.SetLoggingEnabled(true);
38+
spdlog::info("Camera<{}> on", CameraId(inum));
3839
}
3940

4041
void react(CameraOff const&) final {
4142
Lights<inum>::dispatch(LightsOff());
42-
base::pipeline_runner_.Stop();
4343

44-
// future will not be valid at start-up since pipeline task not started yet
45-
if (base::pipeline_future_.valid()) {
46-
try {
47-
base::pipeline_future_.get();
48-
} catch (std::exception const& e) {
49-
base::error_ = e.what();
50-
base::has_error_ = false; // don't retrigger in controller
51-
base::template transit<camera::Error<inum>>();
52-
return;
53-
}
54-
}
5544
base::template transit<camera::Off<inum>>();
5645
}
5746

47+
void react(LightsOff const&) final {
48+
base::pipeline_runner_.SetLoggingEnabled(false);
49+
spdlog::debug("Camera<{}> stop logging", CameraId(inum));
50+
}
51+
52+
void react(LightsOn const&) final {
53+
base::pipeline_runner_.SetLoggingEnabled(true);
54+
spdlog::debug("Camera<{}> start logging", CameraId(inum));
55+
}
56+
5857
void exit() final { base::SetStatus(DE_ON, false); }
5958
};
6059

@@ -67,7 +66,21 @@ class Off : public Camera<inum> {
6766

6867
void entry() final {
6968
base::SetStatus(DE_OFF, true);
70-
spdlog::info("Camera<{}{}> off", DEADEYE_UNIT, inum);
69+
base::pipeline_runner_.SetLoggingEnabled(false);
70+
base::pipeline_runner_.Stop();
71+
72+
// future will not be valid at start-up since pipeline task not started yet
73+
if (base::pipeline_future_.valid()) {
74+
try {
75+
base::pipeline_future_.get();
76+
} catch (std::exception const& e) {
77+
base::error_ = e.what();
78+
base::has_error_ = false; // don't re-trigger in controller
79+
base::template transit<camera::Error<inum>>();
80+
return;
81+
}
82+
}
83+
spdlog::info("Camera<{}> off", CameraId(inum));
7184
}
7285

7386
void react(CameraOn const&) final {
@@ -88,20 +101,20 @@ class Error : public Camera<inum> {
88101
void entry() final {
89102
base::SetStatus(DE_ERROR, true);
90103
Lights<inum>::dispatch(LightsOff());
91-
spdlog::error("Camera<{}{}> error: {}", DEADEYE_UNIT, inum, base::error_);
104+
spdlog::error("Camera<{}> error: {}", CameraId(inum), base::error_);
92105
}
93106

94107
void react(CameraOn const&) final {
95108
base::SetStatus(DE_ON, false);
96-
spdlog::warn("Camera<{}{}> attempting to turn on camera in error state: {}",
97-
DEADEYE_UNIT, inum, base::error_);
109+
spdlog::warn("Camera<{}> attempting to turn on camera in error state: {}",
110+
CameraId(inum), base::error_);
98111
}
99112

100113
void react(CameraOff const&) final {
101114
base::SetStatus(DE_OFF, false);
102115
spdlog::warn(
103116
"Camera<{}{}> attempting to turn off camera in error state: {}",
104-
DEADEYE_UNIT, inum, base::error_);
117+
CameraId(inum), base::error_);
105118
}
106119

107120
void exit() final { base::SetStatus(DE_ERROR, false); }

daemon/src/hardware/camera.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "config/capture_config.h"
1818
#include "config/pipeline_config.h"
1919
#include "config/stream_config.h"
20+
#include "events.h"
2021
#include "pipeline/runner.h"
2122

2223
namespace deadeye {
@@ -26,21 +27,6 @@ class Pipeline;
2627
template <class T>
2728
using owner [[maybe_unused]] = T;
2829

29-
// ---------------------------------------------------------------------------
30-
// Events
31-
//
32-
struct CameraOn : tinyfsm::Event {};
33-
struct CameraOff : tinyfsm::Event {};
34-
struct ConfigCapture : tinyfsm::Event {
35-
CaptureConfig config;
36-
};
37-
struct ConfigPipeline : tinyfsm::Event {
38-
PipelineConfig config;
39-
};
40-
struct ConfigStream : tinyfsm::Event {
41-
StreamConfig config;
42-
};
43-
4430
// ---------------------------------------------------------------------------
4531
// Camera FSM
4632
//
@@ -69,6 +55,8 @@ class Camera : public tinyfsm::Fsm<Camera<inum>> {
6955

7056
[[maybe_unused]] virtual void react(CameraOn const&) {}
7157
[[maybe_unused]] virtual void react(CameraOff const&) {}
58+
[[maybe_unused]] virtual void react(LightsOn const&) {}
59+
[[maybe_unused]] virtual void react(LightsOff const&) {}
7260
[[maybe_unused]] virtual void react(ConfigCapture const& c) {
7361
Camera<inum>::pipeline_runner_.Configure(c.config);
7462
}
@@ -97,7 +85,7 @@ class Camera : public tinyfsm::Fsm<Camera<inum>> {
9785

9886
// state variable definitions
9987
template <int inum>
100-
Runner Camera<inum>::pipeline_runner_;
88+
Runner Camera<inum>::pipeline_runner_; // NOLINT(cert-err58-cpp)
10189

10290
template <int inum>
10391
std::future<void> Camera<inum>::pipeline_future_;

daemon/src/hardware/events.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2022 Stryke Force FRC 2767
2+
#pragma once
3+
4+
#include <tinyfsm.hpp>
5+
6+
#include "config/capture_config.h"
7+
#include "config/pipeline_config.h"
8+
#include "config/stream_config.h"
9+
10+
namespace deadeye {
11+
struct CameraOn : tinyfsm::Event {};
12+
struct CameraOff : tinyfsm::Event {};
13+
14+
struct LightsOn : tinyfsm::Event {};
15+
struct LightsOff : tinyfsm::Event {};
16+
struct LightsBlink : tinyfsm::Event {};
17+
18+
struct ConfigCapture : tinyfsm::Event {
19+
CaptureConfig config;
20+
};
21+
struct ConfigPipeline : tinyfsm::Event {
22+
PipelineConfig config;
23+
};
24+
struct ConfigStream : tinyfsm::Event {
25+
StreamConfig config;
26+
};
27+
28+
} // namespace deadeye

daemon/src/hardware/lights.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <thread>
99
#include <tinyfsm.hpp>
1010

11+
#include "camera.h"
12+
1113
namespace {
1214
using namespace std::literals:: // NOLINT(build/namespaces_literals)
1315
chrono_literals;
@@ -29,7 +31,7 @@ class On : public Lights<inum> {
2931

3032
void entry() override {
3133
base::SetStatus(DE_ON, true);
32-
spdlog::info("Lights<{}{}> on", DEADEYE_UNIT, inum);
34+
spdlog::info("Lights<{}> on", CameraId(inum));
3335
base::led_.On();
3436
base::on_ = true;
3537
}
@@ -39,6 +41,7 @@ class On : public Lights<inum> {
3941
}
4042

4143
void react(LightsOff const&) override {
44+
Camera<inum>::dispatch(LightsOff());
4245
base::template transit<lights::Off<inum>>();
4346
}
4447

@@ -55,18 +58,18 @@ class Blinking : public Lights<inum> {
5558
base::cancel_task_ = false;
5659
base::task_future_ = std::async(std::launch::async, [] {
5760
while (true) {
58-
spdlog::trace("Lights<{}{}> on", DEADEYE_UNIT, inum);
61+
spdlog::trace("Lights<{}> on", CameraId(inum));
5962
base::led_.On();
6063
std::this_thread::sleep_for(kBlinkPeriod);
6164
if (base::cancel_task_.load()) break;
6265

63-
spdlog::trace("Lights<{}{}> off", DEADEYE_UNIT, inum);
66+
spdlog::trace("Lights<{}> off", CameraId(inum));
6467
base::led_.Off();
6568
std::this_thread::sleep_for(kBlinkPeriod);
6669
if (base::cancel_task_.load()) break;
6770
}
6871
});
69-
spdlog::info("Lights<{}{}> blink", DEADEYE_UNIT, inum);
72+
spdlog::info("Lights<{}> blink", CameraId(inum));
7073
}
7174

7275
#pragma clang diagnostic push
@@ -77,8 +80,8 @@ class Blinking : public Lights<inum> {
7780
try {
7881
base::task_future_.get();
7982
} catch (std::exception const& e) {
80-
spdlog::error("Lights<{}{}> error while cancelling blink: {}",
81-
DEADEYE_UNIT, inum, e.what());
83+
spdlog::error("Lights<{}> error while cancelling blink: {}",
84+
CameraId(inum), e.what());
8285
}
8386
}
8487
}
@@ -103,12 +106,13 @@ class Off : public Lights<inum> {
103106

104107
void entry() override {
105108
base::SetStatus(DE_OFF, true);
106-
spdlog::info("Lights<{}{}> off", DEADEYE_UNIT, inum);
109+
spdlog::info("Lights<{}> off", CameraId(inum));
107110
base::led_.Off();
108111
base::on_ = false;
109112
}
110113

111114
void react(LightsOn const&) override {
115+
Camera<inum>::dispatch(LightsOn());
112116
base::template transit<lights::On<inum>>();
113117
}
114118

daemon/src/hardware/lights.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@
1212
#include <tinyfsm.hpp>
1313

1414
#include "config.h"
15+
#include "events.h"
1516
#include "led_drive.h"
1617

1718
namespace deadeye {
18-
// ---------------------------------------------------------------------------
19-
// Events
20-
//
21-
struct LightsOn : tinyfsm::Event {};
22-
struct LightsOff : tinyfsm::Event {};
23-
struct LightsBlink : tinyfsm::Event {};
2419

2520
// ---------------------------------------------------------------------------
2621
// Lights FSM

daemon/src/log/frame_logger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class FrameLogger {
4141
}
4242

4343
void Run() {
44+
cancel_ = false;
4445
future_ = std::async(std::launch::async, &logger::FrameLoggerBase::Run,
4546
logger_.get());
4647
spdlog::debug("Logger::Run starting async logging task");
@@ -54,7 +55,7 @@ class FrameLogger {
5455

5556
void Stop() {
5657
cancel_ = true;
57-
future_.wait();
58+
if (future_.valid()) future_.wait();
5859
spdlog::debug("Logger::Stop stopped async logging task");
5960
}
6061

daemon/src/log/frame_logger_base.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <sys/stat.h>
77

88
#include <string>
9-
#include <utility>
109

1110
using ::deadeye::logger::FrameLoggerBase;
1211

@@ -16,20 +15,26 @@ FrameLoggerBase::FrameLoggerBase(const int inum, const FrameLogConfig& config,
1615
std::atomic<bool>& cancel)
1716
: id_{CameraId(inum)},
1817
enabled_{config.fps > 0 && CheckMount(config) && CheckDir(config)},
18+
state_{state},
1919
queue_(queue),
2020
cancel_{cancel},
2121
client_logger_{inum} {
22-
template_ =
23-
fmt::format("{}/{}/{}-{{}}.jpg", config.path, id_, state.sequence++);
22+
base_template_ =
23+
fmt::format("{}/{}/{{:04d}}-{{{{:04d}}}}.jpg", config.path, id_);
24+
spdlog::debug("FrameLoggerBase<{}>: base_template_ = {}", id_,
25+
base_template_);
2426
}
2527

2628
void FrameLoggerBase::Run() {
27-
client_logger_.Info(
28-
fmt::format("FrameLogger<{}>: first log {}", id_, GetFrameImagePath(1)));
29+
seq_template_ = fmt::format(base_template_, state_.sequence++);
30+
spdlog::debug("FrameLoggerBase<{}>: seq_template_ = {}", id_, seq_template_);
31+
32+
client_logger_.Info(fmt::format("FrameLoggerBase<{}>: first log {}", id_,
33+
GetFrameImagePath(1)));
2934

3035
RunLoop();
3136

32-
client_logger_.Info(fmt::format("FrameLogger<{}>: last log {}", id_,
37+
client_logger_.Info(fmt::format("FrameLoggerBase<{}>: last log {}", id_,
3338
GetFrameImagePath(frame_count_ - 1)));
3439
}
3540

daemon/src/log/frame_logger_base.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ class FrameLoggerBase {
3939
virtual ~FrameLoggerBase() = default;
4040

4141
inline std::string GetFrameImagePath(int frame_count) {
42-
return fmt::format(template_, frame_count);
42+
return fmt::format(seq_template_, frame_count);
4343
}
4444

4545
void Run();
4646

4747
protected:
4848
std::string id_;
4949
bool enabled_;
50-
std::string template_;
50+
FrameLoggerState& state_;
51+
std::string base_template_;
52+
std::string seq_template_;
5153
int frame_count_{1};
5254
std::atomic<bool>& cancel_;
5355
FrameLoggerQueue& queue_;

0 commit comments

Comments
 (0)