-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanner_greedy.cpp
More file actions
228 lines (195 loc) · 7.13 KB
/
planner_greedy.cpp
File metadata and controls
228 lines (195 loc) · 7.13 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
/*=================================================================
*
* planner.c
*
*=================================================================*/
#include <math.h>
#include <mex.h>
#include <iostream>
using namespace std;
/* Input Arguments */
#define MAP_IN prhs[0]
#define ROBOT_IN prhs[1]
#define TARGET_TRAJ prhs[2]
#define TARGET_POS prhs[3]
#define CURR_TIME prhs[4]
#define COLLISION_THRESH prhs[5]
#define NUMBER_OBJECTS prhs[6]
#define OBJECT_TRAJ prhs[7]
#define OBJECT_SIZE prhs[8]
#define NUMBER_TARGET prhs[9]
#define CAUGHT prhs[10]
/* Output Arguments */
#define ACTION_OUT plhs[0]
//access to the map is shifted to account for 0-based indexing in the map, whereas
//1-based indexing in matlab (so, robotpose and goalpose are 1-indexed)
#define GETMAPINDEX(X, Y, XSIZE, YSIZE) ((Y-1)*XSIZE + (X-1))
#if !defined(MAX)
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#endif
#if !defined(MIN)
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#endif
#define NUMOFDIRS 8
bool collCheck(int* objPose, int x, int y, int num, double* obj_size) //return true if given pose hits dyn object
{
int szX = int(obj_size[0]);
int szY = int(obj_size[1]);
int curObX;
int curObY;
for (int i = 0; i < num; i++)
{
curObX = int(objPose[2 * i]);
curObY = int(objPose[2 * i + 1]); //pass object position
if(x > (curObX - szX/2)
&& x < (curObX + szX/2)
&& y > (curObY - szY / 2)
&& y < (curObY + szY / 2))
return true;
}
return false;
}
static void planner(
double* map,
int collision_thresh,
int x_size,
int y_size,
int robotposeX,
int robotposeY,
int target_steps,
double* target_traj,
double* targetposeV,
int curr_time,
double* action_ptr,
int num_obj,
double* object_traj_set,
double* obj_size,
int num_tar,
double* caught
)
{
/*New added data
*
* targetposeV is a 2 x number of targets vector
* object size is a 2 x 1 vector of x and y size
*/
// 8-connected grid
int dX[NUMOFDIRS] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dY[NUMOFDIRS] = {-1, 0, 1, -1, 1, -1, 0, 1};
// for now greedily move towards the final target position,
// but this is where you can put your planner
int goalposeX = (int) target_traj[target_steps-1];
int goalposeY = (int) target_traj[target_steps-1+target_steps];
// printf("robot: %d %d;\n", robotposeX, robotposeY);
// printf("goal: %d %d;\n", goalposeX, goalposeY);
int bestX = 0, bestY = 0; // robot will not move if greedy action leads to collision
double olddisttotarget = (double)sqrt(((robotposeX-goalposeX)*(robotposeX-goalposeX) + (robotposeY-goalposeY)*(robotposeY-goalposeY)));
double disttotarget;
for(int dir = 0; dir < NUMOFDIRS; dir++)
{
int newx = robotposeX + dX[dir];
int newy = robotposeY + dY[dir];
if (newx >= 1 && newx <= x_size && newy >= 1 && newy <= y_size)
{
if (((int)map[GETMAPINDEX(newx,newy,x_size,y_size)] >= 0) && ((int)map[GETMAPINDEX(newx,newy,x_size,y_size)] < collision_thresh)) //if free
{
disttotarget = (double)sqrt(((newx-goalposeX)*(newx-goalposeX) + (newy-goalposeY)*(newy-goalposeY)));
if(disttotarget < olddisttotarget)
{
olddisttotarget = disttotarget;
bestX = dX[dir];
bestY = dY[dir];
}
}
}
}
robotposeX = robotposeX + bestX;
robotposeY = robotposeY + bestY;
action_ptr[0] = robotposeX;
action_ptr[1] = robotposeY;
return;
}
// prhs contains input parameters (4):
// 1st is matrix with all the obstacles
// 2nd is a row vector <x,y> for the robot position
// 3rd is a matrix with the target trajectory
// 4th is an integer C, the collision threshold for the map
// plhs should contain output parameters (1):
// 1st is a row vector <dx,dy> which corresponds to the action that the robot should make
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray*prhs[] )
{
/* Check for proper number of arguments */
if (nrhs != 11) {
mexErrMsgIdAndTxt( "MATLAB:planner:invalidNumInputs",
"More input arguments required.");
} else if (nlhs != 1) {
mexErrMsgIdAndTxt( "MATLAB:planner:maxlhs",
"One output argument required.");
}
/* get the dimensions of the map and the map matrix itself*/
int x_size = mxGetM(MAP_IN);
int y_size = mxGetN(MAP_IN);
double* map = mxGetPr(MAP_IN);
/* get the dimensions of the robotpose and the robotpose itself*/
int robotpose_M = mxGetM(ROBOT_IN);
int robotpose_N = mxGetN(ROBOT_IN);
if(robotpose_M != 1 || robotpose_N != 2){
mexErrMsgIdAndTxt( "MATLAB:planner:invalidrobotpose",
"robotpose vector should be 1 by 2.");
}
double* robotposeV = mxGetPr(ROBOT_IN);
int robotposeX = (int)robotposeV[0];
int robotposeY = (int)robotposeV[1];
/* get the dimensions of the goalpose and the goalpose itself*/
int targettraj_M = mxGetM(TARGET_TRAJ);
int targettraj_N = mxGetN(TARGET_TRAJ);
if(targettraj_M < 1 )//|| targettraj_N != 2)
{
mexErrMsgIdAndTxt( "MATLAB:planner:invalidtargettraj",
"targettraj vector should be M by 2.");
}
double* targettrajV = mxGetPr(TARGET_TRAJ);
int target_steps = targettraj_M;
/* get the current position of the target*/
int targetpose_M = mxGetM(TARGET_POS);
int targetpose_N = mxGetN(TARGET_POS);
if(targetpose_M != 1 ){//|| targetpose_N != 2){
mexErrMsgIdAndTxt( "MATLAB:planner:invalidtargetpose",
"targetpose vector should be 1 by 2.");
}
double* targetposeV = mxGetPr(TARGET_POS);
//int targetposeX = (int)targetposeV[0];
//int targetposeY = (int)targetposeV[1];
/* get the current timestep the target is at*/
int curr_time = mxGetScalar(CURR_TIME);
/* Create a matrix for the return action */
ACTION_OUT = mxCreateNumericMatrix( (mwSize)1, (mwSize)2, mxDOUBLE_CLASS, mxREAL);
double* action_ptr = (double*) mxGetData(ACTION_OUT);
/* Get collision threshold for problem */
int collision_thresh = (int) mxGetScalar(COLLISION_THRESH);
//Get the new stuff
int num_obj = mxGetScalar(NUMBER_OBJECTS);
int num_tar = mxGetScalar(NUMBER_TARGET);
double* object_traj_set = mxGetPr(OBJECT_TRAJ);
double* obj_size = mxGetPr(OBJECT_SIZE);
double* caught = mxGetPr(OBJECT_SIZE);
/* Do the actual planning in a subroutine */
planner(map,
collision_thresh,
x_size,
y_size,
robotposeX, robotposeY,
target_steps,
targettrajV,
targetposeV,
curr_time,
&action_ptr[0],
num_obj,
object_traj_set,
obj_size,
num_tar,
caught);
// printf("DONE PLANNING!\n");
return;
}