-
Notifications
You must be signed in to change notification settings - Fork 0
/
gps.c
184 lines (157 loc) · 4.28 KB
/
gps.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
#include "gps.h"
#include "navigation.h"
#include "sbp.h"
#include "system.h"
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// see header file for description
void gps_init(char* ip, char* port) {
// assign the chosen ip address and port of Piksi
tcp_ip_addr = ip;
tcp_ip_port = port;
// set descriptor, and call the setup function
socket_desc = -1;
printf("about to set up socket\n");
setup_socket();
printf("about to state_init\n");
sbp_state_init(&s);
/* Register nodes and callbacks,
* 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
);
sbp_register_callback(
&s,
SBP_MSG_IMU_RAW,
&sbp_imu_raw_callback,
NULL,
&imu_raw_node
);
printf("about to start thread\n");
// start a thread which will keep updating location bitch
pthread_create(&gps_update_thread, NULL, gps_thread, NULL);
}
// see header file for description
void gps_finish() {
close_socket();
free(tcp_ip_addr);
free(tcp_ip_port);
// end that thread, bitch
pthread_join(gps_update_thread, NULL);
}
// see header file for description
void setup_socket() {
struct sockaddr_in server;
printf("entered setup_socket\n");
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
fprintf(stderr, "Could not create socket\n");
}
printf("passed if\n");
memset(&server, '0', sizeof(server));
printf("passed memset\n");
server.sin_addr.s_addr = inet_addr(tcp_ip_addr);
//server.sin_addr.s_addr = htonl(INADDR_ANY);
printf("passed server thing\n");
server.sin_family = AF_INET;
printf("passed server.sin thing\n");
server.sin_port = htons(atoi(tcp_ip_port));
printf("about to enter second if\n");
if (connect(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0) {
fprintf(stderr, "Connection error\n");
}
printf("done\n");
}
// see header file for description
void close_socket() {
close(socket_desc);
}
// see header file for description
u32 socket_read(u8* buff, u32 n, void* context) {
(void)context;
u32 result;
result = read(socket_desc, buff, n);
return result;
}
double get_latitude() {
return pos_llh.lat;
}
double get_longitude() {
return pos_llh.lon;
}
double get_height() {
return pos_llh.height;
}
double get_time() {
return pos_llh.tow;
}
double get_error() {
return (pos_llh.h_accuracy + pos_llh.v_accuracy) / 2.0;
}
// see header file for description
void* gps_thread() {
// sbp process (hence reading of GPS data) will need to loop FOREVER
while (1) {
// Use mutual exclusion to prevent bullshit
pthread_mutex_lock(&mutex);
sbp_process(&s, &socket_read);
pthread_mutex_unlock(&mutex);
}
}
// see header file for description
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;
}
void sbp_imu_raw_callback(u16 sender_id, u8 len, u8 msg[], void* context) {
imu_raw = *(msg_imu_raw_t*)msg;
}