00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #ifndef _STATE_MACHINE_CPP_
00036 #define _STATE_MACHINE_CPP_
00037
00038 #include "follow_pedestrian.h"
00039
00040 void state_machine(void)
00041 {
00042
00043
00044 a_h = (((double)rand())/(double)RAND_MAX);
00045
00046
00047
00048
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00073
00074
00075
00076
00077
00078 printf("status.target_found=%d\n",status.target_found);
00079 printf("status.last_id=%d\n",status.last_id);
00080
00082 status.target_found=false;
00083 int should_move=false;
00084 double dist_to_target=1000;
00085
00086
00087
00088 print_status(&status);
00092 switch(status.state)
00093 {
00095 case INITIALISE:
00096
00097
00099
00101
00102 foveationcontrol_home();
00103
00104
00105 status.direction = DIRECTION_IN_FRONT;
00106 set_high_command_msg_direction_speed(command_msg, status.direction, SPEED_STOP);
00107 set_high_command_msg_lights(command_msg, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_ON);
00108
00109
00111 status.previous_state=INITIALISE;
00112 status.state=SEARCHING;
00114
00115
00116 break;
00118 case SEARCHING:
00119
00120
00122 if (status.previous_state!=SEARCHING)
00123 {
00124 status.tic_searching=ros::Time::now();
00125 }
00126
00128
00129 status.target_found = search_for_new_target(&targets, &status,&search_area);
00130 printf("status.target_found=%d\n",status.target_found);
00131 status.last_id=status.target_id;
00132
00133 play_sound(4, 10);
00134 foveationcontrol_sweeping();
00135
00136
00137 status.direction = command_msg.steering_wheel;
00138 set_high_command_msg_direction_speed(command_msg, status.direction, SPEED_STOP);
00139 set_high_command_msg_lights(command_msg, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_ON);
00140
00142 if(status.target_found && !is_safe_using_lasers())
00143 {
00144 status.previous_state=SEARCHING;
00145 status.state=TRACKING_NOT_SAFE;
00147 play_sound(2, 10);
00148 }
00149 else if(status.target_found)
00150 {
00151 status.previous_state=SEARCHING;
00152 status.state=TRACKING;
00154 play_sound(0, -1);
00155 }
00156 break;
00158 case TRACKING:
00159
00160
00161 if (status.previous_state!=TRACKING)
00162 {
00163 status.tic_tracking=ros::Time::now();
00164 }
00165
00167 foveationcontrol(&status);
00168 status.target_found=get_new_target_information(&targets, &status);
00169
00170
00171 play_sound(1, 15);
00172
00173
00174
00175
00176 status.direction = atan2(status.current_y,status.current_x);
00177 set_high_command_msg_direction_speed(command_msg, status.direction, SPEED_MOVE_SLOW);
00178 set_high_command_msg_lights(command_msg, LIGHT_OFF, LIGHT_ON, LIGHT_OFF, LIGHT_OFF, LIGHT_ON);
00179
00181 if(status.reset_tracking==true)
00182 {
00183 status.previous_state=TRACKING;
00184 status.state=SEARCHING;
00185 status.reset_tracking = false;
00187 play_sound(5, -1);
00188 }
00189 else if(status.target_found==false || status.current_x<3.0)
00190 {
00191 status.previous_state=TRACKING;
00192 status.state=TARGET_LOST;
00194 play_sound(5, -1);
00195
00196 }else if (!is_safe_using_lasers())
00197 {
00198 status.previous_state=TRACKING;
00199 status.state=TRACKING_NOT_SAFE;
00201 if (dist_to_target<6)
00202 play_sound(6, -1);
00203 else
00204 play_sound(2, -1);
00205 }
00206 break;
00208 case TRACKING_NOT_SAFE:
00209
00210
00211 if (status.previous_state!=TRACKING_NOT_SAFE)
00212 {
00213 status.tic_tracking_not_safe=ros::Time::now();
00214 }
00215
00217 status.target_found = get_new_target_information(&targets, &status);
00218 foveationcontrol(&status);
00219
00220 if (status.target_found)
00221 {
00222 dist_to_target = sqrt(pow(status.current_x-2.7,2) + pow(status.current_y,2));
00223 printf("status.target_id=%d\n",status.target_id);
00224 printf("dist_to_target=%f\n",dist_to_target);
00225 if (dist_to_target<4)
00226 play_sound(6, 10);
00227 else
00228 play_sound(2, 10);
00229 }
00230
00231
00232
00233 status.direction = atan2(status.current_y,status.current_x);
00234 set_high_command_msg_direction_speed(command_msg, status.direction, SPEED_STOP);
00235 set_high_command_msg_lights(command_msg, LIGHT_OFF, LIGHT_ON, LIGHT_ON, LIGHT_ON, LIGHT_ON);
00236
00238 if(status.reset_tracking==true)
00239 {
00240 status.previous_state=TRACKING_NOT_SAFE;
00241 status.state=SEARCHING;
00242 status.reset_tracking = false;
00244
00245 }
00246 else if(status.target_found==false || status.current_x<3.0)
00247 {
00248 status.previous_state=TRACKING_NOT_SAFE;
00249 status.state=TARGET_LOST;
00251 play_sound(5, -1);
00252
00253 }
00254 else if (is_safe_using_lasers())
00255 {
00256 int tmp = status.previous_state;
00257 status.previous_state=TRACKING_NOT_SAFE;
00258 status.state=TRACKING;
00260 if (tmp!=SEARCHING)
00261 {
00262 play_sound(3, -1);
00263 }
00264 else
00265 {
00266 }
00267 }
00268
00269 break;
00271 case TARGET_LOST:
00272
00273
00274
00275
00277 foveationcontrol_home();
00278
00279
00280 set_high_command_msg_direction_speed(command_msg, command_msg.steering_wheel, SPEED_STOP);
00281 set_high_command_msg_lights(command_msg, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_ON);
00282
00283
00285 status.previous_state=TARGET_LOST;
00286 status.state=SEARCHING;
00287
00288
00289 break;
00291 default:
00292
00293
00294 ROS_ERROR("WARNING DANGER unknown state in state machine abort demonstration");
00295
00296 set_high_command_msg_direction_speed(command_msg, command_msg.steering_wheel, SPEED_STOP);
00297 set_high_command_msg_lights(command_msg, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_ON);
00298 break;
00299 }
00300
00301
00302
00303
00304
00305
00306 command_msg.header.stamp = ros::Time::now();
00307 command_pub.publish(command_msg);
00308 }
00309
00310
00311 #endif