This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DriverModelExample.cpp
278 lines (263 loc) · 10.6 KB
/
DriverModelExample.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
/*==========================================================================*/
/* DriverModel.cpp DLL Module for VISSIM */
/* */
/* Interface module for external driver models. */
/* Example version with a very simple driver model. */
/* */
/* Version of 2012-08-28 Lukas Kautzsch */
/*==========================================================================*/
#include "DriverModel.h"
/*==========================================================================*/
double current_speed = 0.0;
double desired_acceleration = 0.0;
double desired_lane_angle = 0.0;
long active_lane_change = 0;
long rel_target_lane = 0;
double desired_velocity = 0.0;
long turning_indicator = 0;
long vehicle_color = RGB(0,0,0);
double lead_vehicle_distance = 999.0;
double lead_vehicle_speed_difference = -99.0;
double lead_vehicle_length = 0.0;
/*==========================================================================*/
BOOL APIENTRY DllMain (HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
/*==========================================================================*/
DRIVERMODEL_API int DriverModelSetValue (long type,
long index1,
long index2,
long long_value,
double double_value,
char *string_value)
{
/* Sets the value of a data object of type <type>, selected by <index1> */
/* and possibly <index2>, to <long_value>, <double_value> or */
/* <*string_value> (object and value selection depending on <type>). */
/* Return value is 1 on success, otherwise 0. */
switch (type) {
case DRIVER_DATA_PATH :
case DRIVER_DATA_TIMESTEP :
case DRIVER_DATA_TIME :
return 1;
case DRIVER_DATA_VEH_ID :
/* reset leading vehicle's data for this new vehicle */
lead_vehicle_distance = 999.0;
lead_vehicle_speed_difference = -99.0;
lead_vehicle_length = 0.0;
return 1;
case DRIVER_DATA_VEH_LANE :
case DRIVER_DATA_VEH_ODOMETER :
case DRIVER_DATA_VEH_LANE_ANGLE :
case DRIVER_DATA_VEH_LATERAL_POSITION :
return 1;
case DRIVER_DATA_VEH_VELOCITY :
current_speed = double_value;
return 1;
case DRIVER_DATA_VEH_ACCELERATION :
case DRIVER_DATA_VEH_LENGTH :
case DRIVER_DATA_VEH_WIDTH :
case DRIVER_DATA_VEH_WEIGHT :
case DRIVER_DATA_VEH_MAX_ACCELERATION :
return 1;
case DRIVER_DATA_VEH_TURNING_INDICATOR :
turning_indicator = long_value;
return 1;
case DRIVER_DATA_VEH_CATEGORY :
case DRIVER_DATA_VEH_PREFERRED_REL_LANE :
case DRIVER_DATA_VEH_USE_PREFERRED_LANE :
return 1;
case DRIVER_DATA_VEH_DESIRED_VELOCITY :
desired_velocity = double_value;
return 1;
case DRIVER_DATA_VEH_X_COORDINATE :
case DRIVER_DATA_VEH_Y_COORDINATE :
case DRIVER_DATA_VEH_TYPE :
return 1;
case DRIVER_DATA_VEH_COLOR :
vehicle_color = long_value;
return 1;
case DRIVER_DATA_VEH_CURRENT_LINK :
return 0; /* (To avoid getting sent lots of DRIVER_DATA_VEH_NEXT_LINKS messages) */
/* Must return 1 if these messages are to be sent from VISSIM! */
case DRIVER_DATA_VEH_NEXT_LINKS :
case DRIVER_DATA_VEH_ACTIVE_LANE_CHANGE :
case DRIVER_DATA_VEH_REL_TARGET_LANE :
case DRIVER_DATA_NVEH_ID :
case DRIVER_DATA_NVEH_LANE_ANGLE :
case DRIVER_DATA_NVEH_LATERAL_POSITION :
return 1;
case DRIVER_DATA_NVEH_DISTANCE :
if (index1 == 0 && index2 == 1) { /* leading vehicle on own lane */
lead_vehicle_distance = double_value;
}
return 1;
case DRIVER_DATA_NVEH_REL_VELOCITY :
if (index1 == 0 && index2 == 1) { /* leading vehicle on own lane */
lead_vehicle_speed_difference = double_value;
}
return 1;
case DRIVER_DATA_NVEH_ACCELERATION :
return 1;
case DRIVER_DATA_NVEH_LENGTH :
if (index1 == 0 && index2 == 1) { /* leading vehicle on own lane */
lead_vehicle_length = double_value;
}
return 1;
case DRIVER_DATA_NVEH_WIDTH :
case DRIVER_DATA_NVEH_WEIGHT :
case DRIVER_DATA_NVEH_TURNING_INDICATOR :
case DRIVER_DATA_NVEH_CATEGORY :
case DRIVER_DATA_NVEH_LANE_CHANGE :
case DRIVER_DATA_NO_OF_LANES :
case DRIVER_DATA_LANE_WIDTH :
case DRIVER_DATA_LANE_END_DISTANCE :
case DRIVER_DATA_RADIUS :
case DRIVER_DATA_MIN_RADIUS :
case DRIVER_DATA_DIST_TO_MIN_RADIUS :
case DRIVER_DATA_SLOPE :
case DRIVER_DATA_SLOPE_AHEAD :
case DRIVER_DATA_SIGNAL_DISTANCE :
case DRIVER_DATA_SIGNAL_STATE :
case DRIVER_DATA_SIGNAL_STATE_START :
case DRIVER_DATA_SPEED_LIMIT_DISTANCE :
case DRIVER_DATA_SPEED_LIMIT_VALUE :
return 1;
case DRIVER_DATA_DESIRED_ACCELERATION :
desired_acceleration = double_value;
return 1;
case DRIVER_DATA_DESIRED_LANE_ANGLE :
desired_lane_angle = double_value;
return 1;
case DRIVER_DATA_ACTIVE_LANE_CHANGE :
active_lane_change = long_value;
return 1;
case DRIVER_DATA_REL_TARGET_LANE :
rel_target_lane = long_value;
return 1;
default :
return 0;
}
}
/*--------------------------------------------------------------------------*/
DRIVERMODEL_API int DriverModelGetValue (long type,
long index1,
long index2,
long *long_value,
double *double_value,
char **string_value)
{
/* Gets the value of a data object of type <type>, selected by <index1> */
/* and possibly <index2>, and writes that value to <*double_value>, */
/* <*float_value> or <**string_value> (object and value selection */
/* depending on <type>). */
/* Return value is 1 on success, otherwise 0. */
switch (type) {
case DRIVER_DATA_STATUS :
*long_value = 0;
return 1;
case DRIVER_DATA_VEH_TURNING_INDICATOR :
*long_value = turning_indicator;
return 1;
case DRIVER_DATA_VEH_DESIRED_VELOCITY :
*double_value = desired_velocity;
return 1;
case DRIVER_DATA_VEH_COLOR :
*long_value = vehicle_color;
return 1;
case DRIVER_DATA_WANTS_SUGGESTION :
*long_value = 1;
return 1;
case DRIVER_DATA_DESIRED_ACCELERATION : {
/* ### start commenting out from here for "do nothing" */
double net_distance = lead_vehicle_distance - lead_vehicle_length;
double lead_vehicle_speed = current_speed - lead_vehicle_speed_difference;
double desired_distance = lead_vehicle_speed; /* times 1 s */
if (lead_vehicle_speed_difference > 0) {
/* we are faster than the leading vehicle */
if (lead_vehicle_speed > 0) {
if (net_distance > desired_distance) {
/* slow down to leading vehicle's speed with 1 s time gap */
desired_acceleration = - lead_vehicle_speed_difference
* lead_vehicle_speed_difference
/ (net_distance - desired_distance)
/ 2.0;
}
else {
/* try to increase distance */
desired_acceleration = - lead_vehicle_speed_difference - 1.0;
}
} /* if (lead_vehicle_speed > 0) */
else {
/* leading vehicle is standing still */
if (net_distance < 2.1) {
desired_acceleration = -9.9; /* emergency braking */
}
else {
/* brake to standstill in 2.0 m distance */
desired_acceleration = - lead_vehicle_speed_difference
* lead_vehicle_speed_difference
/ (net_distance - 2.0)
/ 2.0;
}
}
} /* if (lead_vehicle_speed_difference > 0) */
else {
/* accelerate to min of leading vehicle's speed and own desired speed */
double new_speed = desired_velocity;
if (lead_vehicle_speed < desired_velocity) {
new_speed = lead_vehicle_speed;
}
desired_acceleration = new_speed - current_speed;
}
/* ### comment out until here for "do nothing" */
*double_value = desired_acceleration;
return 1;
}
case DRIVER_DATA_DESIRED_LANE_ANGLE :
*double_value = desired_lane_angle;
return 1;
case DRIVER_DATA_ACTIVE_LANE_CHANGE :
*long_value = active_lane_change;
return 1;
case DRIVER_DATA_REL_TARGET_LANE :
*long_value = rel_target_lane;
return 1;
case DRIVER_DATA_SIMPLE_LANECHANGE :
*long_value = 1;
return 1;
default :
return 0;
}
}
/*==========================================================================*/
DRIVERMODEL_API int DriverModelExecuteCommand (long number)
{
/* Executes the command <number> if that is available in the driver */
/* module. Return value is 1 on success, otherwise 0. */
switch (number) {
case DRIVER_COMMAND_INIT :
return 1;
case DRIVER_COMMAND_CREATE_DRIVER :
return 1;
case DRIVER_COMMAND_KILL_DRIVER :
return 1;
case DRIVER_COMMAND_MOVE_DRIVER :
return 1;
default :
return 0;
}
}
/*==========================================================================*/
/* Ende of DriverModel.cpp */
/*==========================================================================*/