.c文件-创建两个线程,一个用于将接收的数据写入文件,一个用于监听(监听到指令后进行介绍操作)
1 #include "test_umb.h" 2 3 static umb_inst_id_t g_safety_inst_id[7]; 4 suglog_info g_safety_sug_info = {0}; 5 ctxlog_info g_ctx_info = {0}; 6 7 FILE *p_sug_file; 8 9 pthread_t listen_pb; 10 pthread_t sug_pb; 11 pthread_t ctx_pb; 12 uint64 old_time_interval = 0; 13 14 void umb_init_for_test(void); 15 static int sug_cb(umb_inst_id_t inst_id, umb_msgbuff_t *data, umb_timestamp_t *tm, void *param); 16 static int ctx_cb(umb_inst_id_t inst_id, umb_msgbuff_t *data, umb_timestamp_t *tm, void *param); 17 //void *sug_pthread(void *arg); 18 void *ctx_pthread(void *arg); 19 uint32 write_log(FILE *p_file,uint32 type); 20 void *listen_pthread(void *arg); 21 22 23 void main(void) 24 { 25 sint32 ret = 0; 26 char filename[100]; 27 time_t time_curr = time(NULL); 28 struct tm *tm_curr = localtime(&time_curr); 29 // umb_version_t version; 30 31 snprintf(filename,22,"sug%d%02d%02d%02d%02d%02d.log",(tm_curr->tm_year+1900),tm_curr->tm_mon+1,tm_curr->tm_mday, 32 tm_curr->tm_hour,tm_curr->tm_min,tm_curr->tm_sec); 33 34 umb_init_for_test(); 35 // umb_get_version(&version); 36 // printf("umb version is %d-%d-%d",version.major,version.minor,version.patch); 37 38 p_sug_file = fopen(filename,"a+"); 39 40 g_safety_sug_info.status = 1; 41 g_ctx_info.status = 1; 42 43 printf("start safety log...\n"); 44 45 // ret = pthread_create(&sug_pb,NULL,sug_pthread,NULL); 46 // if (0 != ret) { 47 // printf("sug_pthread create failed!\n"); 48 // } 49 ret = pthread_create(&ctx_pb,NULL,ctx_pthread,NULL); 50 if (0 != ret) { 51 printf("ctx_pthread create failed!\n"); 52 } 53 ret = pthread_create(&listen_pb,NULL,listen_pthread,NULL); 54 if (0 != ret) { 55 printf("listen_pthread create failed!\n"); 56 } 57 58 pthread_join(listen_pb,NULL); 59 sleep(1); 60 // printf("exit...\r\n"); 61 // while(1) { 62 // sleep(10); 63 // } 64 return; 65 66 } 67 68 void umb_init_for_test(void) 69 { 70 umb_qosparam_t qos = {0}; 71 qos.frag = 1; 72 qos.reliable = 0; 73 umb_init(NULL, NULL); 74 umb_sub_reg(&g_safety_inst_id[0], 1032, 166, sug_cb, NULL); 75 umb_sub_reg(&g_safety_inst_id[2], 1036, 301, ctx_cb, NULL); 76 // umb_sub_adv_reg(&g_safety_inst_id[2], 1036, 301, ctx_cb, &qos); 77 } 78 79 80 static int sug_cb(umb_inst_id_t inst_id, umb_msgbuff_t *data, umb_timestamp_t *tm, void *param) 81 { 82 (void)tm; 83 (void)param; 84 85 if (data->len <= sizeof(SafetySug)) 86 { 87 memcpy(&(g_safety_sug_info.sug), (*data).buff, sizeof(unsigned char) * (data->len)); 88 g_safety_sug_info.count++; 89 g_safety_sug_info.flag = 1; 90 } 91 } 92 93 static int ctx_cb(umb_inst_id_t inst_id, umb_msgbuff_t *data, umb_timestamp_t *tm, void *param) 94 { 95 (void)tm; 96 (void)param; 97 uint16 count = 0; 98 uint16 sonar_count = 0; 99 uint16 visual_count = 0; 100 uint8 temp_buff[CTX_SIZE_MAX]; 101 102 103 if (data->len < CTX_SIZE_MAX) 104 { 105 memcpy(temp_buff, (*data).buff, data->len); 106 107 memcpy(&(g_ctx_info.car_status), temp_buff, sizeof(CarStatus)); 108 count = sizeof(CarStatus); 109 110 if(temp_buff[count] > 0) { 111 g_ctx_info.sonar_obstl.count = (temp_buff[count]>64)?64:temp_buff[count]; 112 sonar_count = sizeof(SonarObstacleEntry)*g_ctx_info.sonar_obstl.count; 113 memcpy(&(g_ctx_info.sonar_obstl.arr), &(temp_buff[count+1]), sonar_count); 114 count += sonar_count +1; 115 } 116 else { 117 count += 1; 118 } 119 if((temp_buff[count] > 0)&&(count < CTX_SIZE_MAX)) { 120 g_ctx_info.visual_obstl.count = (temp_buff[count]>208)?208:temp_buff[count]; 121 visual_count = sizeof(VisualObstacleEntry)*g_ctx_info.visual_obstl.count; 122 memcpy(&(g_ctx_info.visual_obstl.arr), &(temp_buff[count+1]), visual_count); 123 count += visual_count+1; 124 } 125 else { 126 count += 1; 127 } 128 if(count < (CTX_SIZE_MAX-8)) { 129 memcpy(&(g_ctx_info.obstl_time_period),&(temp_buff[count+1]),sizeof(uint64)); 130 } 131 g_ctx_info.count++; 132 g_ctx_info.flag = 1; 133 } 134 } 135 136 137 //void *sug_pthread(void *arg) 138 //{ 139 // (void)arg; 140 141 // while(g_safety_sug_info.status) { 142 // if (1 == g_safety_sug_info.flag) { 143 // write_log(p_sug_file,SUG_TYPE); 144 // g_safety_sug_info.flag = 0; 145 // } 146 // } 147 //} 148 149 void *ctx_pthread(void *arg) 150 { 151 (void)arg; 152 153 while(g_ctx_info.status) { 154 if (1 == g_ctx_info.flag) { 155 write_log(p_sug_file,CTX_TYPE); 156 g_ctx_info.flag = 0; 157 } 158 { 159 pthread_testcancel(); 160 } 161 usleep(100); 162 } 163 } 164 165 166 uint32 write_log(FILE *p_file,UMB_DATA_TYPE type) 167 { 168 uint16 i = 0; 169 static uint16 cnt = 0; 170 171 time_t time_log = time(NULL); 172 struct tm *tm_log = localtime(&time_log); 173 fprintf(p_file,"%02d-%02d-%02d\r\n",tm_log->tm_hour,tm_log->tm_min,tm_log->tm_sec); 174 // if (SUG_TYPE == type) { 175 fprintf(p_file," sug status:\r\n"); 176 { 177 fprintf(p_file," run_status:%d\r\n",g_safety_sug_info.sug.run_status); 178 fprintf(p_file," win:%d\r\n",g_safety_sug_info.sug.win); 179 fprintf(p_file," brake_pedal_vehicle:%.3f\r\n",g_safety_sug_info.sug.brake_pedal_vehicle); 180 fprintf(p_file," brake_pedal_safety_out:%.3f\r\n",g_safety_sug_info.sug.brake_pedal_safety_out); 181 fprintf(p_file," min_dist:%.3f, x:%.3f, y%.3f\r\n",g_safety_sug_info.sug.obst_info.min_dist, 182 g_safety_sug_info.sug.obst_info.point.x, 183 g_safety_sug_info.sug.obst_info.point.y); 184 fprintf(p_file," count:%d\r\n",g_safety_sug_info.count); 185 } 186 // } 187 188 // if (CTX_TYPE == type) { 189 fprintf(p_file," car status:\r\n"); 190 { 191 fprintf(p_file," vehicle_speed:%.3f\r\n",g_ctx_info.car_status.vehicle_speed); 192 fprintf(p_file," steering_angle:%.3f\r\n",g_ctx_info.car_status.steering_angle); 193 fprintf(p_file," gear:%d\r\n",g_ctx_info.car_status.gear); 194 fprintf(p_file," brake_pedal:%.3f\r\n",g_ctx_info.car_status.brake_pedal); 195 } 196 197 fprintf(p_file," sonar obstl:\r\n"); 198 { 199 fprintf(p_file," count:%d\r\n",g_ctx_info.sonar_obstl.count); 200 for (i = 0; i < g_ctx_info.sonar_obstl.count; i++) { 201 fprintf(p_file," obstl:x:%.3f, y:%.3f, d:%.3f\r\n",g_ctx_info.sonar_obstl.arr[i].location.x, 202 g_ctx_info.sonar_obstl.arr[i].location.y, 203 g_ctx_info.sonar_obstl.arr[i].distance); 204 } 205 } 206 207 fprintf(p_file," visual obstl:\r\n"); 208 { 209 fprintf(p_file," count:%d\r\n",g_ctx_info.visual_obstl.count); 210 for (i = 0; i < g_ctx_info.visual_obstl.count; i++) { 211 fprintf(p_file," obstl:x:%.3f, y:%.3f, d:%.3f\r\n",g_ctx_info.visual_obstl.arr[i].location.x, 212 g_ctx_info.visual_obstl.arr[i].location.y, 213 g_ctx_info.visual_obstl.arr[i].distance); 214 } 215 } 216 217 fprintf(p_file," obstal HZ:\r\n"); 218 { 219 fprintf(p_file," sonar interval:%d",g_ctx_info.obstl_time_period); 220 } 221 if(old_time_interval == g_ctx_info.obstl_time_period) { 222 cnt++; 223 if (cnt > 3) { 224 fprintf(p_file," error\r\n"); 225 } 226 } 227 else { 228 fprintf(p_file,"\r\n"); 229 cnt = 0; 230 } 231 fprintf(p_file," count:%d\r\n",g_ctx_info.count); 232 old_time_interval = g_ctx_info.obstl_time_period; 233 // } 234 235 return 0; 236 } 237 238 void *listen_pthread(void *arg) 239 { 240 (void)arg; 241 242 while (1) 243 { 244 char buff[50]; 245 scanf("%s", buff); 246 if (strcmp("q", buff) == 0) 247 { 248 // g_safety_sug_info.status = 0; 249 g_ctx_info.status = 0; 250 251 fflush(p_sug_file); 252 fclose(p_sug_file); 253 pthread_cancel(ctx_pb); 254 pthread_join(ctx_pb,NULL); 255 usleep(200000); 256 printf("exit log...\r\n"); 257 break; 258 } 259 usleep(200000); 260 } 261 }
.h文件
1 #ifndef TEST_UMB 2 #define TEST_UMB 1 3 4 #include "umb.h" 5 #include <stdio.h> 6 #include <time.h> 7 #include <stdarg.h> 8 #include <pthread.h> 9 #include <string.h> 10 #include <unistd.h> 11 12 #define CTX_SIZE_MAX 2890 13 14 typedef float float32; 15 typedef unsigned int uint32; 16 typedef unsigned short uint16; 17 typedef unsigned char uint8; 18 typedef int sint32; 19 typedef unsigned long uint64; 20 21 typedef enum { 22 SUG_TYPE = 0, 23 CTX_TYPE, 24 25 } UMB_DATA_TYPE; 26 27 typedef enum { 28 GEAR_NONE = 0, 29 GEAR_PARKING = 1, 30 GEAR_REVERSE = 2, 31 GEAR_NEUTRAL = 3, 32 GEAR_DRIVE = 4, 33 } Gear; 34 35 36 typedef struct _obstacle_point { 37 float32 x; 38 float32 y; 39 } obstacle_point; 40 41 typedef struct _min_dist_obstacle { 42 float32 min_dist; 43 obstacle_point point; 44 } min_dist_obstacle; 45 46 typedef struct _SafetySug { 47 uint32 run_status; // 1-safety run 0-safety exit 48 uint32 win; // 1-safety win 0-safety nonthing 49 float32 brake_pedal_vehicle; // current vehicle brake pedal 50 float32 brake_pedal_safety_out; // 0-safety nonthing, other-safety out 51 min_dist_obstacle obst_info; // safety obstacle info 52 uint32 reverse[3]; //reverse 53 } SafetySug; 54 55 typedef struct _suglog_info { 56 SafetySug sug; 57 uint32 flag; 58 uint32 count; 59 uint32 status; 60 } suglog_info; 61 62 typedef struct { 63 float32 vehicle_speed; // m/s 64 float32 steering_angle; // deg 65 Gear gear; // described above 66 float32 brake_pedal; 67 } CarStatus; 68 69 typedef struct { 70 obstacle_point location; 71 float32 distance; 72 } SonarObstacleEntry; 73 74 typedef struct { 75 SonarObstacleEntry arr[2 * 32]; 76 uint32 count; 77 } SonarObstaclesPept; 78 79 typedef struct { 80 obstacle_point location; 81 float32 distance; 82 } VisualObstacleEntry; 83 84 typedef struct { 85 VisualObstacleEntry arr[208]; 86 uint32 count; 87 } VisualObstaclesPept; 88 89 90 typedef struct _ctxlog_info { 91 CarStatus car_status; 92 SonarObstaclesPept sonar_obstl; 93 VisualObstaclesPept visual_obstl; 94 uint64 obstl_time_period; 95 uint32 flag; 96 uint32 count; 97 uint32 status; 98 } ctxlog_info; 105 #endif /* TEST_UMB */
makefile:
1 CC = gcc 2 LIBS = -L /home/user/umb/umb_output/lib -lumb 3 LIBS += -lpthread 4 SOURCES = test_umb.c test_umb.h 5 OBJECTS = test_umb.o 6 TARGET = test_umb 7 #INCPATH += -I/home/user/ 8 #INCPATH += -I/home/ubuntu/umb/umb_output/lib/ 9 INCPATH += -I/home/user/umb/umb_output/include/ 10 all build:: $(TARGET) 11 $(OBJECTS): $(SOURCES) 12 $(CC) -c $(INCPATH) -o "$@" "$<" 13 14 $(TARGET): $(OBJECTS) 15 $(CC) -g -o $@ $(OBJECTS) $(LIBS) 16 17 clean: 18 rm -rf *.o 19 rm -rf test_umb