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
22393bcf
Commit
22393bcf
authored
Jan 26, 2026
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
加入了可以坐人的大车
parent
3e4f19d9
Show whitespace changes
Inline
Side-by-side
Showing
24 changed files
with
515 additions
and
49 deletions
+515
-49
device_identity.c
app/device_identity/device_identity.c
+4
-0
device_identity.h
app/device_identity/device_identity.h
+1
-0
pthread_open.c
app/main/pthread_open.c
+1
-2
Makefile
build/Makefile
+27
-0
main
build/main
+0
-0
progress.marks
build/third_party/mosquitto/apps/CMakeFiles/progress.marks
+1
-1
progress.make
...osquitto_ctrl/CMakeFiles/mosquitto_ctrl.dir/progress.make
+2
-2
progress.make
...itto_passwd/CMakeFiles/mosquitto_passwd.dir/progress.make
+1
-1
progress.marks
...mosquitto/apps/mosquitto_passwd/CMakeFiles/progress.marks
+1
-1
progress.make
...itto/lib/CMakeFiles/libmosquitto_static.dir/progress.make
+6
-6
progress.marks
...d/third_party/mosquitto/plugins/CMakeFiles/progress.marks
+1
-1
progress.make
.../CMakeFiles/mosquitto_message_timestamp.dir/progress.make
+1
-1
progress.marks
...uitto/plugins/message-timestamp/CMakeFiles/progress.marks
+1
-1
progress.make
...arty/mosquitto/src/CMakeFiles/mosquitto.dir/progress.make
+16
-16
car0105_control.c
drivers/devicecontrol/car0105_control.c
+376
-0
car0105_control.h
drivers/devicecontrol/car0105_control.h
+13
-0
devcontrol_common.c
drivers/devicecontrol/devcontrol_common.c
+25
-1
devcontrol_common.h
drivers/devicecontrol/devcontrol_common.h
+5
-0
pg0403_serial.c
drivers/devicecontrol/pg0403_serial.c
+6
-8
pg0403_serial.h
drivers/devicecontrol/pg0403_serial.h
+0
-7
device_init.c
drivers/gpio/device_init.c
+11
-0
gpio_control.c
drivers/gpio/gpio_control.c
+7
-1
fcsgdevintroduce.md
fcsgdevintroduce.md
+1
-0
common.h
include/common.h
+8
-0
No files found.
app/device_identity/device_identity.c
View file @
22393bcf
...
@@ -91,6 +91,7 @@ int hash_insert_init(HashTable_t *HashTable_t) {
...
@@ -91,6 +91,7 @@ int hash_insert_init(HashTable_t *HashTable_t) {
insert
(
HashTable_t
,
"0102"
,
CAR_0102
);
insert
(
HashTable_t
,
"0102"
,
CAR_0102
);
insert
(
HashTable_t
,
"0103"
,
CAR_0103
);
insert
(
HashTable_t
,
"0103"
,
CAR_0103
);
insert
(
HashTable_t
,
"0104"
,
CAR_0104
);
insert
(
HashTable_t
,
"0104"
,
CAR_0104
);
insert
(
HashTable_t
,
"0105"
,
CAR_0105
);
insert
(
HashTable_t
,
"0201"
,
TANK_0201
);
insert
(
HashTable_t
,
"0201"
,
TANK_0201
);
insert
(
HashTable_t
,
"0202"
,
TANK_0202
);
insert
(
HashTable_t
,
"0202"
,
TANK_0202
);
insert
(
HashTable_t
,
"0203"
,
TANK_0203
);
insert
(
HashTable_t
,
"0203"
,
TANK_0203
);
...
@@ -116,6 +117,9 @@ int device_judg(CodeEnum_t code,char *sub_str) {
...
@@ -116,6 +117,9 @@ int device_judg(CodeEnum_t code,char *sub_str) {
}
else
if
(
code
==
CAR_0104
)
{
}
else
if
(
code
==
CAR_0104
)
{
device_init
(
DEVICE_CAR0104
);
device_init
(
DEVICE_CAR0104
);
my_zlog_info
(
"使用推土机,使用型号%s"
,
sub_str
);
my_zlog_info
(
"使用推土机,使用型号%s"
,
sub_str
);
}
if
(
code
==
CAR_0105
)
{
device_init
(
DEVICE_CAR0105
);
my_zlog_info
(
"使用人坐超大车,型号%s"
,
sub_str
);
}
else
if
(
code
==
TANK_0202
)
{
}
else
if
(
code
==
TANK_0202
)
{
device_init
(
DEVICE_TANK0202
);
device_init
(
DEVICE_TANK0202
);
my_zlog_info
(
"使用型号%s"
,
sub_str
);
my_zlog_info
(
"使用型号%s"
,
sub_str
);
...
...
app/device_identity/device_identity.h
View file @
22393bcf
...
@@ -12,6 +12,7 @@ typedef enum {
...
@@ -12,6 +12,7 @@ typedef enum {
CAR_0102
,
CAR_0102
,
CAR_0103
,
CAR_0103
,
CAR_0104
,
CAR_0104
,
CAR_0105
,
TANK_0201
,
TANK_0201
,
TANK_0202
,
TANK_0202
,
TANK_0203
,
TANK_0203
,
...
...
app/main/pthread_open.c
View file @
22393bcf
...
@@ -42,10 +42,9 @@ int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat,
...
@@ -42,10 +42,9 @@ int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat,
// 出现意外自动停止
// 出现意外自动停止
void
*
thread_exit_time
(
void
*
arg
)
void
*
thread_exit_time
(
void
*
arg
)
{
{
pg0403_serial_run
();
device_serial_exit_run
();
//串口设备的异常退出和接收运行
while
(
1
)
while
(
1
)
{
{
if
(
get_self_control_index
()
==
false
)
if
(
get_self_control_index
()
==
false
)
{
{
...
...
build/Makefile
View file @
22393bcf
...
@@ -617,6 +617,30 @@ drivers/devicecontrol/car0104_control.c.s:
...
@@ -617,6 +617,30 @@ drivers/devicecontrol/car0104_control.c.s:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/devicecontrol/car0104_control.c.s
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/devicecontrol/car0104_control.c.s
.PHONY
:
drivers/devicecontrol/car0104_control.c.s
.PHONY
:
drivers/devicecontrol/car0104_control.c.s
drivers/devicecontrol/car0105_control.o
:
drivers/devicecontrol/car0105_control.c.o
.PHONY
:
drivers/devicecontrol/car0105_control.o
# target to build an object file
drivers/devicecontrol/car0105_control.c.o
:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/devicecontrol/car0105_control.c.o
.PHONY
:
drivers/devicecontrol/car0105_control.c.o
drivers/devicecontrol/car0105_control.i
:
drivers/devicecontrol/car0105_control.c.i
.PHONY
:
drivers/devicecontrol/car0105_control.i
# target to preprocess a source file
drivers/devicecontrol/car0105_control.c.i
:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/devicecontrol/car0105_control.c.i
.PHONY
:
drivers/devicecontrol/car0105_control.c.i
drivers/devicecontrol/car0105_control.s
:
drivers/devicecontrol/car0105_control.c.s
.PHONY
:
drivers/devicecontrol/car0105_control.s
# target to generate assembly for a file
drivers/devicecontrol/car0105_control.c.s
:
$(MAKE)
$(MAKESILENT)
-f
CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/devicecontrol/car0105_control.c.s
.PHONY
:
drivers/devicecontrol/car0105_control.c.s
drivers/devicecontrol/devcontrol_common.o
:
drivers/devicecontrol/devcontrol_common.c.o
drivers/devicecontrol/devcontrol_common.o
:
drivers/devicecontrol/devcontrol_common.c.o
.PHONY
:
drivers/devicecontrol/devcontrol_common.o
.PHONY
:
drivers/devicecontrol/devcontrol_common.o
...
@@ -2069,6 +2093,9 @@ help:
...
@@ -2069,6 +2093,9 @@ help:
@
echo
"... drivers/devicecontrol/car0104_control.o"
@
echo
"... drivers/devicecontrol/car0104_control.o"
@
echo
"... drivers/devicecontrol/car0104_control.i"
@
echo
"... drivers/devicecontrol/car0104_control.i"
@
echo
"... drivers/devicecontrol/car0104_control.s"
@
echo
"... drivers/devicecontrol/car0104_control.s"
@
echo
"... drivers/devicecontrol/car0105_control.o"
@
echo
"... drivers/devicecontrol/car0105_control.i"
@
echo
"... drivers/devicecontrol/car0105_control.s"
@
echo
"... drivers/devicecontrol/devcontrol_common.o"
@
echo
"... drivers/devicecontrol/devcontrol_common.o"
@
echo
"... drivers/devicecontrol/devcontrol_common.i"
@
echo
"... drivers/devicecontrol/devcontrol_common.i"
@
echo
"... drivers/devicecontrol/devcontrol_common.s"
@
echo
"... drivers/devicecontrol/devcontrol_common.s"
...
...
build/main
View file @
22393bcf
No preview for this file type
build/third_party/mosquitto/apps/CMakeFiles/progress.marks
View file @
22393bcf
2
3
2
2
build/third_party/mosquitto/apps/mosquitto_ctrl/CMakeFiles/mosquitto_ctrl.dir/progress.make
View file @
22393bcf
CMAKE_PROGRESS_1
=
CMAKE_PROGRESS_1
=
81
CMAKE_PROGRESS_2
=
81
CMAKE_PROGRESS_2
=
CMAKE_PROGRESS_3
=
CMAKE_PROGRESS_3
=
CMAKE_PROGRESS_4
=
82
CMAKE_PROGRESS_4
=
82
CMAKE_PROGRESS_5
=
CMAKE_PROGRESS_5
=
...
...
build/third_party/mosquitto/apps/mosquitto_passwd/CMakeFiles/mosquitto_passwd.dir/progress.make
View file @
22393bcf
CMAKE_PROGRESS_1
=
90
CMAKE_PROGRESS_1
=
CMAKE_PROGRESS_2
=
CMAKE_PROGRESS_2
=
CMAKE_PROGRESS_3
=
91
CMAKE_PROGRESS_3
=
91
CMAKE_PROGRESS_4
=
CMAKE_PROGRESS_4
=
...
...
build/third_party/mosquitto/apps/mosquitto_passwd/CMakeFiles/progress.marks
View file @
22393bcf
3
2
build/third_party/mosquitto/lib/CMakeFiles/libmosquitto_static.dir/progress.make
View file @
22393bcf
...
@@ -8,8 +8,8 @@ CMAKE_PROGRESS_7 =
...
@@ -8,8 +8,8 @@ CMAKE_PROGRESS_7 =
CMAKE_PROGRESS_8
=
CMAKE_PROGRESS_8
=
CMAKE_PROGRESS_9
=
18
CMAKE_PROGRESS_9
=
18
CMAKE_PROGRESS_10
=
CMAKE_PROGRESS_10
=
CMAKE_PROGRESS_11
=
19
CMAKE_PROGRESS_11
=
CMAKE_PROGRESS_12
=
CMAKE_PROGRESS_12
=
19
CMAKE_PROGRESS_13
=
CMAKE_PROGRESS_13
=
CMAKE_PROGRESS_14
=
20
CMAKE_PROGRESS_14
=
20
CMAKE_PROGRESS_15
=
CMAKE_PROGRESS_15
=
...
@@ -34,11 +34,11 @@ CMAKE_PROGRESS_33 =
...
@@ -34,11 +34,11 @@ CMAKE_PROGRESS_33 =
CMAKE_PROGRESS_34
=
CMAKE_PROGRESS_34
=
CMAKE_PROGRESS_35
=
27
CMAKE_PROGRESS_35
=
27
CMAKE_PROGRESS_36
=
CMAKE_PROGRESS_36
=
CMAKE_PROGRESS_37
=
28
CMAKE_PROGRESS_37
=
CMAKE_PROGRESS_38
=
CMAKE_PROGRESS_38
=
28
CMAKE_PROGRESS_39
=
CMAKE_PROGRESS_39
=
CMAKE_PROGRESS_40
=
29
CMAKE_PROGRESS_40
=
CMAKE_PROGRESS_41
=
CMAKE_PROGRESS_41
=
29
CMAKE_PROGRESS_42
=
CMAKE_PROGRESS_42
=
CMAKE_PROGRESS_43
=
30
CMAKE_PROGRESS_43
=
30
CMAKE_PROGRESS_44
=
CMAKE_PROGRESS_44
=
...
...
build/third_party/mosquitto/plugins/CMakeFiles/progress.marks
View file @
22393bcf
5
6
build/third_party/mosquitto/plugins/message-timestamp/CMakeFiles/mosquitto_message_timestamp.dir/progress.make
View file @
22393bcf
CMAKE_PROGRESS_1
=
CMAKE_PROGRESS_1
=
CMAKE_PROGRESS_2
=
CMAKE_PROGRESS_2
=
90
build/third_party/mosquitto/plugins/message-timestamp/CMakeFiles/progress.marks
View file @
22393bcf
0
1
build/third_party/mosquitto/src/CMakeFiles/mosquitto.dir/progress.make
View file @
22393bcf
...
@@ -12,17 +12,17 @@ CMAKE_PROGRESS_11 =
...
@@ -12,17 +12,17 @@ CMAKE_PROGRESS_11 =
CMAKE_PROGRESS_12
=
CMAKE_PROGRESS_12
=
CMAKE_PROGRESS_13
=
59
CMAKE_PROGRESS_13
=
59
CMAKE_PROGRESS_14
=
CMAKE_PROGRESS_14
=
CMAKE_PROGRESS_15
=
CMAKE_PROGRESS_15
=
60
CMAKE_PROGRESS_16
=
60
CMAKE_PROGRESS_16
=
CMAKE_PROGRESS_17
=
CMAKE_PROGRESS_17
=
CMAKE_PROGRESS_18
=
CMAKE_PROGRESS_18
=
61
CMAKE_PROGRESS_19
=
61
CMAKE_PROGRESS_19
=
CMAKE_PROGRESS_20
=
CMAKE_PROGRESS_20
=
CMAKE_PROGRESS_21
=
CMAKE_PROGRESS_21
=
62
CMAKE_PROGRESS_22
=
62
CMAKE_PROGRESS_22
=
CMAKE_PROGRESS_23
=
CMAKE_PROGRESS_23
=
CMAKE_PROGRESS_24
=
CMAKE_PROGRESS_24
=
63
CMAKE_PROGRESS_25
=
63
CMAKE_PROGRESS_25
=
CMAKE_PROGRESS_26
=
CMAKE_PROGRESS_26
=
CMAKE_PROGRESS_27
=
64
CMAKE_PROGRESS_27
=
64
CMAKE_PROGRESS_28
=
CMAKE_PROGRESS_28
=
...
@@ -41,14 +41,14 @@ CMAKE_PROGRESS_40 =
...
@@ -41,14 +41,14 @@ CMAKE_PROGRESS_40 =
CMAKE_PROGRESS_41
=
CMAKE_PROGRESS_41
=
CMAKE_PROGRESS_42
=
69
CMAKE_PROGRESS_42
=
69
CMAKE_PROGRESS_43
=
CMAKE_PROGRESS_43
=
CMAKE_PROGRESS_44
=
CMAKE_PROGRESS_44
=
70
CMAKE_PROGRESS_45
=
70
CMAKE_PROGRESS_45
=
CMAKE_PROGRESS_46
=
CMAKE_PROGRESS_46
=
CMAKE_PROGRESS_47
=
CMAKE_PROGRESS_47
=
71
CMAKE_PROGRESS_48
=
71
CMAKE_PROGRESS_48
=
CMAKE_PROGRESS_49
=
CMAKE_PROGRESS_49
=
CMAKE_PROGRESS_50
=
CMAKE_PROGRESS_50
=
72
CMAKE_PROGRESS_51
=
72
CMAKE_PROGRESS_51
=
CMAKE_PROGRESS_52
=
CMAKE_PROGRESS_52
=
CMAKE_PROGRESS_53
=
73
CMAKE_PROGRESS_53
=
73
CMAKE_PROGRESS_54
=
CMAKE_PROGRESS_54
=
...
@@ -70,7 +70,7 @@ CMAKE_PROGRESS_69 =
...
@@ -70,7 +70,7 @@ CMAKE_PROGRESS_69 =
CMAKE_PROGRESS_70
=
CMAKE_PROGRESS_70
=
CMAKE_PROGRESS_71
=
79
CMAKE_PROGRESS_71
=
79
CMAKE_PROGRESS_72
=
CMAKE_PROGRESS_72
=
CMAKE_PROGRESS_73
=
CMAKE_PROGRESS_73
=
80
CMAKE_PROGRESS_74
=
80
CMAKE_PROGRESS_74
=
CMAKE_PROGRESS_75
=
CMAKE_PROGRESS_75
=
drivers/devicecontrol/car0105_control.c
0 → 100644
View file @
22393bcf
#include "car0105_control.h"
#include "modules_common.h"
#include "common.h"
#include "gpio_common.h"
static
uint8_t
forward_open_command
[]
=
{
0x00
,
0xF1
,
0xFF
};
static
uint8_t
forward_close_command
[]
=
{
0x00
,
0x01
,
0xFF
};
static
uint8_t
back_open_command
[]
=
{
0x00
,
0xF2
,
0xFF
};
static
uint8_t
back_close_command
[]
=
{
0x00
,
0x02
,
0xFF
};
static
uint8_t
left_open_command
[]
=
{
0x00
,
0xF3
,
0xFF
};
static
uint8_t
left_close_command
[]
=
{
0x00
,
0x03
,
0xFF
};
static
uint8_t
right_open_command
[]
=
{
0x00
,
0xF4
,
0xFF
};
static
uint8_t
right_close_command
[]
=
{
0x00
,
0x04
,
0xFF
};
static
SerialPort
car_serial
;
static
bool
serial_stop_index
=
0
;
void
car0105_serial_stop
();
// 辅助函数:将整数波特率转换为 termios 定义的 speed_t
static
speed_t
get_baud_rate
(
int
baud_rate
)
{
switch
(
baud_rate
)
{
case
9600
:
return
B9600
;
case
19200
:
return
B19200
;
case
38400
:
return
B38400
;
case
57600
:
return
B57600
;
case
115200
:
return
B115200
;
case
230400
:
return
B230400
;
case
460800
:
return
B460800
;
case
921600
:
return
B921600
;
default:
return
B9600
;
// 默认
}
}
/**
* 打开串口
* 对应原 C++: bool SerialPort::open(speed_t baud_rate)
*/
static
int
serial_open
(
SerialPort
*
sp
,
const
char
*
port_name
,
int
baud_rate
)
{
if
(
sp
->
is_open
)
{
fprintf
(
stderr
,
"Warning: Port %s is already open.
\n
"
,
sp
->
port_name
);
return
1
;
// 视为成功
}
// 保存端口名
strncpy
(
sp
->
port_name
,
port_name
,
sizeof
(
sp
->
port_name
)
-
1
);
// 打开串口
// O_RDWR: 读写模式
// O_NOCTTY: 不作为控制终端
// O_NDELAY: 非阻塞打开(防止DCD线不在线时在此阻塞)
sp
->
fd
=
open
(
sp
->
port_name
,
O_RDWR
|
O_NOCTTY
|
O_NDELAY
);
if
(
sp
->
fd
==
-
1
)
{
fprintf
(
stderr
,
"Error: Failed to open serial port %s - %s
\n
"
,
sp
->
port_name
,
strerror
(
errno
));
return
0
;
// 失败
}
// 方法1:保持非阻塞标志
//fcntl(sp->fd, F_SETFL, O_NONBLOCK);
// 恢复为阻塞模式(对应原代码中的 fcntl(fd_, F_SETFL, 0))
fcntl
(
sp
->
fd
,
F_SETFL
,
0
);
struct
termios
options
;
if
(
tcgetattr
(
sp
->
fd
,
&
options
)
!=
0
)
{
fprintf
(
stderr
,
"Error: tcgetattr failed - %s
\n
"
,
strerror
(
errno
));
close
(
sp
->
fd
);
sp
->
fd
=
-
1
;
return
0
;
}
// 设置波特率
speed_t
speed
=
get_baud_rate
(
baud_rate
);
cfsetispeed
(
&
options
,
speed
);
cfsetospeed
(
&
options
,
speed
);
// 设置控制标志 (8N1 配置)
options
.
c_cflag
&=
~
PARENB
;
// 无校验
options
.
c_cflag
&=
~
CSTOPB
;
// 1位停止位
options
.
c_cflag
&=
~
CSIZE
;
// 清除数据位掩码
options
.
c_cflag
|=
CS8
;
// 8位数据位
// 启用接收器并设置本地模式
options
.
c_cflag
|=
(
CLOCAL
|
CREAD
);
// 设置输入标志 (禁用软件流控)
options
.
c_iflag
&=
~
(
IXON
|
IXOFF
|
IXANY
);
// 设置本地标志 (Raw模式: 无规范模式,无回显,无信号)
options
.
c_lflag
&=
~
(
ICANON
|
ECHO
|
ECHOE
|
ISIG
);
// 设置输出标志 (Raw模式)
options
.
c_oflag
&=
~
OPOST
;
// 设置控制字符
// VMIN=0, VTIME=0: 纯非阻塞轮询读取(原代码逻辑)
// 这意味着 read() 会立即返回,如果没有数据则返回0
options
.
c_cc
[
VMIN
]
=
0
;
options
.
c_cc
[
VTIME
]
=
0
;
// 应用设置
if
(
tcsetattr
(
sp
->
fd
,
TCSANOW
,
&
options
)
!=
0
)
{
fprintf
(
stderr
,
"Error: tcsetattr failed - %s
\n
"
,
strerror
(
errno
));
close
(
sp
->
fd
);
sp
->
fd
=
-
1
;
return
0
;
}
// 清空缓冲区
tcflush
(
sp
->
fd
,
TCIOFLUSH
);
sp
->
is_open
=
1
;
my_zlog_info
(
"Serial port %s opened successfully."
,
sp
->
port_name
);
return
1
;
// 成功
}
/**
* 发送数据
* C语言中通常使用 write
*/
static
int
serial_send
(
SerialPort
*
sp
,
uint8_t
*
data
,
int
len
)
{
if
(
!
sp
||
!
sp
->
is_open
)
{
my_zlog_error
(
"Port not open or invalid pointer."
);
return
-
1
;
}
int
total_written
=
0
;
int
left
=
len
;
while
(
left
>
0
)
{
int
written
=
write
(
sp
->
fd
,
data
+
total_written
,
left
);
if
(
written
<
0
)
{
if
(
errno
==
EINTR
)
{
continue
;
// 被信号中断,重试
}
if
(
errno
==
EAGAIN
||
errno
==
EWOULDBLOCK
)
{
// 如果是非阻塞模式,可能需要做 select/poll 或者稍后重试
// 这里简单处理为错误,或者根据你的架构休眠一下
my_zlog_error
(
"Write buffer full."
);
return
-
1
;
}
my_zlog_error
(
"Write failed - %s"
,
strerror
(
errno
));
return
-
1
;
}
total_written
+=
written
;
left
-=
written
;
}
delay_ms
(
10
);
return
total_written
;
}
/**
* 关闭串口
* 对应原 C++: void SerialPort::close()
*/
static
void
serial_close
(
SerialPort
*
sp
)
{
// 原代码中有 stopListening(); C语言中如果没有线程需要手动停止,这里省略
if
(
sp
->
is_open
)
{
sp
->
is_open
=
0
;
close
(
sp
->
fd
);
sp
->
fd
=
-
1
;
my_zlog_info
(
"Serial port %s closed.
\n
"
,
sp
->
port_name
);
}
}
// 初始化结构体
static
void
serial_init
(
SerialPort
*
sp
)
{
sp
->
fd
=
-
1
;
sp
->
is_open
=
0
;
memset
(
sp
->
port_name
,
0
,
sizeof
(
sp
->
port_name
));
}
int
car0105_serial_init
(){
serial_init
(
&
car_serial
);
// 1. 设置端口号
// 香橙派/树莓派的USB转串口通常是 /dev/ttyUSB0 或 /dev/ttyACM0
// 板载UART通常是 /dev/ttyS1 或 /dev/ttyS3 (取决于具体型号和DT配置)
const
char
*
dev_path
=
"/dev/ttyUSB0"
;
if
(
!
serial_open
(
&
car_serial
,
dev_path
,
9600
))
{
return
-
1
;
}
car0105_serial_stop
();
//serial_send(&car_serial, start_continuous, sizeof(start_continuous));
my_zlog_info
(
"串口初始化成功"
);
return
0
;
}
// ==========================================
// 3. 核心处理函数 (对应 onSerialMessageReceived)
// ==========================================
/**
* 解析并处理接收到的数据
* @param buffer: 接收数据的缓冲区
* @param len: 接收到的数据长度
*/
static
int
process_serial_data
(
const
uint8_t
*
buffer
,
int
len
)
{
if
(
len
<
3
){
return
-
1
;
}
if
(
buffer
[
2
]
==
0xFF
||
buffer
[
3
]
==
0xEF
){
my_zlog_info
(
"收到串口数据:%02X %02X %02X %02X"
,
buffer
[
0
],
buffer
[
1
],
buffer
[
2
],
buffer
[
3
]);
return
0
;
}
else
{
my_zlog_info
(
"串口数据大于3字节,但格式不正确"
);
return
-
1
;
}
}
int
car0105_serial_init_close
(){
serial_close
(
&
car_serial
);
my_zlog_info
(
"串口关闭成功"
);
return
0
;
}
/**
* @brief 等待串口连接成功
* @param serial_port 串口文件描述符(由串口初始化函数返回)
* @param max_retries 最大重试次数,0表示无限重试
* @param retry_interval 重试间隔(秒)
* @return 0: 连接成功, -1: 连接失败
*/
int
wait_for_serial_connection
(
int
(
*
connect_func
)(
void
),
int
max_retries
,
int
retry_interval
)
{
int
retries
=
0
;
while
(
max_retries
==
0
||
retries
<
max_retries
)
{
my_zlog_info
(
"尝试连接串口 (第 %d 次)..."
,
retries
+
1
);
int
result
=
connect_func
();
if
(
result
>=
0
)
{
my_zlog_info
(
"串口连接成功!"
);
return
0
;
}
else
{
my_zlog_warn
(
"串口连接失败,%d秒后重试..."
,
retry_interval
);
if
(
max_retries
>
0
)
{
retries
++
;
if
(
retries
>=
max_retries
)
{
my_zlog_error
(
"达到最大重试次数 (%d 次)"
,
max_retries
);
break
;
}
}
delay_s
(
retry_interval
);
}
}
return
-
1
;
}
int
car0105_serial_run
(){
uint8_t
rx_buffer
[
1024
];
my_zlog_info
(
"设备为串口打开设备"
);
if
(
wait_for_serial_connection
(
car0105_serial_init
,
500
,
10
)
==
0
){
my_zlog_info
(
"串口启动成功"
);
}
else
{
my_zlog_info
(
"串口启动失败"
);
car0105_serial_init_close
();
return
-
1
;
}
while
(
1
){
delay_ms
(
10
);
static
int
send_serial_count
=
0
;
int
n
=
read
(
car_serial
.
fd
,
rx_buffer
,
sizeof
(
rx_buffer
));
int
serial_index
=
process_serial_data
(
rx_buffer
,
n
);
if
(
serial_stop_index
==
false
){
//接收函数
send_serial_count
++
;
if
(
send_serial_count
>=
55
)
send_serial_count
=
55
;
}
if
(
serial_index
<
0
)
serial_stop_index
=
false
;
else
if
(
serial_index
==
0
&&
serial_stop_index
==
true
)
{
send_serial_count
=
0
;
}
if
(
send_serial_count
==
55
){
continue
;
}
if
(
send_serial_count
>
50
){
car0105_serial_stop
();
if
(
send_serial_count
>=
55
)
send_serial_count
=
55
;
serial_stop_index
=
false
;
}
}
car0105_serial_init_close
();
return
0
;
}
void
car0105_serial_stop
(){
serial_send
(
&
car_serial
,
back_close_command
,
sizeof
(
back_close_command
));
serial_send
(
&
car_serial
,
forward_close_command
,
sizeof
(
forward_close_command
));
serial_send
(
&
car_serial
,
left_close_command
,
sizeof
(
left_close_command
));
serial_send
(
&
car_serial
,
right_close_command
,
sizeof
(
right_close_command
));
static
int
i
=
0
;
i
++
;
if
(
i
>
5
){
my_zlog_info
(
"串口超时,停止运行"
);
i
=
0
;
}
}
/*车的速度转向函数*/
void
car0105_mode_1_flont
(
unsigned
char
gval
)
{
if
(
gval
<
50
)
{
my_zlog_info
(
"val:%d"
,
gval
);
serial_send
(
&
car_serial
,
forward_close_command
,
sizeof
(
forward_close_command
));
serial_send
(
&
car_serial
,
back_close_command
,
sizeof
(
back_close_command
));
}
else
{
serial_send
(
&
car_serial
,
forward_open_command
,
sizeof
(
forward_open_command
));
}
}
void
car0105_mode_2_back
(
unsigned
char
gval
)
{
if
(
gval
<
50
)
{
serial_send
(
&
car_serial
,
forward_close_command
,
sizeof
(
forward_close_command
));
serial_send
(
&
car_serial
,
back_close_command
,
sizeof
(
back_close_command
));
}
else
{
serial_send
(
&
car_serial
,
back_open_command
,
sizeof
(
back_open_command
));
}
}
void
car0105_mode_3_left
(
unsigned
char
gval
)
{
if
(
gval
<
45
){
serial_send
(
&
car_serial
,
left_close_command
,
sizeof
(
left_close_command
));
serial_send
(
&
car_serial
,
right_close_command
,
sizeof
(
right_close_command
));
}
else
{
serial_send
(
&
car_serial
,
left_open_command
,
sizeof
(
left_open_command
));
}
}
void
car0105_mode_4_right
(
unsigned
char
gval
)
{
if
(
gval
<
45
){
serial_send
(
&
car_serial
,
left_close_command
,
sizeof
(
left_close_command
));
serial_send
(
&
car_serial
,
right_close_command
,
sizeof
(
right_close_command
));
}
else
{
serial_send
(
&
car_serial
,
right_open_command
,
sizeof
(
right_open_command
));
}
}
void
car0105_control_change
(
unsigned
char
*
buf
)
{
//车速度和转向引脚数值处理函数
unsigned
char
mode
=
buf
[
1
];
unsigned
char
val
=
buf
[
2
];
serial_stop_index
=
true
;
switch
(
mode
){
case
1
:
car0105_mode_1_flont
(
val
);
break
;
case
2
:
car0105_mode_2_back
(
val
);
break
;
case
3
:
car0105_mode_3_left
(
val
);
break
;
case
4
:
car0105_mode_4_right
(
val
);
break
;
default
:
break
;
}
}
\ No newline at end of file
drivers/devicecontrol/car0105_control.h
0 → 100644
View file @
22393bcf
#ifndef CAR0105_CONTROL_H
#define CAR0105_CONTROL_H
int
car0105_serial_run
();
void
car0105_control_change
(
unsigned
char
*
buf
);
void
car0105_serial_stop
();
#endif
\ No newline at end of file
drivers/devicecontrol/devcontrol_common.c
View file @
22393bcf
...
@@ -20,6 +20,10 @@ const device_didrive device_didrive_control_config_t[]={
...
@@ -20,6 +20,10 @@ const device_didrive device_didrive_control_config_t[]={
.
device_didrive_control
=
car0104_change
.
device_didrive_control
=
car0104_change
},
},
{
{
.
device_id
=
DEVICE_CAR0105
,
.
device_didrive_control
=
car0105_control_change
},
{
.
device_id
=
DEVICE_TANK0202
,
.
device_id
=
DEVICE_TANK0202
,
.
device_didrive_control
=
tank0202_change
.
device_didrive_control
=
tank0202_change
},
},
...
@@ -86,6 +90,14 @@ const device_abnormal_close_t devcontrol_config_t[]= {
...
@@ -86,6 +90,14 @@ const device_abnormal_close_t devcontrol_config_t[]= {
},
},
{
{
.
device_id
=
DEVICE_CAR0105
,
.
device_abnormal_stop
=
car0105_serial_stop
,
.
device_close
=
NULL
,
// TANK0206没有单独的关闭函数
.
gpio_pin_pulled
=
pin_all_default
,
.
gpio_pwm_pulled
=
pwm_all_default
},
{
.
device_id
=
DEVICE_TANK0202
,
.
device_id
=
DEVICE_TANK0202
,
.
device_abnormal_stop
=
tank0202_middle
,
.
device_abnormal_stop
=
tank0202_middle
,
.
device_close
=
device_poilthread_close
,
.
device_close
=
device_poilthread_close
,
...
@@ -164,7 +176,7 @@ void device_stop(int device_id) {
...
@@ -164,7 +176,7 @@ void device_stop(int device_id) {
}
}
if
(
!
config
)
{
if
(
!
config
)
{
my_zlog_error
(
"Error: Device gpio stop ID %d not found!"
,
device_id
);
my_zlog_error
(
"Error: Device gpio stop
is low
ID %d not found!"
,
device_id
);
return
;
return
;
}
}
...
@@ -217,3 +229,14 @@ void device_walk_control(int device_id,unsigned char *valt) {
...
@@ -217,3 +229,14 @@ void device_walk_control(int device_id,unsigned char *valt) {
config
->
device_didrive_control
(
valt
);
// 速度控制初始化
config
->
device_didrive_control
(
valt
);
// 速度控制初始化
}
}
/*
* 串口设备启动
*/
void
device_serial_exit_run
(){
if
(
g_device_type
==
DEVICE_CAR0105
){
car0105_serial_run
();
}
else
if
(
g_device_type
==
DEVICE_PG_GPS0403
){
pg0403_serial_run
();
}
else
return
;
}
\ No newline at end of file
drivers/devicecontrol/devcontrol_common.h
View file @
22393bcf
...
@@ -7,6 +7,7 @@
...
@@ -7,6 +7,7 @@
#include "car0102_control.h"
#include "car0102_control.h"
#include "car0103_control.h"
#include "car0103_control.h"
#include "car0104_control.h"
#include "car0104_control.h"
#include "car0105_control.h"
#include "ptz0401_control.h"
#include "ptz0401_control.h"
#include "tank0202_control.h"
#include "tank0202_control.h"
#include "tank0203_control.h"
#include "tank0203_control.h"
...
@@ -22,6 +23,7 @@
...
@@ -22,6 +23,7 @@
#define DEVICE_CAR0102 102 //莽牛小车
#define DEVICE_CAR0102 102 //莽牛小车
#define DEVICE_CAR0103 103 //推土机
#define DEVICE_CAR0103 103 //推土机
#define DEVICE_CAR0104 104 //挖机
#define DEVICE_CAR0104 104 //挖机
#define DEVICE_CAR0105 105 //做人超大车
#define DEVICE_TANK0201 201 //废弃坦克
#define DEVICE_TANK0201 201 //废弃坦克
#define DEVICE_TANK0202 202 //
#define DEVICE_TANK0202 202 //
#define DEVICE_TANK0203 203 //M1A2美国坦克
#define DEVICE_TANK0203 203 //M1A2美国坦克
...
@@ -64,4 +66,6 @@ void device_end_close(int device_id);
...
@@ -64,4 +66,6 @@ void device_end_close(int device_id);
void
device_walk_control
(
int
device_id
,
unsigned
char
*
valt
);
void
device_walk_control
(
int
device_id
,
unsigned
char
*
valt
);
void
device_serial_exit_run
();
#endif
#endif
\ No newline at end of file
drivers/devicecontrol/pg0403_serial.c
View file @
22393bcf
...
@@ -52,7 +52,7 @@ float run_kalman(Kalman1D *k, float measurement) {
...
@@ -52,7 +52,7 @@ float run_kalman(Kalman1D *k, float measurement) {
// 辅助函数:将整数波特率转换为 termios 定义的 speed_t
// 辅助函数:将整数波特率转换为 termios 定义的 speed_t
speed_t
get_baud_rate
(
int
baud_rate
)
{
s
tatic
s
peed_t
get_baud_rate
(
int
baud_rate
)
{
switch
(
baud_rate
)
{
switch
(
baud_rate
)
{
case
9600
:
return
B9600
;
case
9600
:
return
B9600
;
case
19200
:
return
B19200
;
case
19200
:
return
B19200
;
...
@@ -70,7 +70,7 @@ speed_t get_baud_rate(int baud_rate) {
...
@@ -70,7 +70,7 @@ speed_t get_baud_rate(int baud_rate) {
* 打开串口
* 打开串口
* 对应原 C++: bool SerialPort::open(speed_t baud_rate)
* 对应原 C++: bool SerialPort::open(speed_t baud_rate)
*/
*/
int
serial_open
(
SerialPort
*
sp
,
const
char
*
port_name
,
int
baud_rate
)
{
static
int
serial_open
(
SerialPort
*
sp
,
const
char
*
port_name
,
int
baud_rate
)
{
if
(
sp
->
is_open
)
{
if
(
sp
->
is_open
)
{
fprintf
(
stderr
,
"Warning: Port %s is already open.
\n
"
,
sp
->
port_name
);
fprintf
(
stderr
,
"Warning: Port %s is already open.
\n
"
,
sp
->
port_name
);
return
1
;
// 视为成功
return
1
;
// 视为成功
...
@@ -152,7 +152,7 @@ int serial_open(SerialPort *sp, const char *port_name, int baud_rate) {
...
@@ -152,7 +152,7 @@ int serial_open(SerialPort *sp, const char *port_name, int baud_rate) {
* 发送数据
* 发送数据
* C语言中通常使用 write
* C语言中通常使用 write
*/
*/
int
serial_send
(
SerialPort
*
sp
,
uint8_t
*
data
,
int
len
)
{
static
int
serial_send
(
SerialPort
*
sp
,
uint8_t
*
data
,
int
len
)
{
if
(
!
sp
->
is_open
)
{
if
(
!
sp
->
is_open
)
{
my_zlog_error
(
"Error: Port not open."
);
my_zlog_error
(
"Error: Port not open."
);
return
-
1
;
return
-
1
;
...
@@ -169,7 +169,7 @@ int serial_send(SerialPort *sp, uint8_t *data, int len) {
...
@@ -169,7 +169,7 @@ int serial_send(SerialPort *sp, uint8_t *data, int len) {
* 关闭串口
* 关闭串口
* 对应原 C++: void SerialPort::close()
* 对应原 C++: void SerialPort::close()
*/
*/
void
serial_close
(
SerialPort
*
sp
)
{
static
void
serial_close
(
SerialPort
*
sp
)
{
// 原代码中有 stopListening(); C语言中如果没有线程需要手动停止,这里省略
// 原代码中有 stopListening(); C语言中如果没有线程需要手动停止,这里省略
if
(
sp
->
is_open
)
{
if
(
sp
->
is_open
)
{
...
@@ -181,7 +181,7 @@ void serial_close(SerialPort *sp) {
...
@@ -181,7 +181,7 @@ void serial_close(SerialPort *sp) {
}
}
// 初始化结构体
// 初始化结构体
void
serial_init
(
SerialPort
*
sp
)
{
static
void
serial_init
(
SerialPort
*
sp
)
{
sp
->
fd
=
-
1
;
sp
->
fd
=
-
1
;
sp
->
is_open
=
0
;
sp
->
is_open
=
0
;
memset
(
sp
->
port_name
,
0
,
sizeof
(
sp
->
port_name
));
memset
(
sp
->
port_name
,
0
,
sizeof
(
sp
->
port_name
));
...
@@ -213,7 +213,7 @@ int pg0403_serial_init_send(){
...
@@ -213,7 +213,7 @@ int pg0403_serial_init_send(){
* @param buffer: 接收数据的缓冲区
* @param buffer: 接收数据的缓冲区
* @param len: 接收到的数据长度
* @param len: 接收到的数据长度
*/
*/
void
process_serial_data
(
const
uint8_t
*
buffer
,
int
len
)
{
static
void
process_serial_data
(
const
uint8_t
*
buffer
,
int
len
)
{
// 1. 基础长度检查
// 1. 基础长度检查
if
(
len
<
17
)
{
if
(
len
<
17
)
{
return
;
return
;
...
@@ -326,7 +326,6 @@ int pg0403_serial_run(){
...
@@ -326,7 +326,6 @@ int pg0403_serial_run(){
uint8_t
rx_buffer
[
1024
];
uint8_t
rx_buffer
[
1024
];
int
total_bytes
=
0
;
int
total_bytes
=
0
;
if
(
g_device_type
==
DEVICE_PG_GPS0403
){
my_zlog_info
(
"设备为串口打开设备"
);
my_zlog_info
(
"设备为串口打开设备"
);
init_filters
();
init_filters
();
if
(
pg0403_serial_init_send
()
==
0
){
if
(
pg0403_serial_init_send
()
==
0
){
...
@@ -336,7 +335,6 @@ int pg0403_serial_run(){
...
@@ -336,7 +335,6 @@ int pg0403_serial_run(){
pg0403_serial_init_close
();
pg0403_serial_init_close
();
return
-
1
;
return
-
1
;
}
}
}
else
return
-
1
;
while
(
1
){
while
(
1
){
static
int
send_mqtt_count
=
0
;
static
int
send_mqtt_count
=
0
;
int
n
=
read
(
pg_serial
.
fd
,
rx_buffer
,
sizeof
(
rx_buffer
));
int
n
=
read
(
pg_serial
.
fd
,
rx_buffer
,
sizeof
(
rx_buffer
));
...
...
drivers/devicecontrol/pg0403_serial.h
View file @
22393bcf
...
@@ -10,13 +10,6 @@
...
@@ -10,13 +10,6 @@
#define KALMAN_R 0.01f
#define KALMAN_R 0.01f
#define KALMAN_Q 0.01f
#define KALMAN_Q 0.01f
// 定义一个结构体表示串口,整洁
typedef
struct
{
int
fd
;
char
port_name
[
64
];
int
is_open
;
}
SerialPort
;
// 单个维度的卡尔曼状态
// 单个维度的卡尔曼状态
typedef
struct
{
typedef
struct
{
float
estimate
;
// 当前的最优估计值
float
estimate
;
// 当前的最优估计值
...
...
drivers/gpio/device_init.c
View file @
22393bcf
...
@@ -52,6 +52,17 @@ const deviceconfig_t device_configs[] = {
...
@@ -52,6 +52,17 @@ const deviceconfig_t device_configs[] = {
.
device_control_stop
=
car0104_stop
,
/* 补充速度控制函数 */
.
device_control_stop
=
car0104_stop
,
/* 补充速度控制函数 */
.
emergency_code
=
104
.
emergency_code
=
104
},
},
// car0105配置(根据您的需求补充)
{
.
device_id
=
DEVICE_CAR0105
,
.
device_name
=
"car0105"
,
.
gpio_pins
=
{
5
,
6
,
7
,
9
,
10
,
16
,
20
,
22
,
23
,
24
,
25
,
26
,
-
1
},
/* 补充GPIO引脚 */
.
gpio_pwms
=
{
27
,
-
1
},
.
gpio_inputs
=
{
-
1
},
.
device_pwm_init
=
physics_pwm_init
,
.
device_control_stop
=
car0101_middle_pwm
,
/* 补充速度控制函数 */
.
emergency_code
=
105
},
// tank0202配置(根据您的需求补充)
// tank0202配置(根据您的需求补充)
{
{
.
device_id
=
DEVICE_TANK0202
,
.
device_id
=
DEVICE_TANK0202
,
...
...
drivers/gpio/gpio_control.c
View file @
22393bcf
...
@@ -238,6 +238,12 @@ const gpiocontrol_t gpio_configs[] = {
...
@@ -238,6 +238,12 @@ const gpiocontrol_t gpio_configs[] = {
.
device_pwm_value
=
public_pwm_value
.
device_pwm_value
=
public_pwm_value
},
},
{
{
.
device_id
=
DEVICE_CAR0105
,
.
category_id
=
LAND_CAR
,
.
device_pin_value
=
public_pin_value
,
.
device_pwm_value
=
public_pwm_value
},
{
.
device_id
=
DEVICE_TANK0202
,
.
device_id
=
DEVICE_TANK0202
,
.
category_id
=
MARINE_TANK
,
.
category_id
=
MARINE_TANK
,
.
device_pin_value
=
public_pin_value
,
.
device_pin_value
=
public_pin_value
,
...
@@ -314,7 +320,7 @@ void device_gpio_control(int device_id,int pin,int val) {
...
@@ -314,7 +320,7 @@ void device_gpio_control(int device_id,int pin,int val) {
}
}
if
(
!
gpio_control_config_t
)
{
if
(
!
gpio_control_config_t
)
{
my_zlog_error
(
"Error: Device gpio
stop
ID %d not found!"
,
device_id
);
my_zlog_error
(
"Error: Device gpio
control
ID %d not found!"
,
device_id
);
return
;
return
;
}
}
...
...
fcsgdevintroduce.md
View file @
22393bcf
...
@@ -30,6 +30,7 @@
...
@@ -30,6 +30,7 @@
-
car0102 为小车 最大速度为200
-
car0102 为小车 最大速度为200
-
car0103 为挖机 最大速度为200,更据电池电压具体调速。原电池为7.6v,大概为140左右
-
car0103 为挖机 最大速度为200,更据电池电压具体调速。原电池为7.6v,大概为140左右
-
car0104 为推土机 最大速度为200,更据电池电压具体调速。原电池为7.6v,大概为140左右
-
car0104 为推土机 最大速度为200,更据电池电压具体调速。原电池为7.6v,大概为140左右
-
car0105 为超大车,改遥控器,很多都没有
-
ptz0401 为炮台,有限位。
-
ptz0401 为炮台,有限位。
-
0403 为定位设备
-
0403 为定位设备
-
0501 为机械狗
-
0501 为机械狗
...
...
include/common.h
View file @
22393bcf
...
@@ -47,4 +47,11 @@
...
@@ -47,4 +47,11 @@
#include "delay.h"
#include "delay.h"
#include "mylog.h"
#include "mylog.h"
// 定义一个结构体表示串口,整洁
typedef
struct
{
int
fd
;
char
port_name
[
64
];
int
is_open
;
}
SerialPort
;
#endif // COMMON_H
#endif // COMMON_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