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
d789dfb9
Commit
d789dfb9
authored
Mar 27, 2025
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加了船的逻辑
parent
e7bdceae
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
431 additions
and
63 deletions
+431
-63
gpio_pwm.h
lib/gpio_pwm.h
+1
-1
main.h
lib/main.h
+8
-4
mqtt.h
lib/mqtt.h
+3
-0
pthrpoll.h
lib/pthrpoll.h
+19
-9
main
main
+0
-0
gpio_pwm.c
src/gpio_pwm.c
+294
-31
mqtt.c
src/mqtt.c
+9
-1
pthrpoll.c
src/pthrpoll.c
+95
-16
main.c
usr/main.c
+2
-1
No files found.
lib/gpio_pwm.h
View file @
d789dfb9
...
...
@@ -14,7 +14,7 @@ void pin_all_default();//拉低车的控制引脚
void
pin_value
(
int
pin
,
int
value
);
//控制引脚高低
void
pin_ship_init
();
void
ship_speed_change
(
unsigned
char
*
buf
);
void
ship_speed_change
(
void
*
buf
);
void
ship_stop_pwm
();
//船pwm停止函数
void
pin_all_ship_default
();
//拉低船引脚
...
...
lib/main.h
View file @
d789dfb9
...
...
@@ -16,6 +16,10 @@
#include "delay.h"
#include "opensh.h"
#include "INA226.h"
#include "pthrpoll.h"
#define filename "/home/orangepi/car/master/Deviceld.txt"
pthread_t
thread
[
4
];
...
...
@@ -34,11 +38,12 @@ int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh
if
(
pthread_create
(
&
thread
[
2
],
NULL
,
opensh
,
NULL
)
!=
0
){
perror
(
"Failed to create thread 2"
);
return
2
;
}
if
(
pthread_create
(
&
thread
[
3
],
NULL
,
Mqtt_onnect
,
NULL
)
!=
0
)
{
}
if
(
pthread_create
(
&
thread
[
3
],
NULL
,
Mqtt_onnect
,
NULL
)
!=
0
)
{
perror
(
"Failed to create thread 3"
);
return
3
;
}
return
0
;
return
10086
;
}
...
...
@@ -46,8 +51,7 @@ void *AppExit(void *arg){ //出现意外自动停止
while
(
1
){
Delay_Ms
(
0
,
100
);
gPwmCount
++
;
if
(
gPwmCount
>=
5
)
{
if
(
gPwmCount
>=
5
)
{
if
(
AppExit_pin_pwm
==
1
)
{
//车异常问题处理
midde_pwm
();
pin_all_default
();
...
...
lib/mqtt.h
View file @
d789dfb9
...
...
@@ -15,11 +15,14 @@
#include "opensh.h"
#include "gpio_pwm.h"
#include "INA226.h"
#include "pthrpoll.h"
extern
char
*
TOPIC
;
//="app2dev/controlcar0004"
extern
char
*
TOPIC2
;
//="dev2app/controlcar0004"
extern
char
*
TOPIC3
;
//= "controlcar0004"
extern
ThreadPool
*
pool
;
#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883
...
...
lib/pthrpoll.h
View file @
d789dfb9
#ifndef
PTHRPOL
L_H
#define
PTHRPOL
L_H
#ifndef
THREAD_POO
L_H
#define
THREAD_POO
L_H
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
// 任务结构体
typedef
struct
Task
{
void
(
*
function
)(
void
*
);
...
...
@@ -27,12 +25,24 @@ typedef struct {
// 线程池结构体
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
;
// 新增:回收线程条件变量
}
ThreadPool
;
// 初始化线程池
ThreadPool
*
thread_pool_init
(
int
min_threads
,
int
max_threads
);
// 向线程池添加任务
int
thread_pool_add_task
(
ThreadPool
*
pool
,
void
(
*
function
)(
void
*
),
void
*
argument
);
// 销毁线程池
void
thread_pool_destroy
(
ThreadPool
*
pool
);
#endif
\ No newline at end of file
#endif // THREAD_POOL_H
\ No newline at end of file
main
View file @
d789dfb9
No preview for this file type
src/gpio_pwm.c
View file @
d789dfb9
#include "gpio_pwm.h"
#include <stdbool.h>
// 定义 PWM 引脚的 WiringPi 编号
#define PWM_PIN_SPEED 21
#define PWM_PIN_CHANGE 2
int
count_mode
=
0
;
const
int
gpioWPi
[]
=
{
5
,
6
,
7
,
10
,
16
,
20
,
22
,
23
,
24
,
25
,
26
,
27
};
//车能使用高低引脚
const
int
gpioWPi_ship
[]
=
{
5
,
6
,
7
,
10
,
16
,
20
,
22
,
23
,
24
,
25
,
26
,
27
};
//船能使用高低引脚
...
...
@@ -233,12 +236,12 @@ void speed_change(unsigned char *buf) {//车速度和转向引脚数值处理函
/*船的速度转向*/
void
ship_mode_
1_
flont
(
unsigned
char
gval
)
{
void
ship_mode_flont
(
unsigned
char
gval
)
{
digitalWrite
(
5
,
HIGH
);
digitalWrite
(
7
,
LOW
);
digitalWrite
(
24
,
HIGH
);
digitalWrite
(
26
,
LOW
);
if
(
gval
<
50
)
{
if
(
gval
<
=
50
)
{
pwmWrite
(
PWM_PIN_SPEED
,
0
);
pwmWrite
(
PWM_PIN_CHANGE
,
0
);
}
else
if
(
gval
<=
55
)
{
...
...
@@ -295,13 +298,13 @@ void ship_mode_1_flont(unsigned char gval) {
}
}
void
ship_mode_
2_
back
(
unsigned
char
gval
)
void
ship_mode_back
(
unsigned
char
gval
)
{
digitalWrite
(
5
,
LOW
);
digitalWrite
(
7
,
HIGH
);
digitalWrite
(
24
,
LOW
);
digitalWrite
(
26
,
HIGH
);
if
(
gval
<
50
)
{
if
(
gval
<
=
50
)
{
pwmWrite
(
PWM_PIN_SPEED
,
0
);
pwmWrite
(
PWM_PIN_CHANGE
,
0
);
}
else
if
(
gval
<=
55
)
{
...
...
@@ -358,41 +361,301 @@ void ship_mode_2_back(unsigned char gval)
}
}
void
ship_mode_
3_left
(
unsigned
char
gval
)
{
digitalWrite
(
24
,
LOW
);
void
ship_mode_
left_flont
(
unsigned
char
gval
)
{
digitalWrite
(
24
,
HIGH
);
digitalWrite
(
26
,
LOW
);
digitalWrite
(
5
,
LOW
);
digitalWrite
(
7
,
LOW
);
if
(
gval
<=
50
)
{
pwmWrite
(
PWM_PIN_SPEED
,
0
);
pwmWrite
(
PWM_PIN_CHANGE
,
0
);
}
else
if
(
gval
<=
55
)
{
pwmWrite
(
PWM_PIN_SPEED
,
200
);
pwmWrite
(
PWM_PIN_CHANGE
,
200
);
}
else
if
(
gval
<=
60
)
{
pwmWrite
(
PWM_PIN_SPEED
,
300
);
pwmWrite
(
PWM_PIN_CHANGE
,
300
);
}
else
if
(
gval
<=
65
)
{
pwmWrite
(
PWM_PIN_SPEED
,
350
);
pwmWrite
(
PWM_PIN_CHANGE
,
350
);
}
else
if
(
gval
<=
70
)
{
pwmWrite
(
PWM_PIN_SPEED
,
400
);
pwmWrite
(
PWM_PIN_CHANGE
,
400
);
}
else
if
(
gval
<=
75
)
{
pwmWrite
(
PWM_PIN_SPEED
,
450
);
pwmWrite
(
PWM_PIN_CHANGE
,
450
);
}
else
if
(
gval
<=
90
)
{
pwmWrite
(
PWM_PIN_SPEED
,
500
);
pwmWrite
(
PWM_PIN_CHANGE
,
500
);
}
else
if
(
gval
<=
100
)
{
pwmWrite
(
PWM_PIN_SPEED
,
550
);
pwmWrite
(
PWM_PIN_CHANGE
,
550
);
}
else
if
(
gval
<=
110
)
{
pwmWrite
(
PWM_PIN_SPEED
,
600
);
pwmWrite
(
PWM_PIN_CHANGE
,
600
);
}
else
if
(
gval
<=
120
)
{
pwmWrite
(
PWM_PIN_SPEED
,
650
);
pwmWrite
(
PWM_PIN_CHANGE
,
650
);
}
else
if
(
gval
<=
130
)
{
pwmWrite
(
PWM_PIN_SPEED
,
700
);
pwmWrite
(
PWM_PIN_CHANGE
,
700
);
}
else
if
(
gval
<=
140
)
{
pwmWrite
(
PWM_PIN_SPEED
,
750
);
pwmWrite
(
PWM_PIN_CHANGE
,
750
);
}
else
if
(
gval
<=
150
)
{
pwmWrite
(
PWM_PIN_SPEED
,
800
);
pwmWrite
(
PWM_PIN_CHANGE
,
800
);
}
else
if
(
gval
<=
160
)
{
pwmWrite
(
PWM_PIN_SPEED
,
850
);
pwmWrite
(
PWM_PIN_CHANGE
,
850
);
}
else
if
(
gval
<=
170
)
{
pwmWrite
(
PWM_PIN_SPEED
,
900
);
pwmWrite
(
PWM_PIN_CHANGE
,
900
);
}
else
if
(
gval
<=
180
)
{
pwmWrite
(
PWM_PIN_SPEED
,
950
);
pwmWrite
(
PWM_PIN_CHANGE
,
950
);
}
else
if
(
gval
<=
190
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
else
if
(
gval
<=
200
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
}
void
ship_mode_4_right
(
unsigned
char
gval
)
{
digitalWrite
(
5
,
LOW
);
void
ship_mode_right_flont
(
unsigned
char
gval
)
{
digitalWrite
(
5
,
HIGH
);
digitalWrite
(
7
,
LOW
);
digitalWrite
(
24
,
LOW
);
digitalWrite
(
26
,
LOW
);
if
(
gval
<=
50
)
{
pwmWrite
(
PWM_PIN_SPEED
,
0
);
pwmWrite
(
PWM_PIN_CHANGE
,
0
);
}
else
if
(
gval
<=
55
)
{
pwmWrite
(
PWM_PIN_SPEED
,
200
);
pwmWrite
(
PWM_PIN_CHANGE
,
200
);
}
else
if
(
gval
<=
60
)
{
pwmWrite
(
PWM_PIN_SPEED
,
300
);
pwmWrite
(
PWM_PIN_CHANGE
,
300
);
}
else
if
(
gval
<=
65
)
{
pwmWrite
(
PWM_PIN_SPEED
,
350
);
pwmWrite
(
PWM_PIN_CHANGE
,
350
);
}
else
if
(
gval
<=
70
)
{
pwmWrite
(
PWM_PIN_SPEED
,
400
);
pwmWrite
(
PWM_PIN_CHANGE
,
400
);
}
else
if
(
gval
<=
75
)
{
pwmWrite
(
PWM_PIN_SPEED
,
450
);
pwmWrite
(
PWM_PIN_CHANGE
,
450
);
}
else
if
(
gval
<=
90
)
{
pwmWrite
(
PWM_PIN_SPEED
,
500
);
pwmWrite
(
PWM_PIN_CHANGE
,
500
);
}
else
if
(
gval
<=
100
)
{
pwmWrite
(
PWM_PIN_SPEED
,
550
);
pwmWrite
(
PWM_PIN_CHANGE
,
550
);
}
else
if
(
gval
<=
110
)
{
pwmWrite
(
PWM_PIN_SPEED
,
600
);
pwmWrite
(
PWM_PIN_CHANGE
,
600
);
}
else
if
(
gval
<=
120
)
{
pwmWrite
(
PWM_PIN_SPEED
,
650
);
pwmWrite
(
PWM_PIN_CHANGE
,
650
);
}
else
if
(
gval
<=
130
)
{
pwmWrite
(
PWM_PIN_SPEED
,
700
);
pwmWrite
(
PWM_PIN_CHANGE
,
700
);
}
else
if
(
gval
<=
140
)
{
pwmWrite
(
PWM_PIN_SPEED
,
750
);
pwmWrite
(
PWM_PIN_CHANGE
,
750
);
}
else
if
(
gval
<=
150
)
{
pwmWrite
(
PWM_PIN_SPEED
,
800
);
pwmWrite
(
PWM_PIN_CHANGE
,
800
);
}
else
if
(
gval
<=
160
)
{
pwmWrite
(
PWM_PIN_SPEED
,
850
);
pwmWrite
(
PWM_PIN_CHANGE
,
850
);
}
else
if
(
gval
<=
170
)
{
pwmWrite
(
PWM_PIN_SPEED
,
900
);
pwmWrite
(
PWM_PIN_CHANGE
,
900
);
}
else
if
(
gval
<=
180
)
{
pwmWrite
(
PWM_PIN_SPEED
,
950
);
pwmWrite
(
PWM_PIN_CHANGE
,
950
);
}
else
if
(
gval
<=
190
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
else
if
(
gval
<=
200
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
}
void
ship_speed_change
(
unsigned
char
*
buf
)
//船速度和转向引脚数值处理函数
{
unsigned
char
mode
=
buf
[
1
];
unsigned
char
val
=
buf
[
2
];
void
ship_mode_left_back
(
unsigned
char
gval
)
{
digitalWrite
(
5
,
LOW
);
digitalWrite
(
7
,
LOW
);
digitalWrite
(
24
,
LOW
);
digitalWrite
(
26
,
HIGH
);
if
(
gval
<=
50
)
{
pwmWrite
(
PWM_PIN_SPEED
,
0
);
pwmWrite
(
PWM_PIN_CHANGE
,
0
);
}
else
if
(
gval
<=
55
)
{
pwmWrite
(
PWM_PIN_SPEED
,
200
);
pwmWrite
(
PWM_PIN_CHANGE
,
200
);
}
else
if
(
gval
<=
60
)
{
pwmWrite
(
PWM_PIN_SPEED
,
300
);
pwmWrite
(
PWM_PIN_CHANGE
,
300
);
}
else
if
(
gval
<=
65
)
{
pwmWrite
(
PWM_PIN_SPEED
,
350
);
pwmWrite
(
PWM_PIN_CHANGE
,
350
);
}
else
if
(
gval
<=
70
)
{
pwmWrite
(
PWM_PIN_SPEED
,
400
);
pwmWrite
(
PWM_PIN_CHANGE
,
400
);
}
else
if
(
gval
<=
75
)
{
pwmWrite
(
PWM_PIN_SPEED
,
450
);
pwmWrite
(
PWM_PIN_CHANGE
,
450
);
}
else
if
(
gval
<=
90
)
{
pwmWrite
(
PWM_PIN_SPEED
,
500
);
pwmWrite
(
PWM_PIN_CHANGE
,
500
);
}
else
if
(
gval
<=
100
)
{
pwmWrite
(
PWM_PIN_SPEED
,
550
);
pwmWrite
(
PWM_PIN_CHANGE
,
550
);
}
else
if
(
gval
<=
110
)
{
pwmWrite
(
PWM_PIN_SPEED
,
600
);
pwmWrite
(
PWM_PIN_CHANGE
,
600
);
}
else
if
(
gval
<=
120
)
{
pwmWrite
(
PWM_PIN_SPEED
,
650
);
pwmWrite
(
PWM_PIN_CHANGE
,
650
);
}
else
if
(
gval
<=
130
)
{
pwmWrite
(
PWM_PIN_SPEED
,
700
);
pwmWrite
(
PWM_PIN_CHANGE
,
700
);
}
else
if
(
gval
<=
140
)
{
pwmWrite
(
PWM_PIN_SPEED
,
750
);
pwmWrite
(
PWM_PIN_CHANGE
,
750
);
}
else
if
(
gval
<=
150
)
{
pwmWrite
(
PWM_PIN_SPEED
,
800
);
pwmWrite
(
PWM_PIN_CHANGE
,
800
);
}
else
if
(
gval
<=
160
)
{
pwmWrite
(
PWM_PIN_SPEED
,
850
);
pwmWrite
(
PWM_PIN_CHANGE
,
850
);
}
else
if
(
gval
<=
170
)
{
pwmWrite
(
PWM_PIN_SPEED
,
900
);
pwmWrite
(
PWM_PIN_CHANGE
,
900
);
}
else
if
(
gval
<=
180
)
{
pwmWrite
(
PWM_PIN_SPEED
,
950
);
pwmWrite
(
PWM_PIN_CHANGE
,
950
);
}
else
if
(
gval
<=
190
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
else
if
(
gval
<=
200
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
}
void
ship_mode_right_back
(
unsigned
char
gval
)
{
digitalWrite
(
5
,
LOW
);
digitalWrite
(
7
,
HIGH
);
digitalWrite
(
24
,
LOW
);
digitalWrite
(
26
,
LOW
);
if
(
gval
<
50
)
{
pwmWrite
(
PWM_PIN_SPEED
,
0
);
pwmWrite
(
PWM_PIN_CHANGE
,
0
);
}
else
if
(
gval
<=
55
)
{
pwmWrite
(
PWM_PIN_SPEED
,
200
);
pwmWrite
(
PWM_PIN_CHANGE
,
200
);
}
else
if
(
gval
<=
60
)
{
pwmWrite
(
PWM_PIN_SPEED
,
300
);
pwmWrite
(
PWM_PIN_CHANGE
,
300
);
}
else
if
(
gval
<=
65
)
{
pwmWrite
(
PWM_PIN_SPEED
,
350
);
pwmWrite
(
PWM_PIN_CHANGE
,
350
);
}
else
if
(
gval
<=
70
)
{
pwmWrite
(
PWM_PIN_SPEED
,
400
);
pwmWrite
(
PWM_PIN_CHANGE
,
400
);
}
else
if
(
gval
<=
75
)
{
pwmWrite
(
PWM_PIN_SPEED
,
450
);
pwmWrite
(
PWM_PIN_CHANGE
,
450
);
}
else
if
(
gval
<=
90
)
{
pwmWrite
(
PWM_PIN_SPEED
,
500
);
pwmWrite
(
PWM_PIN_CHANGE
,
500
);
}
else
if
(
gval
<=
100
)
{
pwmWrite
(
PWM_PIN_SPEED
,
550
);
pwmWrite
(
PWM_PIN_CHANGE
,
550
);
}
else
if
(
gval
<=
110
)
{
pwmWrite
(
PWM_PIN_SPEED
,
600
);
pwmWrite
(
PWM_PIN_CHANGE
,
600
);
}
else
if
(
gval
<=
120
)
{
pwmWrite
(
PWM_PIN_SPEED
,
650
);
pwmWrite
(
PWM_PIN_CHANGE
,
650
);
}
else
if
(
gval
<=
130
)
{
pwmWrite
(
PWM_PIN_SPEED
,
700
);
pwmWrite
(
PWM_PIN_CHANGE
,
700
);
}
else
if
(
gval
<=
140
)
{
pwmWrite
(
PWM_PIN_SPEED
,
750
);
pwmWrite
(
PWM_PIN_CHANGE
,
750
);
}
else
if
(
gval
<=
150
)
{
pwmWrite
(
PWM_PIN_SPEED
,
800
);
pwmWrite
(
PWM_PIN_CHANGE
,
800
);
}
else
if
(
gval
<=
160
)
{
pwmWrite
(
PWM_PIN_SPEED
,
850
);
pwmWrite
(
PWM_PIN_CHANGE
,
850
);
}
else
if
(
gval
<=
170
)
{
pwmWrite
(
PWM_PIN_SPEED
,
900
);
pwmWrite
(
PWM_PIN_CHANGE
,
900
);
}
else
if
(
gval
<=
180
)
{
pwmWrite
(
PWM_PIN_SPEED
,
950
);
pwmWrite
(
PWM_PIN_CHANGE
,
950
);
}
else
if
(
gval
<=
190
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
else
if
(
gval
<=
200
)
{
pwmWrite
(
PWM_PIN_SPEED
,
1000
);
pwmWrite
(
PWM_PIN_CHANGE
,
1000
);
}
}
switch
(
mode
)
{
case
1
:
ship_mode_1_flont
(
val
);
break
;
case
2
:
ship_mode_2_back
(
val
);
break
;
case
3
:
ship_mode_3_left
(
val
);
break
;
case
4
:
ship_mode_4_right
(
val
);
break
;
default:
break
;
}
typedef
struct
{
unsigned
char
buf
[
3
];
}
ShipSpeedParams
;
void
ship_speed_change
(
void
*
arg
)
{
//船速度和转向引脚数值处理函数
ShipSpeedParams
*
params
=
(
ShipSpeedParams
*
)
arg
;
unsigned
char
*
buf
=
params
->
buf
;
unsigned
char
mode
=
buf
[
1
];
unsigned
char
val
=
buf
[
2
];
if
(
val
==
0
)
count_mode
=
0
;
if
(
mode
==
1
&&
count_mode
==
0
)
ship_mode_flont
(
val
);
if
(
mode
==
2
&&
count_mode
==
0
)
ship_mode_back
(
val
);
if
(
mode
==
3
)
count_mode
=
3
;
if
(
mode
==
4
)
count_mode
=
4
;
if
(
val
==
0
)
count_mode
=
0
;
if
(
mode
==
1
&&
count_mode
==
3
){
ship_mode_left_flont
(
val
);
}
else
if
(
mode
==
1
&&
count_mode
==
4
)
{
ship_mode_right_flont
(
val
);
}
if
(
mode
==
2
&&
count_mode
==
3
){
ship_mode_left_back
(
val
);
}
else
if
(
mode
==
2
&&
count_mode
==
4
)
{
ship_mode_right_back
(
val
);
}
// switch(count_mode) {
// case 1:
// ship_mode_1_flont(val);
// break;
// case 2:
// ship_mode_2_back(val);
// break;
// case 3:
// ship_mode_3_left(val);
// break;
// case 4:
// ship_mode_4_right(val);
// break;
// default:
// break;
// }
}
...
...
src/mqtt.c
View file @
d789dfb9
...
...
@@ -13,6 +13,8 @@ uint8_t AppExit_pin_pwm=0;//判断坦克或者车的退出
int
rc
=
0
;
//判断mqtt是否成功创建
ThreadPool
*
pool
;
//线程池线程函数
unsigned
char
modeTemp
=
0
;
unsigned
char
typeTemp
=
0
;
unsigned
char
pinTemp
=
0
;
...
...
@@ -95,7 +97,13 @@ void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm
printf
(
"modeTemp:%d
\n
"
,
gvalt
[
1
]);
printf
(
"valTemp:%d
\n
"
,
gvalt
[
2
]);
if
(
AppExit_pin_pwm
==
1
)
speed_change
(
gvalt
);
if
(
AppExit_pin_pwm
==
3
)
ship_speed_change
(
gvalt
);
if
(
AppExit_pin_pwm
==
3
)
{
if
(
thread_pool_add_task
(
pool
,
ship_speed_change
,
gvalt
)
!=
0
)
{
printf
(
"Failed to add task
\n
"
);
}
}
}
void
message_4
(
cJSON
*
body
,
cJSON
*
pin_setctrl
){
//message 为4时候
...
...
src/pthrpoll.c
View file @
d789dfb9
#include "pthrpoll.h"
// 初始化任务队列
int
task_queue_init
(
TaskQueue
*
queue
)
{
static
int
task_queue_init
(
TaskQueue
*
queue
)
{
queue
->
head
=
NULL
;
queue
->
tail
=
NULL
;
queue
->
size
=
0
;
...
...
@@ -16,7 +17,7 @@ int task_queue_init(TaskQueue *queue) {
}
// 向任务队列中添加任务
int
task_queue_add
(
TaskQueue
*
queue
,
void
(
*
function
)(
void
*
),
void
*
argument
)
{
static
int
task_queue_add
(
TaskQueue
*
queue
,
void
(
*
function
)(
void
*
),
void
*
argument
)
{
Task
*
task
=
(
Task
*
)
malloc
(
sizeof
(
Task
));
if
(
task
==
NULL
)
{
return
-
1
;
...
...
@@ -42,7 +43,7 @@ int task_queue_add(TaskQueue *queue, void (*function)(void *), void *argument) {
}
// 从任务队列中取出任务
Task
*
task_queue_remove
(
TaskQueue
*
queue
,
ThreadPool
*
pool
)
{
static
Task
*
task_queue_remove
(
TaskQueue
*
queue
,
ThreadPool
*
pool
)
{
pthread_mutex_lock
(
&
queue
->
mutex
);
while
(
queue
->
size
==
0
&&
!
pool
->
shutdown
)
{
...
...
@@ -67,9 +68,13 @@ Task *task_queue_remove(TaskQueue *queue, ThreadPool *pool) {
}
// 工作线程函数
void
*
worker_thread
(
void
*
arg
)
{
static
void
*
worker_thread
(
void
*
arg
)
{
ThreadPool
*
pool
=
(
ThreadPool
*
)
arg
;
pthread_mutex_lock
(
&
pool
->
mutex
);
pool
->
active_threads
++
;
pthread_mutex_unlock
(
&
pool
->
mutex
);
while
(
1
)
{
Task
*
task
=
task_queue_remove
(
&
pool
->
task_queue
,
pool
);
if
(
task
==
NULL
)
{
...
...
@@ -79,14 +84,54 @@ void *worker_thread(void *arg) {
task
->
function
(
task
->
argument
);
free
(
task
->
argument
);
// 释放任务参数
free
(
task
);
// 释放任务本身
// 通知回收线程可能有空闲线程
pthread_cond_signal
(
&
pool
->
reaper_cond
);
}
pthread_mutex_lock
(
&
pool
->
mutex
);
pool
->
active_threads
--
;
pthread_mutex_unlock
(
&
pool
->
mutex
);
return
NULL
;
}
// 回收线程函数
static
void
*
reaper_thread
(
void
*
arg
)
{
ThreadPool
*
pool
=
(
ThreadPool
*
)
arg
;
while
(
!
pool
->
shutdown
)
{
pthread_mutex_lock
(
&
pool
->
mutex
);
// 检查是否需要回收线程
while
(
pool
->
active_threads
<=
pool
->
min_threads
||
(
pool
->
task_queue
.
size
>
0
&&
pool
->
active_threads
<=
pool
->
task_queue
.
size
))
{
pthread_cond_wait
(
&
pool
->
reaper_cond
,
&
pool
->
mutex
);
if
(
pool
->
shutdown
)
{
pthread_mutex_unlock
(
&
pool
->
mutex
);
return
NULL
;
}
}
// 计算可以回收的线程数量
int
excess_threads
=
pool
->
active_threads
-
pool
->
min_threads
;
if
(
excess_threads
>
0
&&
pool
->
active_threads
>
pool
->
min_threads
)
{
// 通过添加空任务来让工作线程退出
for
(
int
i
=
0
;
i
<
excess_threads
;
i
++
)
{
task_queue_add
(
&
pool
->
task_queue
,
NULL
,
NULL
);
}
}
pthread_mutex_unlock
(
&
pool
->
mutex
);
sleep
(
1
);
// 避免过于频繁检查
}
return
NULL
;
}
// 初始化线程池
ThreadPool
*
thread_pool_init
(
int
thread_count
)
{
if
(
thread_count
<=
0
)
{
ThreadPool
*
thread_pool_init
(
int
min_threads
,
int
max_threads
)
{
if
(
min_threads
<=
0
||
max_threads
<=
0
||
min_threads
>
max_threads
)
{
errno
=
EINVAL
;
return
NULL
;
}
...
...
@@ -96,7 +141,7 @@ ThreadPool *thread_pool_init(int thread_count) {
return
NULL
;
}
pool
->
threads
=
(
pthread_t
*
)
malloc
(
thread_count
*
sizeof
(
pthread_t
));
pool
->
threads
=
(
pthread_t
*
)
malloc
(
max_threads
*
sizeof
(
pthread_t
));
if
(
pool
->
threads
==
NULL
)
{
free
(
pool
);
return
NULL
;
...
...
@@ -108,9 +153,14 @@ ThreadPool *thread_pool_init(int thread_count) {
return
NULL
;
}
pool
->
thread_count
=
thread_count
;
pool
->
min_threads
=
min_threads
;
pool
->
max_threads
=
max_threads
;
pool
->
thread_count
=
min_threads
;
pool
->
active_threads
=
0
;
pool
->
shutdown
=
0
;
if
(
pthread_mutex_init
(
&
pool
->
mutex
,
NULL
)
!=
0
)
{
if
(
pthread_mutex_init
(
&
pool
->
mutex
,
NULL
)
!=
0
||
pthread_cond_init
(
&
pool
->
reaper_cond
,
NULL
)
!=
0
)
{
pthread_mutex_destroy
(
&
pool
->
task_queue
.
mutex
);
pthread_cond_destroy
(
&
pool
->
task_queue
.
cond
);
free
(
pool
->
threads
);
...
...
@@ -118,7 +168,8 @@ ThreadPool *thread_pool_init(int thread_count) {
return
NULL
;
}
for
(
int
i
=
0
;
i
<
thread_count
;
i
++
)
{
// 创建工作线程
for
(
int
i
=
0
;
i
<
min_threads
;
i
++
)
{
if
(
pthread_create
(
&
pool
->
threads
[
i
],
NULL
,
worker_thread
,
pool
)
!=
0
)
{
// 创建线程失败,关闭已创建的线程
pool
->
shutdown
=
1
;
...
...
@@ -129,6 +180,7 @@ ThreadPool *thread_pool_init(int thread_count) {
}
pthread_mutex_destroy
(
&
pool
->
mutex
);
pthread_cond_destroy
(
&
pool
->
reaper_cond
);
pthread_mutex_destroy
(
&
pool
->
task_queue
.
mutex
);
pthread_cond_destroy
(
&
pool
->
task_queue
.
cond
);
free
(
pool
->
threads
);
...
...
@@ -137,6 +189,24 @@ ThreadPool *thread_pool_init(int thread_count) {
}
}
// 创建回收线程
if
(
pthread_create
(
&
pool
->
reaper_thread
,
NULL
,
reaper_thread
,
pool
)
!=
0
)
{
pool
->
shutdown
=
1
;
pthread_cond_broadcast
(
&
pool
->
task_queue
.
cond
);
for
(
int
i
=
0
;
i
<
min_threads
;
i
++
)
{
pthread_join
(
pool
->
threads
[
i
],
NULL
);
}
pthread_mutex_destroy
(
&
pool
->
mutex
);
pthread_cond_destroy
(
&
pool
->
reaper_cond
);
pthread_mutex_destroy
(
&
pool
->
task_queue
.
mutex
);
pthread_cond_destroy
(
&
pool
->
task_queue
.
cond
);
free
(
pool
->
threads
);
free
(
pool
);
return
NULL
;
}
return
pool
;
}
...
...
@@ -151,6 +221,14 @@ int thread_pool_add_task(ThreadPool *pool, void (*function)(void *), void *argum
pthread_mutex_unlock
(
&
pool
->
mutex
);
return
-
1
;
}
// 如果任务队列过长且可以创建更多线程,则创建新线程
if
(
pool
->
task_queue
.
size
>
pool
->
active_threads
&&
pool
->
thread_count
<
pool
->
max_threads
)
{
if
(
pthread_create
(
&
pool
->
threads
[
pool
->
thread_count
],
NULL
,
worker_thread
,
pool
)
==
0
)
{
pool
->
thread_count
++
;
}
}
pthread_mutex_unlock
(
&
pool
->
mutex
);
// 复制参数,确保生命周期
...
...
@@ -173,19 +251,21 @@ void thread_pool_destroy(ThreadPool *pool) {
pool
->
shutdown
=
1
;
pthread_mutex_unlock
(
&
pool
->
mutex
);
// 唤醒所有
等待的
线程
// 唤醒所有线程
pthread_cond_broadcast
(
&
pool
->
task_queue
.
cond
);
pthread_cond_signal
(
&
pool
->
reaper_cond
);
// 等待所有线程退出
for
(
int
i
=
0
;
i
<
pool
->
thread_count
;
i
++
)
{
pthread_join
(
pool
->
threads
[
i
],
NULL
);
}
pthread_join
(
pool
->
reaper_thread
,
NULL
);
// 清理剩余任务
Task
*
task
=
pool
->
task_queue
.
head
;
while
(
task
!=
NULL
)
{
Task
*
next
=
task
->
next
;
free
(
task
->
argument
);
if
(
task
->
argument
)
free
(
task
->
argument
);
free
(
task
);
task
=
next
;
}
...
...
@@ -195,8 +275,6 @@ void thread_pool_destroy(ThreadPool *pool) {
pthread_mutex_destroy
(
&
pool
->
task_queue
.
mutex
);
pthread_cond_destroy
(
&
pool
->
task_queue
.
cond
);
pthread_mutex_destroy
(
&
pool
->
mutex
);
pthread_cond_destroy
(
&
pool
->
reaper_cond
);
free
(
pool
);
}
}
\ No newline at end of file
usr/main.c
View file @
d789dfb9
...
...
@@ -22,8 +22,9 @@ int Device_File_Init() {
AppExit_pin_pwm
=
2
;
//车的异常停止值
free
(
sub_str
);
}
else
if
(
strcmp
(
sub_str
,
"03"
)
==
0
){
//船的编码
pwm_speed
();
pwm_speed
();
pin_ship_init
();
//pwm初始化,车为停止
pool
=
thread_pool_init
(
1
,
1
);
AppExit_pin_pwm
=
3
;
//车的异常停止值
free
(
sub_str
);
}
...
...
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