Skip to content

Commit

Permalink
identify camera by id not by video channel; make ui respond to window…
Browse files Browse the repository at this point in the history
… resolution; add parameters for RoadUsbCameraID and DriverUSBCameraID;
  • Loading branch information
BogGyver committed Apr 12, 2020
1 parent fbaa2ec commit f9aa22b
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 25 deletions.
2 changes: 2 additions & 0 deletions common/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ class UnknownKeyName(Exception):
"Offroad_PandaFirmwareMismatch": [TxType.CLEAR_ON_MANAGER_START, TxType.CLEAR_ON_PANDA_DISCONNECT],
"Offroad_InvalidTime": [TxType.CLEAR_ON_MANAGER_START],
"Offroad_IsTakingSnapshot": [TxType.CLEAR_ON_MANAGER_START],
"DriverUsbCameraID": [TxType.PERSISTENT],
"RoadUsbCameraID": [TxType.PERSISTENT],
}


Expand Down
18 changes: 12 additions & 6 deletions selfdrive/camerad/cameras/camera_webcam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <opencv2/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include "common/params.h"

extern volatile sig_atomic_t do_exit;
#define FRAME_WIDTH 1164
Expand All @@ -37,16 +38,16 @@ void camera_release_buffer(void *cookie, int buf_idx) {
}


