Commit 80c9d008 authored by 957dd's avatar 957dd

加入了新线程射击打人

parent 00e9e89a
No preview for this file type
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
#include "modules_common.h" #include "modules_common.h"
#include "gpio_common.h" #include "gpio_common.h"
#define CAR0102_PIN27_PWM_DUTY 60 #define CAR0102_PIN27_PWM_DUTY 80
#define CAR0102_PIN27_STEP_MS 1000 #define CAR0102_PIN27_STEP_MS 500
#define CAR0102_PIN27_BOOTSTRAP_MS 1000
#define CAR0102_PIN27_DEBOUNCE_MS 1000
#define CAR0102_PIN27_PIN_FWD 5 #define CAR0102_PIN27_PIN_FWD 5
#define CAR0102_PIN27_PIN_REV 7 #define CAR0102_PIN27_PIN_REV 7
#define CAR0102_PIN27_IDLE_POLL_MS 10 #define CAR0102_PIN27_IDLE_POLL_MS 10
...@@ -21,7 +23,9 @@ static car0102_pin27_ctx_t s_pin27_ctx = { ...@@ -21,7 +23,9 @@ static car0102_pin27_ctx_t s_pin27_ctx = {
.pool = NULL, .pool = NULL,
.runtime.v = { .runtime.v = {
.fsm = CAR0102_PIN27_FSM_NONE, .fsm = CAR0102_PIN27_FSM_NONE,
.active = false, .bootstrap_done = false,
.shutdown = false,
.last_trigger_ms = 0,
}, },
}; };
...@@ -35,14 +39,14 @@ static void car0102_pin27_unlock(void) ...@@ -35,14 +39,14 @@ static void car0102_pin27_unlock(void)
pthread_mutex_unlock(&s_pin27_ctx.mutex); pthread_mutex_unlock(&s_pin27_ctx.mutex);
} }
static car0102_pin27_runtime_t car0102_pin27_runtime_get(void) static bool car0102_pin27_shutdown_get(void)
{ {
car0102_pin27_runtime_t rt; bool shutdown;
car0102_pin27_lock(); car0102_pin27_lock();
rt = s_pin27_ctx.runtime; shutdown = s_pin27_ctx.runtime.v.shutdown;
car0102_pin27_unlock(); car0102_pin27_unlock();
return rt; return shutdown;
} }
static void car0102_pin27_pwm_stop_locked(void) static void car0102_pin27_pwm_stop_locked(void)
...@@ -58,35 +62,18 @@ static void car0102_pin27_pwm_stop(void) ...@@ -58,35 +62,18 @@ static void car0102_pin27_pwm_stop(void)
car0102_pin27_unlock(); car0102_pin27_unlock();
} }
static void car0102_pin27_pwm_apply_locked(int fwd_duty, int rev_duty) static void car0102_pin27_pwm_apply(int fwd_duty, int rev_duty)
{ {
if (!s_pin27_ctx.runtime.v.active) {
car0102_pin27_pwm_stop_locked();
return;
}
softPwmWrite(CAR0102_PIN27_PIN_FWD, fwd_duty); softPwmWrite(CAR0102_PIN27_PIN_FWD, fwd_duty);
softPwmWrite(CAR0102_PIN27_PIN_REV, rev_duty); softPwmWrite(CAR0102_PIN27_PIN_REV, rev_duty);
} }
static void car0102_pin27_pwm_apply(int fwd_duty, int rev_duty)
{
car0102_pin27_lock();
car0102_pin27_pwm_apply_locked(fwd_duty, rev_duty);
car0102_pin27_unlock();
}
/*
* 可中断等待:每 10ms 睡眠一次并检查 active,避免忙等占满 CPU。
* 序列执行期间不持锁,防止长时间阻塞 MQTT 线程。
*/
static void car0102_pin27_delay_ms_interruptible(int ms) static void car0102_pin27_delay_ms_interruptible(int ms)
{ {
car0102_pin27_runtime_t rt;
int elapsed = 0; int elapsed = 0;
while (elapsed < ms) { while (elapsed < ms) {
rt = car0102_pin27_runtime_get(); if (car0102_pin27_shutdown_get()) {
if (!rt.v.active) {
return; return;
} }
delay_ms(CAR0102_PIN27_WAIT_SLICE_MS); delay_ms(CAR0102_PIN27_WAIT_SLICE_MS);
...@@ -94,26 +81,40 @@ static void car0102_pin27_delay_ms_interruptible(int ms) ...@@ -94,26 +81,40 @@ static void car0102_pin27_delay_ms_interruptible(int ms)
} }
} }
static void car0102_pin27_sequence_run(void) static void car0102_pin27_bootstrap_run(void)
{ {
my_zlog_info("car0102 pin27 sequence start: pin%d %d%% %dms", my_zlog_debug("car0102 pin27 bootstrap: pin%d %d%% %dms",
CAR0102_PIN27_PIN_FWD, CAR0102_PIN27_PWM_DUTY, CAR0102_PIN27_STEP_MS); CAR0102_PIN27_PIN_FWD, CAR0102_PIN27_PWM_DUTY, CAR0102_PIN27_BOOTSTRAP_MS);
car0102_pin27_pwm_apply(CAR0102_PIN27_PWM_DUTY, 0); car0102_pin27_pwm_apply(CAR0102_PIN27_PWM_DUTY, 0);
car0102_pin27_delay_ms_interruptible(CAR0102_PIN27_BOOTSTRAP_MS);
car0102_pin27_pwm_stop();
}
static void car0102_pin27_sequence_run(void)
{
my_zlog_debug("car0102 pin27 cycle: pin%d->pin%d->brake %d%% %dms",
CAR0102_PIN27_PIN_REV, CAR0102_PIN27_PIN_FWD,
CAR0102_PIN27_PWM_DUTY, CAR0102_PIN27_STEP_MS);
car0102_pin27_pwm_apply(0, CAR0102_PIN27_PWM_DUTY);
car0102_pin27_delay_ms_interruptible(CAR0102_PIN27_STEP_MS); car0102_pin27_delay_ms_interruptible(CAR0102_PIN27_STEP_MS);
if (car0102_pin27_shutdown_get()) {
car0102_pin27_pwm_stop();
return;
}
if (!car0102_pin27_runtime_get().v.active) { car0102_pin27_pwm_apply(CAR0102_PIN27_PWM_DUTY, 0);
car0102_pin27_delay_ms_interruptible(CAR0102_PIN27_STEP_MS);
if (car0102_pin27_shutdown_get()) {
car0102_pin27_pwm_stop(); car0102_pin27_pwm_stop();
return; return;
} }
car0102_pin27_pwm_apply(0, CAR0102_PIN27_PWM_DUTY); car0102_pin27_pwm_apply(CAR0102_PIN27_PWM_DUTY, CAR0102_PIN27_PWM_DUTY);
my_zlog_info("car0102 pin27 sequence step2: pin%d %d%% %dms",
CAR0102_PIN27_PIN_REV, CAR0102_PIN27_PWM_DUTY, CAR0102_PIN27_STEP_MS);
car0102_pin27_delay_ms_interruptible(CAR0102_PIN27_STEP_MS); car0102_pin27_delay_ms_interruptible(CAR0102_PIN27_STEP_MS);
car0102_pin27_pwm_stop(); car0102_pin27_pwm_stop();
my_zlog_info("car0102 pin27 sequence done");
} }
static void car0102_pin27_task_function(void *arg) static void car0102_pin27_task_function(void *arg)
...@@ -122,13 +123,21 @@ static void car0102_pin27_task_function(void *arg) ...@@ -122,13 +123,21 @@ static void car0102_pin27_task_function(void *arg)
free(arg); free(arg);
} }
my_zlog_info("car0102 pin27 thread task start"); my_zlog_debug("car0102 pin27 thread task start");
while (1) { car0102_pin27_bootstrap_run();
car0102_pin27_lock();
s_pin27_ctx.runtime.v.bootstrap_done = true;
if (s_pin27_ctx.runtime.v.fsm == CAR0102_PIN27_FSM_NONE) {
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_IDLE;
}
car0102_pin27_unlock();
while (!car0102_pin27_shutdown_get()) {
bool run_sequence = false; bool run_sequence = false;
car0102_pin27_lock(); car0102_pin27_lock();
if (s_pin27_ctx.runtime.v.fsm == CAR0102_PIN27_FSM_PENDING && if (s_pin27_ctx.runtime.v.fsm == CAR0102_PIN27_FSM_PENDING) {
s_pin27_ctx.runtime.v.active) {
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_RUNNING; s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_RUNNING;
run_sequence = true; run_sequence = true;
} }
...@@ -138,7 +147,8 @@ static void car0102_pin27_task_function(void *arg) ...@@ -138,7 +147,8 @@ static void car0102_pin27_task_function(void *arg)
car0102_pin27_sequence_run(); car0102_pin27_sequence_run();
car0102_pin27_lock(); car0102_pin27_lock();
if (s_pin27_ctx.runtime.v.fsm == CAR0102_PIN27_FSM_RUNNING) { if (!s_pin27_ctx.runtime.v.shutdown &&
s_pin27_ctx.runtime.v.fsm == CAR0102_PIN27_FSM_RUNNING) {
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_IDLE; s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_IDLE;
} }
car0102_pin27_unlock(); car0102_pin27_unlock();
...@@ -146,6 +156,8 @@ static void car0102_pin27_task_function(void *arg) ...@@ -146,6 +156,8 @@ static void car0102_pin27_task_function(void *arg)
delay_ms(CAR0102_PIN27_IDLE_POLL_MS); delay_ms(CAR0102_PIN27_IDLE_POLL_MS);
} }
my_zlog_debug("car0102 pin27 thread task exit");
} }
static int car0102_pin27_pthrpoll_task_init(void) static int car0102_pin27_pthrpoll_task_init(void)
...@@ -189,9 +201,20 @@ static int car0102_pin27_pthrpoll_task_init(void) ...@@ -189,9 +201,20 @@ static int car0102_pin27_pthrpoll_task_init(void)
return 0; return 0;
} }
void car0102_pin27_startup(int device_id)
{
if (device_id != DEVICE_CAR0102) {
return;
}
if (car0102_pin27_pthrpoll_task_init() != 0) {
my_zlog_error("car0102 pin27 startup failed");
}
}
static int car0102_pin27_handle(int pin, int val) static int car0102_pin27_handle(int pin, int val)
{ {
bool need_init = false; long long now;
int rc = 0; int rc = 0;
if (pin != 27) { if (pin != 27) {
...@@ -200,32 +223,45 @@ static int car0102_pin27_handle(int pin, int val) ...@@ -200,32 +223,45 @@ static int car0102_pin27_handle(int pin, int val)
car0102_pin27_lock(); car0102_pin27_lock();
if (s_pin27_ctx.runtime.v.shutdown) {
car0102_pin27_unlock();
return 0;
}
/* 松手 val=0 不参与电机驱动控制,长按/点按均只由 val=1 触发 */
if (val == 0) { if (val == 0) {
s_pin27_ctx.runtime.v.active = false;
car0102_pin27_pwm_stop_locked();
car0102_pin27_unlock(); car0102_pin27_unlock();
return 0; return 0;
} }
s_pin27_ctx.runtime.v.active = true; if (!s_pin27_ctx.runtime.v.bootstrap_done) {
car0102_pin27_unlock();
return 0;
}
if (s_pin27_ctx.runtime.v.fsm == CAR0102_PIN27_FSM_NONE) { if (s_pin27_ctx.runtime.v.fsm != CAR0102_PIN27_FSM_IDLE) {
need_init = true; car0102_pin27_unlock();
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_IDLE; return 0;
} }
if (s_pin27_ctx.runtime.v.fsm == CAR0102_PIN27_FSM_IDLE) { now = get_current_time_millis();
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_PENDING; if (s_pin27_ctx.runtime.v.last_trigger_ms > 0 &&
(now - s_pin27_ctx.runtime.v.last_trigger_ms) < CAR0102_PIN27_DEBOUNCE_MS) {
car0102_pin27_unlock();
return 0;
} }
s_pin27_ctx.runtime.v.last_trigger_ms = now;
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_PENDING;
car0102_pin27_unlock(); car0102_pin27_unlock();
if (need_init) { if (s_pin27_ctx.pool == NULL) {
rc = car0102_pin27_pthrpoll_task_init(); rc = car0102_pin27_pthrpoll_task_init();
if (rc != 0) { if (rc != 0) {
car0102_pin27_lock(); car0102_pin27_lock();
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_NONE; s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_NONE;
s_pin27_ctx.runtime.v.active = false; s_pin27_ctx.runtime.v.bootstrap_done = false;
s_pin27_ctx.runtime.v.last_trigger_ms = 0;
car0102_pin27_unlock(); car0102_pin27_unlock();
} }
} }
...@@ -246,11 +282,13 @@ void car0102_pin27_thread_close(void) ...@@ -246,11 +282,13 @@ void car0102_pin27_thread_close(void)
ThreadPool_t *pool = NULL; ThreadPool_t *pool = NULL;
car0102_pin27_lock(); car0102_pin27_lock();
s_pin27_ctx.runtime.v.active = false; s_pin27_ctx.runtime.v.shutdown = true;
car0102_pin27_pwm_stop_locked(); car0102_pin27_pwm_stop_locked();
pool = s_pin27_ctx.pool; pool = s_pin27_ctx.pool;
s_pin27_ctx.pool = NULL; s_pin27_ctx.pool = NULL;
s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_NONE; s_pin27_ctx.runtime.v.fsm = CAR0102_PIN27_FSM_NONE;
s_pin27_ctx.runtime.v.bootstrap_done = false;
s_pin27_ctx.runtime.v.last_trigger_ms = 0;
car0102_pin27_unlock(); car0102_pin27_unlock();
if (pool != NULL) { if (pool != NULL) {
......
...@@ -3,26 +3,29 @@ ...@@ -3,26 +3,29 @@
#include "common.h" #include "common.h"
/* pin27 正反转序列状态机 */ /* pin27 正反转+刹车序列状态机 */
typedef enum { typedef enum {
CAR0102_PIN27_FSM_NONE = 0, /* 未创建线程池 */ CAR0102_PIN27_FSM_NONE = 0, /* 未创建线程池 */
CAR0102_PIN27_FSM_IDLE, /* 线程空闲,可接收新触发 */ CAR0102_PIN27_FSM_IDLE, /* 空闲,可接收新触发 */
CAR0102_PIN27_FSM_PENDING, /* 已入队,待工作线程执行 */ CAR0102_PIN27_FSM_PENDING, /* 已入队,待工作线程执行 */
CAR0102_PIN27_FSM_RUNNING, /* 正反转序列执行中 */ CAR0102_PIN27_FSM_RUNNING, /* 5/7 循环与刹车执行中 */
} car0102_pin27_fsm_t; } car0102_pin27_fsm_t;
/* 受互斥锁保护的运行态(fsm + active 成组读写) */
typedef union { typedef union {
struct { struct {
car0102_pin27_fsm_t fsm; car0102_pin27_fsm_t fsm;
bool active; bool bootstrap_done;
bool shutdown;
long long last_trigger_ms;
} v; } v;
} car0102_pin27_runtime_t; } car0102_pin27_runtime_t;
/* 0102 收到 pin27 时触发正反转序列(仿坦克 shot_back 接口) */ /* 设备启动时拉起线程并完成 pin7 预定位 */
void car0102_pin27_startup(int device_id);
/* 0102 收到 pin27 val=1 触发循环;val=0 忽略,不参与电机驱动 */
void car0102_pin27_stop_control(int device_id, int pin, int val); void car0102_pin27_stop_control(int device_id, int pin, int val);
/* 程序退出时销毁 pin27 线程池 */
void car0102_pin27_thread_close(void); void car0102_pin27_thread_close(void);
#endif #endif
...@@ -97,8 +97,8 @@ static const device_abnormal_close_t s_devcontrol_config[]= { ...@@ -97,8 +97,8 @@ static const device_abnormal_close_t s_devcontrol_config[]= {
.device_id = DEVICE_CAR0102, .device_id = DEVICE_CAR0102,
.device_abnormal_stop = car0102_speed_stop, .device_abnormal_stop = car0102_speed_stop,
.device_close = car0102_pin27_thread_close, .device_close = car0102_pin27_thread_close,
.gpio_pin_pulled=pin_all_default, .gpio_pin_pulled = car0102_pin_all_default,
.gpio_pwm_pulled=pwm_all_default .gpio_pwm_pulled = car0102_pwm_all_default
}, },
{ {
......
...@@ -238,7 +238,7 @@ void device_poilthread_close(){ ...@@ -238,7 +238,7 @@ void device_poilthread_close(){
void tank_shot_stop_control(int device_id,int pin,int val) { void tank_shot_stop_control(int device_id,int pin,int val) {
if (!s_tank_common_config || s_tank_common_config->device_id != device_id) { if (!s_tank_common_config || s_tank_common_config->device_id != device_id) {
// 查找设备配置 s_tank_common_config = NULL;
for(int i = 0; s_tank_common_configs[i].device_id != -1; i++) { for(int i = 0; s_tank_common_configs[i].device_id != -1; i++) {
if(s_tank_common_configs[i].device_id == device_id) { if(s_tank_common_configs[i].device_id == device_id) {
s_tank_common_config = &s_tank_common_configs[i]; s_tank_common_config = &s_tank_common_configs[i];
......
...@@ -264,6 +264,10 @@ void device_init(int device_id) { ...@@ -264,6 +264,10 @@ void device_init(int device_id) {
config->device_pwm_init(); // PWM初始化 config->device_pwm_init(); // PWM初始化
config->device_control_stop(); // 速度控制初始化 config->device_control_stop(); // 速度控制初始化
if (device_id == DEVICE_CAR0102) {
car0102_pin27_startup(device_id);
}
my_zlog_debug("%s initialized successfully!", config->device_name); my_zlog_debug("%s initialized successfully!", config->device_name);
} }
\ No newline at end of file
...@@ -452,7 +452,8 @@ void car0102_pin_value(int pin,int value) { //引脚控制 ...@@ -452,7 +452,8 @@ void car0102_pin_value(int pin,int value) { //引脚控制
return; return;
} }
if (pin == 27) { if (pin == 27) {
/* pin27 正反转序列由 car0102_pin27_stop_control 线程处理,不做 digitalWrite */ /* pin27 由独立线程处理 5/7 循环与刹车,不走电机驱动 GPIO 路径 */
car0102_pin27_stop_control(g_device_type, pin, value);
return; return;
} }
if(value==1) { if(value==1) {
......
...@@ -73,12 +73,31 @@ void tank0204_pin_all_default() {//全部至低电平,坦克0204专用 ...@@ -73,12 +73,31 @@ void tank0204_pin_all_default() {//全部至低电平,坦克0204专用
} }
} }
void car0102_pin_all_default() {//car0102异常停止:pin5/7由pin27序列管理,不拉低
for (int i = 0; i < g_gpiocount; i++) {
if (g_gpiowpi[i] == 16 || g_gpiowpi[i] == 20 || g_gpiowpi[i] == 22 ||
g_gpiowpi[i] == 5 || g_gpiowpi[i] == 7) {
continue;
}
digitalWrite(g_gpiowpi[i], LOW);
}
}
void pwm_all_default() {//全部至低电平,车和坦克共用 void pwm_all_default() {//全部至低电平,车和坦克共用
for (int i=0;i<g_gpio_softpwmcount;i++) { for (int i=0;i<g_gpio_softpwmcount;i++) {
softPwmWrite(g_gpioPwm[i], 0); softPwmWrite(g_gpioPwm[i], 0);
} }
} }
void car0102_pwm_all_default() {//car0102异常停止:pin5/7由pin27序列管理,不置0
for (int i = 0; i < g_gpio_softpwmcount; i++) {
if (g_gpioPwm[i] == 5 || g_gpioPwm[i] == 7) {
continue;
}
softPwmWrite(g_gpioPwm[i], 0);
}
}
void tankpwm_default() {//全部至低电平,车和坦克共用 void tankpwm_default() {//全部至低电平,车和坦克共用
for (int i=0;i<g_gpio_softpwmcount;i++) { for (int i=0;i<g_gpio_softpwmcount;i++) {
......
...@@ -24,8 +24,10 @@ void init_gpio_input(const int *values_pwm);//gpio读取 ...@@ -24,8 +24,10 @@ void init_gpio_input(const int *values_pwm);//gpio读取
// 定义 PWM 引脚的 WiringPi 编号 // 定义 PWM 引脚的 WiringPi 编号
void pin_all_default();//拉低车的控制引脚 void pin_all_default();//拉低车的控制引脚
void tank0204_pin_all_default();//拉低坦克专用的控制引脚 void tank0204_pin_all_default();//拉低坦克专用的控制引脚
void car0102_pin_all_default();//car0102异常停止时保留pin5/7
void pwm_all_default();//软件pwm置0引脚 void pwm_all_default();//软件pwm置0引脚
void car0102_pwm_all_default();//car0102异常停止时保留pin5/7软PWM
void physics_pwm_init(); void physics_pwm_init();
void tankpwm_default();//坦克专用pwm置0引脚 void tankpwm_default();//坦克专用pwm置0引脚
......
...@@ -93,7 +93,6 @@ void self_device_control_task(){ ...@@ -93,7 +93,6 @@ void self_device_control_task(){
if(valt[2]>2) device_walk_control(g_device_type,valt); if(valt[2]>2) device_walk_control(g_device_type,valt);
else if(valt[2]<=2) { else if(valt[2]<=2) {
device_gpio_control(g_device_type,valt[1],valt[2]); device_gpio_control(g_device_type,valt[1],valt[2]);
car0102_pin27_stop_control(g_device_type, valt[1], valt[2]);
} }
} }
......
...@@ -195,8 +195,9 @@ void message_4(cJSON *body) ...@@ -195,8 +195,9 @@ void message_4(cJSON *body)
device_gpio_control(g_device_type, s_valt[1], s_valt[2]); device_gpio_control(g_device_type, s_valt[1], s_valt[2]);
tank_shot_stop_control(g_device_type, s_valt[1], s_valt[2]); if (g_device_type != DEVICE_CAR0102) {
car0102_pin27_stop_control(g_device_type, s_valt[1], s_valt[2]); tank_shot_stop_control(g_device_type, s_valt[1], s_valt[2]);
}
} }
// 当接收到2时候验证 // 当接收到2时候验证
......
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