-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoythread.cpp
More file actions
362 lines (285 loc) · 11 KB
/
joythread.cpp
File metadata and controls
362 lines (285 loc) · 11 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "joythread.h"
// constructor for setting joystick id (JOYSTICKID1) and initializing JOYINFOEX
JoyThread::JoyThread()
{
runFlag = true; // running flag ON
// initialize JOYINFOEX
joyInfo.dwSize = sizeof JOYINFOEX;
joyInfo.dwFlags = JOY_RETURNALL;
}
void JoyThread::startJoyThread(int joyId, QStringList leftMotorList, QStringList rightMotorList, QStringList indi1MotorList, QStringList indi2MotorList, int numMotors)
{
qDebug()<<"startJoyThread: "<<currentThreadId();
// set joystick id and motor ids
this->joyId = joyId;
// set motor ids
this->numMotors = numMotors;
this->leftMotorList = leftMotorList;
this->rightMotorList = rightMotorList;
this->indi1MotorList = indi1MotorList;
this->indi2MotorList = indi2MotorList;
// stop thread if thread is already running
if(isRunning())
stop();
// start thread
start();
runFlag = true;
}
void JoyThread::startServerJoyThread(int joyId, QTcpSocket *mySocket, QStringList leftMotorList, QStringList rightMotorList, QStringList indi1MotorList, QStringList indi2MotorList, int numMotors)
{
qDebug()<<"startSocketJoyThread: "<<currentThreadId();
// set IP address, TCP port and motor ids
this->mySocket = mySocket;
// set connection mode
connectMode = SERVER_MODE;
// start thread
startJoyThread(joyId, leftMotorList, rightMotorList, indi1MotorList, indi2MotorList, numMotors);
}
void JoyThread::startDeviceJoyThread(int joyId, RoboteqDevice *myDevice, QStringList leftMotorList, QStringList rightMotorList, QStringList indi1MotorList, QStringList indi2MotorList, int numMotors)
{
qDebug()<<"startDeviceJoyThread: "<<currentThreadId();
// set roboteQ device
this->myDevice = myDevice;
// set connection mode
connectMode = DEVICE_MODE;
// start thread
startJoyThread(joyId, leftMotorList, rightMotorList, indi1MotorList, indi2MotorList, numMotors);
}
// method for getting joystick position and sending to UI
void JoyThread::run()
{
//---------------------------
// test if each motor is available (alive)
aliveMotorList.clear();
for(int motorID=1;motorID<=numMotors;motorID++){
// move motor
SetCommand("_VAR",motorID,500,connectMode);
// mesure amp value
double sumAmp = 0;
for(int ampTrial=0;ampTrial<10;ampTrial++)
sumAmp = sumAmp + GetValue("_VAR",motorID+10,connectMode).toDouble();
// stop motor
SetCommand("_VAR",motorID,0,connectMode);
// add motor id if average amp is higher than threshold
if(sumAmp/10 > 1)
aliveMotorList.append(QString::number(motorID));
qDebug() << "average Amp=" << sumAmp/10;
}
setAliveMotors(aliveMotorList);
//---------------------------
while(runFlag) // loop if runFlag on
{
// get joystick position
if (joyGetPosEx(joyId, &joyInfo) == JOYERR_NOERROR){
//convert to QString
QString x = QString::number(joyInfo.dwXpos);
QString y = QString::number(joyInfo.dwYpos);
QString z = QString::number(joyInfo.dwZpos);
QString r = QString::number(joyInfo.dwRpos);
QString bt = QString::number(joyInfo.dwButtons);
// emit joystick value
emit printJoyValue(x,y,z,r,bt);
//----------------------
// convert to motor value
double throttleValue = ((double)joyInfo.dwYpos - joyMidValue)/joyMidValue*motorMaxValue;
double steeringRatio = ((double)joyInfo.dwXpos - joyMidValue)/joyMidValue;
double indiThrottleValue1 = ((double)joyInfo.dwRpos - joyMidValue)/joyMidValue*motorMaxValue;
double indiThrottleValue2 = ((double)joyInfo.dwZpos - joyMidValue)/joyMidValue*motorMaxValue;
double leftMotorValue = 0;
double rightMotorValue = 0;
// rotation at the same position
if(abs(throttleValue) < throttleMaxRotValue){
leftMotorValue = limitValue(-steeringRatio*motorMaxValue,motorMinValue,motorMaxValue);
rightMotorValue = limitValue( steeringRatio*motorMaxValue,motorMinValue,motorMaxValue);
// move
}else{
leftMotorValue = limitValue((1 + steeringRatio)*throttleValue,motorMinValue,motorMaxValue);
rightMotorValue = limitValue((1 - steeringRatio)*throttleValue,motorMinValue,motorMaxValue);
}
qDebug("throttleValue=%f steeringRatio=%f leftMotor=%d rightMotor=%d indiThrottleValue1=%d indiThrottleValue2=%d",throttleValue,steeringRatio,(int)leftMotorValue,(int)rightMotorValue,(int)indiThrottleValue1, (int)indiThrottleValue2);
//----------------------
//----------------------
// send command to server
QString tmpStr;
QByteArray sendMsg;
QByteArray recvMsg;
//----------
// set velocity value
//if(!abs(((double)joyInfo.dwYpos - joyMidValue)) && !abs(((double)joyInfo.dwXpos - joyMidValue))){
if(abs(((double)joyInfo.dwRpos - joyMidValue)) || abs(((double)joyInfo.dwZpos - joyMidValue))){
//if(abs(indiThrottleValue1) > throttleMaxRotValue || abs(indiThrottleValue2) > throttleMaxRotValue){
//if(joyInfo.dwButtons&JOY_BUTTON7){
// individual motors group 1
for(QString id: indi1MotorList)
SetCommand("_VAR",id.toInt(),(int)indiThrottleValue1,connectMode);
// individual motors group 2
for(QString id: indi2MotorList)
SetCommand("_VAR",id.toInt(),(int)indiThrottleValue2,connectMode);
}else{
// left motors, inverse value
for(QString id: leftMotorList)
SetCommand("_VAR",id.toInt(),-leftMotorValue,connectMode);
// right motors
for(QString id: rightMotorList)
SetCommand("_VAR",id.toInt(),rightMotorValue,connectMode);
}
//----------
//----------
// get motor power
QStringList pow;
for(int i=1;i<=numMotors;i++)
pow.append(GetValue("_VAR",i+10,connectMode));
// emit motor power signals
emit printPowerValue(pow);
//----------
/*
//----------
// get encoder value
// motor 1
QStringList enc;
enc.append(GetValueSocket("_C",1));
enc.append(GetValueSocket("_C",2));
// emit encoder signals
emit printEncoderValue(enc);
//----------
*/
Sleep(100);
//----------------------
}
}
//----------------------
// loops is over and then disconnect
if(connectMode == SERVER_MODE){
// close socket connection
mySocket->write("_DIS ");
// wait for writing command data and receiving feedback from server
mySocket->waitForBytesWritten();
}else if(connectMode == DEVICE_MODE){
// close device connection
myDevice->Disconnect();
}
//----------------------
}
// send specified command with id and value to the server
QByteArray JoyThread::SetCommandDevice(QString cmd, quint8 id, int val)
{
// send command to RoboteQ driver
int status = 0;
if(!cmd.compare("_VAR")){
if((status = myDevice->SetCommand(_VAR, id, val)) != RQ_SUCCESS)
qDebug() << "failed --> "<<status;
else
qDebug() << "succeeded.";
}
// convert "status" integer to QByteArray
QByteArray recvMsg;
recvMsg.append(QString::number(status));
return recvMsg;
}
// send specified command with id and value to the server
QByteArray JoyThread::GetValueDevice(QString cmd, quint8 id)
{
int recvMsgInt=0;
int status = 0;
// receive feedback from RoboteQ driver
if(!cmd.compare("_VAR")){
if((status = myDevice->GetValue(_VAR, id, recvMsgInt)) != RQ_SUCCESS)
qDebug()<<"GetValue failed --> "<<status;
else
qDebug()<<"GetValue succeeded.";
}
// convert "status"recvMsgInt" integer to QByteArray
QByteArray recvMsg;
recvMsg.append(QString::number(recvMsgInt));
qDebug() << "recvMsg" << recvMsg;
return recvMsg;
}
// send specified command with id and value to the server
QByteArray JoyThread::SetCommandSocket(QString cmd, quint8 id, int val)
{
// make command
QByteArray sendMsg = QString("%1 %2 %3").arg(cmd).arg(id).arg(val).toUtf8();
// send command to server through socket "mySocket"
mySocket->write(sendMsg);
// wait for writing command data and receiving feedback from server
mySocket->waitForBytesWritten();
mySocket->waitForReadyRead();
// receive feedback from server
QByteArray recvMsg = mySocket->read(256);
/*
// print out messages
qDebug(sendMsg);
qDebug(recvMsg);
*/
return recvMsg;
}
// send specified command with id and value to the server
QByteArray JoyThread::GetValueSocket(QString cmd, quint8 id)
{
// make command
QByteArray sendMsg = QString("%1 %2").arg(cmd).arg(id).toUtf8();
// send command to server through socket "mySocket"
mySocket->write(sendMsg);
// wait for writing command data and receiving feedback from server
mySocket->waitForBytesWritten();
mySocket->waitForReadyRead();
// receive feedback from server
QByteArray recvMsg = mySocket->read(256);
/*
// print out messages
qDebug(sendMsg);
qDebug(recvMsg);
*/
return recvMsg;
}
// send specified command with id and value to the server or driver
QByteArray JoyThread::SetCommand(QString cmd, quint8 id, int val, int mode)
{
QByteArray recvMsg;
switch(mode){
case SERVER_MODE:
recvMsg = SetCommandSocket(cmd,id,val);
case DEVICE_MODE:
recvMsg = SetCommandDevice(cmd,id,val);
}
return recvMsg;
}
// send specified command with id and value to the server or driver
QByteArray JoyThread::GetValue(QString cmd, quint8 id, int mode)
{
QByteArray recvMsg;
switch(mode){
case SERVER_MODE:
recvMsg = GetValueSocket(cmd,id);
case DEVICE_MODE:
recvMsg = GetValueDevice(cmd,id);
}
return recvMsg;
}
// method for stopping thred
void JoyThread::stop()
{
runFlag = false; // running flag OFF
wait();
}
// method for changing invidual controlled motor id
void JoyThread::changeMotorIDs(QStringList leftMotorList, QStringList rightMotorList, QStringList indi1MotorList, QStringList indi2MotorList)
{
this->leftMotorList = leftMotorList;
this->rightMotorList = rightMotorList;
this->indi1MotorList = indi1MotorList;
this->indi2MotorList = indi2MotorList;
}
// destructor
JoyThread::~JoyThread()
{
runFlag = false;
wait();
}
// limit value in then range between min and max
double JoyThread::limitValue(double value,double min,double max)
{
value = (value < min) ? min : value;
value = (value > max) ? max : value;
return value;
}