diff options
Diffstat (limited to 'udp2lcm/udp2lcm.c')
-rw-r--r-- | udp2lcm/udp2lcm.c | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/udp2lcm/udp2lcm.c b/udp2lcm/udp2lcm.c index fdf32c8..76f41b0 100644 --- a/udp2lcm/udp2lcm.c +++ b/udp2lcm/udp2lcm.c | |||
@@ -1,16 +1,6 @@ | |||
1 | // Encoded in UTF-8 | 1 | // Encoded in UTF-8 |
2 | |||
3 | /* | ||
4 | * struct path_ctrl_t | ||
5 | * { | ||
6 | * int8_t cmd; | ||
7 | * int8_t speed; | ||
8 | * } | ||
9 | * 这是与轮控模块通信的数据结构,speed为0-100表示速度 | ||
10 | * cmd为0停,1前,2左,3右 | ||
11 | */ | ||
12 | |||
13 | #include "udp2lcm.h" | 2 | #include "udp2lcm.h" |
3 | #include <sys/signal.h> | ||
14 | 4 | ||
15 | pthread_t udpRecv, udpSend; | 5 | pthread_t udpRecv, udpSend; |
16 | int8_t heartBeat[9] = {0}; | 6 | int8_t heartBeat[9] = {0}; |
@@ -19,7 +9,19 @@ struct sockaddr_in clientAddr; | |||
19 | char clientIP[20] = ""; | 9 | char clientIP[20] = ""; |
20 | lcm_t *lcm; | 10 | lcm_t *lcm; |
21 | 11 | ||
12 | pid_t httpServerPid = -1; | ||
13 | |||
14 | void sigIntHandler(int sig) { | ||
15 | if (httpServerPid != -1) { | ||
16 | kill(httpServerPid, SIGINT); | ||
17 | } | ||
18 | printf("Exiting...\n"); | ||
19 | exit(sig); | ||
20 | } | ||
21 | |||
22 | int main() { | 22 | int main() { |
23 | // 当遇到Ctrl-C时,递归地杀死所有子进程 | ||
24 | signal(SIGINT, sigIntHandler); | ||
23 | if (!LCMInit()) { | 25 | if (!LCMInit()) { |
24 | fprintf(stderr, "Error: Failed to initialize LCM\n"); | 26 | fprintf(stderr, "Error: Failed to initialize LCM\n"); |
25 | return -1; | 27 | return -1; |
@@ -28,11 +30,10 @@ int main() { | |||
28 | pthread_mutex_init(&heartBeatMutex, NULL); | 30 | pthread_mutex_init(&heartBeatMutex, NULL); |
29 | // 开启udp接收线程 | 31 | // 开启udp接收线程 |
30 | pthread_create(&udpRecv, NULL, udpRecvHandler, NULL); | 32 | pthread_create(&udpRecv, NULL, udpRecvHandler, NULL); |
31 | pid_t httpServerPid = -1; | ||
32 | if ((httpServerPid = fork()) == 0) { | 33 | if ((httpServerPid = fork()) == 0) { |
33 | // 拉起服务器进程,手机端需要申请的文件/mnt/cf/mapfile/defaultMap.txt | 34 | // 拉起服务器进程,手机端需要申请的文件/data/test/defaultMap.txt.txt |
34 | // ATTENTION: 这里的路径需要根据实际存储地图的路径来修改 | 35 | // ATTENTION: 这里的路径需要根据实际存储地图的路径来修改 |
35 | chdir("/mnt/cf/mapfile/"); | 36 | chdir("/data/test"); |
36 | /* | 37 | /* |
37 | * execlp使用系统调用exec()执行一个程序 | 38 | * execlp使用系统调用exec()执行一个程序 |
38 | * 第一个参数是文件名,将在PATH中查找,最后一个参数必须是NULL | 39 | * 第一个参数是文件名,将在PATH中查找,最后一个参数必须是NULL |
@@ -48,7 +49,8 @@ int main() { | |||
48 | } | 49 | } |
49 | 50 | ||
50 | bool LCMInit() { | 51 | bool LCMInit() { |
51 | if ((lcm = lcm_create("udpm://239.255.76.67:7667?ttl=1")) == NULL) { | 52 | // 多播地址范围:224.0.0.0~239.255.255.255 |
53 | if ((lcm = lcm_create(NULL)) == NULL) { | ||
52 | fprintf(stderr, "Error: Failed to create Path Ctrl LCM\n"); | 54 | fprintf(stderr, "Error: Failed to create Path Ctrl LCM\n"); |
53 | return false; | 55 | return false; |
54 | } | 56 | } |
@@ -86,12 +88,34 @@ void parseCmd(const char *buffer, int bytesReceived) { | |||
86 | path.speed = 0; | 88 | path.speed = 0; |
87 | path_ctrl_t_publish(lcm, "wheel_ctrl", &path); | 89 | path_ctrl_t_publish(lcm, "wheel_ctrl", &path); |
88 | 90 | ||
91 | printf("Save Map!\n"); | ||
89 | robotCtrlInit(&robotCtrlData, 0, 32, 0, 0, 0, 0, 0); | 92 | robotCtrlInit(&robotCtrlData, 0, 32, 0, 0, 0, 0, 0); |
90 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | 93 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); |
91 | |||
92 | pose_t_subscribe(lcm, "CURRENTPOSE", poseHandler, NULL); | 94 | pose_t_subscribe(lcm, "CURRENTPOSE", poseHandler, NULL); |
95 | freeRobotCtrl(&robotCtrlData); | ||
96 | |||
97 | sleep(2); | ||
98 | |||
99 | robotCtrlInit(&robotCtrlData, 0, 10, 0, 7, 0, 0, 0); | ||
100 | pthread_mutex_lock(&heartBeatMutex); | ||
101 | robotCtrlData.dparams[4] = (double)(heartBeat[3] << 8 + heartBeat[4]); | ||
102 | robotCtrlData.dparams[5] = (double)(heartBeat[5] << 8 + heartBeat[6]); | ||
103 | robotCtrlData.dparams[6] = (double)(heartBeat[7] << 8 + heartBeat[8]); | ||
104 | pthread_mutex_unlock(&heartBeatMutex); | ||
105 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | ||
106 | freeRobotCtrl(&robotCtrlData); | ||
93 | } else if (buffer[0] == 3) { | 107 | } else if (buffer[0] == 3) { |
94 | // 手机发来的终点坐标,转发给算法模块 | 108 | // 手机发来的终点坐标,转发给算法模块 |
109 | int x, y, sita; | ||
110 | x = buffer[3] << 8 | buffer[4]; | ||
111 | y = buffer[5] << 8 | buffer[6]; | ||
112 | sita = buffer[7] << 8 | buffer[8]; | ||
113 | robotCtrlInit(&robotCtrlData, 0, 20, 0, 3, 0, 0, 0); | ||
114 | robotCtrlData.dparams[0] = x; | ||
115 | robotCtrlData.dparams[1] = y; | ||
116 | robotCtrlData.dparams[2] = sita; | ||
117 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | ||
118 | freeRobotCtrl(&robotCtrlData); | ||
95 | } | 119 | } |
96 | } | 120 | } |
97 | 121 | ||