-
Notifications
You must be signed in to change notification settings - Fork 8
/
Communication.cpp
72 lines (51 loc) · 1.41 KB
/
Communication.cpp
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
/*
* File: Communication.cpp
* Author: Jan Dufek
*/
#include "Communication.hpp"
Communication::Communication(const char * ip_address, const short port) {
// Create socket descriptor. We want to use datagram UDP.
socket_descriptor = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (socket_descriptor < 0) {
cout << "Error creating socket descriptor." << endl;
}
// Create socket address
socket_address.sin_family = AF_INET;
socket_address.sin_addr.s_addr = inet_addr(ip_address);
socket_address.sin_port = htons(port);
}
Communication::Communication(const Communication& orig) {
}
Communication::~Communication() {
stop_robot();
close_communication();
}
/**
* Sends command to USV's ground control station.
*
* @param command
*/
void Communication::send_command(Command& command) {
// Pack datagram
double package[2];
package[0] = command.get_throttle();
package[1] = command.get_rudder();
// Send throttle
sendto(socket_descriptor, &package, 2 * sizeof (double), 0, (struct sockaddr *) &socket_address, sizeof (socket_address));
}
/**
* Stop USV.
*
*/
void Communication::stop_robot() {
Command * stop_command = new Command(0, 0);
send_command(* stop_command);
}
/**
* Close all communication.
*
*/
void Communication::close_communication() {
// Close socket
close(socket_descriptor);
}