diff options
Diffstat (limited to 'udp2lcm/udp2lcm.c')
-rw-r--r-- | udp2lcm/udp2lcm.c | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/udp2lcm/udp2lcm.c b/udp2lcm/udp2lcm.c index 76f41b0..9ac380f 100644 --- a/udp2lcm/udp2lcm.c +++ b/udp2lcm/udp2lcm.c | |||
@@ -30,6 +30,7 @@ int main() { | |||
30 | pthread_mutex_init(&heartBeatMutex, NULL); | 30 | pthread_mutex_init(&heartBeatMutex, NULL); |
31 | // 开启udp接收线程 | 31 | // 开启udp接收线程 |
32 | pthread_create(&udpRecv, NULL, udpRecvHandler, NULL); | 32 | pthread_create(&udpRecv, NULL, udpRecvHandler, NULL); |
33 | pose_t_subscribe(lcm, "CURRENTPOSE", poseHandler, NULL); | ||
33 | if ((httpServerPid = fork()) == 0) { | 34 | if ((httpServerPid = fork()) == 0) { |
34 | // 拉起服务器进程,手机端需要申请的文件/data/test/defaultMap.txt.txt | 35 | // 拉起服务器进程,手机端需要申请的文件/data/test/defaultMap.txt.txt |
35 | // ATTENTION: 这里的路径需要根据实际存储地图的路径来修改 | 36 | // ATTENTION: 这里的路径需要根据实际存储地图的路径来修改 |
@@ -43,7 +44,8 @@ int main() { | |||
43 | execlp("python", "python", "-m", "http.server", NULL); | 44 | execlp("python", "python", "-m", "http.server", NULL); |
44 | } | 45 | } |
45 | while (true) { | 46 | while (true) { |
46 | pause(); | 47 | // pause(); |
48 | lcm_handle(lcm); | ||
47 | } | 49 | } |
48 | return 0; | 50 | return 0; |
49 | } | 51 | } |
@@ -91,28 +93,34 @@ void parseCmd(const char *buffer, int bytesReceived) { | |||
91 | printf("Save Map!\n"); | 93 | printf("Save Map!\n"); |
92 | robotCtrlInit(&robotCtrlData, 0, 32, 0, 0, 0, 0, 0); | 94 | robotCtrlInit(&robotCtrlData, 0, 32, 0, 0, 0, 0, 0); |
93 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | 95 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); |
94 | pose_t_subscribe(lcm, "CURRENTPOSE", poseHandler, NULL); | ||
95 | freeRobotCtrl(&robotCtrlData); | 96 | freeRobotCtrl(&robotCtrlData); |
96 | 97 | ||
97 | sleep(2); | 98 | sleep(2); |
98 | 99 | ||
99 | robotCtrlInit(&robotCtrlData, 0, 10, 0, 7, 0, 0, 0); | 100 | robotCtrlInit(&robotCtrlData, 0, 10, 0, 7, 0, 0, 0); |
100 | pthread_mutex_lock(&heartBeatMutex); | 101 | pthread_mutex_lock(&heartBeatMutex); |
101 | robotCtrlData.dparams[4] = (double)(heartBeat[3] << 8 + heartBeat[4]); | 102 | int16_t x, y, sita; |
102 | robotCtrlData.dparams[5] = (double)(heartBeat[5] << 8 + heartBeat[6]); | 103 | x = swapEndian(*(int16_t *)&heartBeat[3]); |
103 | robotCtrlData.dparams[6] = (double)(heartBeat[7] << 8 + heartBeat[8]); | 104 | y = swapEndian(*(int16_t *)&heartBeat[5]); |
105 | sita = swapEndian(*(int16_t *)&heartBeat[7]); | ||
106 | printf("NOW point: x: %d, y: %d, sita: %d\n", x, y, sita); | ||
107 | robotCtrlData.dparams[4] = (double)x / 20; | ||
108 | robotCtrlData.dparams[5] = (double)y / 20; | ||
109 | robotCtrlData.dparams[6] = (double)sita * M_PI / 180; | ||
104 | pthread_mutex_unlock(&heartBeatMutex); | 110 | pthread_mutex_unlock(&heartBeatMutex); |
105 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | 111 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); |
106 | freeRobotCtrl(&robotCtrlData); | 112 | freeRobotCtrl(&robotCtrlData); |
107 | } else if (buffer[0] == 3) { | 113 | } else if (buffer[0] == 3) { |
108 | // 手机发来的终点坐标,转发给算法模块 | 114 | // 手机发来的终点坐标,大端党,取之后发给算法模块 |
109 | int x, y, sita; | 115 | int x, y, sita; |
110 | x = buffer[3] << 8 | buffer[4]; | 116 | x = swapEndian(*(int16_t *)&buffer[3]); |
111 | y = buffer[5] << 8 | buffer[6]; | 117 | y = swapEndian(*(int16_t *)&buffer[5]); |
112 | sita = buffer[7] << 8 | buffer[8]; | 118 | sita = 0; |
119 | // printf("End point: x: %d, y: %d, sita: %d\n", x, y, sita); | ||
120 | // printf("Original buffer: ", buffer); | ||
113 | robotCtrlInit(&robotCtrlData, 0, 20, 0, 3, 0, 0, 0); | 121 | robotCtrlInit(&robotCtrlData, 0, 20, 0, 3, 0, 0, 0); |
114 | robotCtrlData.dparams[0] = x; | 122 | robotCtrlData.dparams[0] = (double)x / 20; |
115 | robotCtrlData.dparams[1] = y; | 123 | robotCtrlData.dparams[1] = (double)y / 20; |
116 | robotCtrlData.dparams[2] = sita; | 124 | robotCtrlData.dparams[2] = sita; |
117 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); | 125 | robot_control_t_publish(lcm, "ROBOT_CONTROL", &robotCtrlData); |
118 | freeRobotCtrl(&robotCtrlData); | 126 | freeRobotCtrl(&robotCtrlData); |
@@ -122,9 +130,9 @@ void parseCmd(const char *buffer, int bytesReceived) { | |||
122 | void poseHandler(const lcm_recv_buf_t *rbuf, const char *channel, | 130 | void poseHandler(const lcm_recv_buf_t *rbuf, const char *channel, |
123 | const pose_t *msg, void *userdata) { | 131 | const pose_t *msg, void *userdata) { |
124 | int16_t x, y, sita; | 132 | int16_t x, y, sita; |
125 | x = (int16_t)msg->pos[0]; | 133 | x = (int16_t)((double)msg->pos[0] * 20); |
126 | y = (int16_t)msg->pos[1]; | 134 | y = (int16_t)((double)msg->pos[1] * 20); |
127 | sita = (int16_t)msg->pos[2]; | 135 | sita = (int16_t)((double)msg->pos[2] * 180 / M_PI); |
128 | while (sita > 180) | 136 | while (sita > 180) |
129 | sita -= 360; | 137 | sita -= 360; |
130 | while (sita < -180) | 138 | while (sita < -180) |
@@ -133,10 +141,13 @@ void poseHandler(const lcm_recv_buf_t *rbuf, const char *channel, | |||
133 | heartBeat[0] = 0x03; | 141 | heartBeat[0] = 0x03; |
134 | heartBeat[1] = heartBeat[2] = 0; | 142 | heartBeat[1] = heartBeat[2] = 0; |
135 | heartBeat[3] = x >> 8; | 143 | heartBeat[3] = x >> 8; |
136 | heartBeat[4] = x; | 144 | heartBeat[4] = x & 0xff; |
137 | heartBeat[5] = y >> 8; | 145 | heartBeat[5] = y >> 8; |
138 | heartBeat[6] = y; | 146 | heartBeat[6] = y & 0xff; |
139 | heartBeat[7] = sita >> 8; | 147 | heartBeat[7] = sita >> 8; |
140 | heartBeat[8] = sita; | 148 | heartBeat[8] = sita; |
141 | pthread_mutex_unlock(&heartBeatMutex); | 149 | pthread_mutex_unlock(&heartBeatMutex); |
142 | } \ No newline at end of file | 150 | printf("curpose: x: %d, y: %d, sita: %d\n", x, y, sita); |
151 | } | ||
152 | |||
153 | int16_t swapEndian(int16_t val) { return (val << 8) | ((val >> 8) & 0xFF); } \ No newline at end of file | ||