Question
I need use a robot to map out a maze using dfs and C language. It will need to be able to retrieve distance and
I need use a robot to map out a maze using dfs and C language. It will need to be able to retrieve distance and map out the maze. This is the code i have so far...
/* Blank Simple Project.c */ #include "simpletools.h" // Include simple tools #include "abdrive.h" // abdrive library #include "ping.h" // Include ping header #include "servo.h"
#include
/* direction 0 left direction 1 right direction 2 forward direction 3 back visited 1 not visisted 0 */
struct node { int distance; int direction; int visited; struct node *next; };
struct node *head = NULL; //struct node *current = NULL;
//display the list void printList() { struct node *ptr = head; printf(" [head] =>"); //start from the beginning while(ptr != NULL) { printf("=> %d ",ptr->distance); //printf(" "); printf(" %d ",ptr->direction); //printf(" "); printf(" %d ",ptr->visited); ptr = ptr->next; } printf(" [null] "); }
//insert link at the first location void insert(int distance, int direction, int visited) { //create a link struct node *link = (struct node*) malloc(sizeof(struct node)); //link->key = key; link->distance = distance; link->direction = direction; link->visited = visited; //point it to old first node link->next = head; //point first to new first node head = link; }
struct node *delete(struct node *head) { struct node *temp =head; struct node *t = NULL; if(head->next==NULL) { free(head); head=NULL; } else { while(temp->next != NULL) { t=temp; temp=temp->next; } free(t->next); t->next=NULL; } return head; }
int main() // Main function { int distLeft0 = 0; int distRight0 = 0; int stein = 0;
while(1) // Repeat indefinitely { int firstDist = 0; int lastDist = 0; int distance; int new; int direction; distance = ping_cm(8); // Get cm distance from Ping))) //firstDist = distance; pause(100); //new = ping_cm(8); //print("%d ", distance); if(distance > 4){ int distanceIn = ping_cm(8); while(distanceIn > 7){ //drive_speed(25,25); drive_goto(45,45); distanceIn = ping_cm(8); //print("%d%c distanceIn ", distanceIn); insert(distanceIn,direction,1); direction = 2; } } //drive_getTicks(&distLeft0, &distRight0); //print("distLeft0 = %d, distRight0 = %d ", distLeft0, distRight0); //print("%c distance = %d%c cm", // Display distance // HOME, distance, CLREOL); if(distance < 4) { print("%c Hello"); drive_speed(0,0); pause(1000); servo_angle(16, 0); // right pause(1000); distance = ping_cm(8); if(distance < 15) { // check right high(26); pause(1000); low(26); servo_angle(16, 1800); // left pause(3000); distance = ping_cm(8); if(distance < 15) { high(27); pause(1000); low(27); //exit(0); head = delete(head); } else { drive_goto(-25,25); // turn right distance = 1; } } else { drive_goto(25,-25); // turn right distance = 0; } servo_angle(16, 900); // middle pause(3000); //drive_goto(-50,50); } /* } servo_angle(16, 900); // middle pause(3000); servo_angle(16, 0); // right pause(3000); servo_angle(16, 1800); // left pause(3000); servo_angle(16, 900); // middle pause(3000); servo_stop(); // Stop servo process */ } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started