void open_gl_stream_def(CameraState * s, int video_id, int width, int height, char ** strm_def) {
void open_gl_stream_def(CameraState * s, char* camera_id, int width, int height, char ** strm_def) {
printf("OPENGLSTREAM");
std::string strm_template="v4l2src device=/dev/video%d ! video/x-raw,width=%d,height=%d,framerate=%d/1,format=YUY2 !"
std::string strm_template="v4l2src device=/dev/v4l/by-id/%s ! video/x-raw,width=%d,height=%d,framerate=%d/1,format=YUY2 !"
" nvvidconv ! video/x-raw(memory:NVMM),format=I420 !"
" nvvidconv ! video/x-raw,format=BGRx !"
" videoconvert ! video/x-raw,format=BGR !"
" videoscale ! video/x-raw,width=%d,height=%d !"
" appsink ";
* strm_def = (char*)calloc(300,1);
sprintf(*strm_def,strm_template.c_str(),video_id, width, height, s->fps, s->ci.frame_width, s->ci.frame_height);
* strm_def = (char*)calloc(600,1);
sprintf(*strm_def,strm_template.c_str(),camera_id, width, height, s->fps, s->ci.frame_width, s->ci.frame_height);
printf(" GL Stream :[%s]\n",*strm_def);
}

Expand All @@ -67,7 +68,9 @@ static void* rear_thread(void *arg) {
CameraState* s = (CameraState*)arg;
char * strm_def;
printf("open_GL");
open_gl_stream_def(s,1, 800, 600, &strm_def);
char * cameraId_value;
const int result = read_db_value(NULL, "RoadUsbCameraID", &cameraId_value, NULL);
open_gl_stream_def(s,cameraId_value, 800, 600, &strm_def);
cv::VideoCapture cap_rear(strm_def); // road
free(strm_def);

Expand Down Expand Up @@ -122,7 +125,10 @@ void front_thread(CameraState *s) {
int err;
printf("OPEN FRONT");
char * strm_def;
open_gl_stream_def(s,0,640,480, &strm_def);
char * cameraId_value;
const int result = read_db_value(NULL, "DriverUsbCameraID", &cameraId_value, NULL);
if (result != 0) return;
open_gl_stream_def(s,cameraId_value,640,480, &strm_def);
cv::VideoCapture cap_front(strm_def); // driver
free(strm_def);

Expand Down
2 changes: 2 additions & 0 deletions selfdrive/car/tesla/carstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ def __init__(self, CP):
self.ldwNumbPeriod = 1.5
self.tapBlinkerExtension = 2
self.ahbOffDuration = 5
self.roadCameraID = ""
self.driverCameraID = ""
#read config file
read_config_file(self)
### END OF MAIN CONFIG OPTIONS ###
Expand Down
35 changes: 35 additions & 0 deletions selfdrive/car/tesla/readconfig.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import configparser
from common.params import Params
import subprocess

default_config_file_path = '/data/bb_openpilot.cfg'

Expand All @@ -10,6 +12,7 @@ class ConfigFile():
def read(self, into, config_path):
configr = configparser.RawConfigParser()
file_changed = False
params = Params()

try:
configr.read(config_path)
Expand All @@ -22,10 +25,12 @@ def read(self, into, config_path):

main_section = 'OP_CONFIG'
pref_section = 'OP_PREFERENCES'
jetson_section = 'JETSON_PREFERENCES'
logging_section = 'LOGGING'
config = configparser.RawConfigParser(allow_no_value=True)
config.add_section(main_section)
config.add_section(pref_section)
config.add_section(jetson_section)
config.add_section(logging_section)

#user_handle -> userHandle
Expand Down Expand Up @@ -217,6 +222,7 @@ def read(self, into, config_path):
comment = 'If you use an aftermarket Tesla Bosch Radar that already has a coded VIN, you will have to enter that VIN value here.'
)
file_changed |= didUpdate

if into.radarVIN == '':
into.radarVIN = default_radar_vin
file_changed = True
Expand Down Expand Up @@ -338,6 +344,33 @@ def read(self, into, config_path):
)
file_changed |= didUpdate

#jetson_road_camera_id -> roadCameraID
into.roadCameraID, didUpdate = self.read_config_entry(
config, configr, prev_file_contents, section = jetson_section,
entry = 'jetson_road_camera_id', entry_type = str,
default_value = 'NotSet',
comment = 'ID of camera facing road, as seen in ls -al /dev/v4l/by-id'
)
file_changed |= didUpdate

#jetson_driver_camera_id -> driverCameraID
into.driverCameraID, didUpdate = self.read_config_entry(
config, configr, prev_file_contents, section = jetson_section,
entry = 'jetson_driver_camera_id', entry_type = str,
default_value = 'NotSet',
comment = 'ID of camera facing driver, as seen in ls -al /dev/v4l/by-id'
)
file_changed |= didUpdate

#check camera_id values against LiveParams
savedRoadCameraID = params.get("RoadUsbCameraID")
savedDriverCameraID = params.get("DriverUsbCameraID")
if into.driverCameraID != savedDriverCameraID:
params.put("DriverUsbCameraID",into.driverCameraID)
if into.roadCameraID != savedRoadCameraID:
params.put("RoadUsbCameraID",into.roadCameraID)


into.shouldLogCanErrors, didUpdate = self.read_config_entry(
config, configr, prev_file_contents, section = logging_section,
entry = 'should_log_can_errors', entry_type = bool,
Expand Down Expand Up @@ -427,6 +460,8 @@ class CarSettings():
tapBlinkerExtension = None
ahbOffDuration = None
usesApillarHarness = None
roadCameraID = None
driverCameraID = None

def __init__(self, optional_config_file_path = default_config_file_path):
config_file = ConfigFile()
Expand Down
3 changes: 1 addition & 2 deletions selfdrive/car/tesla/readconfig.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/bash
CFG_FILE=/data/bb_openpilot.cfg
CFG_CONTENT=$(cat $CFG_FILE | sed -r '/[^=]+=[^=]+/!d' | sed -r 's/\s+=\s/=/g')
eval "$CFG_CONTENT"
eval "export $CFG_CONTENT"
2 changes: 1 addition & 1 deletion selfdrive/ui/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if larch == "aarch64":
linkflags = ['-Wl,-rpath=/system/lib64,-rpath=/system/comma/usr/lib']
else:
src += ['linux.cc']
libs += ['pthread', 'glfw']
libs += ['pthread', 'glfw', 'X11']
linkflags = []

env.Program('_ui', src,
Expand Down
33 changes: 33 additions & 0 deletions selfdrive/ui/bbui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

#include "cereal/gen/c/ui.capnp.h"

#if !defined(QCOM) && !defined(QCOM2)
#ifndef __APPLE__
#define GLFW_INCLUDE_ES2
#else
#define GLFW_INCLUDE_GLCOREARB
#endif

#define GLFW_INCLUDE_GLEXT
#include <GLFW/glfw3.h>
int linux_abs_x = 0;
int linux_abs_y = 0;
UIState *mouse_ui_state;
#endif
// TODO: this is also hardcoded in common/transformations/camera.py


Expand Down Expand Up @@ -251,7 +264,13 @@ int bb_ui_draw_measure( UIState *s, const char* bb_value, const char* bb_uom, c
}



bool bb_handle_ui_touch( UIState *s, int touch_x, int touch_y) {
#if !defined(QCOM) && !defined(QCOM2)
touch_x = (int)(vwp_w * touch_x / 1280);
touch_y = (int)(vwp_h * touch_y / 720);
printf("Linux mouse up at %d, %d ( %d, %d)\n",(int)touch_x, (int)touch_y, linux_abs_x, linux_abs_y);
#endif
for(int i=0; i<6; i++) {
if (s->b.btns_r[i] > 0) {
if ((abs(touch_x - s->b.btns_x[i]) < s->b.btns_r[i]) && (abs(touch_y - s->b.btns_y[i]) < s->b.btns_r[i])) {
Expand Down Expand Up @@ -292,6 +311,20 @@ bool bb_handle_ui_touch( UIState *s, int touch_x, int touch_y) {
return false;
};

#if !defined(QCOM) && !defined(QCOM2)
void bb_mouse_event_handler(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {

double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
int w_width,w_height;
glfwGetWindowSize(window, &linux_abs_x, &linux_abs_y);
bb_handle_ui_touch(mouse_ui_state,(int) xpos, (int)ypos);
}
}
#endif


int bb_get_button_status( UIState *s, char *btn_name) {
int ret_status = -1;
for (int i = 0; i< 6; i++) {
Expand Down
9 changes: 9 additions & 0 deletions selfdrive/ui/bbuistate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ typedef struct UICstmButton {
} UICstmButton;

typedef struct BBUIState {
float scr_scale_x;
float scr_scale_y;
int scr_w;
int scr_h;
float scr_device_factor;
float scr_scissor_offset;
#if !defined(QCOM) && !defined(QCOM2)
Display *scr_display;
#endif
int touch_last_x;
int touch_last_y;
bool touch_last;
Expand Down
14 changes: 8 additions & 6 deletions selfdrive/ui/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
#endif

#define GLFW_INCLUDE_GLEXT
#include <GLFW/glfw3.h>
#include "GLFW/glfw3.h"

typedef struct FramebufferState FramebufferState;
typedef struct TouchState TouchState;

extern "C" {

FramebufferState* framebuffer_init(
FramebufferState* framebuffer_init_linux(
const char* name, int32_t layer, int alpha,
int *out_w, int *out_h) {
int *out_w, int *out_h, GLFWmousebuttonfun mouse_event_handler) {
glfwInit();

#ifndef __APPLE__
Expand All @@ -41,7 +39,9 @@ FramebufferState* framebuffer_init(
if (!window) {
printf("glfwCreateWindow failed\n");
}

if (mouse_event_handler != NULL) {
glfwSetMouseButtonCallback(window,mouse_event_handler);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(0);

Expand All @@ -55,6 +55,8 @@ FramebufferState* framebuffer_init(

return (FramebufferState*)window;
}
extern "C" {


void framebuffer_set_power(FramebufferState *s, int mode) {
}
Expand Down
30 changes: 24 additions & 6 deletions selfdrive/ui/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,13 @@ static void ui_draw_vision(UIState *s) {

// Draw video frames
glEnable(GL_SCISSOR_TEST);
glViewport(ui_viz_rx+ui_viz_ro, s->fb_h-(box_y+box_h), viz_w, box_h);
#if defined(QCOM) || defined(QCOM2)
glViewport(ui_viz_rx+ui_viz_ro, s->fb_h-(box_y+box_h), viz_w , box_h);
glScissor(ui_viz_rx, s->fb_h-(box_y+box_h), ui_viz_rw, box_h);
#else
glViewport(0, s->b.scr_h + s->b.scr_scissor_offset, s->b.scr_w, s->b.scr_w * 0.751);
glScissor(0, s->b.scr_h + s->b.scr_scissor_offset, s->b.scr_w, s->b.scr_w * 0.751);
#endif
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
draw_frame(s);
Expand All @@ -819,6 +824,7 @@ static void ui_draw_vision(UIState *s) {
glClear(GL_STENCIL_BUFFER_BIT);

nvgBeginFrame(s->vg, s->fb_w, s->fb_h, 1.0f);
nvgScale(s->vg,s->b.scr_scale_x,s->b.scr_scale_y);
nvgSave(s->vg);

// Draw augmented elements
Expand Down Expand Up @@ -858,6 +864,8 @@ void ui_draw(UIState *s) {
ui_draw_sidebar(s);
ui_draw_vision(s);
} else {
nvgScale(s->vg,s->b.scr_scale_x,s->b.scr_scale_y);

ui_draw_blank(s);
if (!s->scene.uilayout_sidebarcollapsed) {
ui_draw_sidebar(s);
Expand Down Expand Up @@ -918,8 +926,8 @@ static const char frame_fragment_shader[] =
#endif

static const mat4 device_transform = {{
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.68, 0.0, 0.0, 0.0,
0.0, 0.68, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
}};
Expand Down Expand Up @@ -1040,9 +1048,19 @@ void ui_nvg_init(UIState *s) {
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
}

s->front_frame_mat = matmul(device_transform, full_to_wide_frame_transform);
s->rear_frame_mat = matmul(device_transform, frame_transform);
#if defined(QCOM) || defined(QCOM2)
s->rear_frame_mat = matmul(device_transform, frame_transform);
s->front_frame_mat = matmul(device_transform, full_to_wide_frame_transform);
#else
mat4 device_transform_bb = {{
s->b.scr_device_factor, 0.0, 0.0, 0.0,
0.0, s->b.scr_device_factor, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
}};
s->rear_frame_mat = matmul(device_transform_bb, frame_transform);
s->front_frame_mat = matmul(device_transform_bb, full_to_wide_frame_transform);
#endif

for(int i = 0;i < UI_BUF_COUNT; i++) {
s->khr[i] = NULL;
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/ui/ui
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export LD_LIBRARY_PATH="/system/lib64:$LD_LIBRARY_PATH"
TBP=/data/tinkla_buddy_pro
if test -f "$TBP"; then
exec /usr/bin/startx ./_ui
exec /usr/bin/startx ./_ui -rez 800 480
else
exec ./_ui
fi
Loading

0 comments on commit f9aa22b

Please sign in to comment.