Skip to content

Commit

Permalink
优化代码结构
Browse files Browse the repository at this point in the history
  • Loading branch information
sfxfs committed Oct 9, 2023
1 parent add50dd commit 57f6dbd
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 48 deletions.
6 changes: 3 additions & 3 deletions protocols/jsonrpc-c.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
* Only for HTTP
*/

#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#include "picohttpparser.h"
#include "jsonrpc-c.h"
Expand Down
33 changes: 13 additions & 20 deletions user/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,13 @@ static void rov_info_write_initial_value(struct rov_info* info)
{
memset(info, 0, sizeof(rov_info_t));//变量初始化

propeller_params_init_freq(&info->propeller.pwm_freq_calibration);
propeller_params_init(&info->propeller.front_right);
propeller_params_init(&info->propeller.front_left);
propeller_params_init(&info->propeller.center_right);
propeller_params_init(&info->propeller.center_left);
propeller_params_init(&info->propeller.back_right);
propeller_params_init(&info->propeller.back_left);

rocket_ratio_params_init(&info->rocket.ratio_x);
rocket_ratio_params_init(&info->rocket.ratio_y);
rocket_ratio_params_init(&info->rocket.ratio_z);
rocket_ratio_params_init(&info->rocket.ratio_yaw);

pid_ctl_params_init(&info->pidScale.yaw);
pid_ctl_params_init(&info->pidScale.depth);

dev_ctl_params_init(&info->devCtl.light);
dev_ctl_params_init(&info->devCtl.yuntai);
dev_ctl_params_init(&info->devCtl.arm);
propeller_params_all_init(&info->propeller);

rocket_ratio_params_all_init(&info->rocket);

pid_ctl_params_all_init(&info->pidScale);

dev_ctl_params_all_init(&info->devCtl);
}

/**
Expand All @@ -56,6 +44,11 @@ static void rov_info_write_initial_value(struct rov_info* info)
int rov_config_write_json_to_file(cJSON *params)
{
char *temp = cJSON_Print(params);
if (temp == NULL)
{
log_e("cjson print error, invaild cjson data");
return -1;
}
FILE *fp = fopen(CONFIG_FILE_PATH, "w");
if (fp == NULL)
{
Expand Down Expand Up @@ -98,7 +91,7 @@ int rov_config_write_to_file(struct rov_info* info)
}

/**
* @brief 读取配置文件
* @brief 读取配置文件(记得用完要 delete)
* @return 由读取到的配置文件创建的 cjson 结构体
*/
cJSON *rov_config_read_from_file_return_cjson()
Expand Down
7 changes: 7 additions & 0 deletions user/config/parameters/dev_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ void dev_ctl_params_init(struct pwmDev_parameters *params)
params->speed = 50;
}

void dev_ctl_params_all_init(dev_ctl_t *params)
{
dev_ctl_params_init(&params->light);
dev_ctl_params_init(&params->yuntai);
dev_ctl_params_init(&params->arm);
}

