@ -1,9 +1,8 @@
# include <stdio.h>
# include <stdio.h>
# include <stdlib.h>
# include <stdlib.h>
# include <unistd.h >
# include <mutex >
# include <eigen3/Eigen/Dense>
# include <eigen3/Eigen/Dense>
# include "visionbuf.h"
# include "visionipc_client.h"
# include "visionipc_client.h"
# include "common/swaglog.h"
# include "common/swaglog.h"
# include "common/clutil.h"
# include "common/clutil.h"
@ -14,12 +13,12 @@
ExitHandler do_exit ;
ExitHandler do_exit ;
// globals
// globals
bool run_model ;
bool live_calib_seen ;
mat3 cur_transform ;
mat3 cur_transform ;
pthread_mutex_t transform_lock ;
std : : mutex transform_lock ;
void * live_thread ( void * arg ) {
void calibration_thread ( ) {
set_thread_name ( " live " ) ;
set_thread_name ( " calibration " ) ;
set_realtime_priority ( 50 ) ;
set_realtime_priority ( 50 ) ;
SubMaster sm ( { " liveCalibration " } ) ;
SubMaster sm ( { " liveCalibration " } ) ;
@ -60,61 +59,18 @@ void* live_thread(void *arg) {
transform . v [ i ] = warp_matrix ( i / 3 , i % 3 ) ;
transform . v [ i ] = warp_matrix ( i / 3 , i % 3 ) ;
}
}
mat3 model_transform = matmul3 ( yuv_transform , transform ) ;
mat3 model_transform = matmul3 ( yuv_transform , transform ) ;
pthread_mutex_lock ( & transform_lock ) ;
std : : lock_guard lk ( transform_lock ) ;
cur_transform = model_transform ;
cur_transform = model_transform ;
run_model = true ;
live_calib_seen = true ;
pthread_mutex_unlock ( & transform_lock ) ;
}
}
}
}
return NULL ;
}
}
int main ( int argc , char * * argv ) {
void run_model ( ModelState & model , VisionIpcClient & vipc_client ) {
int err ;
set_realtime_priority ( 54 ) ;
# ifdef QCOM
set_core_affinity ( 2 ) ;
# elif QCOM2
// CPU usage is much lower when pinned to a single big core
set_core_affinity ( 4 ) ;
# endif
pthread_mutex_init ( & transform_lock , NULL ) ;
// start calibration thread
pthread_t live_thread_handle ;
err = pthread_create ( & live_thread_handle , NULL , live_thread , NULL ) ;
assert ( err = = 0 ) ;
// messaging
// messaging
PubMaster pm ( { " modelV2 " , " cameraOdometry " } ) ;
PubMaster pm ( { " modelV2 " , " cameraOdometry " } ) ;
SubMaster sm ( { " lateralPlan " , " roadCameraState " } ) ;
SubMaster sm ( { " lateralPlan " , " roadCameraState " } ) ;
// cl init
cl_device_id device_id = cl_get_device_id ( CL_DEVICE_TYPE_DEFAULT ) ;
cl_context context = CL_CHECK_ERR ( clCreateContext ( NULL , 1 , & device_id , NULL , NULL , & err ) ) ;
// init the models
ModelState model ;
model_init ( & model , device_id , context ) ;
LOGW ( " models loaded, modeld starting " ) ;
VisionIpcClient vipc_client = VisionIpcClient ( " camerad " , VISION_STREAM_YUV_BACK , true , device_id , context ) ;
while ( ! do_exit ) {
if ( ! vipc_client . connect ( false ) ) {
util : : sleep_for ( 100 ) ;
continue ;
}
break ;
}
// loop
while ( ! do_exit ) {
VisionBuf * b = & vipc_client . buffers [ 0 ] ;
LOGW ( " connected with buffer size: %d (%d x %d) " , b - > len , b - > width , b - > height ) ;
// setup filter to track dropped frames
// setup filter to track dropped frames
const float dt = 1. / MODEL_FREQ ;
const float dt = 1. / MODEL_FREQ ;
const float ts = 10.0 ; // filter time constant (s)
const float ts = 10.0 ; // filter time constant (s)
@ -127,16 +83,14 @@ int main(int argc, char **argv) {
uint32_t run_count = 0 ;
uint32_t run_count = 0 ;
while ( ! do_exit ) {
while ( ! do_exit ) {
VisionIpcBufExtra extra ;
VisionIpcBufExtra extra = { } ;
VisionBuf * buf = vipc_client . recv ( & extra ) ;
VisionBuf * buf = vipc_client . recv ( & extra ) ;
if ( buf = = nullptr ) {
if ( buf = = nullptr ) continue ;
continue ;
}
pthread_mutex_lock ( & transform_lock ) ;
transform_lock . lock ( ) ;
mat3 model_transform = cur_transform ;
mat3 model_transform = cur_transform ;
const bool run_model_this_iter = run_model ;
const bool run_model_this_iter = live_calib_seen ;
pthread_mutex_unlock ( & transform_lock ) ;
transform_lock . unlock ( ) ;
if ( sm . update ( 0 ) > 0 ) {
if ( sm . update ( 0 ) > 0 ) {
// TODO: path planner timeout?
// TODO: path planner timeout?
@ -144,7 +98,6 @@ int main(int argc, char **argv) {
frame_id = sm [ " roadCameraState " ] . getRoadCameraState ( ) . getFrameId ( ) ;
frame_id = sm [ " roadCameraState " ] . getRoadCameraState ( ) . getFrameId ( ) ;
}
}
double mt1 = 0 , mt2 = 0 ;
if ( run_model_this_iter ) {
if ( run_model_this_iter ) {
run_count + + ;
run_count + + ;
@ -153,12 +106,10 @@ int main(int argc, char **argv) {
vec_desire [ desire ] = 1.0 ;
vec_desire [ desire ] = 1.0 ;
}
}
mt1 = millis_since_boot ( ) ;
double mt1 = millis_since_boot ( ) ;
ModelDataRaw model_buf = model_eval_frame ( & model , buf - > buf_cl , buf - > width , buf - > height ,
ModelDataRaw model_buf =
model_eval_frame ( & model , buf - > buf_cl , buf - > width , buf - > height ,
model_transform , vec_desire ) ;
model_transform , vec_desire ) ;
mt2 = millis_since_boot ( ) ;
double mt2 = millis_since_boot ( ) ;
float model_execution_time = ( mt2 - mt1 ) / 1000.0 ;
float model_execution_time = ( mt2 - mt1 ) / 1000.0 ;
// tracked dropped frames
// tracked dropped frames
@ -175,16 +126,47 @@ int main(int argc, char **argv) {
last = mt1 ;
last = mt1 ;
last_vipc_frame_id = extra . frame_id ;
last_vipc_frame_id = extra . frame_id ;
}
}
}
}
int main ( int argc , char * * argv ) {
set_realtime_priority ( 54 ) ;
# ifdef QCOM
set_core_affinity ( 2 ) ;
# elif QCOM2
// CPU usage is much lower when pinned to a single big core
set_core_affinity ( 4 ) ;
# endif
// start calibration thread
std : : thread thread = std : : thread ( calibration_thread ) ;
// cl init
cl_device_id device_id = cl_get_device_id ( CL_DEVICE_TYPE_DEFAULT ) ;
cl_context context = CL_CHECK_ERR ( clCreateContext ( NULL , 1 , & device_id , NULL , NULL , & err ) ) ;
// init the models
ModelState model ;
model_init ( & model , device_id , context ) ;
LOGW ( " models loaded, modeld starting " ) ;
VisionIpcClient vipc_client = VisionIpcClient ( " camerad " , VISION_STREAM_YUV_BACK , true , device_id , context ) ;
while ( ! do_exit & & ! vipc_client . connect ( false ) ) {
util : : sleep_for ( 100 ) ;
}
}
// run the models
// vipc_client.connected is false only when do_exit is true
if ( vipc_client . connected ) {
const VisionBuf * b = & vipc_client . buffers [ 0 ] ;
LOGW ( " connected with buffer size: %d (%d x %d) " , b - > len , b - > width , b - > height ) ;
run_model ( model , vipc_client ) ;
}
}
model_free ( & model ) ;
model_free ( & model ) ;
LOG ( " joining calibration thread " ) ;
LOG ( " joining live_thread " ) ;
thread . join ( ) ;
err = pthread_join ( live_thread_handle , NULL ) ;
assert ( err = = 0 ) ;
CL_CHECK ( clReleaseContext ( context ) ) ;
CL_CHECK ( clReleaseContext ( context ) ) ;
pthread_mutex_destroy ( & transform_lock ) ;
return 0 ;
return 0 ;
}
}