-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectControls.cpp
More file actions
209 lines (184 loc) · 4.4 KB
/
Copy pathDirectControls.cpp
File metadata and controls
209 lines (184 loc) · 4.4 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
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <sstream>
#include <thread> // std::thread
#include "DirectControls.h"
using namespace std;
const char HANDSHAKE_IN_MSG[] = "Hello Gardien!";
const char HANDSHAKE_OUT_MSG[] = "Hello Overloard!";
int opt = 1;
/*
#define PORT_BASE 8300
#define PORT_THROTTLE PORT_BASE + 0
#define PORT_PITCH PORT_BASE + 1
#define PORT_ROLL PORT_BASE + 2
#define PORT_YAW PORT_BASE + 3
#define PORT_AUX1 PORT_BASE + 4
#define PORT_AUX2 PORT_BASE + 5*/
void DirectController::InitSequence()
{
for (int i = 0; i < 6; i++)
{
send(server_fd[i], HANDSHAKE_IN_MSG, strlen(HANDSHAKE_IN_MSG), 0);
// TODO: Place mechanism to receive back handshake and if not matching, Panic!
char *buff = (char *)malloc(1024);
int valread = read(server_fd[i], buff, 1024);
if (strncmp(buff, HANDSHAKE_OUT_MSG, strlen(HANDSHAKE_OUT_MSG)))
{
std::cout << "Gardien Could not establish Connection / Handshake Failure...\n";
throw "Handshake Failed!";
}
printf("Handshake Successful, Connection Established!\n");
}
disarm();
balance();
disarm();
cout << "Initialization Sequence Completed...\n";
}
DirectController::DirectController(char *ip, int portBase)
{
ConnectChannel(ip, portBase + 0, 0); //PORT_THROTTLE
ConnectChannel(ip, portBase + 1, 1); //PORT_PITCH
ConnectChannel(ip, portBase + 2, 2); //PORT_ROLL
ConnectChannel(ip, portBase + 3, 3); //PORT_YAW
ConnectChannel(ip, portBase + 4, 4); //PORT_AUX1
ConnectChannel(ip, portBase + 5, 5); //PORT_AUX2
InitSequence();
}
DirectController::~DirectController()
{
}
int DirectController::ConnectChannel(char *ip, int port, int channel) // This would create a port for a particular channel
{
int sfd;
struct sockaddr_in *address = new struct sockaddr_in;
// Creating socket file descriptor
if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
address->sin_family = AF_INET;
address->sin_port = htons(port);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, ip, &(address->sin_addr)) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sfd, (struct sockaddr *)address, sizeof(struct sockaddr_in)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
try
{
if (server_fd.size() <= channel)
{
server_fd.push_back(sfd);
addresses.push_back(address);
}
else
{
// TODO: Free up the existing sockets
server_fd[channel] = sfd;
addresses[channel] = address;
}
}
catch (...)
{
perror("Error while Saving the Socket Memory Structures");
exit(EXIT_FAILURE);
}
cout << "Socket for port " << port << " Connected Successfully!" << endl;
return 0;
}
void DirectController::arm()
{
cmd(0, 255, 127, 127);
sleep(1);
cmd(0, 127, 127, 127);
cout << "ARMed Successfully...\n";
}
void DirectController::disarm()
{
cmd(0, 0, 127, 127);
sleep(1);
cmd(0, 0, 127, 127);
cout << "Disarmed Successfully...\n";
}
void DirectController::balance()
{
cmd(-1, -1, 255, 255);
sleep(0.01);
cmd(-1, -1, 127, 127);
}
void DirectController::altitudeHold()
{
//cmd(-1, -1, 127, 127);
}
/* APIs for channel Controls */
void DirectController::cmd(int throttle, int yaw, int roll, int pitch, int aux1, int aux2)
{
setThrottle(throttle);
setYaw(yaw);
setRoll(roll);
setPitch(pitch);
setAux1(aux1);
setAux2(aux2);
}
void DirectController::sendCommand(int val, int channel)
{
try
{
if (val == -1)
{
val = channelBuffs[channel];
}
else
{
channelBuffs[channel] = val;
}
stringstream ss;
ss << ".[:" << val << ":]";
string msg = ss.str();
send(server_fd[channel], msg.c_str(), msg.size(), 0);
}
catch (exception &e)
{
printf("\n{ERROR: %s", e.what());
}
}
void DirectController::setThrottle(int val)
{
sendCommand(val, 0);
}
void DirectController::setPitch(int val)
{
sendCommand(val, 1);
}
void DirectController::setYaw(int val)
{
sendCommand(val, 3);
}
void DirectController::setRoll(int val)
{
sendCommand(val, 2);
}
void DirectController::setAux1(int val)
{
sendCommand(val, 4);
}
void DirectController::setAux2(int val)
{
sendCommand(val, 5);
}