Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Simple skeletontest program for in-robot API */
- #include "allcode_api.h"
- #include <stdlib.h>
- #define NORMAL_SPEED 20
- #define SENSOR_STATE 0
- #define DRIVE_STATE 1
- #define WALL_STATE 2
- #define LIGHT_STATE 3
- #define LINE_STATE 4
- #define WALL_CELL 1
- #define PASSAGE_CELL 2
- #define UNKNOWN_CELL 3
- //create a function to turn "90" degrees that checks the sensor for the direction its turning, should see a decrease then increase until it starts going back down again you're aligned
- /*
- r/l 90d code needs a bit of work (could turn into function because 90*2 = 185, maybe a theme :thinkingemoiji:)
- FA_ResetEncoders();
- int l = FA_ReadEncoder(0);
- int r = FA_ReadEncoder(1);
- FA_LCDClear();
- set_motors(NORMAL_SPEED, -NORMAL_SPEED);
- while((FA_ReadEncoder(0) < l+185) && (FA_ReadEncoder(1) < r+185)) {
- //checking code for sensors (a way to prevent locking)
- }
- set_motors(0, 0);
- */
- /*FA_LCDClear();
- FA_LCDPrint("Right:", 15, 5, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(FA_ReadIR(IR_RIGHT), 60, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDPrint("Left:", 15, 5, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(FA_ReadIR(IR_LEFT), 60, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_DelayMillis(200);*/
- typedef struct cell_information {
- int wall_north;
- int wall_south;
- int wall_east;
- int wall_west;
- } cell_info;
- typedef struct cell {
- int state;
- int visited;
- int direction_entered;
- cell_info *landmark;
- } cell;
- cell map[4][4];
- int currentLoc[2];
- void drive();
- void readSensors();
- void followWall();
- void set_speed(int active, int left, int right);
- void idle(int behav);
- void set_motors(int left, int right);
- void detectLight();
- void detectLine();
- void drive_straight(int behav, int left, int right);
- void turnLeftEnc(int behav, int enc);
- void validateCell();
- void setupCells();
- void resetCell(int x, int y);
- void logCell();
- void straightenUp();
- void print_cell_info_debug();
- void moveToCenter();
- void updateLoc();
- void updateDirections();
- void explore();
- void print_cell();
- void setEncSpeed(int param, int enc, int speed);
- int wheelTicks[2];
- //TODO
- int motorTPMS[2];
- int call = 0;
- int encSpeed[2];
- MX_UINT32 encTime;
- int active[5];
- int left_speed[5];
- int right_speed[5];
- int a = 0;
- int d = 0;
- int direction = 0;
- int time = 0;
- int * directions;
- int firstCell = 1;
- int drivecounter = 0;
- int oldspeed[2], newspeed[2];
- void getSensorData();
- int sensors[2];
- MX_UINT32 sensorTime;
- int latch = 0;
- void getSensorData() {
- if(!latch) {
- sensorTime = FA_ClockMS();
- latch = 1;
- }
- if(FA_ClockMS() >= sensorTime+5) {
- sensors[0] = FA_ReadIR(IR_LEFT);
- sensors[1] = FA_ReadIR(IR_RIGHT);
- }
- }
- int main()
- {
- FA_RobotInit();
- FA_CompassInit();
- FA_LCDBacklight(50);
- setupCells();
- currentLoc[0] = 0;
- currentLoc[1] = 0;
- validateCell();
- //print_cell_info_debug();
- oldspeed[0] = 0;
- oldspeed[1] = 0;
- encSpeed[0] = 20;
- encSpeed[1] = 20;
- /*if(!call) {
- FA_SetMotors(20,20);
- FA_ResetEncoders();
- set_speed(WALL_STATE, encSpeed[0], encSpeed[1]);
- wheelTicks[0] = FA_ReadEncoder(0);
- wheelTicks[1] = FA_ReadEncoder(1);
- encTime = FA_ClockMS();
- call = 1;
- }
- if(FA_ClockMS() > encTime+50) {
- FA_LCDClear();
- FA_LCDNumber(wheelTicks[0], 20, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(wheelTicks[1], 40, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(FA_ReadEncoder(0), 20, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(FA_ReadEncoder(1), 40, 15, FONT_NORMAL, LCD_OPAQUE);
- call = 0;
- FA_SetMotors(0,0);
- FA_DelayMillis(1000);
- }*/
- while(1)
- {
- //FA_LCDPrint("Right:", 15, 5, 0, FONT_NORMAL, LCD_OPAQUE);
- //FA_LCDNumber(FA_ReadIR(IR_RIGHT), 60, 0, FONT_NORMAL, LCD_OPAQUE);
- //FA_LCDPrint("Left:", 15, 5, 15, FONT_NORMAL, LCD_OPAQUE);
- //FA_LCDNumber(FA_ReadIR(IR_LEFT), 60, 15, FONT_NORMAL, LCD_OPAQUE);
- //FA_DelayMillis(200);
- //FA_DelayMillis(2000);
- /*if(FA_ReadIR(IR_RIGHT) > 450) {
- straightenUp(1);
- } else if(FA_ReadIR(IR_LEFT) > 450 ) {
- straightenUp(0);
- }*/
- /*if((FA_ReadIR(IR_RIGHT) > 200) && (FA_ReadIR(IR_LEFT) > 200)) {
- if(((FA_ReadIR(IR_RIGHT) >= 340) && (FA_ReadIR(IR_RIGHT) <= 360)) && ((FA_ReadIR(IR_LEFT) >= 340) && (FA_ReadIR(IR_LEFT) <= 360))) {
- drive_straight(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- } else if(FA_ReadIR(IR_LEFT) > 700) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-10); //+15 -10 relies on left
- } else if(FA_ReadIR(IR_RIGHT) > 700) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_RIGHT) > 500) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_LEFT) > 500) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_RIGHT) > 360) {
- set_speed(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_LEFT) > 360) {
- set_speed(WALL_STATE, NORMAL_SPEED+10, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_RIGHT) < 340) {
- set_speed(WALL_STATE, NORMAL_SPEED+10, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_LEFT) < 340) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_RIGHT) < 280) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_LEFT) < 280) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+15);
- } else {
- set_speed(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- }
- } else */
- getSensorData();
- /*if((sensors[1] > 200) && (FA_ReadIR(IR_LEFT) > 200)) {
- if(((sensors[1] >= 340) && (sensors[1] <= 360)) && ((sensors[0] >= 340) && (sensors[0] <= 360))) {
- drive_straight(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- } else if(sensors[0] > 700) {
- setEncSpeed(1,3,5);
- } else if(sensors[1] > 700) {
- setEncSpeed(0,2,5);
- } else if(sensors[1] > 500) {
- setEncSpeed(0,2,5);
- } else if(sensors[0] > 500) {
- setEncSpeed(1,3,5);
- } else if(sensors[1] > 360) {
- setEncSpeed(0,1,5);
- } else if(sensors[0] > 360) {
- setEncSpeed(1,2,5);
- } else if(sensors[1] < 280) { //one of these vals is wrong, left side works fine right doesn't maybe make all calls to turn the left wheel faster speed*1.5 since left wheel is bad
- setEncSpeed(1,3,5);
- } else if(sensors[0] < 280) {
- setEncSpeed(0,2,5);
- } else if(sensors[1] < 340) {
- setEncSpeed(1,3,5);
- } else if(sensors[0] < 340) {
- setEncSpeed(0,2,5);
- }
- } else if(sensors[1] > 200) {
- if(sensors[1] > 700) {
- setEncSpeed(0,2,5);
- } else if(sensors[1] > 500) {
- setEncSpeed(0,2,5);
- } else if(sensors[1] > 360) {
- setEncSpeed(0,1,5);
- } else if((sensors[1] >= 340) && (sensors[1] <= 360)) {
- drive_straight(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- } else if(sensors[1] < 280) {
- setEncSpeed(1,3,5);
- } else if(sensors[1] < 340) {
- setEncSpeed(1,3,5);
- } else {
- //set_speed(WALL_STATE, NORMAL_SPEED+10, NORMAL_SPEED);
- }
- } else if(sensors[0] > 200) {
- if(sensors[0] > 700) {
- setEncSpeed(1,3,5);
- } else if(sensors[0] > 500) {
- setEncSpeed(1,3,5);
- } else if(sensors[0] > 360) {
- setEncSpeed(1,1,10);
- } else if((sensors[0] >= 340) && (sensors[0] <= 360)) { //don't change anything below this, right side good
- drive_straight(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- } else if(sensors[0] < 280) {
- setEncSpeed(0,2,5);
- } else if(sensors[0] < 340) {
- setEncSpeed(0,2,5);
- }
- } else {
- idle(WALL_STATE);
- }*/
- //} else {
- // set_speed(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- //set_speed(WALL_STATE, 0,0);
- //}
- print_cell();
- detectLine();
- detectLight();
- readSensors();
- followWall();
- drive();
- if(active[LIGHT_STATE]) {
- newspeed[0] = left_speed[LIGHT_STATE];
- newspeed[1] = right_speed[LIGHT_STATE];
- if((newspeed[0] != oldspeed[0]) || (newspeed[1] != oldspeed[1])) {
- FA_SetMotors(newspeed[0], newspeed[1]);
- oldspeed[0] = newspeed[0];
- oldspeed[1] = newspeed[1];
- FA_ResetEncoders();
- }
- } else if(active[LINE_STATE]) {
- validateCell();
- //print_cell_info_debug();
- } else if(active[SENSOR_STATE]) {
- if(d) {
- FA_Right(a);
- } else {
- FA_Left(a);
- }
- /*if(FA_ReadIR(IR_LEFT) > 400) {
- straightenUp(0);
- } else if(FA_ReadIR(IR_RIGHT) > 400){
- straightenUp(1);
- }*/
- oldspeed[0] = 0;
- oldspeed[1] = 0;
- encSpeed[0] = NORMAL_SPEED;
- encSpeed[1] = NORMAL_SPEED;
- call = 0;
- FA_ResetEncoders();
- } else if(active[WALL_STATE]) {
- newspeed[0] = left_speed[WALL_STATE];
- newspeed[1] = right_speed[WALL_STATE];
- if((newspeed[0] != oldspeed[0]) || (newspeed[1] != oldspeed[1])) {
- FA_SetMotors(newspeed[0], newspeed[1]);
- oldspeed[0] = newspeed[0];
- oldspeed[1] = newspeed[1];
- FA_ResetEncoders();
- }
- } else if(active[DRIVE_STATE]) {
- newspeed[0] = left_speed[DRIVE_STATE];
- newspeed[1] = right_speed[DRIVE_STATE];
- if((newspeed[0] != oldspeed[0]) || (newspeed[1] != oldspeed[1])) {
- FA_SetMotors(newspeed[0], newspeed[1]);
- oldspeed[0] = newspeed[0];
- oldspeed[1] = newspeed[1];
- FA_ResetEncoders();
- }
- }
- }
- return 0;
- }
- void set_motors(int left, int right) {
- FA_SetMotors(left, right);
- }
- void drive() {
- drive_straight(DRIVE_STATE, NORMAL_SPEED, NORMAL_SPEED);
- }
- void readSensors() {
- /*
- obstacle avoidence reactive code, hierarchy = front > right > left
- */
- if(FA_ReadIR(IR_FRONT) > 650) {
- if((FA_ReadIR(IR_RIGHT) > 200) && //380
- (FA_ReadIR(IR_LEFT) > 200)) { //550
- a = 90;
- d = 0;
- active[SENSOR_STATE] = 1;
- direction -= 1;
- if(direction == 4) direction = 0;
- //set_speed(SENSOR_STATE, -NORMAL_SPEED, -NORMAL_SPEED); //if front, left and right detected, replace with turn 180
- } else if(FA_ReadIR(IR_RIGHT) > 200) {
- a = 90;
- d = 0;
- active[SENSOR_STATE] = 1;
- direction -= 1;
- if(direction == -1) direction = 3;
- } else if(FA_ReadIR(IR_LEFT) > 200) {
- a = 90;
- d = 1;
- active[SENSOR_STATE] = 1;
- direction += 1;
- if(direction == 4) direction = 0;
- } else if(FA_ReadIR(IR_FRONT_RIGHT) > 1800) { //if front left detected
- //set_speed(SENSOR_STATE, -NORMAL_SPEED, NORMAL_SPEED); //replace with 90 degree left
- a = 90;
- d = 0;
- active[SENSOR_STATE] = 1;
- direction -= 1;
- if(direction == 4) direction = 0;
- } else if(FA_ReadIR(IR_FRONT_LEFT) > 1800) { //if front right detected
- //set_speed(SENSOR_STATE, NORMAL_SPEED, -NORMAL_SPEED); //replace with 90 degree right
- a = 90;
- d = 1;
- active[SENSOR_STATE] = 1;
- direction += 1;
- if(direction == -1) direction = 3;
- } else {
- //set_speed(SENSOR_STATE, NORMAL_SPEED, -NORMAL_SPEED); //opt for taking left > right, replace with 90 degree left
- a = 90;
- d = 0;
- active[SENSOR_STATE] = 1;
- direction -= 1;
- if(direction == 4) direction = 0;
- }
- /*} else if(FA_ReadIR(IR_FRONT_RIGHT) > 1800) {
- //set_speed(SENSOR_STATE, -NORMAL_SPEED, NORMAL_SPEED); //sharp turn left, could be made better
- a = 90;
- d = 0;
- active[SENSOR_STATE] = 1;
- direction += 1;
- if(direction == 4) direction = 0;
- } else if(FA_ReadIR(IR_FRONT_LEFT) > 1800) {
- //set_speed(SENSOR_STATE, NORMAL_SPEED, -NORMAL_SPEED); //sharp turn right, could be made better
- a = 90;
- d = 1;
- active[SENSOR_STATE] = 1;
- direction -= 1;
- if(direction == -1) direction = 3;*/
- } else {
- idle(SENSOR_STATE);
- }
- }
- void detectLight() {
- //if(FA_ReadLight() < 150) { //if it's a shady boi stop before you get robbed
- // set_speed(LIGHT_STATE, 0, 0);
- //} else {
- // idle(LIGHT_STATE);
- //}
- }
- void detectLine() {
- if((FA_ReadLine(1) < 50) && (FA_ReadLine(0) < 50)) {
- active[LINE_STATE] = 1;
- } else {
- idle(LINE_STATE);
- }
- }
- void turnLeftEnc(int behav, int enc) { //make right encoder turn faster than left
- if(FA_ClockMS() > time+5) {
- if(FA_ReadEncoder(1) > FA_ReadEncoder(0)+enc) {
- drive_straight(behav, NORMAL_SPEED, NORMAL_SPEED);
- } else if(FA_ReadEncoder(0)+enc > FA_ReadEncoder(1)){
- set_speed(behav, NORMAL_SPEED, NORMAL_SPEED+10);
- }
- time = FA_ClockMS();
- }
- }
- void followWall() {
- if((FA_ReadIR(IR_RIGHT) > 200) && (FA_ReadIR(IR_LEFT) > 200)) {
- if(((FA_ReadIR(IR_RIGHT) >= 340) && (FA_ReadIR(IR_RIGHT) <= 360)) && ((FA_ReadIR(IR_LEFT) >= 340) && (FA_ReadIR(IR_LEFT) <= 360))) {
- drive_straight(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- } else if(FA_ReadIR(IR_LEFT) > 700) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-15); //+15 -10 relies on left
- } else if(FA_ReadIR(IR_RIGHT) > 700) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_RIGHT) > 500) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_LEFT) > 500) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_RIGHT) > 360) {
- set_speed(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_LEFT) > 360) {
- set_speed(WALL_STATE, NORMAL_SPEED+10, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_RIGHT) < 340) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_LEFT) < 340) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_RIGHT) < 280) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-15);
- } else if(FA_ReadIR(IR_LEFT) < 280) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+15);
- } else {
- /*else favour leaning towards the right wall until other statements
- are triggered*/
- set_speed(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- }
- } else if(FA_ReadIR(IR_RIGHT) > 200) {
- if(FA_ReadIR(IR_RIGHT) > 700) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_RIGHT) > 500) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_RIGHT) > 360) {
- set_speed(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED+10);
- } else if((FA_ReadIR(IR_RIGHT) >= 340) && (FA_ReadIR(IR_RIGHT) <= 360)) {
- drive_straight(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- } else if(FA_ReadIR(IR_RIGHT) < 340) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_RIGHT) < 280) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-15);
- } else {
- set_speed(WALL_STATE, NORMAL_SPEED+10, NORMAL_SPEED);
- }
- } else if(FA_ReadIR(IR_LEFT) > 200) {
- if(FA_ReadIR(IR_LEFT) > 700) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-15); //maybe replace with a quik turn my dood
- } else if(FA_ReadIR(IR_LEFT) > 500) {
- set_speed(WALL_STATE, NORMAL_SPEED+15, NORMAL_SPEED-10);
- } else if(FA_ReadIR(IR_LEFT) > 360) {
- set_speed(WALL_STATE, NORMAL_SPEED+10, NORMAL_SPEED-10);
- } else if((FA_ReadIR(IR_LEFT) >= 340) && (FA_ReadIR(IR_LEFT) <= 360)) { //don't change anything below this, right side good
- drive_straight(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED);
- } else if(FA_ReadIR(IR_LEFT) < 340) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+10);
- } else if(FA_ReadIR(IR_LEFT) < 280) {
- set_speed(WALL_STATE, NORMAL_SPEED-10, NORMAL_SPEED+15);
- } else {
- set_speed(WALL_STATE, NORMAL_SPEED, NORMAL_SPEED+10);
- }
- } else {
- idle(WALL_STATE);
- }
- }
- //todo
- void setEncSpeed(int param, int inc, int speed) {
- if(!call) {
- set_speed(WALL_STATE, encSpeed[0], encSpeed[1]);
- wheelTicks[0] = FA_ReadEncoder(0);
- wheelTicks[1] = FA_ReadEncoder(1);
- encTime = FA_ClockMS();
- call = 1;
- }
- if(FA_ClockMS() > encTime+30) {
- motorTPMS[0] = FA_ReadEncoder(0) - wheelTicks[0];
- motorTPMS[1] = FA_ReadEncoder(1) - wheelTicks[1];
- switch(param) {
- case 0:
- encSpeed[0] = NORMAL_SPEED;
- if (motorTPMS[1] > (motorTPMS[0] + inc)) {
- //going faster
- set_speed(WALL_STATE, encSpeed[0], encSpeed[1] -= speed);
- } else if (motorTPMS[1] == (motorTPMS[0]+inc)) {
- //turning at desired angle
- } else {
- set_speed(WALL_STATE, encSpeed[0], encSpeed[1] += speed);
- }
- break;
- case 1:
- encSpeed[1] = NORMAL_SPEED;
- if (motorTPMS[0] > (motorTPMS[1] + inc)) {
- set_speed(WALL_STATE, encSpeed[0]-=speed, encSpeed[1]); //make this faster or call with left faster than right, test it out but following is solid on one side
- } else if (motorTPMS[1] == (motorTPMS[0]+inc)) {
- } else {
- set_speed(WALL_STATE, encSpeed[0]+=speed, encSpeed[1]);
- }
- break;
- default:
- break;
- }
- call = 0;
- }
- }
- //create var to make sure it never sets the same speed twice, keep old speed and new speed etc
- void drive_straight(int behav, int left, int right) {
- if(drivecounter == 0) {
- if(FA_ReadEncoder(1) > FA_ReadEncoder(0)) {
- set_speed(behav, NORMAL_SPEED+10, NORMAL_SPEED);
- } else if(FA_ReadEncoder(0) > FA_ReadEncoder(1)){
- set_speed(behav, NORMAL_SPEED, NORMAL_SPEED+10);
- } else {
- set_speed(behav, NORMAL_SPEED, NORMAL_SPEED);
- }
- }
- drivecounter+=1;
- if(drivecounter == 10) {
- drivecounter = 0;
- }
- }
- void set_speed(int behav, int left, int right) {
- left_speed[behav] = left;
- right_speed[behav] = right;
- active[behav] = 1;
- }
- void idle(int behav) {
- active[behav] = 0;
- }
- void setupCells() {
- int i, j;
- for (i = 0; i < 4; i++) {
- for (j = 0; j < 4; j++) {
- map[i][j].direction_entered = (int) NULL;
- map[i][j].visited = 0;
- map[i][j].state = 3;
- cell_info *temp = malloc(sizeof(cell_info));
- temp->wall_east = 0;
- temp->wall_west = 0;
- temp->wall_north = 0;
- temp->wall_south = 0;
- map[i][j].landmark = temp;
- }
- }
- }
- void resetCell(int x, int y) {
- map[x][y].direction_entered = (int) NULL;
- map[x][y].visited = 0;
- map[x][y].state = 3;
- free(map[x][y].landmark);
- cell_info *temp = malloc(sizeof(cell_info));
- temp->wall_east = 0;
- temp->wall_west = 0;
- temp->wall_north = 0;
- temp->wall_south = 0;
- map[x][y].landmark = temp;
- }
- void updateLoc() {
- int i;
- //this will be called after you've moved since the direction you're facing will be the direction you entered the cell at
- switch(direction) {
- case 0: //N
- if (currentLoc[0] == 0) { //shift everything down 1
- for (i = 0; i < 4; i++) {
- cell temp, temp2;
- temp = map[1][i];
- map[1][i] = map[0][i];
- resetCell(0, i); //wipe cell in future
- temp2 = map[2][i];
- map[2][i] = temp;
- map[3][i] = temp2;
- }
- currentLoc[0] += 1;
- }
- currentLoc[0] -= 1;
- break;
- case 1: //E
- if (currentLoc[1] == 3) { //shift everything left 1
- for (i = 0; i < 4; i++) {
- cell temp, temp2;
- temp = map[i][2];
- map[i][2] = map[i][3];
- resetCell(i, 3); //wipe cell in future
- temp2 = map[i][1];
- map[i][1] = temp;
- map[i][0] = temp2;
- }
- currentLoc[1] -= 1;
- }
- currentLoc[1] += 1;
- break;
- case 2: //S
- if (currentLoc[0] == 3) { //shift everything up 1
- for (i = 0; i < 4; i++) {
- cell temp, temp2;
- temp = map[2][i];
- map[2][i] = map[3][i];
- resetCell(3, i); //wipe cell in future
- temp2 = map[1][i];
- map[1][i] = temp;
- map[0][i] = temp2;
- }
- currentLoc[0] -= 1;
- }
- currentLoc[0] += 1;
- break;
- case 3: //W
- if (currentLoc[1] == 0) { //shift everything right 1
- for (i = 0; i < 4; i++) {
- cell temp, temp2;
- temp = map[i][1];
- map[i][1] = map[i][0];
- resetCell(i, 0); //wipe cell in future
- temp2 = map[i][2];
- map[i][2] = temp;
- map[i][3] = temp2;
- }
- currentLoc[1] += 1;
- }
- currentLoc[1] -= 1;
- break;
- default: //doesn't matter will never get here
- break;
- }
- if(map[currentLoc[0]][currentLoc[1]].visited) map[currentLoc[0]][currentLoc[1]].visited+=1;
- }
- void logCell() {
- int numwalls = 0;
- cell tempcell;//think it's an issue here somewhere?
- tempcell.direction_entered = direction; //take current direction 0 = N, 1 = E, 2 = S, 3 = W
- tempcell.visited = 1; // set to visited since you've been there
- tempcell.landmark = malloc(sizeof(cell_info)); //malloc may cause issues on x16 compiler
- if(FA_ReadIR(directions[1]) > 200) {
- tempcell.landmark->wall_east = 1;
- numwalls+=1;
- } else {
- tempcell.landmark->wall_east = 0;
- }
- if(FA_ReadIR(directions[3]) > 200) { //not sure if west/east
- tempcell.landmark->wall_west = 1;
- numwalls+=1;
- } else {
- tempcell.landmark->wall_west = 0;
- }
- if(FA_ReadIR(directions[0]) > 200) {
- tempcell.landmark->wall_north = 1;
- numwalls+=1;
- } else {
- tempcell.landmark->wall_north = 0;
- }
- if(FA_ReadIR(directions[2]) > 200) {
- tempcell.landmark->wall_south = 1;
- numwalls+=1;
- } else {
- tempcell.landmark->wall_south = 0;
- }
- if(numwalls >= 2) {
- tempcell.state = 2;
- } else {
- tempcell.state = 1;
- }
- map[currentLoc[0]][currentLoc[1]] = tempcell;
- if(firstCell) firstCell = 0;
- }
- void moveToCenter() {
- while((FA_ReadEncoder(0) < 290) && (FA_ReadEncoder(1) < 290)) {
- followWall();
- drive();
- if(active[WALL_STATE]) {
- newspeed[0] = left_speed[WALL_STATE];
- newspeed[1] = right_speed[WALL_STATE];
- if((newspeed[0] != oldspeed[0]) || (newspeed[1] != oldspeed[1])) {
- FA_SetMotors(newspeed[0], newspeed[1]);
- oldspeed[0] = newspeed[0];
- oldspeed[1] = newspeed[1];
- }
- } else if(active[DRIVE_STATE]) {
- newspeed[0] = left_speed[DRIVE_STATE];
- newspeed[1] = right_speed[DRIVE_STATE];
- if((newspeed[0] != oldspeed[0]) || (newspeed[1] != oldspeed[1])) {
- FA_SetMotors(newspeed[0], newspeed[1]);
- oldspeed[0] = newspeed[0];
- oldspeed[1] = newspeed[1];
- }
- }
- }
- }
- void updateDirections() {
- directions = malloc(4*sizeof(int));
- switch(direction) {
- case 0: //n
- directions[0] = IR_FRONT;
- directions[1] = IR_RIGHT;
- directions[2] = IR_REAR;
- directions[3] = IR_LEFT;
- break;
- case 1: //e
- directions[0] = IR_LEFT;
- directions[1] = IR_FRONT;
- directions[2] = IR_RIGHT;
- directions[3] = IR_REAR;
- break;
- case 2: //s
- directions[0] = IR_REAR;
- directions[1] = IR_LEFT;
- directions[2] = IR_FRONT;
- directions[3] = IR_RIGHT;
- break;
- case 3: //w
- directions[0] = IR_RIGHT;
- directions[1] = IR_REAR;
- directions[2] = IR_LEFT;
- directions[3] = IR_FRONT;
- break;
- }
- }
- void explore() {
- int lowest[2];
- lowest[0] = 100; //big starting number
- lowest[1] = 4;
- if(!map[currentLoc[0]][currentLoc[1]].landmark->wall_north) {
- if(map[currentLoc[0]-1][currentLoc[1]].visited < lowest[0]) { //north
- lowest[0] = map[currentLoc[0]-1][currentLoc[1]].visited;
- lowest[1] = 0; //north
- }
- }
- if(!map[currentLoc[0]][currentLoc[1]].landmark->wall_east) { //will have a problem with things out of bounds maybe?
- if(map[currentLoc[0]][currentLoc[1]+1].visited < lowest[0]) { //east
- lowest[0] = map[currentLoc[0]][currentLoc[1]+1].visited;
- lowest[1] = 1; //east
- }
- }
- if(!map[currentLoc[0]][currentLoc[1]].landmark->wall_south) {
- if(map[currentLoc[0]+1][currentLoc[1]].visited < lowest[0]) { //south
- lowest[0] = map[currentLoc[0]+1][currentLoc[1]].visited;
- lowest[1] = 2; //south
- }
- }
- if(!map[currentLoc[0]][currentLoc[1]].landmark->wall_west) {
- if(map[currentLoc[0]+1][currentLoc[1]-1].visited < lowest[0]) { //west
- lowest[0] = map[currentLoc[0]][currentLoc[1]-1].visited;
- lowest[1] = 3; //west
- }
- }
- /*FA_LCDClear();
- FA_LCDPrint("N:", 10, 10, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_north, 30, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDPrint("E:", 50, 50, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_east, 70, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDPrint("S:", 10, 10, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_south, 30, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDPrint("W:", 50, 50, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_west, 70, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_DelayMillis(5000);*/
- if(direction > lowest[1]) {
- while(direction != lowest[1]) {
- FA_Left(90);
- direction-=1;
- }
- } else if(direction < lowest[1]) {
- while(direction != lowest[1]) {
- FA_Right(90);
- direction+=1;
- }
- }
- FA_PlayNote(400,100);
- }
- void validateCell() {
- /*
- go to center of cell
- start state gathering to see what you want to do
- anaylze > decide where to go > straighten up final
- */
- //Getting to center and aligning with north code
- FA_ResetEncoders();
- if(!firstCell) {
- moveToCenter();
- updateLoc();
- }
- //checking code to see if you're in the center, check all walls and align accordingly
- //instead of turning change which sensors you're reading from so make it so you don't need to move around
- if(!map[currentLoc[0]][currentLoc[1]].visited) {
- updateDirections();
- logCell();
- }
- /*
- if(FA_ReadIR(IR_LEFT) > 400) {
- straightenUp(0);
- } else if(FA_ReadIR(IR_RIGHT) > 400){
- straightenUp(1);
- }
- */
- if(map[currentLoc[0]][currentLoc[1]].visited) {
- //use a currentlowest var xd
- explore();
- /*if(FA_ReadIR(IR_LEFT) > 400) {
- straightenUp(0);
- } else if(FA_ReadIR(IR_RIGHT) > 400){
- straightenUp(1);
- }*/
- }
- //Getting sensor data and creating/adding cell code
- /*if(firstCell) {
- map[currentLoc[0]][currentLoc[1]] = tempcell;
- firstCell = 0;
- } else {
- addCell(tempcell);
- }
- if(direction > olddir) {
- while(direction != olddir) {
- FA_Left(90);
- direction-=1;
- }
- } else if(direction < olddir) {
- while(direction != olddir) {
- FA_Right(90);
- direction+=1;
- }
- }
- FA_DelayMillis(1000);
- oldspeed[0] = 0;
- oldspeed[1] = 0;*/
- }
- void print_cell_info_debug() {
- FA_SetMotors(0,0);
- int opt = 0;
- while((FA_ReadSwitch(0) != 1) && (FA_ReadSwitch(1) != 1)) {
- FA_LCDClear();
- switch(opt) {
- case 0:
- FA_LCDPrint("N:", 15, 5, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_north, 20, 0, FONT_NORMAL, LCD_OPAQUE);
- break;
- case 1:
- FA_LCDPrint("E:", 15, 5, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_east, 20, 0, FONT_NORMAL, LCD_OPAQUE);
- break;
- case 2:
- FA_LCDPrint("S:", 15, 5, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_south, 20, 0, FONT_NORMAL, LCD_OPAQUE);
- break;
- case 3:
- FA_LCDPrint("W:", 15, 5, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].landmark->wall_west, 20, 0, FONT_NORMAL, LCD_OPAQUE);
- break;
- default:
- break;
- }
- FA_LCDPrint("Direction:", 15, 5, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].direction_entered, 70, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_DelayMillis(500);
- opt+=1;
- if(opt == 4) opt = 0;
- }
- FA_LCDClear();
- }
- /*
- go into reactive state machine when in line function to vote and decide on what to do (i.e turn for map follow, don't repeat scan squares etc)
- */
- void straightenUp(int walls) {
- int irval;
- long counter = 0;
- int flag = 0;
- int first = 1;
- switch(walls) {
- case 0: //left
- irval = FA_ReadIR(IR_LEFT);
- FA_SetMotors(NORMAL_SPEED, -NORMAL_SPEED);
- while(1) {
- if(counter == 100000) {
- if(first) {
- if(irval > (FA_ReadIR(IR_LEFT)+15)) {
- flag = 1;
- break;
- }
- }
- if(irval > (FA_ReadIR(IR_LEFT)+15)) break; //10 or 15 idk
- irval = FA_ReadIR(IR_LEFT);
- counter = 0;
- first = 0;
- }
- counter += 1;
- }
- if(flag) {
- counter = 0;
- FA_PlayNote(400,200);
- FA_SetMotors(-NORMAL_SPEED, NORMAL_SPEED);
- while(1) {
- if(counter == 100000) {
- if(irval < (FA_ReadIR(IR_LEFT)+15)) break; //10 or 15 idk
- irval = FA_ReadIR(IR_LEFT);
- counter = 0;
- }
- counter += 1;
- }
- }
- break;
- case 1: //right
- irval = FA_ReadIR(IR_RIGHT);
- FA_SetMotors(-NORMAL_SPEED, NORMAL_SPEED);
- while(1) {
- if(counter == 100000) {
- if(first) {
- if(irval > (FA_ReadIR(IR_RIGHT)+5)) {
- flag = 1;
- break;
- }
- }
- if(irval > (FA_ReadIR(IR_RIGHT)+5)) break; //10 or 15 idk
- irval = FA_ReadIR(IR_RIGHT);
- counter = 0;
- }
- counter += 1;
- first = 0;
- }
- if(flag) {
- counter = 0;
- FA_SetMotors(-NORMAL_SPEED, NORMAL_SPEED);
- while(1) {
- if(counter == 100000) {
- if(irval < (FA_ReadIR(IR_RIGHT)+5)) break; //10 or 15 idk
- irval = FA_ReadIR(IR_RIGHT);
- counter = 0;
- }
- counter += 1;
- }
- }
- break;
- }
- }
- void print_cell() {
- FA_LCDClear();
- FA_LCDPrint("Y:", 60, 60, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(currentLoc[0], 70, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDPrint("X:", 80, 80, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(currentLoc[1], 90, 0, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDPrint("Vis:", 80, 80, 15, FONT_NORMAL, LCD_OPAQUE);
- FA_LCDNumber(map[currentLoc[0]][currentLoc[1]].visited, 100, 15, FONT_NORMAL, LCD_OPAQUE);
- if(map[currentLoc[0]][currentLoc[1]].landmark->wall_north) FA_LCDLine(0,5,5,5);
- if(map[currentLoc[0]][currentLoc[1]].landmark->wall_east) FA_LCDLine(5,5,5,10);
- if(map[currentLoc[0]][currentLoc[1]].landmark->wall_south) FA_LCDLine(0,10,5,10);
- if(map[currentLoc[0]][currentLoc[1]].landmark->wall_west) FA_LCDLine(0,5,0,10);
- /*int x = 5;
- int y = 5;
- FA_LCDLine(x-5,y,x,y);
- FA_LCDLine(x,y,x,y+5);
- FA_LCDLine(x-5,y+5,x,y+5);
- FA_LCDLine(x-5,y,x-5,y+5);
- FA_LCDLine(x-5+10,y,x+10,y);
- FA_LCDLine(x+10,y,x+10,y+5);
- FA_LCDLine(x-5+10,y+5,x+10,y+5);
- FA_LCDLine(x-5+10,y,x-5+10,y+5);
- FA_LCDLine(x-5+20,y,x+20,y);
- FA_LCDLine(x+20,y,x+20,y+5);
- FA_LCDLine(x-5+20,y+5,x+20,y+5);
- FA_LCDLine(x-5+20,y,x-5+20,y+5);
- FA_LCDLine(x-5+30,y,x+30,y);
- FA_LCDLine(x+30,y,x+30,y+5);
- FA_LCDLine(x-5+30,y+5,x+30,y+5);
- FA_LCDLine(x-5+30,y,x-5+30,y+5);*/
- /*int i, j;
- int y = 5;
- for(i = 0; i < 4; i++) {
- int x = 5;
- for(j = 0; j < 4; i++) {
- if(map[i][j].landmark->wall_north) FA_LCDLine(x-5,y,x,y);
- if(map[i][j].landmark->wall_east) FA_LCDLine(x,y,x,y+5);
- if(map[i][j].landmark->wall_south) FA_LCDLine(x-5,y+5,x,y+5);
- if(map[i][j].landmark->wall_west) FA_LCDLine(x-5,y,x-5,y+5);
- x = x + 10;
- }
- y = y + 10;
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement