camerad: add CameraConfig struct for initializing CameraState in constructor (#33065)

* Add CameraConfig struct for initializing CameraState in constructor

* init member variables
old-commit-hash: 75f49f84f3
This commit is contained in:
Dean Lee 2024-07-26 04:09:03 +08:00 committed by GitHub
parent 7ddd4a8e59
commit 5b5e3ef053
4 changed files with 97 additions and 66 deletions

View File

@ -324,7 +324,7 @@ void camerad_thread() {
#endif
{
MultiCameraState cameras = {};
MultiCameraState cameras;
VisionIpcServer vipc_server("camerad", device_id, context);
cameras_open(&cameras);

View File

@ -18,9 +18,6 @@ enum CameraType {
};
// for debugging
const bool env_disable_road = getenv("DISABLE_ROAD") != NULL;
const bool env_disable_wide_road = getenv("DISABLE_WIDE_ROAD") != NULL;
const bool env_disable_driver = getenv("DISABLE_DRIVER") != NULL;
const bool env_debug_frames = getenv("DEBUG_FRAMES") != NULL;
const bool env_log_raw_frames = getenv("LOG_RAW_FRAMES") != NULL;
const bool env_ctrl_exp_from_params = getenv("CTRL_EXP_FROM_PARAMS") != NULL;

View File

@ -26,6 +26,14 @@ const int MIPI_SETTLE_CNT = 33; // Calculated by camera_freqs.py
extern ExitHandler do_exit;
CameraState::CameraState(MultiCameraState *multi_camera_state, const CameraConfig &config)
: multi_cam_state(multi_camera_state),
camera_num(config.camera_num),
stream_type(config.stream_type),
focal_len(config.focal_len),
enabled(config.enabled) {
}
int CameraState::clear_req_queue() {
struct cam_req_mgr_flush_info req_mgr_flush_request = {0};
req_mgr_flush_request.session_hdl = session_handle;
@ -425,48 +433,38 @@ void CameraState::set_exposure_rect() {
}
void CameraState::sensor_set_parameters() {
target_grey_fraction = 0.3;
dc_gain_enabled = false;
dc_gain_weight = ci->dc_gain_min_weight;
gain_idx = ci->analog_gain_rec_idx;
exposure_time = 5;
cur_ev[0] = cur_ev[1] = cur_ev[2] = (1 + dc_gain_weight * (ci->dc_gain_factor-1) / ci->dc_gain_max_weight) * ci->sensor_analog_gains[gain_idx] * exposure_time;
}
void CameraState::camera_map_bufs(MultiCameraState *s) {
void CameraState::camera_map_bufs() {
for (int i = 0; i < FRAME_BUF_COUNT; i++) {
// configure ISP to put the image in place
struct cam_mem_mgr_map_cmd mem_mgr_map_cmd = {0};
mem_mgr_map_cmd.mmu_hdls[0] = s->device_iommu;
mem_mgr_map_cmd.mmu_hdls[0] = multi_cam_state->device_iommu;
mem_mgr_map_cmd.num_hdl = 1;
mem_mgr_map_cmd.flags = CAM_MEM_FLAG_HW_READ_WRITE;
mem_mgr_map_cmd.fd = buf.camera_bufs[i].fd;
int ret = do_cam_control(s->video0_fd, CAM_REQ_MGR_MAP_BUF, &mem_mgr_map_cmd, sizeof(mem_mgr_map_cmd));
int ret = do_cam_control(multi_cam_state->video0_fd, CAM_REQ_MGR_MAP_BUF, &mem_mgr_map_cmd, sizeof(mem_mgr_map_cmd));
LOGD("map buf req: (fd: %d) 0x%x %d", buf.camera_bufs[i].fd, mem_mgr_map_cmd.out.buf_handle, ret);
buf_handle[i] = mem_mgr_map_cmd.out.buf_handle;
}
enqueue_req_multi(1, FRAME_BUF_COUNT, 0);
}
void CameraState::camera_init(MultiCameraState *s, VisionIpcServer * v, cl_device_id device_id, cl_context ctx, VisionStreamType yuv_type, float focal_len) {
void CameraState::camera_init(VisionIpcServer * v, cl_device_id device_id, cl_context ctx) {
if (!enabled) return;
LOGD("camera init %d", camera_num);
request_id_last = 0;
skipped = true;
buf.init(device_id, ctx, this, v, FRAME_BUF_COUNT, yuv_type);
camera_map_bufs(s);
buf.init(device_id, ctx, this, v, FRAME_BUF_COUNT, stream_type);
camera_map_bufs();
fl_pix = focal_len / ci->pixel_size_mm;
set_exposure_rect();
}
void CameraState::camera_open(MultiCameraState *multi_cam_state_, int camera_num_, bool enabled_) {
multi_cam_state = multi_cam_state_;
camera_num = camera_num_;
enabled = enabled_;
void CameraState::camera_open() {
if (!enabled) return;
if (!openSensor()) {
@ -660,9 +658,9 @@ void CameraState::linkDevices() {
}
void cameras_init(VisionIpcServer *v, MultiCameraState *s, cl_device_id device_id, cl_context ctx) {
s->driver_cam.camera_init(s, v, device_id, ctx, VISION_STREAM_DRIVER, DRIVER_FL_MM);
s->road_cam.camera_init(s, v, device_id, ctx, VISION_STREAM_ROAD, ROAD_FL_MM);
s->wide_road_cam.camera_init(s, v, device_id, ctx, VISION_STREAM_WIDE_ROAD, WIDE_FL_MM);
s->driver_cam.camera_init(v, device_id, ctx);
s->road_cam.camera_init(v, device_id, ctx);
s->wide_road_cam.camera_init(v, device_id, ctx);
s->pm = new PubMaster({"roadCameraState", "driverCameraState", "wideRoadCameraState", "thumbnail"});
}
@ -706,11 +704,11 @@ void cameras_open(MultiCameraState *s) {
ret = HANDLE_EINTR(ioctl(s->video0_fd, VIDIOC_SUBSCRIBE_EVENT, &sub));
LOGD("req mgr subscribe: %d", ret);
s->driver_cam.camera_open(s, 2, !env_disable_driver);
s->driver_cam.camera_open();
LOGD("driver camera opened");
s->road_cam.camera_open(s, 1, !env_disable_road);
s->road_cam.camera_open();
LOGD("road camera opened");
s->wide_road_cam.camera_open(s, 0, !env_disable_wide_road);
s->wide_road_cam.camera_open();
LOGD("wide road camera opened");
}
@ -972,6 +970,12 @@ void process_road_camera(MultiCameraState *s, CameraState *c, int cnt) {
c->set_camera_exposure(set_exposure_target(b, c->ae_xywh, skip, skip));
}
MultiCameraState::MultiCameraState()
: driver_cam(this, DRIVER_CAMERA_CONFIG),
road_cam(this, ROAD_CAMERA_CONFIG),
wide_road_cam(this, WIDE_ROAD_CAMERA_CONFIG) {
}
void cameras_run(MultiCameraState *s) {
LOG("-- Starting threads");
std::vector<std::thread> threads;

View File

@ -11,67 +11,94 @@
#define FRAME_BUF_COUNT 4
#define ROAD_FL_MM 8.0f
#define WIDE_FL_MM 1.71f
#define DRIVER_FL_MM 1.71f
struct CameraConfig {
int camera_num;
VisionStreamType stream_type;
float focal_len; // millimeters
bool enabled;
};
const CameraConfig WIDE_ROAD_CAMERA_CONFIG = {
.camera_num = 0,
.stream_type = VISION_STREAM_WIDE_ROAD,
.focal_len = 1.71,
.enabled = !getenv("DISABLE_WIDE_ROAD"),
};
const CameraConfig ROAD_CAMERA_CONFIG = {
.camera_num = 1,
.stream_type = VISION_STREAM_ROAD,
.focal_len = 8.0,
.enabled = !getenv("DISABLE_ROAD"),
};
const CameraConfig DRIVER_CAMERA_CONFIG = {
.camera_num = 2,
.stream_type = VISION_STREAM_DRIVER,
.focal_len = 1.71,
.enabled = !getenv("DISABLE_DRIVER"),
};
class CameraState {
public:
MultiCameraState *multi_cam_state;
MultiCameraState *multi_cam_state = nullptr;
std::unique_ptr<const SensorInfo> ci;
bool enabled;
bool enabled = true;
VisionStreamType stream_type;
float focal_len = 0;
std::mutex exp_lock;
int exposure_time;
bool dc_gain_enabled;
int dc_gain_weight;
int gain_idx;
float analog_gain_frac;
int exposure_time = 5;
bool dc_gain_enabled = false;
int dc_gain_weight = 0;
int gain_idx = 0;
float analog_gain_frac = 0;
float cur_ev[3];
float best_ev_score;
int new_exp_g;
int new_exp_t;
float cur_ev[3] = {};
float best_ev_score = 0;
int new_exp_g = 0;
int new_exp_t = 0;
Rect ae_xywh;
float measured_grey_fraction;
float target_grey_fraction;
Rect ae_xywh = {};
float measured_grey_fraction = 0;
float target_grey_fraction = 0.3;
unique_fd sensor_fd;
unique_fd csiphy_fd;
int camera_num;
float fl_pix;
int camera_num = 0;
float fl_pix = 0;
CameraState(MultiCameraState *multi_camera_state, const CameraConfig &config);
void handle_camera_event(void *evdat);
void update_exposure_score(float desired_ev, int exp_t, int exp_g_idx, float exp_gain);
void set_camera_exposure(float grey_frac);
void sensors_start();
void camera_open(MultiCameraState *multi_cam_state, int camera_num, bool enabled);
void camera_open();
void set_exposure_rect();
void sensor_set_parameters();
void camera_map_bufs(MultiCameraState *s);
void camera_init(MultiCameraState *s, VisionIpcServer *v, cl_device_id device_id, cl_context ctx, VisionStreamType yuv_type, float focal_len);
void camera_map_bufs();
void camera_init(VisionIpcServer *v, cl_device_id device_id, cl_context ctx);
void camera_close();
int32_t session_handle;
int32_t sensor_dev_handle;
int32_t isp_dev_handle;
int32_t csiphy_dev_handle;
int32_t session_handle = -1;
int32_t sensor_dev_handle = -1;
int32_t isp_dev_handle = -1;
int32_t csiphy_dev_handle = -1;
int32_t link_handle;
int32_t link_handle = -1;
int buf0_handle;
int buf_handle[FRAME_BUF_COUNT];
int sync_objs[FRAME_BUF_COUNT];
int request_ids[FRAME_BUF_COUNT];
int request_id_last;
int frame_id_last;
int idx_offset;
bool skipped;
int buf0_handle = 0;
int buf_handle[FRAME_BUF_COUNT] = {};
int sync_objs[FRAME_BUF_COUNT] = {};
int request_ids[FRAME_BUF_COUNT] = {};
int request_id_last = 0;
int frame_id_last = 0;
int idx_offset = 0;
bool skipped = true;
CameraBuf buf;
MemoryManager mm;
@ -95,16 +122,19 @@ private:
Params params;
};
typedef struct MultiCameraState {
class MultiCameraState {
public:
MultiCameraState();
unique_fd video0_fd;
unique_fd cam_sync_fd;
unique_fd isp_fd;
int device_iommu;
int cdm_iommu;
int device_iommu = -1;
int cdm_iommu = -1;
CameraState road_cam;
CameraState wide_road_cam;
CameraState driver_cam;
PubMaster *pm;
} MultiCameraState;
};