Commit 634d5df3 authored by 957dd's avatar 957dd

加入了开炮后再退后

parent 56071c38
cmake_minimum_required(VERSION 3.10)
project(car
VERSION 1.2.11
VERSION 1.2.12
LANGUAGES C
)
......
No preview for this file type
......@@ -9,8 +9,8 @@ int tank_shot_back_stop(unsigned char pin,unsigned char val);
const tank_common_back *g_tank_common_config_t = NULL;
long long shot_device_time_start =0 ;
long long shot_device_time_end = 0;
int g_tank_shot_index=0;//状态机,用于状态机,坦克接收射击倒退状态机全局变量
int g_tank_shot_index_cool=1;//状态机,用于冷却状态机,坦克接收射击倒退状态机全局变量
/*击打后退速度设置,混空模式下为单路,单独模式下为双路控制,后续需要优化*/
void tank_shot_back(unsigned char gval) {
......@@ -46,24 +46,24 @@ void tank_shot_back(unsigned char gval) {
const tank_common_back tank_common_config_t[]={
{
.device_id = DEVICE_TANK0202,
.back_time=25,//后退多少ms
.back_interval_back=300, //间隔多少ms才能再次使用
.shot_back_speed=80,
.back_time=30,//后退多少ms
.back_interval_back=1000, //间隔多少次才能再次使用,一次等于5ms
.shot_back_speed=90,
.shot_back=tank_shot_back_stop
},
{
.device_id = DEVICE_TANK0203,
.back_time=25,//后退多少ms
.back_interval_back=300, //间隔多少ms才能再次使用
.shot_back_speed=80,
.back_time=30,//后退多少ms
.back_interval_back=1000, //间隔多少次才能再次使用,一次等于5ms
.shot_back_speed=90,
.shot_back=tank_shot_back_stop
},
{
.device_id = DEVICE_TANK0204,
.back_time=25,//后退多少ms
.back_interval_back=300, //间隔多少ms才能再次使用
.shot_back_speed=80,
.back_time=30,//后退多少ms
.back_interval_back=1000, //间隔多少次才能再次使用,一次等于5ms
.shot_back_speed=90,
.shot_back=tank_shot_back_stop
},
......@@ -93,17 +93,19 @@ void tank_shot_back_stop_task_function(void *arg) {
// }
// }else tank_shot_back(0);
//下面一部分为射击后按时间后退
long long interval=shot_device_time_start-shot_device_time_end;
if(g_device_delay_back_count > g_tank_common_config_t->back_time&&g_device_delay_back_count < (g_tank_common_config_t->back_time+30))
tank_shot_back(0);
if(interval > g_tank_common_config_t->back_interval_back){
if(g_device_delay_back_count <= g_tank_common_config_t->back_time) {
tank_shot_back(g_tank_common_config_t->shot_back_speed);
my_zlog_debug("操作耗时: %lld 毫秒", interval);
}
if(g_device_delay_back_count <= g_tank_common_config_t->back_time) {
tank_shot_back(g_tank_common_config_t->shot_back_speed);
my_zlog_debug("击打成功");
}
else if(g_device_delay_back_count > g_tank_common_config_t->back_time&&g_device_delay_back_count < (g_tank_common_config_t->back_time+30)){
tank_shot_back(0);
}
else if(g_device_delay_back_count>g_tank_common_config_t->back_interval_back){
g_tank_shot_index = 1;
}else{
delay_ms(1);
}
}
......@@ -122,8 +124,7 @@ void tank_shot_pthrpoll_task_init(){
/*坦克后退射击*/
int tank_shot_back_stop(unsigned char pin,unsigned char val){
static int shot_count=0;
shot_device_time_start=get_current_time_millis();
if(pin !=27){
my_zlog_debug("非27引脚");
return 1;
......@@ -131,20 +132,19 @@ int tank_shot_back_stop(unsigned char pin,unsigned char val){
if(val == 0) {
tank_shot_back(0);
shot_count=2;
}
if(val != 0) {
if(shot_count == 0){
if(g_tank_shot_index == 0){
tank_shot_pthrpoll_task_init();
shot_count=1;
g_tank_shot_index=1;
}
shot_count=1;
if(shot_count == 1){
if(g_tank_shot_index == 1 && g_tank_shot_index_cool==0){
g_device_delay_back_count =0;
g_tank_shot_index = 2;
g_tank_shot_index_cool=1;
}
}
shot_device_time_end=get_current_time_millis();
}
/*销毁坦克使用的线程池,让其正常销毁,只有在tank相关设备号下才有用,最后销毁都会到device——common.h中*/
......
......@@ -17,4 +17,6 @@ void tank_shot_stop_control(int device_id,unsigned char pin,unsigned char val);
/*关闭线程*/
void tank_thread_close();
extern int g_tank_shot_index_cool;
#endif
\ No newline at end of file
......@@ -34,14 +34,14 @@ uint64_t g_tank_shot_ms=1000; //射击时间
TankFireControl g_device_shot_t; // 真正的结构体变量
/*获取时间戳函数*/
uint64_t get_shot_timestamp_ms() {
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) {
my_zlog_error("gettimeofday failed");
return 0;
}
return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
// uint64_t get_shot_timestamp_ms() {
// struct timeval tv;
// if (gettimeofday(&tv, NULL) == -1) {
// my_zlog_error("gettimeofday failed");
// return 0;
// }
// return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
// }
/**
* @brief 初始化坦克射击控制器
......@@ -72,15 +72,13 @@ int device_shot_cooling_init(){
return 0;
}
/**
* @brief 检查坦克射击状态
* @param this 控制器指针
* @return 0=允许射击, -1=禁止射击(冷却中或射击中)
*/
int device_fire_check(TankFireControl* this) {
uint64_t current_time = get_shot_timestamp_ms(); // 假设已实现此函数
uint64_t current_time = get_current_time_millis(); // 假设已实现此函数
switch (this->state) {
case TANK_STATE_READY:
......@@ -99,6 +97,7 @@ int device_fire_check(TankFireControl* this) {
my_zlog_info("shotend,cooling");
this->last_shot_end_time = current_time;
this->state = TANK_STATE_COOLDOWN;
g_tank_shot_index_cool=0;
return -1;
}
my_zlog_info("shot continue");
......
......@@ -31,5 +31,7 @@ void device_gpio_control(int device_id,int pin,int val);
int device_shot_cooling_init();
extern ThreadPool_t *g_pool_device_gpio_control_t;
extern int g_tank_shot_index;
#endif
\ No newline at end of file
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