blob: 560e9eec1ef831c2ac2322340cd2801c894fd71c [file] [log] [blame]
Austin Schuhdb2ed9d2022-12-26 14:02:26 -08001#include "absl/strings/str_cat.h"
2#include "absl/strings/str_split.h"
3#include "aos/events/shm_event_loop.h"
4#include "aos/init.h"
Austin Schuh9f164e92022-12-29 16:15:28 -08005#include "aos/realtime.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -08006#include "frc971/vision/media_device.h"
7#include "frc971/vision/v4l2_reader.h"
8
9DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
Ravago Jonesa0a2e062023-01-03 21:45:18 -080010DEFINE_bool(lowlight_camera, false, "Switch to use imx462 image sensor.");
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080011
Maxwell Hendersonad312342023-01-10 12:07:47 -080012namespace y2023 {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080013namespace vision {
14namespace {
15
16using namespace frc971::vision;
17
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080018void CameraReaderMain() {
19 std::optional<MediaDevice> media_device = FindMediaDevice("platform:rkisp1");
20
21 if (VLOG_IS_ON(1)) {
22 media_device->Log();
23 }
24
Ravago Jonesa0a2e062023-01-03 21:45:18 -080025 int width = 1296;
26 int height = 972;
27 int color_format = MEDIA_BUS_FMT_SBGGR10_1X10;
28 std::string camera_device_string = "ov5647 4-0036";
29 if (FLAGS_lowlight_camera) {
30 width = 1920;
31 height = 1080;
32 color_format = MEDIA_BUS_FMT_SRGGB10_1X10;
33 camera_device_string = "imx290 4-0036";
34 }
35
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080036 media_device->Reset();
37
Ravago Jonesa0a2e062023-01-03 21:45:18 -080038 Entity *camera = media_device->FindEntity(camera_device_string);
39 camera->pads()[0]->SetSubdevFormat(width, height, color_format);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080040
41 Entity *rkisp1_csi = media_device->FindEntity("rkisp1_csi");
Ravago Jonesa0a2e062023-01-03 21:45:18 -080042 rkisp1_csi->pads()[0]->SetSubdevFormat(width, height, color_format);
43 rkisp1_csi->pads()[1]->SetSubdevFormat(width, height, color_format);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080044
45 // TODO(austin): Should we set this on the link?
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080046 Entity *rkisp1_isp = media_device->FindEntity("rkisp1_isp");
Ravago Jonesa0a2e062023-01-03 21:45:18 -080047 rkisp1_isp->pads(0)->SetSubdevFormat(width, height, color_format);
48 rkisp1_isp->pads(0)->SetSubdevCrop(width, height);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080049
Ravago Jonesa0a2e062023-01-03 21:45:18 -080050 rkisp1_isp->pads(2)->SetSubdevFormat(width, height, MEDIA_BUS_FMT_YUYV8_2X8);
51 rkisp1_isp->pads(2)->SetSubdevCrop(width, height);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080052
53 Entity *rkisp1_resizer_selfpath =
54 media_device->FindEntity("rkisp1_resizer_selfpath");
Ravago Jonesa0a2e062023-01-03 21:45:18 -080055 rkisp1_resizer_selfpath->pads(0)->SetSubdevFormat(width, height,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080056 MEDIA_BUS_FMT_YUYV8_2X8);
Ravago Jonesa0a2e062023-01-03 21:45:18 -080057 rkisp1_resizer_selfpath->pads(1)->SetSubdevFormat(width, height,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080058 MEDIA_BUS_FMT_YUYV8_2X8);
Ravago Jonesa0a2e062023-01-03 21:45:18 -080059 rkisp1_resizer_selfpath->pads(0)->SetSubdevCrop(width, height);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080060
61 Entity *rkisp1_resizer_mainpath =
62 media_device->FindEntity("rkisp1_resizer_mainpath");
Ravago Jonesa0a2e062023-01-03 21:45:18 -080063 rkisp1_resizer_mainpath->pads(0)->SetSubdevFormat(width, height,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080064 MEDIA_BUS_FMT_YUYV8_2X8);
Ravago Jones65469be2023-01-13 21:28:23 -080065
Ravago Jonesa0a2e062023-01-03 21:45:18 -080066 rkisp1_resizer_mainpath->pads(0)->SetSubdevCrop(width, height);
67 rkisp1_resizer_mainpath->pads(1)->SetSubdevFormat(width / 2, height / 2,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080068 MEDIA_BUS_FMT_YUYV8_2X8);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080069
70 Entity *rkisp1_mainpath = media_device->FindEntity("rkisp1_mainpath");
Ravago Jonesa0a2e062023-01-03 21:45:18 -080071 rkisp1_mainpath->SetFormat(width / 2, height / 2, V4L2_PIX_FMT_YUV422P);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080072
73 Entity *rkisp1_selfpath = media_device->FindEntity("rkisp1_selfpath");
Ravago Jonesa0a2e062023-01-03 21:45:18 -080074 rkisp1_selfpath->SetFormat(width, height, V4L2_PIX_FMT_YUYV);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080075
Ravago Jones65469be2023-01-13 21:28:23 -080076 media_device->Enable(
Ravago Jonesa0a2e062023-01-03 21:45:18 -080077 media_device->FindLink(camera_device_string, 0, "rkisp1_csi", 0));
Ravago Jones65469be2023-01-13 21:28:23 -080078 media_device->Enable(
79 media_device->FindLink("rkisp1_csi", 1, "rkisp1_isp", 0));
80 media_device->Enable(
81 media_device->FindLink("rkisp1_isp", 2, "rkisp1_resizer_selfpath", 0));
82 media_device->Enable(
83 media_device->FindLink("rkisp1_isp", 2, "rkisp1_resizer_mainpath", 0));
84
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080085 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
86 aos::configuration::ReadConfig(FLAGS_config);
87
88 aos::ShmEventLoop event_loop(&config.message());
89
90 event_loop.SetRuntimeRealtimePriority(55);
Austin Schuh9f164e92022-12-29 16:15:28 -080091 event_loop.SetRuntimeAffinity(aos::MakeCpusetFromCpus({2}));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080092
Ravago Jones65469be2023-01-13 21:28:23 -080093 RockchipV4L2Reader v4l2_reader(&event_loop, event_loop.epoll(),
Ravago Jonesa0a2e062023-01-03 21:45:18 -080094 rkisp1_selfpath->device(), camera->device());
Ravago Jones65469be2023-01-13 21:28:23 -080095
Ravago Jonesa0a2e062023-01-03 21:45:18 -080096 if (FLAGS_lowlight_camera) {
97 v4l2_reader.SetGain(72);
98 v4l2_reader.SetExposure(30);
99 v4l2_reader.SetBlanking(2480, 45);
100 } else {
101 v4l2_reader.SetGainExt(1000);
102 v4l2_reader.SetExposure(1000);
103 }
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800104
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800105 event_loop.Run();
106}
107
108} // namespace
109} // namespace vision
Maxwell Hendersonad312342023-01-10 12:07:47 -0800110} // namespace y2023
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800111
112int main(int argc, char **argv) {
113 aos::InitGoogle(&argc, &argv);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800114 y2023::vision::CameraReaderMain();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800115}