-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_example2.c
209 lines (179 loc) · 5.58 KB
/
tcp_example2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "gps/navigation.h"
#include "gps/sbp.h"
#include "gps/system.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
char* tcp_ip_addr = NULL;
char* tcp_ip_port = NULL;
/* SBP structs that messages from Piksi will feed. */
msg_pos_llh_t pos_llh;
msg_baseline_ned_t baseline_ned;
msg_vel_ned_t vel_ned;
msg_dops_t dops;
msg_gps_time_t gps_time;
/*
* SBP callback nodes must be statically allocated. Each message ID / callback
* pair must have a unique sbp_msg_callbacks_node_t associated with it.
*/
sbp_msg_callbacks_node_t pos_llh_node;
sbp_msg_callbacks_node_t baseline_ned_node;
sbp_msg_callbacks_node_t vel_ned_node;
sbp_msg_callbacks_node_t dops_node;
sbp_msg_callbacks_node_t gps_time_node;
int socket_desc = -1;
void usage(char* prog_name) {
fprintf(stderr, "usage: %s [-a address -p port]\n", prog_name);
}
void setup_socket() {
struct sockaddr_in server;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
fprintf(stderr, "Could not create socket\n");
}
memset(&server, '0', sizeof(server));
server.sin_addr.s_addr = inet_addr(tcp_ip_addr);
server.sin_family = AF_INET;
server.sin_port = htons(atoi(tcp_ip_port));
if (connect(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0) {
fprintf(stderr, "Connection error\n");
}
}
void close_socket() {
close(socket_desc);
}
void sbp_pos_llh_callback(u16 sender_id, u8 len, u8 msg[], void* context) {
pos_llh = *(msg_pos_llh_t*)msg;
}
void sbp_baseline_ned_callback(u16 sender_id, u8 len, u8 msg[], void* context) {
baseline_ned = *(msg_baseline_ned_t*)msg;
}
void sbp_vel_ned_callback(u16 sender_id, u8 len, u8 msg[], void* context) {
vel_ned = *(msg_vel_ned_t*)msg;
}
void sbp_dops_callback(u16 sender_id, u8 len, u8 msg[], void* context) {
dops = *(msg_dops_t*)msg;
}
void sbp_gps_time_callback(u16 sender_id, u8 len, u8 msg[], void* context) {
gps_time = *(msg_gps_time_t*)msg;
}
u32 socket_read(u8* buff, u32 n, void* context) {
(void)context;
u32 result;
result = read(socket_desc, buff, n);
return result;
}
int main(int argc, char** argv) {
int opt;
int result = 0;
sbp_state_t s;
if (argc <= 2) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
while ((opt = getopt(argc, argv, "a:p:")) != -1) {
switch (opt) {
case 'a':
tcp_ip_addr = (char*)calloc(strlen(optarg) + 1, sizeof(char));
if (!tcp_ip_addr) {
fprintf(stderr, "Cannot allocate memory!\n");
exit(EXIT_FAILURE);
}
strcpy(tcp_ip_addr, optarg);
break;
case 'p':
tcp_ip_port = (char*)calloc(strlen(optarg) + 1, sizeof(char));
if (!tcp_ip_port) {
fprintf(stderr, "Cannot allocate memory!\n");
exit(EXIT_FAILURE);
}
strcpy(tcp_ip_port, optarg);
break;
case 'h':
usage(argv[0]);
exit(EXIT_FAILURE);
default:
break;
}
}
if ((!tcp_ip_addr) || (!tcp_ip_port)) {
fprintf(
stderr,
"Please supply the address and port of the SBP data stream!\n"
);
exit(EXIT_FAILURE);
}
setup_socket();
sbp_state_init(&s);
/* Register a node and callback, and associate them with a specific message ID. */
sbp_register_callback(
&s,
SBP_MSG_GPS_TIME,
&sbp_gps_time_callback,
NULL,
&gps_time_node
);
sbp_register_callback(
&s,
SBP_MSG_POS_LLH,
&sbp_pos_llh_callback,
NULL,
&pos_llh_node
);
sbp_register_callback(
&s,
SBP_MSG_BASELINE_NED,
&sbp_baseline_ned_callback,
NULL,
&baseline_ned_node
);
sbp_register_callback(
&s,
SBP_MSG_VEL_NED,
&sbp_vel_ned_callback,
NULL,
&vel_ned_node
);
sbp_register_callback(
&s,
SBP_MSG_DOPS,
&sbp_dops_callback,
NULL,
&dops_node
);
while (1) {
sbp_process(&s, &socket_read);
fprintf(stdout, "\n\n\n\n");
/* Print GPS time. */
fprintf(stdout, "GPS Time:\n");
fprintf(stdout, "\tWeek\t\t: %6d\n", (int)gps_time.wn);
fprintf(stdout, "%6.2f", ((float)gps_time.tow) / 1e3);
fprintf(stdout, "\n");
/* Print absolute position. */
fprintf(stdout, "Absolute Position:\n");
fprintf(stdout, "%4.10lf", pos_llh.lat);
fprintf(stdout, "%4.10lf", pos_llh.lon);
fprintf(stdout, "%4.10lf", pos_llh.height);
fprintf(stdout, "\tSatellites\t: %02d\n", pos_llh.n_sats);
fprintf(stdout, "\n");
/* Print NED (North/East/Down) baseline (position vector from base to rover). */
fprintf(stdout, "Baseline (mm):\n");
fprintf(stdout, "\tNorth\t\t: %6d\n", (int)baseline_ned.n);
fprintf(stdout, "\tEast\t\t: %6d\n", (int)baseline_ned.e);
fprintf(stdout, "\tDown\t\t: %6d\n", (int)baseline_ned.d);
fprintf(stdout, "\n");
/* Print NED velocity. */
fprintf(stdout, "Velocity (mm/s):\n");
fprintf(stdout, "\tNorth\t\t: %6d\n", (int)vel_ned.n);
fprintf(stdout, "\tEast\t\t: %6d\n", (int)vel_ned.e);
fprintf(stdout, "\tDown\t\t: %6d\n", (int)vel_ned.d);
fprintf(stdout, "\n");
}
close_socket();
free(tcp_ip_addr);
free(tcp_ip_port);
return result;
}