Commit 059c3110 authored by 957dd's avatar 957dd

加入了自控开启和关闭

parent 555a2968
...@@ -50,9 +50,9 @@ void *thread_exit_time(void *arg) { ...@@ -50,9 +50,9 @@ void *thread_exit_time(void *arg) {
g_devcontrol_exit_count=6; g_devcontrol_exit_count=6;
} }
pthread_mutex_unlock(&g_exit_count_mutex); pthread_mutex_unlock(&g_exit_count_mutex);
}else { }else if(get_self_control_index()==true){
set_self_control_time_countfuntion();
delay_ms(20); delay_ms(20);
set_self_control_time_countfuntion();
} }
} }
......
No preview for this file type
...@@ -134,23 +134,23 @@ void tank0204_change(unsigned char *buf) { ...@@ -134,23 +134,23 @@ void tank0204_change(unsigned char *buf) {
tank0204_mode_back(tank0204_front_val+10); tank0204_mode_back(tank0204_front_val+10);
tank0204_mode_lift(0); tank0204_mode_lift(0);
}else if(tank0204_front_t ==0&&tank0204_steering_t==1){ }else if(tank0204_front_t ==0&&tank0204_steering_t==1){
tank0204_mode_lift(tank0204_steering_val + tank0204_count+20); tank0204_mode_lift(tank0204_steering_val + tank0204_count);
tank0204_mode_back(0); tank0204_mode_back(0);
}else if(tank0204_front_t ==0&&tank0204_steering_t==2){ }else if(tank0204_front_t ==0&&tank0204_steering_t==2){
tank0204_mode_right(tank0204_steering_val + tank0204_count+20); tank0204_mode_right(tank0204_steering_val + tank0204_count);
tank0204_mode_back(0); tank0204_mode_back(0);
} }
else if(tank0204_front_t ==1&&tank0204_steering_t==1){ else if(tank0204_front_t ==1&&tank0204_steering_t==1){
tank0204_mode_lift(tank0204_steering_val+ 40); tank0204_mode_lift(tank0204_steering_val+ tank0204_count+100);
tank0204_mode_flont(tank0204_steering_val + 20); tank0204_mode_flont(tank0204_steering_val + 20);
}else if(tank0204_front_t ==1&&tank0204_steering_t==2){ }else if(tank0204_front_t ==1&&tank0204_steering_t==2){
tank0204_mode_right(tank0204_steering_val+ 40); tank0204_mode_right(tank0204_steering_val+ tank0204_count+100);
tank0204_mode_flont(tank0204_steering_val + 20); tank0204_mode_flont(tank0204_steering_val + 20);
}else if(tank0204_front_t ==2&&tank0204_steering_t==1){ }else if(tank0204_front_t ==2&&tank0204_steering_t==1){
tank0204_mode_lift(tank0204_steering_val+ 40); tank0204_mode_lift(tank0204_steering_val+ tank0204_count+100);
tank0204_mode_back(tank0204_steering_val + 20); tank0204_mode_back(tank0204_steering_val + 20);
}else if(tank0204_front_t ==2&&tank0204_steering_t==2){ }else if(tank0204_front_t ==2&&tank0204_steering_t==2){
tank0204_mode_right(tank0204_steering_val+ 40); tank0204_mode_right(tank0204_steering_val+ tank0204_count+100);
tank0204_mode_back(tank0204_steering_val + 20); tank0204_mode_back(tank0204_steering_val + 20);
} }
......
...@@ -5,38 +5,97 @@ ...@@ -5,38 +5,97 @@
#include "drivers_common.h" #include "drivers_common.h"
static ThreadPool_t *g_self_devicecontrol_task_t; static ThreadPool_t *g_self_devicecontrol_task_t;//自控任务线程函数
static bool g_self_device_control_switch_index=false;//自控开关。是否打开或者关闭 static bool g_self_device_control_switch_index=false;//自控开关。是否打开或者关闭
static int self_control_interval=0; static int g_self_control_interval=0;
static int self_control_time_count=0; static int g_self_control_time_count=0;
static pthread_mutex_t g_self_control_time_count_mutex = PTHREAD_MUTEX_INITIALIZER;
static devicecontroltask_t *g_self_device_control_date=NULL;//设备自控全局数据指针
static pthread_mutex_t g_self_device_control_date_mutex = PTHREAD_MUTEX_INITIALIZER;
/* /*
* @brief 设置g_self_devicecontrol_task_t * @brief 释放内存
*/ */
void set_device_tasks(devicecontroltask_t *new_tasks) { void free_device_tasks_memory() {
g_self_devicecontrol_task_t = new_tasks; if(g_self_device_control_date!=NULL){
pthread_mutex_lock(&g_self_device_control_date_mutex);
free(g_self_device_control_date);
g_self_device_control_date=NULL;
my_zlog_info("free g_self_device_control_date");
pthread_mutex_unlock(&g_self_device_control_date_mutex);
} else return;
} }
/* /*
* @brief 自控线程中的任务 * @brief 分配内存封装函数
*/ */
void self_device_control_task(){ void malloc_device_tasks_memory() {
static unsigned char s_valt[4]; pthread_mutex_lock(&g_self_device_control_date_mutex);
device_walk_control(g_device_type,s_valt); if(g_self_device_control_date==NULL){
device_gpio_control(g_device_type,s_valt[1],s_valt[2]); g_self_device_control_date=(devicecontroltask_t*)malloc(sizeof(devicecontroltask_t));
my_zlog_info("malloc g_self_device_control_date");
}
pthread_mutex_unlock(&g_self_device_control_date_mutex);
} }
/* /*
* @brief 自控线程中的计时函数接口,每20ms加一次 * @brief 自控线程中的计时函数接口,每20ms加一次
* @param[in] self_control_time_count,计时次数 * @param[in] self_control_time_count,计时次数
*/ */
void set_self_control_time_countfuntion(){ void set_self_control_time_countfuntion(){
self_control_time_count++; pthread_mutex_lock(&g_self_control_time_count_mutex);
g_self_control_time_count++;
if(g_self_control_time_count>30000) g_self_control_time_count=30001;
pthread_mutex_unlock(&g_self_control_time_count_mutex);
}
/*
* @brief 自控线程中的任务
*/
void self_device_control_task(){
static int i=0;
static unsigned char s_valt[3];
s_valt[1]=1;
s_valt[2]=80;
long long time_now = g_self_control_time_count*20;
if(i>=g_self_device_control_date->id_run_count){
if(g_self_control_interval<time_now){
return;
}
i=0;
}
my_zlog_debug("id_run:%d",g_self_device_control_date->id_run[i]);
my_zlog_debug("cool:%d",g_self_device_control_date->run_cool[g_self_device_control_date->id_run[i]]);
my_zlog_debug("run:%d",g_self_device_control_date->timed_run[g_self_device_control_date->id_run[i]]);
if(time_now > g_self_device_control_date->run_cool[g_self_device_control_date->id_run[i]]&&
time_now <= (g_self_device_control_date->run_cool[g_self_device_control_date->id_run[i]]+g_self_device_control_date->timed_run[g_self_device_control_date->id_run[i]])){
device_walk_control(g_device_type,s_valt);
}else if(time_now >
(g_self_device_control_date->run_cool[g_self_device_control_date->id_run[i]]+g_self_device_control_date->timed_run[g_self_device_control_date->id_run[i]]))
{
s_valt[2]=0;
device_walk_control(g_device_type,s_valt);
pthread_mutex_lock(&g_self_control_time_count_mutex);
g_self_control_time_count=0;
pthread_mutex_unlock(&g_self_control_time_count_mutex);
i=i+1;
}
} }
/* /*
* @brief 自控线程 * @brief 自控线程
*/ */
...@@ -46,6 +105,7 @@ void self_device_control_task_function(){ ...@@ -46,6 +105,7 @@ void self_device_control_task_function(){
if(g_self_device_control_switch_index==true){ if(g_self_device_control_switch_index==true){
self_device_control_task(); self_device_control_task();
}else if(g_self_device_control_switch_index==false){ }else if(g_self_device_control_switch_index==false){
delay_ms(100); delay_ms(100);
} }
} }
...@@ -63,15 +123,6 @@ void self_device_pthrpoll_task_init(){ ...@@ -63,15 +123,6 @@ void self_device_pthrpoll_task_init(){
my_zlog_info("自控线程池打开"); my_zlog_info("自控线程池打开");
} }
/*
* @brief 返回自控是否打开的标识接口
*/
bool get_self_control_index(){
return g_self_device_control_switch_index;
}
/* /*
* @brief 自控的线程开启标识设置。 * @brief 自控的线程开启标识设置。
* @param[in] 当index为,true为开,false为关 * @param[in] 当index为,true为开,false为关
...@@ -81,6 +132,13 @@ void set_self_control_index(bool index){ ...@@ -81,6 +132,13 @@ void set_self_control_index(bool index){
} }
/* /*
* @brief 获取自控的线程开启标识。
*/
bool get_self_control_index(){
return g_self_device_control_switch_index;
}
/*
* @brief 销毁自控的线程 * @brief 销毁自控的线程
*/ */
void self_control_thread_close(){ void self_control_thread_close(){
...@@ -121,7 +179,6 @@ void send_self_contorl_mqtt(){ ...@@ -121,7 +179,6 @@ void send_self_contorl_mqtt(){
cJSON_Delete(root); // 释放 cJSON 对象 cJSON_Delete(root); // 释放 cJSON 对象
} }
/* /*
* @brief MQTT接收自控消息的函数 * @brief MQTT接收自控消息的函数
*/ */
...@@ -138,26 +195,75 @@ void receive_self_contorl_mqtt(cJSON *body){ ...@@ -138,26 +195,75 @@ void receive_self_contorl_mqtt(cJSON *body){
s_switch_status=switch_status->valueint; s_switch_status=switch_status->valueint;
if(s_switch_status == DEVICE_SELF_CONTROL_OPEN){ if(s_switch_status == DEVICE_SELF_CONTROL_OPEN){
if(s_thread_index==0) self_device_pthrpoll_task_init(); if(s_thread_index==0) self_device_pthrpoll_task_init();
g_self_control_time_count=0;
g_self_device_control_switch_index =true; g_self_device_control_switch_index =true;
s_thread_index=1; s_thread_index=1;
}else if(s_switch_status == DEVICE_SELF_CONTROL_CLOSE) { }else if(s_switch_status == DEVICE_SELF_CONTROL_CLOSE) {
g_self_device_control_switch_index =false; g_self_device_control_switch_index =false;
free_device_tasks_memory();
send_self_contorl_mqtt();
return ;
} }
// 2️ id_run 数组 cJSON *interval=cJSON_GetObjectItem(body, "interval");
// cJSON *id_run = cJSON_GetObjectItem(body, "id_run"); g_self_control_interval=interval->valueint;
// g_self_devicecontrol_task_t->id_run_count = 0;
// if (cJSON_IsArray(id_run)) { malloc_device_tasks_memory();//分配内存
// int size = cJSON_GetArraySize(id_run);
// for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX; i++) {
// cJSON *item = cJSON_GetArrayItem(id_run, i);
// g_self_devicecontrol_task_t->id_run[i] = item->valueint;
// g_self_devicecontrol_task_t->id_run_count++;
// }
// }
// 2️ id_run 数组
cJSON *id_run = cJSON_GetObjectItem(body, "id_run");
g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(id_run)) {
int size = cJSON_GetArraySize(id_run);
for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX; i++) {
cJSON *item = cJSON_GetArrayItem(id_run, i);
g_self_device_control_date->id_run[i] = item->valueint-1;
g_self_device_control_date->id_run_count++;
}
}
// 3 run_cool 数组
cJSON *run_cool = cJSON_GetObjectItem(body, "run_cool");
//g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(run_cool)) {
int size = cJSON_GetArraySize(run_cool);
for (int i = 0; i < size&& i < DEVICE_WALK_SIGN_MAX ; i++) {
cJSON *item = cJSON_GetArrayItem(run_cool, i);
g_self_device_control_date->run_cool[g_self_device_control_date->id_run[i]] = item->valueint;
my_zlog_debug("run_cool:%d",g_self_device_control_date->timed_run[g_self_device_control_date->id_run[i]]);
//g_self_device_control_date->id_run_count++;
}
}
// 4 timed_run 数组
cJSON *timed_run = cJSON_GetObjectItem(body, "timed_run");
//g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(timed_run)) {
int size = cJSON_GetArraySize(timed_run);
for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX ; i++) {
cJSON *item = cJSON_GetArrayItem(timed_run, i);
g_self_device_control_date->timed_run[g_self_device_control_date->id_run[i]] = item->valueint;
my_zlog_debug("arry:%d",g_self_device_control_date->timed_run[g_self_device_control_date->id_run[i]]);
//g_self_device_control_date->id_run_count++;
}
}
send_self_contorl_mqtt(); send_self_contorl_mqtt();
my_zlog_debug("count:%d",g_self_device_control_date->id_run_count);
return; return;
}
/*
* @brief MQTT接收自控的相关的动作控制消息的函数
*/
void receive_self_contorl_date_mqtt(cJSON *body){
if (!body) {
my_zlog_error("JSON error!");
return;
}
} }
\ No newline at end of file
...@@ -20,6 +20,13 @@ typedef struct { ...@@ -20,6 +20,13 @@ typedef struct {
int timed_run[DEVICE_WALK_SIGN_MAX]; int timed_run[DEVICE_WALK_SIGN_MAX];
} devicecontroltask_t; } devicecontroltask_t;
typedef struct{
int mode[DEVICE_WALK_SIGN_MAX];
int val[DEVICE_WALK_SIGN_MAX];
int action_count;
}device_automatic_date_t;
void set_self_control_time_countfuntion();//计时 void set_self_control_time_countfuntion();//计时
void self_control_thread_close(); void self_control_thread_close();
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "drivers_common.h" #include "drivers_common.h"
#include "modules_common.h" #include "modules_common.h"
int g_heartbeat_count=0; int g_heartbeat_count=0;//心跳计数
/* /*
* *
...@@ -13,7 +13,7 @@ int g_heartbeat_count=0; ...@@ -13,7 +13,7 @@ int g_heartbeat_count=0;
pthread_mutex_t g_exit_count_mutex = PTHREAD_MUTEX_INITIALIZER;//互斥锁 pthread_mutex_t g_exit_count_mutex = PTHREAD_MUTEX_INITIALIZER;//互斥锁
int g_devcontrol_exit_count=0; int g_devcontrol_exit_count=0;
int g_message_type=0; int g_message_type=0;//接收的mqtt的g_message_type
static unsigned char g_valt[4];//存放mqtt接收的tpye,mode等 static unsigned char g_valt[4];//存放mqtt接收的tpye,mode等
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment