Ghost Source Code/************************************************************************//* EECE 474 Summer Semester 2002 *//************************************************************************//* *//* Team 1 Ghost Microcontroller Code *//* ================================= *//* *//* This program is for controlling the Ghost <strong>robot</strong>. It is designed for *//* a Microchip PIC16F877 running with an oscillator frequency of 4 MHz *//* It interfaces with the obstacle, Pac-Man contact, and Pac-Man beacon- *//* finding sensors. The Ghost estimates the position of Pac-Man using *//* the beacon-finding sensors and with knowledge of its current *//* position, uses a flood-filling maze-solving algorithm to find the *//* shortest path to get to Pac-Man. *//* *//************************************************************************/#include #include "Ghost.h"#device PIC16F877 *=16//#device adc=10#use delay (clock=4000000)#fuses XT, NOWDT, NOPROTECTint turn_counter = 0;int cell_step_counter = 0;int adjust_counter = 0;direction current_orient = N;short next_move_ready = 1;short turn_enable = 0;short turn_180 = 0;short stop = 1;short adjust_left = 0, adjust_right = 0;short backturn_dir = 0;short initial_start = 0;int calibration_stage = 0;int FRONT_OBSTACLE = 245;int MAX_RIGHT = 0;int MAX_LEFT = 0;int MIN_RIGHT = 0;int MIN_LEFT = 0;int MIDDLE_RIGHT = 0;int MIDDLE_LEFT = 0;short pacpos_unknown = 0;int maze[NUM_CELLS] ={NORTH | WEST,NORTH | SOUTH,NORTH,NORTH | SOUTH,NORTH | SOUTH,NORTH | EAST,WEST | EAST,WEST | NORTH,SOUTH,NORTH | SOUTH,NORTH | EAST,WEST | EAST,WEST | EAST,WEST | EAST,NORTH | WEST | SOUTH,NORTH,0,EAST,SOUTH,SOUTH,NORTH,EAST | SOUTH,WEST | EAST,WEST | EAST,NORTH | WEST,NORTH | SOUTH,0,NORTH | SOUTH,EAST | SOUTH,WEST | EAST,WEST | SOUTH,NORTH | SOUTH,SOUTH,NORTH | SOUTH,NORTH | SOUTH,EAST | SOUTH};// _ _ _ _ _ _ --> increasing x// | _ _ _ |// | | _ _ | |
| | |_ |// |_ _ _| | |// | _ _ _| |// |_ _ _ _ _ _|//// ||// \/// increasing yint current_x_pos = INITIAL_X_POS;int current_y_pos = INITIAL_Y_POS;short wall_exists( int x, int y, direction dir ){int cell_no, cell_walls;cell_no = (y*NUM_X_CELLS) + x;}if ((maze[cell_no] | NORTH) && (dir == N))return 1;else if ((maze[cell_no] | EAST) && (dir == E))return 1;else if ((maze[cell_no] | SOUTH) && (dir == S))return 1;else if ((maze[cell_no] | WEST) && (dir == W))return 1;return 0;void move( direction dir ) // move direction one cell{switch (dir){case N:switch (current_orient){case N:// move forwardgo_forward();break;case E:// turn leftturn_left();break;case S:// turn_backwardsturn_backwards();break;case W:// turn rightturn_right();break;}current_orient = N;break;case E:case W:switch (current_orient){case N:// turn rightturn_right();break;case E:// move forwardgo_forward();break;case S:// turn leftturn_left();break;case W:// turn backwardsturn_backwards();break;}current_orient = E;break;switch (current_orient){case N:// turn leftturn_left();break;case E:// turn backwardsturn_backwards();break;case S:// turn rightturn_right();break;case W:// move forward