This repository has been archived by the owner on Jun 25, 2023. It is now read-only.
/
udpServer.cpp
300 lines (237 loc) · 7.71 KB
/
udpServer.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <cstdlib>
#include <list>
#include <mutex>
#include <thread>
#include <string>
#include "AdafruitPwm.h"
// Servoblaster pin assignments
#define THUMB 0
#define INDEX 4
#define MIDDLE 2
#define RING 3
#define PINKY 7
#define SHOULDER_ROTATION 2
#define SHOULDER_FLEXION 2
#define BICEP_FLEX 10
// network parameters
#define BUFSIZE 1024
#define PORT 12345
//settings for armMode
#define REALTIME 1
#define PLAYBACK 0
#define STOP 0
#define RECORD 1
#define PLAY 2
#define PWM_SIGS 8
using namespace std;
int servoPins[PWM_SIGS] = {
THUMB,
INDEX,
MIDDLE,
RING,
PINKY,
SHOULDER_ROTATION,
SHOULDER_FLEXION,
BICEP_FLEX
};
// struct for storing parsed data
typedef struct SleevePacket {
int packetNum;
int adcVals[6];
float bicepYpr[3];
} SleevePacket;
void recvThread(int, struct sockaddr_in);
void cmdIssuer();
int parsePacket(char *, SleevePacket*);
float calcFingerPwm(int adcVal, float adcMin, float adcMax, float pwmMin, float pwmMax);
mutex queueMutex; // locking queue for receiving and issuing commands
list<char *> cmdQueue; //queue for storing input
int armMode = REALTIME; //initially start in real time mode
int playBack = STOP;
/*
* global pwmController object, used by cmdIssuer
*/
AdafruitPwm pwmController = AdafruitPwm();
/*
* Thread for receving data from sleeve
* Pushes data onto queue.
* NOTE: Maybe unpackage here or leave that to the
* cmdIssuer so we can receive faster
*/
void recvThread(int socketHandle, struct sockaddr_in remaddr) {
socklen_t addrlen;
int recvlen;
char buf[BUFSIZE];
while(1) {
//cout << "waiting on port " << PORT << endl;
recvlen = recvfrom(socketHandle, buf, BUFSIZE, 0, (struct sockaddr *)&remaddr, &addrlen);
//cout << "received " << recvlen << " bytes" << endl;
if(recvlen > 0) {
buf[recvlen] = 0;
//cout << "received message: " << buf << endl;
}
queueMutex.lock();
//cout << "pushing data onto queue" << endl;
cmdQueue.push_back(buf);
queueMutex.unlock();
//acknowledge packet received so sleeve can sample again
if(sendto(socketHandle, buf, strlen(buf), 0, (struct sockaddr *)&remaddr, addrlen) < 0) {
cout << "send error" << endl;
return;
}
}
}
/*
* Thread for issuing commands.
* Deques data from the receive queue and issues the commands with servoblaster
* Unpackage data here to offload work from receive thread
*/
void cmdIssuer() {
SleevePacket *pkt;
char *data;
bool validData = false; //only do calcs if data was found in queue
float increasingPitch = 0.0;
float prevPitch = 0.0;
while(1) {
queueMutex.lock();
if(!cmdQueue.empty()) {
data = (char *)cmdQueue.front();
cmdQueue.pop_front();
validData = true;
}
if(validData && ((playBack == PLAY && armMode == PLAYBACK) || (armMode == REALTIME))) {
//put packet into pkt struct
pkt = (SleevePacket *)malloc(sizeof(SleevePacket));
parsePacket(data, pkt);
cout << pkt->packetNum << " "
<< pkt->adcVals[0] << " "
<< pkt->adcVals[1] << " "
<< pkt->adcVals[2] << " "
<< pkt->adcVals[3] << " "
<< pkt->adcVals[4] << " "
<< pkt->adcVals[5] << " "
<< pkt->bicepYpr[0] << " "
<< pkt->bicepYpr[1] << " "
<< pkt->bicepYpr[2]<< endl;
// calculate each pwm signal
float pwmSigs[PWM_SIGS] = {0.0,
calcFingerPwm(pkt->adcVals[1], 745, 930, 7.2, 10.4),
calcFingerPwm(pkt->adcVals[2], 730, 1023, 7.4, 11.5),
calcFingerPwm(pkt->adcVals[3], 680, 1023, 7.0, 10.2),
calcFingerPwm(pkt->adcVals[4], 735, 1023, 5.3, 9.6),
0.0, // shoulder rotation
0.0, // shoulder flex
0.0}; // bicep flex
// thumb calculation
pwmSigs[0] = 11.4*((pkt->adcVals[0] - 725.0)/(1023.0 - 725.0))*20.0;
// shoulder rotation calc
if(pkt->bicepYpr[2] >= 0.0 && pkt->bicepYpr[2] < 60.0 ) { //rotation back
pwmSigs[5] = -1.1667*pkt->bicepYpr[2] + 130.0;
} else if (pkt->bicepYpr[2] < 0.0 && pkt->bicepYpr[2] > -80.0) { //forward rotation
pwmSigs[5] = -1.375*pkt->bicepYpr[2] + 130.0;
} else if ( pkt->bicepYpr[2] >= 60.0) { // out bounds back
pwmSigs[5] = 60.0;
} else if (pkt->bicepYpr[2] <= -80.0) { // out of bounds forward
pwmSigs[5] = 240.0;
}
// shoulder flex calc
if(pkt->bicepYpr[1] > -45.0 && pkt->bicepYpr[1] < 0.0) {
pwmSigs[6] = -1.4 * pkt->bicepYpr[1] + 70.0;
} else if ( pkt->bicepYpr[1] <= -45.0) { // flex out bound
pwmSigs[6] = 140.0;
} else if (pkt->bicepYpr[1] >= 0.0) { // flex in bound
pwmSigs[6] = 70.0;
}
// bicep flex calc
pwmSigs[7] = (3.0+4.5*((pkt->adcVals[5]-730.0)/(990.0-740.0)))*20.0;
string cmds[PWM_SIGS];
for(int i = 0; i < PWM_SIGS; i++) {
cmds[i] = "echo " + to_string(servoPins[i]) + "=" + to_string(pwmSigs[i]) + " > /dev/servoblaster";
cout << cmds[i] << endl;
system(cmds[i].c_str());
}
//don't want mem leaks
free(pkt);
}
queueMutex.unlock();
validData = false;
} // while(1)
}
// These work for index - pinky, thumb is calculated separately because servo was reversed
float calcFingerPwm(int adcVal, float adcMin, float adcMax, float pwmMin, float pwmMax) {
if(adcVal < adcMin) {
return 20*pwmMax;
} else if (adcVal > adcMax) {
return (pwmMax-pwmMin)*20.0;
}
float pulseWidth = (pwmMax - pwmMin*((adcVal - adcMin)/(adcMax - adcMin)))*20.0;
return pulseWidth;
}
// Packet received from client is a space separated string
// Format:
// | Packet Number | Finger ADC Vals (5 of them) | Bicep ADC Val | Bicep Yaw | Bicep Pitch | Bicep Roll |
int parsePacket(char *input, SleevePacket *pkt) {
char *strToken = strtok(input, " "); // split on space
int i = 0;
// first item is packet #
pkt->packetNum = atoi(strToken);
// next 6 items are adc values thumb = 0, index = 1, etc...
// 5 = bicep adc index
for(i = 0; i < 6; i++) {
strToken = strtok(NULL, " ");
pkt->adcVals[i] = atoi(strToken);
}
// next 3 will be floats for bicep yaw, pitch, roll respectively
for( i = 0; i < 3; i++) {
strToken = strtok(NULL, " ");
cout << "token: " << strToken << endl;
pkt->bicepYpr[i] = atof(strToken);
}
return 1;
}
int main(int argc, char** argv) {
if(argc != 2) {
cout << "wrong # of args" << endl << "usage: ./udpServer <server_ip_addr>" << endl;
return 0;
}
char *ipAddr = argv[1]; // first argument in dot format
struct sockaddr_in myaddr; // our address
struct sockaddr_in remaddr; // remote address
socklen_t addrlen = sizeof(remaddr);
int recvlen;
int udpSocket; //socket descriptor
// create udp socket
if((udpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
cout << "could not create socket" << endl;
return 0;
}
// fill out sockaddr_in structure with host IP, host port and family (Always AF_INET for IP)
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
inet_pton(AF_INET, ipAddr, &(myaddr.sin_addr));
myaddr.sin_port = htons(PORT);
if(bind(udpSocket, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
cout << "bind failed" << endl;
return 0;
}
//set up pwmController frequency to 50 Hz
pwmController.setPWMFreq(50);
// dispatch command issuer thread
cout << "Spawning command issuer thread..." << endl;
thread ct(cmdIssuer);
ct.detach();
cout << "Done!" << endl;
//dispatch receiver thread
cout << "Spawning receiver thread..." << endl;
thread rt(recvThread, udpSocket, remaddr);
cout << "Done!" << endl;
rt.join(); // wait for receiver thread to join to keep main running
}