blob: 7de3eb5aafacc03c5fc9f21a32983bd251b26748 [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.");
15
16DEFINE_double(red, 1.252, "Red gain");
17DEFINE_double(green, 1, "Green gain");
18DEFINE_double(blue, 1.96, "Blue gain");
Ravago Jones0f2e3a12023-01-29 16:13:29 -080019DEFINE_double(exposure, 150, "Camera exposure");
milind-ufd08c432023-02-05 15:15:21 -080020DEFINE_bool(send_downsized_images, false,
21 "Whether to send downsized image for driver cam streaming.");
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080022
Maxwell Hendersonad312342023-01-10 12:07:47 -080023namespace y2023 {
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080024namespace vision {
25namespace {
26
27using namespace frc971::vision;
28
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080029void CameraReaderMain() {
30 std::optional<MediaDevice> media_device = FindMediaDevice("platform:rkisp1");
31
32 if (VLOG_IS_ON(1)) {
33 media_device->Log();
34 }
35
milind-ufd08c432023-02-05 15:15:21 -080036 const int kWidth = (FLAGS_lowlight_camera ? 1920 : 1296);
37 const int kHeight = (FLAGS_lowlight_camera ? 1080 : 972);
38 const int kColorFormat = (FLAGS_lowlight_camera ? MEDIA_BUS_FMT_SRGGB10_1X10
39 : MEDIA_BUS_FMT_SBGGR10_1X10);
40
41 const std::string_view kCameraDeviceString =
42 (FLAGS_lowlight_camera ? "arducam-pivariety 4-000c" : "ov5647 4-0036");
43
44 // Scale down the selfpath images so we can log at 30 Hz (but still detect
45 // april tags at a far enough distance)
46 const double kSelfpathScalar = 2.0 / 3.0;
47 const int kSelfpathWidth = kWidth * kSelfpathScalar;
48 const int kSelfpathHeight = kHeight * kSelfpathScalar;
49
50 // Send heavily downsized images to the drivercam. They go over the network,
51 // and in this case frame rate is more important than quality
52 constexpr int kMainpathWidth = 640;
53 constexpr int kMainpathHeight = 480;
Ravago Jonesa0a2e062023-01-03 21:45:18 -080054
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080055 media_device->Reset();
56
milind-ufd08c432023-02-05 15:15:21 -080057 Entity *camera = media_device->FindEntity(kCameraDeviceString);
58 camera->pads()[0]->SetSubdevFormat(kWidth, kHeight, kColorFormat);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080059
60 Entity *rkisp1_csi = media_device->FindEntity("rkisp1_csi");
milind-ufd08c432023-02-05 15:15:21 -080061 rkisp1_csi->pads()[0]->SetSubdevFormat(kWidth, kHeight, kColorFormat);
62 rkisp1_csi->pads()[1]->SetSubdevFormat(kWidth, kHeight, kColorFormat);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080063
64 // TODO(austin): Should we set this on the link?
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080065 Entity *rkisp1_isp = media_device->FindEntity("rkisp1_isp");
milind-ufd08c432023-02-05 15:15:21 -080066 rkisp1_isp->pads(0)->SetSubdevFormat(kWidth, kHeight, kColorFormat);
67 rkisp1_isp->pads(0)->SetSubdevCrop(kWidth, kHeight);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080068
milind-ufd08c432023-02-05 15:15:21 -080069 rkisp1_isp->pads(2)->SetSubdevFormat(kWidth, kHeight,
70 MEDIA_BUS_FMT_YUYV8_2X8);
71 rkisp1_isp->pads(2)->SetSubdevCrop(kWidth, kHeight);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080072
73 Entity *rkisp1_resizer_selfpath =
74 media_device->FindEntity("rkisp1_resizer_selfpath");
milind-ufd08c432023-02-05 15:15:21 -080075 rkisp1_resizer_selfpath->pads(0)->SetSubdevFormat(kWidth, kHeight,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080076 MEDIA_BUS_FMT_YUYV8_2X8);
Ravago Jonese3182ee2023-01-28 22:33:05 -080077 rkisp1_resizer_selfpath->pads(1)->SetSubdevFormat(
milind-ufd08c432023-02-05 15:15:21 -080078 kSelfpathWidth, kSelfpathHeight, MEDIA_BUS_FMT_YUYV8_2X8);
79 rkisp1_resizer_selfpath->pads(0)->SetSubdevCrop(kWidth, kHeight);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080080
81 Entity *rkisp1_resizer_mainpath =
82 media_device->FindEntity("rkisp1_resizer_mainpath");
milind-ufd08c432023-02-05 15:15:21 -080083 rkisp1_resizer_mainpath->pads(0)->SetSubdevFormat(kWidth, kHeight,
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080084 MEDIA_BUS_FMT_YUYV8_2X8);
Ravago Jones65469be2023-01-13 21:28:23 -080085
milind-ufd08c432023-02-05 15:15:21 -080086 rkisp1_resizer_mainpath->pads(0)->SetSubdevCrop(kWidth, kHeight);
87 rkisp1_resizer_mainpath->pads(1)->SetSubdevFormat(
88 kMainpathWidth, kMainpathHeight, MEDIA_BUS_FMT_YUYV8_2X8);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080089
90 Entity *rkisp1_mainpath = media_device->FindEntity("rkisp1_mainpath");
milind-ufd08c432023-02-05 15:15:21 -080091 rkisp1_mainpath->SetFormat(kMainpathWidth, kMainpathHeight,
92 V4L2_PIX_FMT_YUYV);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080093
94 Entity *rkisp1_selfpath = media_device->FindEntity("rkisp1_selfpath");
milind-ufd08c432023-02-05 15:15:21 -080095 rkisp1_selfpath->SetFormat(kSelfpathWidth, kSelfpathHeight,
96 V4L2_PIX_FMT_YUYV);
Austin Schuhdb2ed9d2022-12-26 14:02:26 -080097
Ravago Jones65469be2023-01-13 21:28:23 -080098 media_device->Enable(
milind-ufd08c432023-02-05 15:15:21 -080099 media_device->FindLink(kCameraDeviceString, 0, "rkisp1_csi", 0));
Ravago Jones65469be2023-01-13 21:28:23 -0800100 media_device->Enable(
101 media_device->FindLink("rkisp1_csi", 1, "rkisp1_isp", 0));
102 media_device->Enable(
103 media_device->FindLink("rkisp1_isp", 2, "rkisp1_resizer_selfpath", 0));
104 media_device->Enable(
105 media_device->FindLink("rkisp1_isp", 2, "rkisp1_resizer_mainpath", 0));
106
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800107 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
108 aos::configuration::ReadConfig(FLAGS_config);
109
110 aos::ShmEventLoop event_loop(&config.message());
111
112 event_loop.SetRuntimeRealtimePriority(55);
Austin Schuh9f164e92022-12-29 16:15:28 -0800113 event_loop.SetRuntimeAffinity(aos::MakeCpusetFromCpus({2}));
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800114
milind-ufd08c432023-02-05 15:15:21 -0800115 // Reader for vision processing
116 RockchipV4L2Reader v4l2_reader_selfpath(&event_loop, event_loop.epoll(),
117 rkisp1_selfpath->device(),
118 camera->device());
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800119 if (FLAGS_lowlight_camera) {
milind-ufd08c432023-02-05 15:15:21 -0800120 v4l2_reader_selfpath.SetGainExt(100);
121 v4l2_reader_selfpath.SetVerticalBlanking(1000);
122 v4l2_reader_selfpath.SetExposure(FLAGS_exposure);
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800123 } else {
milind-ufd08c432023-02-05 15:15:21 -0800124 v4l2_reader_selfpath.SetGainExt(1000);
125 v4l2_reader_selfpath.SetExposure(1000);
126 }
127
128 std::unique_ptr<RockchipV4L2Reader> v4l2_reader_mainpath;
129 if (FLAGS_send_downsized_images) {
130 // Reader for driver cam streaming on logger pi, sending lower res images
131 v4l2_reader_mainpath = std::make_unique<RockchipV4L2Reader>(
132 &event_loop, event_loop.epoll(), rkisp1_mainpath->device(),
133 camera->device(), "/camera/downsized");
Ravago Jonesa0a2e062023-01-03 21:45:18 -0800134 }
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800135
Ravago Jonese8700072023-01-14 19:41:56 -0800136 {
137 Entity *rkisp1_params = media_device->FindEntity("rkisp1_params");
138
139 LOG(INFO) << "Opening " << rkisp1_params->device();
140 aos::ScopedFD fd(open(rkisp1_params->device().c_str(), O_RDWR));
141 PCHECK(fd >= 0);
142
143 struct v4l2_capability capability;
144 memset(&capability, 0, sizeof(capability));
145 PCHECK(ioctl(fd.get(), VIDIOC_QUERYCAP, &capability) == 0);
146 CHECK(capability.device_caps & V4L2_CAP_META_OUTPUT);
147
148 // V4L2_META_FMT_RK_ISP1_PARAMS
149 // RK1P
150 uint32_t meta_params_format = (uint32_t)('R') | ((uint32_t)('K') << 8) |
151 ((uint32_t)('1') << 16) |
152 ((uint32_t)('P') << 24);
153 struct v4l2_format format;
154 std::memset(&format, 0, sizeof(format));
155 format.type = V4L2_BUF_TYPE_META_OUTPUT;
156
157 PCHECK(ioctl(fd.get(), VIDIOC_G_FMT, &format) == 0);
158 CHECK_EQ(format.fmt.meta.buffersize, 3048ul);
159 CHECK_EQ(format.fmt.meta.dataformat, meta_params_format);
160
161 struct v4l2_requestbuffers request;
162 memset(&request, 0, sizeof(request));
163 request.count = 1;
164 request.type = V4L2_BUF_TYPE_META_OUTPUT;
165 request.memory = V4L2_MEMORY_USERPTR;
166 PCHECK(ioctl(fd.get(), VIDIOC_REQBUFS, &request) == 0);
167
168 struct rkisp1_params_cfg configuration;
169 memset(&configuration, 0, sizeof(configuration));
170
171 configuration.module_cfg_update |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
172
173 configuration.others.awb_gain_config.gain_red = 256 * FLAGS_red;
174 configuration.others.awb_gain_config.gain_green_r = 256 * FLAGS_green;
175 configuration.others.awb_gain_config.gain_blue = 256 * FLAGS_blue;
176 configuration.others.awb_gain_config.gain_green_b = 256 * FLAGS_green;
177
178 // Enable the AWB gains
179 configuration.module_en_update |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
180 configuration.module_ens |= RKISP1_CIF_ISP_MODULE_AWB_GAIN;
181
182 struct v4l2_buffer buffer;
183 memset(&buffer, 0, sizeof(buffer));
184 buffer.memory = V4L2_MEMORY_USERPTR;
185 buffer.index = 0;
186 buffer.type = V4L2_BUF_TYPE_META_OUTPUT;
187 buffer.m.userptr = reinterpret_cast<uintptr_t>(&configuration);
188 buffer.length = format.fmt.meta.buffersize;
189
190 int type = V4L2_BUF_TYPE_META_OUTPUT;
191 PCHECK(ioctl(fd.get(), VIDIOC_STREAMON, &type) == 0);
192
193 PCHECK(ioctl(fd.get(), VIDIOC_QBUF, &buffer) == 0);
194 CHECK(buffer.flags & V4L2_BUF_FLAG_QUEUED);
195
196 PCHECK(ioctl(fd.get(), VIDIOC_DQBUF, &buffer) == 0);
197 }
198
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800199 event_loop.Run();
200}
201
202} // namespace
203} // namespace vision
Maxwell Hendersonad312342023-01-10 12:07:47 -0800204} // namespace y2023
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800205
206int main(int argc, char **argv) {
207 aos::InitGoogle(&argc, &argv);
Maxwell Hendersonad312342023-01-10 12:07:47 -0800208 y2023::vision::CameraReaderMain();
Austin Schuhdb2ed9d2022-12-26 14:02:26 -0800209}