00001 #include "fsmmove.h"
00002 #include "movehelper.h"
00003 #include "robot.h"
00004 #include "trgen.h"
00005 #include "../../common/map_definitions.h"
00006 #include <aalgorithm.h>
00007 #include <path_simplifier.h>
00008 #include <pathqueue.h>
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <sys/shm.h>
00012 #include <sys/stat.h>
00013 #include <math.h>
00014
00015
00016
00017 GraphMapCell graph[MAP_HEIGHT][MAP_WIDTH];
00018 char *map = NULL;
00019 int shmap_id;
00020
00021 FSM_STATE_DECL(path_planning);
00022
00023 FSM_STATE(main_init) {
00024
00025 ROBOT_LOCK(act_pos);
00026 robot.act_pos.x = (double)ROBOT_AXIS_TO_BACK_MM/1000.0;
00027 robot.act_pos.y = ((double)PLAYGROUND_HEIGHT_MM - ROBOT_WIDTH_MM/2.0)/1000.0;
00028 robot.act_pos.phi = 0;
00029 ROBOT_UNLOCK(act_pos);
00030 FSM_TRANSITION(path_planning);
00031 }
00032
00033 void ShmapInit(void){
00034 const int shmap_size = sizeof(char) * MAP_WIDTH * MAP_HEIGHT;
00035
00036 int i,j;
00037
00038
00039 shmap_id = shmget (SHM_MAP_KEY , shmap_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
00040
00041
00042 map = (char*) shmat (shmap_id, 0, 0);
00043
00044
00045 for (j=0;j<MAP_HEIGHT;j++){
00046 for(i=0;i<MAP_WIDTH;i++){
00047 GETMAPPOS(i,j) = MAP_FREE;
00048 }
00049 }
00050 printf("Map initialized\n");
00051
00052 }
00053
00054 void ShmapFree(void){
00055
00056 shmdt (map);
00057
00058
00059 shmctl (shmap_id, IPC_RMID, 0);
00060 }
00061
00062 #if 0
00063 void path_planner(int xstart,int ystart, int xgoal, int ygoal)
00064 {
00065 int i,j;
00066 int tmpx, tmpy, nbpoints, count;
00067 double *original_path;
00068 PathPoint * simple_path = (PathPoint *) malloc(sizeof(PathPoint));
00069 PathPoint * tmp = NULL;
00070 PathPoint * toFree = NULL;
00071 NodeQueue list;
00072 Node n;
00073 list.first= NULL;
00074 list.last=NULL;
00075
00076
00077 struct TrajectoryConstraints tc = trajectoryConstraintsDefault;
00078
00079
00080
00081
00082 robot_trajectory_new(&tc, 0);
00083
00084
00085 for(i=0;i<MAP_HEIGHT;i++){
00086 for (j=0;j<MAP_WIDTH;j++){
00087 graph[i][j].h=-1;
00088 graph[i][j].f=-1;
00089 graph[i][j].g=0xFFFF;
00090 graph[i][j].bx=-1;
00091 graph[i][j].by=-1;
00092 graph[i][j].processed=0;
00093 }
00094 }
00095 DBG("Searching a path between (%f,%f) and (%f,%f)\n",
00096 GRAPHX2FIELD(xstart), GRAPHY2FIELD(ystart), GRAPHX2FIELD(xgoal), GRAPHY2FIELD(ygoal));
00097
00098
00099 if (aAlgorithm(xstart,ystart, xgoal, ygoal)) DBG("PATH CALCULATED!!\n");
00100 else DBG("ERROR : The path planner can not find a path!");
00101 #define PATH_SIMPLIFIER
00102 #ifdef PATH_SIMPLIFIER
00103
00104 for (j=0;j<MAP_HEIGHT;j++){
00105 for(i=0;i<MAP_WIDTH;i++){
00106 GETMAPPOS(i,j) = MAP_FREE;
00107 }
00108 }
00109
00110 i = xgoal;j = ygoal; nbpoints =1;
00111
00112 DBG("PATH : (%d,%d) ", i ,j);
00113 while ( ! ((i== xstart) && (j == ystart)) ) {
00114 tmpx = graph[j][i].bx;
00115 tmpy = graph[j][i].by;
00116 nbpoints++;
00117 i=tmpx;j=tmpy;
00118 DBG("(%d,%d) ", i ,j);
00119 GETMAPPOS(i,j) = MAP_PATH;
00120 }
00121 DBG("\nTotal Points : %d \n", nbpoints);
00122 GETMAPPOS(xgoal,ygoal) = MAP_GOAL;
00123 GETMAPPOS(xstart,ystart) = MAP_START;
00124
00125
00126
00127 original_path = (double *) malloc(2 * sizeof(double) * (nbpoints-1));
00128
00129
00130 i = xgoal;j = ygoal;
00131
00132 while ( ! ((i== xstart) && (j == ystart)) ) {
00133 tmpx = graph[j][i].bx;
00134 tmpy = graph[j][i].by;
00135 pushNodeLast( &list, tmpx, tmpy);
00136 i=tmpx;j=tmpy;
00137 }
00138
00139 count = 0;
00140 while(!queueIsEmpty( &list)){
00141 popNode( &list, &n);
00142 *(original_path+count*2) = GRAPHX2FIELD(n.x);
00143 *(original_path+count*2+1) = GRAPHY2FIELD(n.y);
00144 count++;
00145 }
00146
00147
00148
00149 count = path_simplifier(original_path, nbpoints-1, simple_path);
00150 tmp = simple_path;
00151 DBG("Nb of points of a simpler path %d :", count);
00152 while(tmp!= NULL){
00153 printf("(%f,%f) ", tmp->x, tmp->y);
00154 robot_trajectory_add_point( tmp->x, tmp->y);
00155 toFree = tmp;
00156 tmp = tmp->next;
00157
00158 free(toFree);
00159 }
00160 DBG("\n");
00161
00162
00163 #endif
00164
00165 #ifndef PATH_SIMPLIFIER
00166
00167 i = xgoal;j = ygoal;
00168
00169 while ( ! ((i== xstart) && (j == ystart)) ) {
00170 tmpx = graph[j][i].bx;
00171 tmpy = graph[j][i].by;
00172 pushNodeLast( &list, tmpx, tmpy);
00173 i=tmpx;j=tmpy;
00174 }
00175
00176
00177 while(!queueIsEmpty( &list)){
00178 popNode( &list, &n);
00179 robot_trajectory_add_point( GRAPHX2FIELD(n.x), GRAPHY2FIELD(n.y));
00180 }
00181 #endif
00182
00183 robot_trajectory_add_final_point(GRAPHX2FIELD(xgoal), GRAPHY2FIELD(ygoal));
00184
00185
00186
00187
00188 }
00189 #endif
00190 FSM_STATE(path_planning)
00191 {
00192 int startx, starty, goalx, goaly;
00193 switch (FSM_EVENT) {
00194 case EV_STATE_ENTERED:
00195 if (map == NULL) ShmapInit();
00196 robot_trajectory_stop();
00197 ROBOT_LOCK(act_pos);
00198 startx=(int) floor(robot.act_pos.x); starty = (int) floor(robot.act_pos.y);
00199 ROBOT_UNLOCK(act_pos);
00200 goalx = (int) floor((rand()%(PLAYGROUND_WIDTH_MM-50)+25)/100);
00201 goaly = (int) floor((rand()%(PLAYGROUND_HEIGHT_MM-50)+25)/100);
00202 DBG("Moving from (%d,%d) to (%d, %d)\n",startx, starty,goalx, goaly);
00203 path_planner( startx, starty,goalx, goaly);
00204 break;
00205 case EV_TRAJECTORY_DONE_AND_CLOSE:
00206 FSM_TIMER(1000);
00207 break;
00208 case EV_TIMER:
00209 FSM_TRANSITION(path_planning);
00210 break;
00211 case EV_START:
00212
00213 break;
00214 case EV_EXIT:
00215 ShmapFree();
00216 break;
00217 default:
00218 break;
00219 }
00220 }
00221
00222 int main()
00223 {
00224 srand(time(0));
00225
00226 robot_init();
00227
00228
00229 FSM(MOT)->debug_states = 1;
00230
00231 robot.fsm[FSM_ID_MAIN].fnc_act = &fsm_state_main_init;
00232 robot.fsm[FSM_ID_MOT].fnc_act = &fsm_state_move_init;
00233
00234 robot.fsm[FSM_ID_DET].fnc_act = &fsm_state_det_start;
00235 robot.fsm[FSM_ID_PICKUP].fnc_act = &fsm_state_start_pickup;
00236
00237
00238 robot_start();
00239 robot_wait();
00240
00241
00242 robot_destroy();
00243
00244 return 0;
00245 }