/**
* @brief pwm设备参数添加(Creat Json and Add param)
* @param info rov_info结构体参数
Expand Down
1 change: 1 addition & 0 deletions user/config/parameters/dev_ctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cJSON* dev_ctl_params_add_to_root(struct pwmDev_parameters *params);
void dev_ctl_params_read_from_root(struct pwmDev_parameters *params, cJSON *node);
void dev_ctl_params_init(struct pwmDev_parameters *params);

void dev_ctl_params_all_init(dev_ctl_t *params);
cJSON* dev_ctl_params_write(struct rov_info* info);
void dev_ctl_params_read(struct rov_info* info, cJSON *node);

Expand Down
6 changes: 6 additions & 0 deletions user/config/parameters/pid_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ void pid_ctl_params_init(struct pid_scale_parameters *params)
params->Kd = 0;
}

void pid_ctl_params_all_init(pid_scale_t *params)
{
pid_ctl_params_init(&params->yaw);
pid_ctl_params_init(&params->depth);
}

/**
* @brief pid控制参数添加(Creat Json and Add param)
* @param info rov_info结构体参数
Expand Down
1 change: 1 addition & 0 deletions user/config/parameters/pid_ctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cJSON* pid_ctl_params_add_to_root(struct pid_scale_parameters *params);
void pid_ctl_params_read_from_root(struct pid_scale_parameters *params, cJSON *node);
void pid_ctl_params_init(struct pid_scale_parameters *params);

void pid_ctl_params_all_init(pid_scale_t *params);
cJSON* pid_ctl_params_write(struct rov_info* info);
void pid_ctl_params_read(struct rov_info* info, cJSON *node);

Expand Down
11 changes: 11 additions & 0 deletions user/config/parameters/propeller.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ void propeller_params_init(struct propeller_parameters *params)
params->power_negative = 0.2;
}

void propeller_params_all_init(propeller_t *params)
{
propeller_params_init_freq(&params->pwm_freq_calibration);
propeller_params_init(&params->front_right);
propeller_params_init(&params->front_left);
propeller_params_init(&params->center_right);
propeller_params_init(&params->center_left);
propeller_params_init(&params->back_right);
propeller_params_init(&params->back_left);
}

/**
* @brief 推进器参数添加(Creat Json and Add params)
* @param info rov_info结构体参数
Expand Down
1 change: 1 addition & 0 deletions user/config/parameters/propeller.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void propeller_params_read_from_root(struct propeller_parameters *params, cJSON
void propeller_params_init_freq(double *params);
void propeller_params_init(struct propeller_parameters *params);

void propeller_params_all_init(propeller_t *params);
cJSON* propeller_params_write(struct rov_info* info);
void propeller_params_read(struct rov_info* info, cJSON *node);

Expand Down
8 changes: 8 additions & 0 deletions user/config/parameters/rocket_ratio.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ void rocket_ratio_params_init(struct r2p_ratio *params)
_rocket_dir_params_init(&params->back_left);
}

void rocket_ratio_params_all_init(rocket_t *params)
{
rocket_ratio_params_init(&params->ratio_x);
rocket_ratio_params_init(&params->ratio_y);
rocket_ratio_params_init(&params->ratio_z);
rocket_ratio_params_init(&params->ratio_yaw);
}

/**
* @brief 各轴参数写入(Creat Json and Add params)
* @param info rov_info结构体参数
Expand Down
1 change: 1 addition & 0 deletions user/config/parameters/rocket_ratio.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cJSON* rocket_ratio_params_add_to_root(struct r2p_ratio *params);
void rocket_ratio_params_read_from_root(struct r2p_ratio *params, cJSON *node);
void rocket_ratio_params_init(struct r2p_ratio *params);

void rocket_ratio_params_all_init(rocket_t *params);
cJSON* rocket_ratio_params_write(struct rov_info* info);
void rocket_ratio_params_read(struct rov_info* info, cJSON *node);

Expand Down
35 changes: 20 additions & 15 deletions user/device/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void write_to_propeller(propeller_t *param)
pwm_controller_write(param->propeller.channel, 0.0f, 7.5 + constrain(2.5 * param->propeller.power_cur, -2.5, 2.5));\
param->propeller.power_last = param->propeller.power_cur;\
param->propeller.power_cur = 0;

CALL_FOR_ALL_PROPELLER(PWM_COTROLLER_WRITE)
#undef PWM_COTROLLER_WRITE
}
Expand All @@ -38,6 +38,11 @@ void *propeller_thread(void *arg)
{
rov_info_t *info = (rov_info_t *)arg;

for (int i = 0; i < 16; i++)
{
pwm_controller_write(i, 0, 0);
}

for (;;)
{
pthread_mutex_lock(&info->thread.mutex.write_propeller);
Expand All @@ -47,16 +52,16 @@ void *propeller_thread(void *arg)
return NULL;
}

void *device_thread(void *arg)
{
rov_info_t *info = (rov_info_t *)arg;
// void *device_thread(void *arg)
// {
// rov_info_t *info = (rov_info_t *)arg;

for (;;)
{
}
// for (;;)
// {
// }

return NULL;
}
// return NULL;
// }

int rov_device_run(struct rov_info* info)
{
Expand All @@ -78,12 +83,12 @@ int rov_device_run(struct rov_info* info)
}
pthread_detach(info->thread.tid.propeller);

log_i("starting thread");
if (pthread_create(&info->thread.tid.device, NULL, device_thread, info) != 0)
{
log_e("thread start failed");
}
pthread_detach(info->thread.tid.device);
// log_i("starting thread");
// if (pthread_create(&info->thread.tid.device, NULL, device_thread, info) != 0)
// {
// log_e("thread start failed");
// }
// pthread_detach(info->thread.tid.device);

return 0;
}
Expand Down
14 changes: 12 additions & 2 deletions user/server/handler/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static double cjson_value_analysis_double(cJSON *params,const char *str)

static cJSON *move_analysis(cJSON* params, struct rov_info* info, move_mode_t mode)
{
if (params == NULL)
return cJSON_CreateNull();
info->rocket.x = cjson_value_analysis_double(params, "x");
info->rocket.y = cjson_value_analysis_double(params, "y");
info->rocket.z = cjson_value_analysis_double(params, "z");
Expand All @@ -27,11 +29,9 @@ static cJSON *move_analysis(cJSON* params, struct rov_info* info, move_mode_t mo
break;
case abs_ctl:
info->rocket.yaw = 0;
// Total_Controller.Yaw_Angle_Control.Expect = -cjson_value_analysis_double(params, "rot");
break;
case rel_clt:
info->rocket.yaw = 0;
// expect_rotate_auv = -cjson_value_analysis_double(params, "rot");
break;
}

Expand All @@ -53,18 +53,24 @@ cJSON *move_syn_handler(jrpc_context *ctx, cJSON *params, cJSON *id)

cJSON *catcher_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
if (params == NULL)
return cJSON_CreateNull();
((dev_ctl_t *)ctx->data)->catcher_clt = params->child->valuedouble > 0 ? pwm_pMove : (params->child->valuedouble < 0 ? pwm_nMove : pwm_noAct);
return cJSON_CreateNull();
}

cJSON *light_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
if (params == NULL)
return cJSON_CreateNull();
((dev_ctl_t *)ctx->data)->light_clt = params->child->valuedouble > 0 ? pwm_pMove : (params->child->valuedouble < 0 ? pwm_nMove : pwm_noAct);
return cJSON_CreateNull();
}

cJSON *depth_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
if (params == NULL)
return cJSON_CreateNull();
((debug_info_t *)ctx->data)->auv_expect_depth = (float)params->child->valuedouble;
return cJSON_CreateNull();
}
Expand All @@ -81,12 +87,16 @@ cJSON *move_relative_handler(jrpc_context *ctx, cJSON *params, cJSON *id)

cJSON *direction_lock_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
if (params == NULL)
return cJSON_CreateNull();
((dev_ctl_t *)ctx->data)->dir_lock = params->child->valueint;
return cJSON_CreateNull();
}

cJSON *depth_lock_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
if (params == NULL)
return cJSON_CreateNull();
((dev_ctl_t *)ctx->data)->depth_lock = params->child->valueint;
return cJSON_CreateNull();
}
23 changes: 15 additions & 8 deletions user/server/handler/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ static int cjson_value_analysis_int(cJSON *params, const char *str)

cJSON *set_debug_mode_enabled_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
if (params == NULL)
return cJSON_CreateNull();
((dev_ctl_t *)ctx->data)->debug_mode_stat = params->child->valueint;
return cJSON_CreateNull();
}

cJSON *set_propeller_pwm_freq_calibration_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
if (params == NULL)
return cJSON_CreateNull();
pwm_controller_set_freq((uint16_t)params->child->valuedouble);
return cJSON_CreateNull();
}

cJSON *set_propeller_values_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
rov_info_t *info = (rov_info_t *)ctx->data;

info->debugInfo.propeller_direct_back_left = cjson_value_analysis_int(params, "back_left");
info->debugInfo.propeller_direct_back_right = cjson_value_analysis_int(params, "back_right");
info->debugInfo.propeller_direct_center_left = cjson_value_analysis_int(params, "center_left");
Expand All @@ -48,19 +53,21 @@ cJSON *set_propeller_values_handler(jrpc_context *ctx, cJSON *params, cJSON *id)

cJSON *set_propeller_parameters_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
propeller_params_read_from_root(&((propeller_t *)ctx->data)->front_right, cJSON_GetObjectItem(params, "front_right"));
propeller_params_read_from_root(&((propeller_t *)ctx->data)->front_left, cJSON_GetObjectItem(params, "front_left"));
propeller_params_read_from_root(&((propeller_t *)ctx->data)->center_right, cJSON_GetObjectItem(params, "center_right"));
propeller_params_read_from_root(&((propeller_t *)ctx->data)->center_left, cJSON_GetObjectItem(params, "center_left"));
propeller_params_read_from_root(&((propeller_t *)ctx->data)->back_right, cJSON_GetObjectItem(params, "back_right"));
propeller_params_read_from_root(&((propeller_t *)ctx->data)->back_left, cJSON_GetObjectItem(params, "back_left"));
// to do ...
// propeller_params_read_from_root(&((propeller_t *)ctx->data)->front_right, cJSON_GetObjectItem(params, "front_right"));
// propeller_params_read_from_root(&((propeller_t *)ctx->data)->front_left, cJSON_GetObjectItem(params, "front_left"));
// propeller_params_read_from_root(&((propeller_t *)ctx->data)->center_right, cJSON_GetObjectItem(params, "center_right"));
// propeller_params_read_from_root(&((propeller_t *)ctx->data)->center_left, cJSON_GetObjectItem(params, "center_left"));
// propeller_params_read_from_root(&((propeller_t *)ctx->data)->back_right, cJSON_GetObjectItem(params, "back_right"));
// propeller_params_read_from_root(&((propeller_t *)ctx->data)->back_left, cJSON_GetObjectItem(params, "back_left"));
return cJSON_CreateNull();
}

cJSON *set_control_loop_parameters_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
pid_ctl_params_read_from_root(&((pid_scale_t *)ctx->data)->yaw, cJSON_GetObjectItem(params, "yaw_lock"));
pid_ctl_params_read_from_root(&((pid_scale_t *)ctx->data)->depth, cJSON_GetObjectItem(params, "depth_lock"));
// to do ...
// pid_ctl_params_read_from_root(&((pid_scale_t *)ctx->data)->yaw, cJSON_GetObjectItem(params, "yaw_lock"));
// pid_ctl_params_read_from_root(&((pid_scale_t *)ctx->data)->depth, cJSON_GetObjectItem(params, "depth_lock"));
return cJSON_CreateNull();
}

Expand Down
1 change: 1 addition & 0 deletions user/server/handler/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ cJSON *debug_info_handler(jrpc_context *ctx, cJSON *params, cJSON *id)

cJSON *update_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
{
// to do ...
return cJSON_CreateNull();
}

0 comments on commit 57f6dbd

Please sign in to comment.