-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTORCSLink.c
98 lines (82 loc) · 2.92 KB
/
TORCSLink.c
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
/* TORCSLink, an interface between TORCS and MATLAB/Simulink
Copyright(C) 2014 Owen McAree
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
#include "TORCSLink.h"
int initTORCSLink() {
/* Set up shared memory */
if (tlInitSharedMemory() != 0) {
return -1;
}
/* If robot restarting is enabled, restart the race before doing anything else */
#ifdef TL_ENABLE_RESTARTS
int timeout = 0, maxTimeout = 300;
/* Set the restart flag and wait for robots to be ready */
tlData->controlFlag = TL_RESTART_RACE;
while (tlData->controlFlag != TL_READY && timeout++<maxTimeout) {
#ifdef _WIN32
Sleep(100);
#elif __linux__
usleep(100000);
#endif
}
if (timeout >= maxTimeout) {
return -2;
}
#endif
return 0;
}
/* Simulink doesn't like passing arrays of structure (No idea why...), so had to split these three up*/
/* Set vehicle controls */
int setVehicleControl(int i, vehicleControl_t con) {
/* If we can't read any data then something went wrong with the initialise
Easiest way to inform user is by returning -1 and letting Simulink deal with it */
if (tlData == NULL) {
return -1; /* Bad connection */
}
tlData->enable = 1;
tlData->vehicle[i].control = con;
return 0;
}
/* Update TORCS by informing it that it has new data for controls then wait until it updates outputs */
int updateTORCS() {
int itr = 0, maxItr = 1E9;
/* If we can't read any data then something went wrong with the initialise
Easiest way to inform user is by returning -1 and letting Simulink deal with it */
if (tlData == NULL) {
return -1; /* Bad connection */
}
/* Inform robot is has new data */
tlData->controlFlag = TL_NEW_DATA;
/* Wait until robots have updated themselves */
while (tlData->controlFlag == TL_NEW_DATA && ++itr<maxItr) {
}
if (itr >= maxItr) {
return -itr; /* No new data in time */
}
return itr;
}
/* Read out vehicle data */
int getVehicleData(int i, vehicleData_t *veh) {
/* If we can't read any data then something went wrong with the initialise
Easiest way to inform user is by returning -1 and letting Simulink deal with it */
if (tlData == NULL) {
return -1; /* Bad connection */
}
(*veh) = tlData->vehicle[i].data;
return 0; /* All is well */
}
int terminateTORCSLink() {
tlData->enable = 0;
tlCloseSharedMemory();
return 0;
}