blob: 3001c047ae599a041826eaee13b6152606abb8b1 [file] [log] [blame]
Ravago Jonese8700072023-01-14 19:41:56 -08001#include <linux/videodev2.h>
2#include <sys/ioctl.h>
3
Austin Schuhdb2ed9d2022-12-26 14:02:26 -08004#include "absl/strings/str_cat.h"
5#include "absl/strings/str_split.h"
6#include "aos/events/shm_event_loop.h"
7#include "aos/init.h"
Austin Schuh9f164e92022-12-29 16:15:28 -08008#include "aos/realtime.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -08009#include "frc971/vision/media_device.h"
10#include "frc971/vision/v4l2_reader.h"
Ravago Jonese8700072023-01-14 19:41:56 -080011#include "y2023/vision/rkisp1-config.h"
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080012
13DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
Ravago Jonese8700072023-01-14 19:41:56 -080014DEFINE_bool(lowlight_camera, true, "Switch to use imx462 image sensor.");
milind-u50c9a582023-05-03 20:05:22 -070015DEFINE_int32(gain, 150, "analogue_gain");
Ravago Jonese8700072023-01-14 19:41:56 -080016
17DEFINE_double(red, 1.252, "Red gain");
18DEFINE_double(green, 1, "Green gain");
19DEFINE_double(blue, 1.96, "Blue gain");
Ravago Jones0f2e3a12023-01-29 16:13:29 -080020DEFINE_double(exposure, 150, "Camera exposure");
milind-ufd08c432023-02-05 15:15:21 -080021DEFINE_bool(send_downsized_images, false,
22 "Whether to send downsized image for driver cam streaming.");
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080023
Maxwell Hendersonad312342023-01-10 12:07:47 -080024namespace y2023 {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080025namespace vision {
26namespace {
27
28using namespace frc971::vision;
29
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080030void CameraReaderMain() {
31 std::optional<MediaDevice> media_device = FindMediaDevice("platform:rkisp1");
32
33 if (VLOG_IS_ON(1)) {
34 media_device->Log();
35 }
36
milind-ufd08c432023-02-05 15:15:21 -080037 const int kWidth = (FLAGS_lowlight_camera ? 1920 : 1296);
38 const int kHeight = (FLAGS_lowlight_camera ? 1080 : 972);
39 const int kColorFormat = (FLAGS_lowlight_camera ? MEDIA_BUS_FMT_SRGGB10_1X10
40 : MEDIA_BUS_FMT_SBGGR10_1X10);
41
42 const std::string_view kCameraDeviceString =
43 (FLAGS_lowlight_camera ? "arducam-pivariety 4-000c" : "ov5647 4-0036");
44
45 // Scale down the selfpath images so we can log at 30 Hz (but still detect
46 // april tags at a far enough distance)
47 const double kSelfpathScalar = 2.0 / 3.0;
48 const int kSelfpathWidth = kWidth * kSelfpathScalar;
49 const int kSelfpathHeight = kHeight * kSelfpathScalar;
50
51 // Send heavily downsized images to the drivercam. They go over the network,
52 // and in this case frame rate is more important than quality
53 constexpr int kMainpathWidth = 640;
54 constexpr int kMainpathHeight = 480;
Ravago Jonesa0a2e062023-01-03 21:45:18 -080055
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080056 media_device->Reset();
57
milind-ufd08c432023-02-05 15:15:21 -080058 Entity *camera = media_device->FindEntity(kCameraDeviceString);
59 camera->pads()[0]->SetSubdevFormat(kWidth, kHeight, kColorFormat);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080060
61 Entity *rkisp1_csi = media_device->FindEntity("rkisp1_csi");
milind-ufd08c432023-02-05 15:15:21 -080062 rkisp1_csi->pads()[0]->SetSubdevFormat(kWidth, kHeight, kColorFormat);
63 rkisp1_csi->pads()[1]->SetSubdevFormat(kWidth, kHeight, kColorFormat);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080064
65 // TODO(austin): Should we set this on the link?
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080066 Entity *rkisp1_isp = media_device->FindEntity("rkisp1_isp");
milind-ufd08c432023-02-05 15:15:21 -080067 rkisp1_isp->pads(0)->SetSubdevFormat(kWidth, kHeight, kColorFormat);
68 rkisp1_isp->pads(0)->SetSubdevCrop(kWidth, kHeight);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080069
milind-ufd08c432023-02-05 15:15:21 -080070 rkisp1_isp->pads(2)->SetSubdevFormat(kWidth, kHeight,
71 MEDIA_BUS_FMT_YUYV8_2X8);
72 rkisp1_isp->pads(2)->SetSubdevCrop(kWidth, kHeight);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080073
74 Entity *rkisp1_resizer_selfpath =
75 media_device->FindEntity("rkisp1_resizer_selfpath");
milind-ufd08c432023-02-05 15:15:21 -080076 rkisp1_resizer_selfpath->pads(0)->SetSubdevFormat(kWidth, kHeight,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080077 MEDIA_BUS_FMT_YUYV8_2X8);
Ravago Jonese3182ee2023-01-28 22:33:05 -080078 rkisp1_resizer_selfpath->pads(1)->SetSubdevFormat(
milind-ufd08c432023-02-05 15:15:21 -080079 kSelfpathWidth, kSelfpathHeight, MEDIA_BUS_FMT_YUYV8_2X8);
80 rkisp1_resizer_selfpath->pads(0)->SetSubdevCrop(kWidth, kHeight);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080081
82 Entity *rkisp1_resizer_mainpath =
83 media_device->FindEntity("rkisp1_resizer_mainpath");
milind-ufd08c432023-02-05 15:15:21 -080084 rkisp1_resizer_mainpath->pads(0)->SetSubdevFormat(kWidth, kHeight,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080085 MEDIA_BUS_FMT_YUYV8_2X8);
Ravago Jones65469be2023-01-13 21:28:23 -080086
milind-ufd08c432023-02-05 15:15:21 -080087 rkisp1_resizer_mainpath->pads(0)->SetSubdevCrop(kWidth, kHeight);
88 rkisp1_resizer_mainpath->pads(1)->SetSubdevFormat(
89 kMainpathWidth, kMainpathHeight, MEDIA_BUS_FMT_YUYV8_2X8);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080090
91 Entity *rkisp1_mainpath = media_device->FindEntity("rkisp1_mainpath");
milind-ufd08c432023-02-05 15:15:21 -080092 rkisp1_mainpath->SetFormat(kMainpathWidth, kMainpathHeight,
93 V4L2_PIX_FMT_YUYV);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080094
95 Entity *rkisp1_selfpath = media_device->FindEntity("rkisp1_selfpath");
milind-ufd08c432023-02-05 15:15:21 -080096 rkisp1_selfpath->SetFormat(kSelfpathWidth, kSelfpathHeight,
97 V4L2_PIX_FMT_YUYV);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080098
Ravago Jones65469be2023-01-13 21:28:23 -080099 media_device->Enable(
milind-ufd08c432023-02-05 15:15:21 -0800100 media_device->FindLink(kCameraDeviceString, 0, "rkisp1_csi", 0));
Ravago Jones65469be2023-01-13 21:28:23 -0800101 media_device->Enable(
102 media_device->FindLink("rkisp1_csi", 1, "rkisp1_isp", 0));
103 media_device->Enable(
104 media_device->FindLink("rkisp1_isp", 2, "rkisp1_resizer_selfpath", 0));
105 media_device->Enable(
106 media_device->FindLink("rkisp1_isp", 2, "rkisp1_resizer_mainpath", 0));
107
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800108 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
109 aos::configuration::ReadConfig(FLAGS_config);
110
111 aos::ShmEventLoop event_loop(&config.message());
112
113 event_loop.SetRuntimeRealtimePriority(55);
Austin Schuh9f164e92022-12-29 16:15:28 -0800114 event_loop.SetRuntimeAffinity(aos::MakeCpusetFromCpus({2}));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800115
milind-ufd08c432023-02-05 15:15:21 -0800116 // Reader for vision processing
117 RockchipV4L2Reader v4l2_reader_selfpath(&event_loop, event_loop.epoll(),
118 rkisp1_selfpath->device(),
119 camera->device());
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800120 if (FLAGS_lowlight_camera) {
Austin Schuh83c2c9c2023-03-22 20:27:24 -0700121 v4l2_reader_selfpath.SetGainExt(FLAGS_gain);
milind-ufd08c432023-02-05 15:15:21 -0800122 v4l2_reader_selfpath.SetVerticalBlanking(1000);
123 v4l2_reader_selfpath.SetExposure(FLAGS_exposure);
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800124 } else {
milind-ufd08c432023-02-05 15:15:21 -0800125 v4l2_reader_selfpath.SetGainExt(1000);
126 v4l2_reader_selfpath.SetExposure(1000);
127 }
128
129 std::unique_ptr<RockchipV4L2Reader> v4l2_reader_mainpath;
130 if (FLAGS_send_downsized_images) {
131 // Reader for driver cam streaming on logger pi, sending lower res images
132 v4l2_reader_mainpath = std::make_unique<RockchipV4L2Reader>(
133 &event_loop, event_loop.epoll(), rkisp1_mainpath->device(),
134 camera->device(), "/camera/downsized");
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800135 }
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800136
Ravago Jonese8700072023-01-14 19:41:56 -0800137 {
138 Entity *rkisp1_params = media_device->FindEntity("rkisp1_params");
139
140 LOG(INFO) << "Opening " << rkisp1_params->device();
141 aos::ScopedFD fd(open(rkisp1_params->device().c_str(), O_RDWR));
142 PCHECK(fd >= 0);
143
144 struct v4l2_capability capability;
145 memset(&capability, 0, sizeof(capability));
146 PCHECK(ioctl(fd.get(), VIDIOC_QUERYCAP, &capability) == 0);
147 CHECK(capability.device_caps & V4L2_CAP_META_OUTPUT);
148
149 // V4L2_META_FMT_RK_ISP1_PARAMS
150 // RK1P
151 uint32_t meta_params_format = (uint32_t)('R') | ((uint32_t)('K') << 8) |
152 ((uint32_t)('1') << 16) |
153 ((uint32_t)('P') << 24);
154 struct v4l2_format format;
155 std::memset(&format, 0, sizeof(format));
156 format.type = V4L2_BUF_TYPE_META_OUTPUT;
157
158 PCHECK(ioctl(fd.get(), VIDIOC_G_FMT, &format) == 0);
159 CHECK_EQ(format.fmt.meta.buffersize, 3048ul);
160 CHECK_EQ(format.fmt.meta.dataformat, meta_params_format);
161
162 struct v4l2_requestbuffers request;
163 memset(&request, 0, sizeof(request));
164 request.count = 1;
165 request.type = V4L2_BUF_TYPE_META_OUTPUT;
166 request.memory = V4L2_MEMORY_USERPTR;
167 PCHECK(ioctl(fd.get(), VIDIOC_REQBUFS, &request) == 0);
168
169 struct rkisp1_params_cfg configuration;
170 memset(&configuration, 0, sizeof(configuration));
171
172 configuration.module_cfg_update |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
173
174 configuration.others.awb_gain_config.gain_red = 256 * FLAGS_red;
175 configuration.others.awb_gain_config.gain_green_r = 256 * FLAGS_green;
176 configuration.others.awb_gain_config.gain_blue = 256 * FLAGS_blue;
177 configuration.others.awb_gain_config.gain_green_b = 256 * FLAGS_green;
178
179 // Enable the AWB gains
180 configuration.module_en_update |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
181 configuration.module_ens |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
182
183 struct v4l2_buffer buffer;
184 memset(&buffer, 0, sizeof(buffer));
185 buffer.memory = V4L2_MEMORY_USERPTR;
186 buffer.index = 0;
187 buffer.type = V4L2_BUF_TYPE_META_OUTPUT;
188 buffer.m.userptr = reinterpret_cast<uintptr_t>(&configuration);
189 buffer.length = format.fmt.meta.buffersize;
190
191 int type = V4L2_BUF_TYPE_META_OUTPUT;
192 PCHECK(ioctl(fd.get(), VIDIOC_STREAMON, &type) == 0);
193
194 PCHECK(ioctl(fd.get(), VIDIOC_QBUF, &buffer) == 0);
195 CHECK(buffer.flags & V4L2_BUF_FLAG_QUEUED);
196
197 PCHECK(ioctl(fd.get(), VIDIOC_DQBUF, &buffer) == 0);
198 }
199
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800200 event_loop.Run();
201}
202
203} // namespace
204} // namespace vision
Maxwell Hendersonad312342023-01-10 12:07:47 -0800205} // namespace y2023
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800206
207int main(int argc, char **argv) {
208 aos::InitGoogle(&argc, &argv);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800209 y2023::vision::CameraReaderMain();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800210}