Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
car-controlserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenzhongjian
car-controlserver
Commits
a2d093f8
Commit
a2d093f8
authored
Aug 05, 2025
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
加入了程序限位
parent
85f391b7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
149 additions
and
170 deletions
+149
-170
main
build/main
+0
-0
tank_common.c
drivers/devicecontrol/tank_common.c
+5
-7
gpio_control.c
drivers/gpio/gpio_control.c
+37
-22
gpio_init.c
drivers/gpio/gpio_init.c
+0
-2
tank_angle.c
drivers/sensors/tank_angle.c
+2
-2
http_request.h
modules/http/http_request.h
+1
-1
pthrpoll.c
modules/thread_pool/pthrpoll.c
+52
-109
pthrpoll.h
modules/thread_pool/pthrpoll.h
+52
-27
No files found.
build/main
View file @
a2d093f8
No preview for this file type
drivers/devicecontrol/tank_common.c
View file @
a2d093f8
...
...
@@ -56,6 +56,9 @@ const tank_common_back tank_common_config_t[]={
void
tank_shot_back_stop_task_function
(
void
*
arg
)
{
//多线程处理坦克发射后退线程池
if
(
arg
!=
NULL
){
free
(
arg
);
}
while
(
1
){
long
long
interval
=
shot_device_time_start
-
shot_device_time_end
;
if
(
g_device_delay_count
>
g_tank_common_config_t
->
back_time
&&
g_device_delay_count
<
(
g_tank_common_config_t
->
back_time
+
30
))
...
...
@@ -69,7 +72,7 @@ void tank_shot_back_stop_task_function(void *arg) {//多线程处理坦克发射
}
}
free
(
arg
);
}
ThreadPool_t
*
pool_tank_t
;
...
...
@@ -78,7 +81,7 @@ void tank_shot_pthrpoll_task_init(){
int
*
arg
=
malloc
(
sizeof
(
int
));
*
arg
=
1
;
pool_tank_t
=
thread_pool_init
(
1
,
1
);
thread_pool_add_task
(
pool_tank_t
,
tank_shot_back_stop_task_function
,
&
arg
);
thread_pool_add_task
(
pool_tank_t
,
tank_shot_back_stop_task_function
,
arg
);
my_zlog_debug
(
"线程池打开"
);
}
...
...
@@ -110,13 +113,8 @@ int tank_shot_back_stop(unsigned char pin,unsigned char val){
/*销毁坦克使用的线程池,让其正常销毁,只有在tank相关设备号下才有用,最后销毁都会到device——common.h中*/
void
tank_thread_close
(){
if
(
pool_tank_t
!=
NULL
){
thread_pool_destroy
(
pool_tank_t
);
}
if
(
g_pool_device_gpio_control_t
!=
NULL
){
thread_pool_destroy
(
g_pool_device_gpio_control_t
);
}
}
void
tank_shot_stop_control
(
int
device_id
,
unsigned
char
pin
,
unsigned
char
val
)
{
...
...
drivers/gpio/gpio_control.c
View file @
a2d093f8
...
...
@@ -10,23 +10,48 @@
const
gpiocontrol_t
*
gpio_control_config_t
=
NULL
;
ThreadPool_t
*
g_pool_device_gpio_control_t
=
NULL
;
void
tank_angle_limit_function
(
void
*
arg
);
ThreadPool_t
*
g_pool_device_gpio_control_t
;
static
bool
s_poll_tank_index
=
0
;
void
public_pin_value
(
int
pin
,
int
value
);
void
public_pwm_value
(
int
pin
,
int
value
);
void
tank0202_pwm_value
(
int
pin
,
int
value
);
void
tank0203_pwm_value
(
int
pin
,
int
value
);
void
tank0206_pwm_value
(
int
pin
,
int
value
);
void
tank_angle_limit_function
(
void
*
arg_gpio
){
if
(
arg_gpio
!=
NULL
)
{
free
(
arg_gpio
);
}
printf
(
"limit task started.
\n
"
);
while
(
1
){
int
limit_status
=
angle_limit
();
if
(
limit_status
==
1
)
{
device_gpio_control
(
g_device_type
,
5
,
0
);
my_zlog_debug
(
"lift limit stop"
);
}
else
if
(
limit_status
==
2
)
{
device_gpio_control
(
g_device_type
,
7
,
0
);
my_zlog_debug
(
"right limit stop"
);
}
else
if
(
limit_status
==
0
)
{
delay_ms
(
5
);
my_zlog_debug
(
"limit stop"
);
}
}
free
(
arg_gpio
);
}
void
device_gpio_control_threadpoll_init
(){
int
*
arg
=
malloc
(
sizeof
(
int
));
*
arg
=
1
;
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
);
int
*
arg_gpio
=
malloc
(
sizeof
(
int
));
my_zlog_info
(
"device_gpio_control_threadpoll_init start
\n
"
);
*
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
);
}
...
...
@@ -91,9 +116,10 @@ void device_gpio_control(int device_id,int pin,int val) {
}
if
(
gpio_control_config_t
&&
(
gpio_control_config_t
->
device_id
==
DEVICE_TANK0202
||
gpio_control_config_t
->
device_id
==
DEVICE_TANK0203
))
(
device_id
==
DEVICE_TANK0202
||
device_id
==
DEVICE_TANK0203
))
{
gpio_control_config_t
->
device_gpio_pthread_create
;
//创建线程,线程关闭在tank.common.h中何tank需要的其他线程关闭
my_zlog_info
(
"线程函数:%d
\n
"
,
device_id
);
gpio_control_config_t
->
device_gpio_pthread_create
();
//创建线程,线程关闭在tank.common.h中何tank需要的其他线程关闭
}
}
...
...
@@ -167,8 +193,7 @@ void tank0202_pwm_value(int pin,int value) { //软件陪我们控制调速
if
(
pin
==
27
){
softPwmWrite
(
pin
,
50
);
}
else
{
if
(
angle_limit
()
==
0
&&
(
pin
==
7
||
pin
==
5
))
softPwmWrite
(
pin
,
30
);
if
(
pin
!=
7
&&
pin
!=
5
)
softPwmWrite
(
pin
,
30
);
softPwmWrite
(
pin
,
30
);
my_zlog_debug
(
"pwm:%d"
,
pin
);
}
...
...
@@ -193,8 +218,7 @@ void tank0203_pwm_value(int pin,int value) { //软件陪我们控制调速
if
(
pin
==
27
){
softPwmWrite
(
pin
,
45
);
}
else
{
if
(
angle_limit
()
==
0
&&
(
pin
==
7
||
pin
==
5
))
softPwmWrite
(
pin
,
30
);
if
(
pin
!=
7
&&
pin
!=
5
)
softPwmWrite
(
pin
,
30
);
softPwmWrite
(
pin
,
30
);
my_zlog_debug
(
"pwm:%d"
,
pin
);
}
...
...
@@ -230,11 +254,3 @@ void tank0206_pwm_value(int pin,int value) { //软件陪我们控制调速
my_zlog_debug
(
"tank0206 pwm"
);
}
void
tank_angle_limit_function
(
void
*
arg
){
while
(
1
){
if
(
angle_limit
()
==
1
)
device_gpio_control
(
g_device_type
,
5
,
0
);
else
if
(
angle_limit
()
==
2
)
device_gpio_control
(
g_device_type
,
7
,
0
);
else
if
(
angle_limit
()
==
0
)
delay_ms
(
5
);
}
}
\ No newline at end of file
drivers/gpio/gpio_init.c
View file @
a2d093f8
...
...
@@ -54,8 +54,6 @@ void pwm_all_default() {//全部至低电平,车和坦克共用
}
/*物理pwm初始化*/
void
physics_pwm_init
()
{
int
pwm_clock
=
24000000
/
(
50
*
1000
);
// 定义 PWM 频率为 50Hz
...
...
drivers/sensors/tank_angle.c
View file @
a2d093f8
...
...
@@ -2,9 +2,9 @@
#include "common.h"
#include "ads1115.h"
#define LIFT_LIMIT 1
65
#define LIFT_LIMIT 1
70
#define MIDDLE_LIMIT 180
#define RIGHT_LIMIT
19
5
#define RIGHT_LIMIT
20
5
double
tank_angle
(){
double
angle
=
0
;
...
...
modules/http/http_request.h
View file @
a2d093f8
...
...
@@ -4,7 +4,7 @@
#include"common.h"// 用于存储HTTP响应数据的结构体
/*2为关闭请求,1为打开*/
#define HTTP_REQUEST_INDEX
2
#define HTTP_REQUEST_INDEX
1
struct
MemoryStruct
{
char
*
memory
;
...
...
modules/thread_pool/pthrpoll.c
View file @
a2d093f8
This diff is collapsed.
Click to expand it.
modules/thread_pool/pthrpoll.h
View file @
a2d093f8
#ifndef
THREAD_POO
L_H
#define
THREAD_POO
L_H
#ifndef
PTHRPOL
L_H
#define
PTHRPOL
L_H
#include "common.h"
// 为了让头文件自给自足,直接包含它所需要的依赖
#include <common.h>
// 任务结构体
typedef
struct
Task
{
void
(
*
function
)(
void
*
);
void
*
argument
;
struct
Task
*
next
;
void
(
*
function
)(
void
*
);
// 函数指针,指向要执行的任务函数
void
*
argument
;
// 传递给任务函数的参数
struct
Task
*
next
;
// 指向下一个任务的指针,构成链表
}
Task
;
// 任务队列结构体
typedef
struct
{
Task
*
head
;
Task
*
tail
;
int
size
;
pthread_mutex_t
mutex
;
pthread_cond_t
cond
;
Task
*
head
;
// 队列头部
Task
*
tail
;
// 队列尾部
int
size
;
// 队列中任务的数量
pthread_mutex_t
mutex
;
// 用于保护任务队列的互斥锁
pthread_cond_t
cond
;
// 用于线程同步的条件变量
}
TaskQueue
;
// 线程池结构体
// 线程池结构体
(此结构体已被简化和明确化)
typedef
struct
{
pthread_t
*
threads
;
pthread_t
reaper_thread
;
// 新增:回收线程
int
thread_count
;
int
min_threads
;
// 新增:最小线程数
int
max_threads
;
// 新增:最大线程数
int
active_threads
;
// 新增:活跃线程数
TaskQueue
task_queue
;
int
shutdown
;
pthread_mutex_t
mutex
;
pthread_cond_t
reaper_cond
;
// 新增:回收线程条件变量
pthread_t
*
threads
;
// 存放线程ID的数组
int
min_threads
;
// 线程池中最小线程数
int
max_threads
;
// 线程池中最大线程数
// --- 字段已重命名和简化 ---
int
live_threads
;
// 当前存活的线程数
int
busy_threads
;
// 当前正在忙碌(执行任务)的线程数(为未来功能增强预留)
TaskQueue
task_queue
;
// 任务队列
int
shutdown
;
// 线程池关闭标志(1表示关闭,0表示运行)
pthread_mutex_t
mutex
;
// 用于保护线程池级别变量(如线程计数)的互斥锁
// --- 已移除的字段 ---
// pthread_t reaper_thread; // 已移除:回收线程的逻辑复杂且存在缺陷。
// pthread_cond_t reaper_cond; // 已移除:没有回收线程后不再需要。
// 'thread_count' 和 'active_threads' 的概念现在由 'live_threads' 更清晰地表示。
}
ThreadPool_t
;
// 初始化线程池
/**
* @brief 初始化线程池。
* @param min_threads 线程池中保持存活的最小线程数。
* @param max_threads 线程池可以扩展到的最大线程数。
* @return 成功时返回新创建的线程池指针,失败时返回 NULL。
*/
ThreadPool_t
*
thread_pool_init
(
int
min_threads
,
int
max_threads
);
// 向线程池添加任务
/**
* @brief 向线程池的任务队列中添加一个新任务。
* @param pool 要添加任务的线程池。
* @param function 要执行的任务函数指针。
* @param argument 传递给任务函数的参数。
* 注意:调用者负责管理此参数的内存。
* 如果参数是动态分配的,任务函数内部应负责释放它。
* @return 成功返回 0,失败返回 -1。
*/
int
thread_pool_add_task
(
ThreadPool_t
*
pool
,
void
(
*
function
)(
void
*
),
void
*
argument
);
// 销毁线程池
/**
* @brief 关闭并清理线程池资源。
* 此函数会等待所有当前正在运行的任务执行完毕,但不会执行队列中剩余的任务。
* @param pool 要销毁的线程池。
*/
void
thread_pool_destroy
(
ThreadPool_t
*
pool
);
#endif //THREAD_POOL_H
\ No newline at end of file
#endif // PTHRPOLL_H
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment