-
Notifications
You must be signed in to change notification settings - Fork 2
/
torcs.cpp
337 lines (309 loc) · 9.84 KB
/
torcs.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
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
//
// Created by zj on 17-6-21.
//
#include <iostream>
#include <unistd.h>
#include <sys/shm.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
using namespace std;
#define image_width 640
#define image_height 480
struct env_to_read{
double steer;
double brake;
double accel;
int gear;
double clutch;
double speed_x;
double speed_y;
double speed_z;
double track_pos;
double track_angle;
double rpm;
double radius;
};
struct env_to_read_29{
float angle_dqn;
float track_dqn[19];
float opponents[36];
float focus[5];
float track_pos_dqn;
float speed_x_dqn;
float speed_y_dqn;
float speed_z_dqn;
float wheel_dqn[4];
float rpm_dqn;
float damage;
float curLapTime;
float lastLapTime;
float distFromStart;
float distRaced;
float fuel;
int racePos;
int gear;
float z;
};
//struct env_to_read_29{
// float angle_dqn;
// float track_dqn[19];
// float track_pos_dqn;
// float speed_x_dqn;
// float speed_y_dqn;
// float speed_z_dqn;
// float wheel_dqn[4];
// float rpm_dqn;
//};
struct env_to_write{
bool is_restart;
double steer;
double brake;
double accel;
int gear;
double clutch;
};
struct shared_use_st
{
int written;
uint8_t data[image_width*image_height*3];
int pause;
int zmq_flag;
int save_flag;
struct env_to_write env_write;
struct env_to_read env_read;
bool is_ready;
bool is_hit_wall;
bool is_finish;
bool is_stuck;
struct env_to_read_29 env_read_29;
bool dqn_ready;
char map_name[100];
char map_ok;
};
class TorcsTool{
private:
void *shm = NULL;
bool flg;
public:
struct shared_use_st *shared;
TorcsTool(int key){
int shmid = shmget((key_t)key, sizeof(struct shared_use_st),0666| IPC_CREAT);
flg = true;
if(shmid == -1){
cout<<"error in shmget"<<endl;
}
shm = shmat(shmid, NULL, 0);
if(shm == (void*)-1){
cout<<"error in shmat"<<endl;
}
shared = (struct shared_use_st*)shm;
shared->written = 0;
shared->pause = 0;
shared->zmq_flag = 0;
shared->save_flag = 0;
shared->env_write.is_restart = false;
shared->env_write.steer = 0;
shared->env_write.brake = 0;
shared->env_write.accel = 0;
shared->env_write.gear = 1;
shared->env_write.clutch = 0;
shared->env_read.speed_x = 0;
shared->env_read.speed_y = 0;
shared->env_read.speed_z = 0;
shared->env_read.steer = 0;
shared->env_read.brake = 0;
shared->env_read.accel = 0;
shared->env_read.gear = 0;
shared->env_read.clutch = 0;
shared->env_read.track_angle = 0;
shared->env_read.track_pos = 0;
shared->env_read.rpm = 0;
shared->env_read.radius = 0;
shared->is_ready = false;
shared->is_hit_wall = false;
shared->is_finish = false;
shared->is_stuck = false;
shared->env_read_29.angle_dqn = 0;
shared->env_read_29.track_pos_dqn = 0;
shared->env_read_29.speed_x_dqn = 0;
shared->env_read_29.speed_y_dqn = 0;
shared->env_read_29.speed_z_dqn = 0;
shared->env_read_29.rpm_dqn = 0;
shared->env_read_29.damage = 0;
shared->env_read_29.curLapTime = 0;
shared->env_read_29.lastLapTime = 0;
shared->env_read_29.distFromStart = 0;
shared->env_read_29.distRaced = 0;
shared->env_read_29.fuel = 0;
shared->env_read_29.racePos = 0;
shared->env_read_29.gear = 0;
shared->env_read_29.z = 0;
shared->dqn_ready = false;
shared->map_ok = '0';
//shared->env_read_29.angle_dqn = 0;
//shared->env_read_29.track_pos_dqn = 0;
//shared->env_read_29.speed_x_dqn = 0;
//shared->env_read_29.speed_y_dqn = 0;
//shared->env_read_29.speed_z_dqn = 0;
//shared->env_read_29.rpm_dqn = 0;
//shared->dqn_ready = false;
// printf("\n********** Memory sharing started, attached at %X **********\n", shm);
}
void reverseGetImageFlag(){ shared->pause = 1 - shared->pause; }
int GetImageFlag(){ return shared->pause; }
int GetImageWrite(){ return shared->written; }
void restart(){ shared->env_write.is_restart = true; }
void stop(){ flg = false; }
void setGear(int _gear){ shared->env_write.gear = _gear;}
void setClutch(double _clutch){ shared->env_write.clutch = _clutch;}
void setAccel(double _accel){ shared->env_write.accel = _accel;}
void setSteer(double _steer){ shared->env_write.steer = _steer;}
void setBrake(double _brake){ shared->env_write.brake = _brake;}
void clearHitWall(){shared->is_hit_wall = false;}
void clearFinish(){shared->is_finish = false;}
void clearStuck(){shared->is_stuck = false;}
void change_map(char* map){
//int len = sizeof(map) / sizeof(char);
//strncpy(shared->map_name,len);
strcpy(shared->map_name,map);
}
void change_map_ok(char map_o){
shared->map_ok = map_o;
}
bool isHitWall(){return shared->is_hit_wall;}
bool isFinish(){return shared->is_finish;}
bool isStuck(){
if(shared->is_ready){
return shared->is_stuck;
}else{
return false;
}
}
double getTrackAngle(){return shared->env_read.track_angle;}
double getSpeed(){ return shared->env_read.speed_x;}
double getSpeed_y(){ return shared->env_read.speed_y;}
double getSpeed_z(){ return shared->env_read.speed_z;}
double getRpm(){ return shared->env_read.rpm;}
double getTrackPos(){ return shared->env_read.track_pos;}
double getRadius(){ return shared->env_read.radius;}
double getBrake(){ return shared->env_read.brake; }
double getSteer(){ return shared->env_read.steer; }
double getClutch(){ return shared->env_read.clutch; }
double getAccel(){ return shared->env_read.accel; }
int getGear(){ return shared->env_read.gear; }
float* getTrack(){return shared->env_read_29.track_dqn; }
float getAngle(){return shared->env_read_29.angle_dqn; }
uint8_t* getImage(){
while(flg){
if(shared->written == 1){
shared->written = 0;
return shared->data;
}
}
}
py::array_t<uint8_t> img(){
uint8_t* im = getImage();
return py::array_t<uint8_t>(std::vector<ptrdiff_t>{480, 640, 3}, im);
}
env_to_read* getStruct(){
while(true){
if(shared->is_ready){
shared->is_ready = false;
return &(shared->env_read);
}
}
}
env_to_read_29* get29Data(){
while(true){
if(shared->dqn_ready){
shared->dqn_ready = false;
return &(shared->env_read_29);
}
}
}
};
PYBIND11_MODULE(t, m) {
m.doc() = "pybind11 example plugin";
// define add function
//m.def("add", &add, "A function which adds two numbers");
// bindings to Pet class
py::class_<TorcsTool>(m, "TorcsTool")
.def(py::init<const int>())
.def("reverse",&TorcsTool::reverseGetImageFlag)
.def("get_p",&TorcsTool::GetImageFlag)
.def("get_w",&TorcsTool::GetImageWrite)
.def("acc",&TorcsTool::setAccel)
.def("get_img", &TorcsTool::img);
}
//extern "C"{
//
//// TorcsTool* getTorcsTool(){return new TorcsTool();}
//
// TorcsTool* torcsTool = NULL;
//
// void init(int shared_key){
// torcsTool = new TorcsTool(shared_key);
// }
//
// void reserveScreenShotFlag(){torcsTool->reverseGetImageFlag();}
//
// void stopTorcsTool(){torcsTool->stop();}
//
// void restart(){torcsTool->restart();}
//
// void setGear( int _gear){ torcsTool->setGear(_gear); }
//
// void setClutch(double _clutch){ torcsTool->setClutch(_clutch);}
//
// void setAccel(double _accel){ torcsTool->setAccel(_accel);}
//
// void setSteer(double _steer){ torcsTool->setSteer(_steer);}
//
// void setBrake(double _brake){ torcsTool->setBrake(_brake);}
//
// void setTrack(char* track_name){ torcsTool->change_map(track_name);}
// void setTrackOk(char track_ok){ torcsTool->change_map_ok(track_ok);}
//
// double getTrackAngle(){torcsTool->getTrackAngle();}
//
// bool isHitWall(){torcsTool->isHitWall();}
//
// bool isFinish(){torcsTool->isFinish();}
// bool isStuck(){torcsTool->isStuck();}
//
// void clearHitWall(){torcsTool->clearHitWall();}
//
// void clearFinish(){torcsTool->clearFinish();}
// void clearStuck(){torcsTool->clearStuck();}
//
// uint8_t* getScreenshot(){return torcsTool->getImage();}
//
// double getSpeedX(){return torcsTool->getSpeed();}
// double getSpeedY(){return torcsTool->getSpeed_y();}
// double getSpeedZ(){return torcsTool->getSpeed_z();}
//
// double getSteer(){return torcsTool->getSteer();}
//
// int getGear(){return torcsTool->getGear();}
//
// double getBrake(){return torcsTool->getBrake();}
//
// double getClutch(){return torcsTool->getClutch();}
//
// double getAccel(){return torcsTool->getAccel();}
//
// double getRpm(){ return torcsTool->getRpm();}
// double getTrackPos(){ return torcsTool->getTrackPos();}
// double getRadius(){ return torcsTool->getRadius();}
//
// env_to_read* getStruct(){return torcsTool->getStruct();}
// env_to_read_29* get29Data(){return torcsTool->get29Data();}
//
// float* getTrack(){return torcsTool->getTrack();}
// float getAngle(){return torcsTool->getAngle(); }
//}