From 076bfc12c02ce38dfdec4a765725660e4e5a5714 Mon Sep 17 00:00:00 2001 From: sfxfs <2501683578@qq.com> Date: Tue, 21 Nov 2023 21:17:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E7=BA=BF=E7=A8=8B=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- user/control/control.c | 11 ++++++++--- user/data-type/system.h | 4 +++- user/device/device.c | 5 +++++ user/main.c | 8 +++++++- user/server/handler/control.c | 6 +++++- user/server/handler/debug.c | 2 +- user/server/server.c | 5 ++--- 7 files changed, 31 insertions(+), 10 deletions(-) diff --git a/user/control/control.c b/user/control/control.c index 4b5b795..3af5f82 100644 --- a/user/control/control.c +++ b/user/control/control.c @@ -20,8 +20,9 @@ void *control_thread(void *arg) rov_info_t *info = (rov_info_t *)arg; for (;;) { - pthread_mutex_lock(&info->system.device.power_output_mtx); - pthread_cond_wait(&info->system.server.recv_cmd_cond, &info->system.device.power_output_mtx); + pthread_mutex_lock(&info->system.control.recv_cmd_mtx); + pthread_cond_wait(&info->system.control.recv_cmd_cond, &info->system.control.recv_cmd_mtx); + log_d("cmd recv, ask for propeller"); if (info->control.flag.debug_mode == false) { rov_manual_control(info); @@ -30,7 +31,8 @@ void *control_thread(void *arg) { rov_debug_control(info); } - pthread_mutex_unlock(&info->system.device.power_output_mtx); + pthread_cond_signal(&info->system.device.power_output_cond); + pthread_mutex_unlock(&info->system.control.recv_cmd_mtx); } return NULL; } @@ -42,6 +44,9 @@ void *control_thread(void *arg) */ int rov_control_run(struct rov_info* info) { + pthread_mutex_init(&info->system.control.recv_cmd_mtx, NULL); + pthread_cond_init(&info->system.control.recv_cmd_cond, NULL); + log_i("starting thread"); if (pthread_create(&info->system.control.main_tid, NULL, control_thread, info) != 0) { diff --git a/user/data-type/system.h b/user/data-type/system.h index ae615a3..eccc927 100644 --- a/user/data-type/system.h +++ b/user/data-type/system.h @@ -6,6 +6,7 @@ typedef struct sys_device { pthread_t main_tid; + pthread_cond_t power_output_cond; pthread_mutex_t power_output_mtx; } sys_device_t; @@ -19,13 +20,14 @@ typedef struct sys_server { pthread_t loss_status_check_tid; pthread_mutex_t loss_status_mtx; - pthread_cond_t recv_cmd_cond; struct server_config config; } sys_server_t; typedef struct sys_control { pthread_t main_tid; + pthread_mutex_t recv_cmd_mtx; + pthread_cond_t recv_cmd_cond; } sys_control_t; typedef struct system diff --git a/user/device/device.c b/user/device/device.c index 22ee7f6..67adb23 100644 --- a/user/device/device.c +++ b/user/device/device.c @@ -53,7 +53,11 @@ void *propeller_thread(void *arg) for (;;) { pthread_mutex_lock(&info->system.device.power_output_mtx); + pthread_cond_wait(&info->system.device.power_output_cond, &info->system.device.power_output_mtx); + + log_d("write to propeller"); write_to_propeller(&info->propeller); + pthread_mutex_unlock(&info->system.device.power_output_mtx); } @@ -68,6 +72,7 @@ void *propeller_thread(void *arg) int rov_device_run(struct rov_info* info) { pthread_mutex_init(&info->system.device.power_output_mtx, NULL); + pthread_cond_init(&info->system.device.power_output_cond, NULL); log_i("starting pwm controller"); if (pwm_controller_init(PCA9685_ADDRESS_A000000, info->propeller.pwm_freq_calibration) < 0) diff --git a/user/main.c b/user/main.c index 61141c8..2b09ef8 100644 --- a/user/main.c +++ b/user/main.c @@ -11,8 +11,14 @@ extern void uvm_loop (void); static void exit_uvm(int sig) { - printf("info: closing uv-master app...\n"); + printf("\ninfo: closing uv-master app...\n"); uvm_deinit(); + printf( " __ __ \n" + " / ) / ) /\n" + " /--< __ , _ /--< __ , _ / \n" + "/___/_/ (_/_system.server.recv_cmd_cond); + log_d("rocket cmd recv, sending cond signal"); + pthread_cond_signal(&info->system.control.recv_cmd_cond); return cJSON_CreateNull(); } diff --git a/user/server/handler/debug.c b/user/server/handler/debug.c index 5ed9df9..15967e3 100644 --- a/user/server/handler/debug.c +++ b/user/server/handler/debug.c @@ -60,7 +60,7 @@ cJSON *set_propeller_values_handler(jrpc_context *ctx, cJSON *params, cJSON *id) info->propeller.front_left.power_debug = cjson_value_analysis_int(params, "front_left"); info->propeller.front_right.power_debug = cjson_value_analysis_int(params, "front_right"); - pthread_cond_signal(&info->system.server.recv_cmd_cond); + pthread_cond_signal(&info->system.control.recv_cmd_cond); return cJSON_CreateNull(); } diff --git a/user/server/server.c b/user/server/server.c index 824af37..0fb3e30 100644 --- a/user/server/server.c +++ b/user/server/server.c @@ -146,8 +146,6 @@ int loss_ctl_timer_init(struct rov_info* info) */ int jsonrpc_server_run(struct rov_info* info, uint8_t debug_level) { - pthread_cond_init(&info->system.server.recv_cmd_cond, NULL); - o = onion_new(O_DETACH_LISTEN | O_NO_SIGTERM); if (o == NULL) { @@ -196,7 +194,8 @@ int jsonrpc_server_stop() { log_i("stop service"); - mln_event_free(ev);//释放事件模块 + if (ev != NULL) + mln_event_free(ev);//释放事件模块 onion_free(o); return 0;