Commit 743a2a89 authored by 957dd's avatar 957dd

加入了设备自控

parent 059c3110
......@@ -42,7 +42,6 @@ void *thread_exit_time(void *arg) {
while(1){
if(get_self_control_index()==false){
delay_ms(100);
pthread_mutex_lock(&g_exit_count_mutex);
g_devcontrol_exit_count++;
if(g_devcontrol_exit_count>=5) {
......
No preview for this file type
......@@ -29,6 +29,15 @@
#define DEVICE_PAO_PTZ0401 401 //云台
#define DEVICE_ROBOT_DOG0501 501 //机械狗
/*
*以下为大类
*/
#define LAND_CAR 1
#define MARINE_TANK 2
#define WATER_TANK 3
#define WATER_SHIP 4
typedef struct {
int device_id; // 设备ID (101, 102等)
void (*device_abnormal_stop)(void); // 车停止
......
......@@ -14,30 +14,11 @@ int g_tank_shot_index_cool=1;//状态机,用于冷却状态机,坦克接收射
void tank_shot_back(unsigned char gval) {
int b=0;
if (gval < 50) {
pwmWrite(PWM_PIN_CHANGE, 75);
pwmWrite(PWM_PIN_SPEED, 75);
}else if (gval <= 60) {
if(g_tank_common_config_t->device_id ==DEVICE_TANK0202) pwmWrite(PWM_PIN_CHANGE, 71);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0203) pwmWrite(PWM_PIN_CHANGE, 79);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0203) pwmWrite(PWM_PIN_SPEED, 71);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0204) pwmWrite(PWM_PIN_CHANGE, 79);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0204) pwmWrite(PWM_PIN_SPEED, 71);
}else if (gval <= 70) {
if(g_tank_common_config_t->device_id ==DEVICE_TANK0202) pwmWrite(PWM_PIN_CHANGE, 70);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0203)pwmWrite(PWM_PIN_CHANGE, 80);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0203) pwmWrite(PWM_PIN_SPEED, 70);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0204)pwmWrite(PWM_PIN_CHANGE, 80);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0204) pwmWrite(PWM_PIN_SPEED, 70);
}else if(gval >70){
int change_1 = 80+(gval-70)/10+b;
int speed_2= 70 - (gval-70)/10-b;
if(g_tank_common_config_t->device_id ==DEVICE_TANK0202) pwmWrite(PWM_PIN_CHANGE, speed_2);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0203) pwmWrite(PWM_PIN_SPEED, speed_2);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0203) pwmWrite(PWM_PIN_CHANGE, change_1);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0204) pwmWrite(PWM_PIN_SPEED, speed_2);
if(g_tank_common_config_t->device_id ==DEVICE_TANK0204) pwmWrite(PWM_PIN_CHANGE, change_1);
}
unsigned char valt[3]={0};
valt[1]=2;
if(gval==0) valt[2]=0;
else valt[2]=gval;
device_walk_control(g_device_type,valt);
}
......@@ -104,7 +85,6 @@ void tank_shot_back_stop_task_function(void *arg) {
delay_ms(1);
}
}
}
......
#include "common.h"
#include "app_device_common.h"
#include "drivers_common.h"
#include "modules_common.h"
#include "gpio_control.h"
#include "tank_angle.h"
#include "device_init.h"
#include "gpio_init.h"
#include "devcontrol_common.h"
#include "http_request.h"
#define GPIO_ID_THREAD_COUNT 3
......@@ -27,24 +25,20 @@ void tank0206_pwm_value(int pin,int value);
void ship0301_pwm_value(int pin,int value);
void dog0501_pwm_value(int pin,int value);
uint64_t g_tank_shot_interval_ms=5000; //冷却时间
uint64_t g_tank_shot_ms=1000; //射击时间
TankFireControl g_device_shot_t; // 真正的结构体变量
/**
* @brief 初始化坦克射击控制器
* @param this 控制器指针
* @param interval_ms 冷却时间(毫秒)
* @param duration_ms 射击持续时间(毫秒)
* @param shot_interval_ms 冷却时间(毫秒)
* @param shot_duration_ms 射击持续时间(毫秒)
*/
void device_shot_fire_init(TankFireControl* this, uint32_t interval_ms, uint32_t duration_ms) {
void device_shot_fire_init(TankFireControl* this) {
this->last_shot_end_time = 0;
this->shooting_start_time = 0;
this->state = TANK_STATE_READY;
this->shot_interval_ms = interval_ms;
this->shot_duration_ms = duration_ms;
this->shot_interval_ms = 5000;
this->shot_duration_ms = 1000;
}
/*
......@@ -52,14 +46,14 @@ void device_shot_fire_init(TankFireControl* this, uint32_t interval_ms, uint32_t
**对需要射击冷却的进行初始化
*/
int device_shot_cooling_init(){
if(g_device_type ==DEVICE_TANK0202||g_device_type ==DEVICE_TANK0203){
my_zlog_info("using %d shot init",g_device_type);
device_shot_fire_init(&g_device_shot_t,g_tank_shot_interval_ms,g_tank_shot_ms);
}else {
return -1;
for(int i=0;i<GPIO_ID_THREAD_COUNT;i++){
if(g_device_type==gpio_device_id[i]){
my_zlog_info("using %d shot init",g_device_type);
device_shot_fire_init(&g_device_shot_t);
return 0;
}
}
return 0;
return -1;
}
/**
......@@ -110,7 +104,7 @@ int device_fire_check(TankFireControl* this) {
int device_shoting_check(int pin,int val){
if(device_fire_check(&g_device_shot_t)!=0){
softPwmWrite(pin, 0);
}else{
}else if(device_fire_check(&g_device_shot_t)==0){
softPwmWrite(pin, val);
}
}
......@@ -149,7 +143,7 @@ void tank_angle_limit_function(void *arg_gpio){
/*角度限位线程池初始化*/
void device_gpio_control_threadpoll_init(){
int *arg_gpio = malloc(sizeof(int));
my_zlog_info("device_gpio_control_threadpoll_init start\n");
my_zlog_info("device_gpio_control_threadpoll_init start");
*arg_gpio = 2;
g_pool_device_gpio_control_t = thread_pool_init(1,1);
thread_pool_add_task(g_pool_device_gpio_control_t, tank_angle_limit_function, arg_gpio);
......@@ -159,59 +153,70 @@ void device_gpio_control_threadpoll_init(){
const gpiocontrol_t gpio_configs[] = {
{
.device_id = DEVICE_CAR0101,
.category_id=LAND_CAR,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_CAR0102,
.category_id=LAND_CAR,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_CAR0103,
.category_id=LAND_CAR,
.device_pin_value =car0103_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_CAR0104,
.category_id=LAND_CAR,
.device_pin_value =car0104_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_TANK0202,
.category_id=MARINE_TANK,
.device_pin_value =public_pin_value,
.device_pwm_value =tank0202_pwm_value,
.device_gpio_pthread_create=device_gpio_control_threadpoll_init
},
{
.device_id = DEVICE_TANK0203,
.category_id=MARINE_TANK,
.device_pin_value =public_pin_value,
.device_pwm_value =tank0203_pwm_value,
.device_gpio_pthread_create=device_gpio_control_threadpoll_init
},
{
.device_id = DEVICE_TANK0204,
.category_id=MARINE_TANK,
.device_pin_value =public_pin_value,
.device_pwm_value =tank0204_pwm_value,
.device_gpio_pthread_create=device_gpio_control_threadpoll_init
},
{
.device_id = DEVICE_TANK0206,
.category_id=WATER_TANK,
.device_pin_value =public_pin_value,
.device_pwm_value =tank0206_pwm_value
},
{
.device_id = DEVICE_SHIP0301,
.category_id=WATER_SHIP,
.device_pin_value =public_pin_value,
.device_pwm_value =ship0301_pwm_value
},
{
.device_id = DEVICE_PAO_PTZ0401,
.category_id=0,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_ROBOT_DOG0501,
.category_id=0,
.device_pin_value =public_pin_value,
.device_pwm_value =dog0501_pwm_value
},
......
......@@ -5,6 +5,7 @@
typedef struct {
int device_id; // 设备ID (101, 102等)备名称
int category_id;
void (*device_pwm_value)(int pin,int val); // PWM初始化函数指针
void (*device_pin_value)(int pin,int value);// 速度控制函数指针
void (*device_gpio_pthread_create)(void);
......
......@@ -17,6 +17,8 @@ static pthread_mutex_t g_self_control_time_count_mutex = PTHREAD_MUTEX_INITIALIZ
static devicecontroltask_t *g_self_device_control_date=NULL;//设备自控全局数据指针
static pthread_mutex_t g_self_device_control_date_mutex = PTHREAD_MUTEX_INITIALIZER;
static device_automatic_date_t g_automatic_date_t[DEVICE_WALK_SIGN_MAX]={0};
/*
* @brief 释放内存
*/
......@@ -61,11 +63,10 @@ void set_self_control_time_countfuntion(){
*/
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;
static unsigned char valt[3];
int time_now = g_self_control_time_count*20;
if(i>=g_self_device_control_date->id_run_count){
......@@ -75,20 +76,29 @@ void self_device_control_task(){
}
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);
if(time_now > g_self_device_control_date->run_cool[i]&&
time_now <= (g_self_device_control_date->run_cool[i]+g_self_device_control_date->timed_run[i]))
{
for(int j=0;j<g_automatic_date_t[g_self_device_control_date->id_run[i]].action_count;j++){
valt[1]=g_automatic_date_t[g_self_device_control_date->id_run[i]].mode[j];
valt[2]=g_automatic_date_t[g_self_device_control_date->id_run[i]].val[j];
if(valt[2]>2) device_walk_control(g_device_type,valt);
else if(valt[2]<=2) device_gpio_control(g_device_type,valt[1],valt[2]);
my_zlog_debug("date:%d",g_automatic_date_t[g_self_device_control_date->id_run[i]].action_count);
my_zlog_debug("mode:%d",g_automatic_date_t[g_self_device_control_date->id_run[i]].mode[j]);
my_zlog_debug("val:%d",g_automatic_date_t[g_self_device_control_date->id_run[i]].val[j]);
}
}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]]))
(g_self_device_control_date->run_cool[i]+g_self_device_control_date->timed_run[i]))
{
s_valt[2]=0;
device_walk_control(g_device_type,s_valt);
device_stop(g_device_type);//停止
pthread_mutex_lock(&g_self_control_time_count_mutex);
g_self_control_time_count=0;
g_self_control_time_count=0;//将计时重置
pthread_mutex_unlock(&g_self_control_time_count_mutex);
i=i+1;
}
......@@ -101,11 +111,16 @@ void self_device_control_task(){
*/
void self_device_control_task_function(){
my_zlog_info("自控线程池循环开始");
static int s_status =0;
while(1){
if(g_self_device_control_switch_index==true){
self_device_control_task();
s_status=1;
}else if(g_self_device_control_switch_index==false){
if(s_status==1) {
device_stop(g_device_type);
s_status=0;
}
delay_ms(100);
}
}
......@@ -170,7 +185,37 @@ void send_self_contorl_mqtt(){
// 将cJSON对象转换为字符串
char *json_str = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",json_str);
my_zlog_info("%s",json_str);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(json_str), json_str, 0, false);
}
cJSON_Delete(root); // 释放 cJSON 对象
}
/*
* @brief MQTT发送自控的相关的动作控制消息的函数,找后端要动作组合
*/
void send_self_contorl_date_mqtt(){
// 创建根对象
cJSON *root = cJSON_CreateObject();
// 创建head对象
cJSON *head = cJSON_CreateObject();
cJSON_AddNumberToObject(head, "message_type", 3019);
cJSON_AddItemToObject(root, "head", head);
// 创建body对象
cJSON *body = cJSON_CreateObject();
cJSON_AddStringToObject(body, "cmd", "device_control_demand_date");
cJSON_AddNumberToObject(body, "status", 1);
cJSON_AddItemToObject(root, "body", body);
// 将cJSON对象转换为字符串
char *json_str = cJSON_PrintUnformatted(root);
my_zlog_info("%s",json_str);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(json_str), json_str, 0, false);
......@@ -179,6 +224,7 @@ void send_self_contorl_mqtt(){
cJSON_Delete(root); // 释放 cJSON 对象
}
/*
* @brief MQTT接收自控消息的函数
*/
......@@ -194,7 +240,10 @@ void receive_self_contorl_mqtt(cJSON *body){
static bool s_thread_index=0;
s_switch_status=switch_status->valueint;
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();
send_self_contorl_date_mqtt();//找后端要动作组合
}
g_self_control_time_count=0;
g_self_device_control_switch_index =true;
s_thread_index=1;
......@@ -217,7 +266,7 @@ void receive_self_contorl_mqtt(cJSON *body){
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[i] = item->valueint;
g_self_device_control_date->id_run_count++;
}
}
......@@ -229,8 +278,7 @@ void receive_self_contorl_mqtt(cJSON *body){
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->run_cool[i] = item->valueint;
//g_self_device_control_date->id_run_count++;
}
}
......@@ -242,28 +290,67 @@ void receive_self_contorl_mqtt(cJSON *body){
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->timed_run[i] = item->valueint;
//g_self_device_control_date->id_run_count++;
}
}
send_self_contorl_mqtt();
my_zlog_debug("count:%d",g_self_device_control_date->id_run_count);
my_zlog_info("count:%d",g_self_device_control_date->id_run_count);
return;
}
/*
* @brief MQTT接收自控的相关的动作控制消息的函数
* @brief MQTT接收自控的相关的动作控制消息的函数,接收后端相应动作组合
*/
void receive_self_contorl_date_mqtt(cJSON *body){
if (!body) {
my_zlog_error("JSON error!");
return;
}
}
\ No newline at end of file
cJSON *switch_status = cJSON_GetObjectItem(body, "switch_status");
static int s_switch_status = 0;
s_switch_status=switch_status->valueint;
cJSON *action_id = cJSON_GetObjectItem(body, "action_id");
static int s_action_id = 0;
s_action_id=action_id->valueint;
if(s_switch_status == 1){
// 2️ id_run 数组
cJSON *action_combination = cJSON_GetObjectItem(body, "action_combination");
if (cJSON_IsArray(action_combination)) {
int size = cJSON_GetArraySize(action_combination);
for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX; i++) {
cJSON *item = cJSON_GetArrayItem(action_combination, i);
g_automatic_date_t[s_action_id].mode[i]=item->valueint;
}
}
cJSON *action_speed = cJSON_GetObjectItem(body, "action_speed");
if (cJSON_IsArray(action_speed)) {
int size = cJSON_GetArraySize(action_speed);
for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX; i++) {
cJSON *item = cJSON_GetArrayItem(action_speed, i);
g_automatic_date_t[s_action_id].val[i]=item->valueint;
g_automatic_date_t[s_action_id].action_count++;
}
}
}else if(s_switch_status == 0){
memset(g_automatic_date_t[s_action_id].mode, 0, sizeof(g_automatic_date_t[s_action_id].mode));
memset(g_automatic_date_t[s_action_id].val, 0, sizeof(g_automatic_date_t[s_action_id].val));
}
my_zlog_info("action_count:%d",g_automatic_date_t[s_action_id].action_count);
return;
}
......@@ -6,7 +6,7 @@
* @brief 设备自己控制模块头文件
*/
#define DEVICE_WALK_SIGN_MAX 10
#define DEVICE_WALK_SIGN_MAX 100
#define DEVICE_SELF_CONTROL_OPEN 1
......@@ -20,19 +20,22 @@ typedef struct {
int timed_run[DEVICE_WALK_SIGN_MAX];
} 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 self_control_thread_close();
void receive_self_contorl_mqtt(cJSON *body);
void receive_self_contorl_date_mqtt(cJSON *body);//数据保存
bool get_self_control_index();
void set_self_control_index(bool index);
......
......@@ -326,6 +326,9 @@ int device_message_receive(cJSON *json){//接收到的控制设备的mqtt消息
receive_self_contorl_mqtt(body);
my_zlog_debug("device self comtrol");
break;
case 2026:
receive_self_contorl_date_mqtt(body);
my_zlog_debug("device self comtrol date save");
default:
break;
}
......
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