-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbicycle_safety.c
244 lines (207 loc) · 5.32 KB
/
bicycle_safety.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
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
// Patrick Musau
// 08-2020
// Safety checking for f1tenth model file
#include "bicycle_safety.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
// provide initial value for global variables
double ** wallCoords = 0;
double *** obstacles = 0;
int file_rows = 0;
int file_columns = 0;
int obstacle_count = 0;
// function that allocates the 2d array of wall points
void allocate_2darr(int rows,int columns)
{
wallCoords = malloc(rows * sizeof(double *));
if(wallCoords == NULL)
{
fprintf(stderr, "out of memory\n");
exit(0);
}
// allocate each of the rows with arrays of length 2
for(int i = 0; i < rows; i++)
{
wallCoords[i] = malloc(columns * sizeof(double));
if(wallCoords[i] == NULL)
{
fprintf(stderr, "out of memory\n");
exit(0);
}
}
}
// free the memory allocated for the wall points
void deallocate_2darr(int rows,int columns)
{
for(int i = 0; i < rows; i++)
free(wallCoords[i]);
free(wallCoords);
printf("Done\n");
}
int countlines(const char * filename)
{
int cnt =0;
FILE *fp;
char line[60];
// open the file
fp = fopen(filename,"r");
if(fp==NULL)
{
printf("Could not open file %s",filename);
}
else
{
while( fgets(line, 60, fp)!=NULL )
{
cnt+=1;
}
fclose(fp);
}
return cnt;
}
// function that loads points of the wall from the file
void load_wallpoints(const char * filename, bool print)
{
char line[60];
char * x, * y;
double xd, yd;
int i;
FILE *wallPoints;
file_rows = countlines(filename);
if(print)
printf("Opening file...with %d points\n", file_rows);
// allocate the memory
allocate_2darr(file_rows,file_columns);
// open the file
wallPoints = fopen(filename,"r");
if(wallPoints==NULL)
{
printf("Could not open file %s\n",filename);
}
else
{
i =0;
while( fgets (line, 60, wallPoints)!=NULL )
{
x = strtok(line,",");
y = strtok(NULL,",");
xd = strtod(x,NULL);
yd = strtod(y,NULL);
wallCoords[i][0] = xd;
wallCoords[i][1] = yd;
i+=1;
}
fclose(wallPoints);
}
}
bool check_safety(HyperRectangle* rect, REAL (*cone)[2])
{
REAL l1[2] = {rect->dims[0].min,rect->dims[1].max};
REAL r1[2] = {rect->dims[0].max,rect->dims[1].min};
REAL l2[2] = {cone[0][0],cone[1][1]};
REAL r2[2] = {cone[0][1],cone[1][0]};
if (l1[0] >= r2[0] || l2[0] >= r1[0])
return true;
if (l1[1] <= r2[1] || l2[1] <= r1[1])
return true;
return false;
}
bool check_safety_obstacles(HyperRectangle* rect)
{
// loop through the obstacles
bool allowed = true;
for (int j = 0; j < obstacle_count; j++)
{
double obs[2][2] = {{obstacles[j][0][0],obstacles[j][0][1]}, {obstacles[j][1][0],obstacles[j][1][1]}};
allowed= check_safety(rect,obs);
if(!allowed)
{
// printf("offending cone [%f, %f], ,[%f, %f]\n",obstacles[j][0][0],obstacles[j][0][1],obstacles[j][1][0],obstacles[j][1][1]);
break;
}
}
return allowed;
}
bool check_safety_wall(HyperRectangle* rect)
{
bool safe = true;
for (int i = 0;i<file_rows;i++)
{
double point[2][2] = {{wallCoords[i][0],wallCoords[i][0]},{wallCoords[i][1],wallCoords[i][1]}};
safe = check_safety(rect,point);
if(!safe)
{
// printf("offending point (%f,%f)\n",wallCoords[i][0],wallCoords[i][1]);
// println(rect);
break;
}
}
return safe;
}
// function that allocates the 3d array for the obstacles
void allocate_obstacles(int num_obstacles,double (*points)[2])
{
int rows = num_obstacles;
obstacle_count = num_obstacles;
int cols = 2;
int height = 2;
int i,j;
double w = 0.13;
double h = 0.13;
obstacles = (double***)malloc(rows * sizeof(double **));
// check if memory was allocated
if(obstacles == NULL)
{
fprintf(stderr, "out of memory\n");
exit(0);
}
for(i=0;i<rows;i++)
{
obstacles[i] = (double **)malloc(cols * sizeof(double*));
// check if memory was allocated
if(obstacles[i] == NULL)
{
fprintf(stderr, "out of memory\n");
exit(0);
}
for(j=0;j<cols;j++)
{
obstacles[i][j] = (double*)malloc(height*sizeof(double));
// check if memory was allocated
if(obstacles[i][j] == NULL)
{
fprintf(stderr, "out of memory\n");
exit(0);
}
}
}
printf("interval list of obstacles: \n");
for(i=0;i<rows;i++)
{
obstacles[i][0][0] = points[i][0]-w/2.0;
obstacles[i][0][1] = points[i][0]+w/2.0;
obstacles[i][1][0] = points[i][1]-h/2.0;
obstacles[i][1][1] = points[i][1]+h/2.0;
printf("[%f,%f], [%f,%f]\n", obstacles[i][0][0],obstacles[i][0][1],obstacles[i][1][0],obstacles[i][1][1]);
}
printf("\n");
}
// free the memory allocated for the wall points
void deallocate_obstacles(int num_obstacles)
{
int rows = num_obstacles;
int cols = 2;
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
free(obstacles[i][j]);
}
free(obstacles[i]);
}
free(obstacles);
}