px4_communicator.cpp
7.98 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/****************************************************************************
*
* Copyright (c) 2020 ThunderFly s.r.o.. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file px4_communicator.cpp
*
* @author ThunderFly s.r.o., Vít Hanousek <info@thunderfly.cz>
* @url https://github.com/ThunderFly-aerospace
*
* PX4 communication socket.
*/
#include "px4_communicator.h"
#include <iostream>
PX4Communicator::PX4Communicator(VehicleState * v)
{
this->vehicle=v;
}
int PX4Communicator::Init(int portOffset)
{
memset((char *) &simulator_mavlink_addr, 0, sizeof(px4_mavlink_addr));
memset((char *) &px4_mavlink_addr, 0, sizeof(px4_mavlink_addr));
simulator_mavlink_addr.sin_family = AF_INET;
simulator_mavlink_addr.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
simulator_mavlink_addr.sin_port = htons(portBase+portOffset);
if ((listenMavlinkSock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
std::cerr<<"PX4 Communicator: Creating TCP socket failed: " << strerror(errno) << std::endl;
}
//do not accumulate messages by waiting for ACK
int yes = 1;
int result = setsockopt(listenMavlinkSock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
if (result != 0)
{
std::cerr<<"PX4 Communicator: setsockopt failed: " << strerror(errno) << std::endl;
}
//try to close as fast as posible
struct linger nolinger;
nolinger.l_onoff = 1;
nolinger.l_linger = 0;
result = setsockopt(listenMavlinkSock, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(nolinger));
if (result != 0)
{
std::cerr<<"PX4 Communicator: setsockopt failed: " << strerror(errno) << std::endl;
}
// The socket reuse is necessary for reconnecting to the same address
// if the socket does not close but gets stuck in TIME_WAIT. This can happen
// if the server is suddenly closed, for example, if the robot is deleted in gazebo.
int socket_reuse = 1;
result = setsockopt(listenMavlinkSock, SOL_SOCKET, SO_REUSEADDR, &socket_reuse, sizeof(socket_reuse));
if (result != 0)
{
std::cerr<<"PX4 Communicator: setsockopt failed: " << strerror(errno) << std::endl;
}
// Same as above but for a given port
result = setsockopt(listenMavlinkSock, SOL_SOCKET, SO_REUSEPORT, &socket_reuse, sizeof(socket_reuse));
if (result != 0)
{
std::cerr<<"PX4 Communicator: setsockopt failed: " << strerror(errno) << std::endl;
}
if (bind(listenMavlinkSock, (struct sockaddr *)&simulator_mavlink_addr, sizeof(simulator_mavlink_addr)) < 0)
{
std::cerr<<"PX4 Communicator: bind failed: " << strerror(errno) << std::endl;
}
errno = 0;
result=listen(listenMavlinkSock, 5);
if (result < 0)
{
std::cerr<<"PX4 Communicator: listen failed: " << strerror(errno) << std::endl;
}
sleep(5);
unsigned int px4_addr_len=sizeof(px4_mavlink_addr);
while(true)
{
px4MavlinkSock = accept(listenMavlinkSock, (struct sockaddr *)&px4_mavlink_addr, &px4_addr_len);
if (px4MavlinkSock<0)
{
std::cerr<<"PX4 Communicator: accept failed: " << strerror(errno) << std::endl;
}
else
{
std::cerr<<"PX4 Communicator: PX4 Connected."<< std::endl;
break;
}
}
return result;
}
/*void PX4Communicator::CheckClientReconect()
{
struct pollfd fds[1] = {};
fds[0].fd = listenMavlinkSock;
fds[0].events = POLLIN;
int p=poll(&fds[0], 1, 1);
if(p<0)
fprintf(stderr,"Pool for new client error\n");
if(p==0)
{
//fprintf(stderr,"No new Client\n");
}
else
{
fprintf(stderr,"New Client Connected to Bridge\n");
close(px4MavlinkSock);
unsigned int px4_addr_len=sizeof(px4_mavlink_addr);;
px4MavlinkSock = accept(listenMavlinkSock, (struct sockaddr *)&px4_mavlink_addr, &px4_addr_len);
}
}*/
int PX4Communicator::Clean()
{
close(px4MavlinkSock);
close(listenMavlinkSock);
return 0;
}
int PX4Communicator::Send(int offset_us)
{
mavlink_message_t msg;
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
int packetlen;
mavlink_hil_sensor_t sensor_msg =vehicle->getSensorMsg(offset_us);
mavlink_msg_hil_sensor_encode_chan(1, 200, MAVLINK_COMM_0, &msg, &sensor_msg);
packetlen = mavlink_msg_to_send_buffer(buffer, &msg);
send(px4MavlinkSock, buffer, packetlen, 0);
if(send(px4MavlinkSock, buffer, packetlen, 0)!=packetlen)
{
std::cerr << "PX4 Communicator: Sent to PX4 failed: "<< strerror(errno) <<std::endl;
return -1;
}
mavlink_hil_gps_t hil_gps_msg=vehicle->hil_gps_msg;
hil_gps_msg.time_usec+=offset_us;
mavlink_msg_hil_gps_encode_chan(1, 200, MAVLINK_COMM_0, &msg, &hil_gps_msg);
packetlen = mavlink_msg_to_send_buffer(buffer, &msg);
if(send(px4MavlinkSock, buffer, packetlen, 0)!=packetlen)
{
std::cerr << "PX4 Communicator: Sent to PX4 failed: " << strerror(errno) <<std::endl;
return -1;
}
return 0;
}
int PX4Communicator::Recieve(bool blocking)
{
mavlink_message_t msg;
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
struct pollfd fds[1] = {};
fds[0].fd = px4MavlinkSock;
fds[0].events = POLLIN;
int p=poll(&fds[0], 1, (blocking?-1:2));
if(p<0)
std::cerr<<"PX4 Communicator: PX4 Pool error\n" << std::endl;
if(p==0)
{
//std::cerr<<"PX4 Communicator:No PX data" <<std::endl;
}
else
{
if(fds[0].revents & POLLIN)
{
unsigned int slen=sizeof(px4_mavlink_addr);
unsigned int len = recvfrom(px4MavlinkSock, buffer, sizeof(buffer), 0, (struct sockaddr *)&px4_mavlink_addr, &slen);
if (len > 0)
{
mavlink_status_t status;
for (unsigned i = 0; i < len; ++i)
{
if (mavlink_parse_char(MAVLINK_COMM_0, buffer[i], &msg, &status))
{
if(msg.msgid==MAVLINK_MSG_ID_HIL_ACTUATOR_CONTROLS)
{
mavlink_hil_actuator_controls_t controls;
mavlink_msg_hil_actuator_controls_decode(&msg, &controls);
vehicle->setPXControls(controls);
return 1;
}
}
}
}
}
}
return 0;
}