go away spammer

Author Topic: wavefront.c admin  (Read 1749 times)

0 Members and 1 Guest are viewing this topic.

Offline iamaldrinTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
wavefront.c admin
« on: August 11, 2011, 09:49:11 PM »
Admin, how can I use your codes from the wavefront tutorial and apply it to my robot? Which part do I have to modify? I need to send the data through a USB output going to a microcontroller using C++.

Offline Gertlex

  • Supreme Robot
  • *****
  • Posts: 763
  • Helpful? 24
  • Nuclear Engineer ยท Roboticist
    • Index of Oddities
Re: wavefront.c admin
« Reply #1 on: August 11, 2011, 09:56:12 PM »
You really need to give more information.  For example what microcontroller, and what you're doing with the bot.

Regards,
-Gert
I

Offline iamaldrinTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: wavefront.c admin
« Reply #2 on: August 11, 2011, 10:26:16 PM »
Code: [Select]
//WAVEFRONT ALGORITHM

#include <stdlib.h>
#include <stdio.h>

//in main, declare map matrix //6x6 map
int nothing=0;
int wall=255;
int goal=1;
int robot=254;

//declare starting robot/goal locations
int robot_x=5;
int robot_y=3;
int goal_x=0;
int goal_y=3;

//map locations
int x=0;
int y=0;

//temp variables
int temp_A=0;
int temp_B=0;
int counter=0;
int steps=0;//to determine how processor intensive the algorithm was

//when searching for a node with a lower value
int minimum_node=250;
int min_node_location=250;
int new_state=1;
int old_state=1;
int trans=50;
int reset_min=250;//anything above this number is a special item, ie a wall or robot

//X is vertical, Y is horizontal
int map[6][6]= {{0,0,0,0,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0},
{255,255,255,255,0,0},
{0,0,0,0,0,0},
{0,0,0,0,0,0}};

FILE *out ;
   
//declare functions here, first
int propagate_wavefront(int robot_x, int robot_y);
void unpropagate(int robot_x, int robot_y);
int min_surrounding_node_value(int x, int y);
void print_map(void);

int main(void)
{
    out = fopen("results.txt","w");
    //fprintf(out,"Starting Wavefront\n");
printf("Starting Wavefront\n\n");

//////////////wavefront code//////////////
        while(map[robot_x][robot_y]!=goal)
                {
        //find new location to go to
        new_state=propagate_wavefront(robot_x,robot_y);
       
        //update new location of robot
        if (new_state==1)
        {
        robot_x--;
        //printf("x=%d y=%d\n\n",robot_x,robot_y);
        }
        if (new_state==2)
        {
        robot_y++;
        //printf("x=%d y=%d\n\n",robot_x,robot_y);
        }
        if (new_state==3)
        {
        robot_x++;
        //printf("x=%d y=%d\n\n",robot_x,robot_y);
        }
        if (new_state==4)
        {
        robot_y--;
        //printf("x=%d y=%d\n\n",robot_x,robot_y);
        }
       
        /*
        //if not pointed in the right direction, rotate
        if (abs(old_state - new_state) == 2)//rotate 180 degrees
        rotate_CCW(200,200);
        if ((old_state - new_state) == 1 || (signed int)(old_state - new_state) == -3)//rotate 90 degrees CW
        rotate_CW(100,200);
        if ((signed int)(old_state - new_state) == -1 || (old_state - new_state) == 3)//rotate 90 degrees CCW
        rotate_CCW(100,200);
       
        //go to new location
        straight(30,100);*/
       
        //make new state the old state
        old_state=new_state;
        trans--;
                }
//////////////////////////////////////////

printf("steps: %d\n", steps);
//fclose(out);
system("PAUSE");
return 0;
}


int propagate_wavefront(int robot_x, int robot_y)
{
//clear old wavefront
unpropagate(robot_x, robot_y);

//start location to begin scan at goal location
map[goal_x][goal_y]=goal;//goal at 3,3 for error

printf("Adding Goal:\n");
print_map();

    counter=0;
    while(counter<50)//allows for recycling until robot is found
        {
        x=0;
        y=0;
    while(x<6 && y<6)//while the map hasnt been fully scanned
    {
    //if this location is a wall or the goal, just ignore it
    if (map[x][y] != wall && map[x][y] != goal)
    {
    //a full trail to the robot has been located, finished!
    if (min_surrounding_node_value(x, y) < reset_min && map[x][y]==robot)
    {
                printf("Finished Wavefront:\n");
                print_map();
    //finshed! tell robot to start moving down path
    return min_node_location;
    }
    //record a value in to this node
    else if (minimum_node!=reset_min)//if this isnt here, 'nothing' will go in the location
    map[x][y]=minimum_node+1;
    }
   
    //go to next node and/or row
    y++;
    if (y==6 && x!=6)
    {
    x++;
    y=0;
    }
    }
    printf("Sweep #: %d\n",counter+1);
        print_map();
    counter++;
        }
    return 0;
}

void unpropagate(int robot_x, int robot_y)//clears old path to determine new path
{
printf("Old Map:\n");
print_map();

    //printf("Starting Unpropagate\n");

//stay within boundary
for (x=0; x<6; x++)
    for (y=0; y<6; y++)
    if (map[x][y] != wall && map[x][y] != goal) //if this location is a wall or goal, just ignore it
       map[x][y] = nothing;//clear that space

//old robot location was deleted, store new robot location in map
map[robot_x][robot_y]=robot;

printf("Unpropagation Complete:\n");
//fprintf(out, "Unpropagation Complete:\n");
print_map();
}

//this function looks at a node and returns the lowest value around that node
int min_surrounding_node_value(int x, int y)
{
minimum_node=reset_min;//reset minimum

//down
if(x < 5)//not out of boundary
if  (map[x+1][y] < minimum_node && map[x+1][y] != nothing)//find the lowest number node, and exclude empty nodes (0's)
    {
minimum_node = map[x+1][y];
min_node_location=3;
            }

//up
if(x > 0)
if  (map[x-1][y] < minimum_node && map[x-1][y] != nothing)
    {
minimum_node = map[x-1][y];
min_node_location=1;
            }

//right
if(y < 5)
if  (map[x][y+1] < minimum_node && map[x][y+1] != nothing)
    {
minimum_node = map[x][y+1];
min_node_location=2;
            }
           
//left
if(y > 0)
if  (map[x][y-1] < minimum_node && map[x][y-1] != nothing)
    {
minimum_node = map[x][y-1];
min_node_location=4;
            }
   
return minimum_node;
}

void print_map(void)
{
for (temp_B=0;temp_B<6;temp_B++)
{
for (temp_A=0;temp_A<6;temp_A++)
{
            if (map[temp_B][temp_A]==wall)
               printf("W ");
            else if (map[temp_B][temp_A]==robot)
               printf("R ");
            else if (map[temp_B][temp_A]==goal)
               printf("G ");
else
               printf("%d ",map[temp_B][temp_A]);

        //fprintf(out, "%d",map[temp_A][temp_B]);
}
printf("\n");//then go to next line Y
//fprintf(out, "\n");
}
printf("\n");
steps++;
}

This is the code. Can I use this to control a microcontroller (any type for now, I forgot which we're going to use.) to move a robot? And how can I send it to the uC through a USB output? Thanks!

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: wavefront.c admin
« Reply #3 on: August 15, 2011, 12:21:20 PM »
Hi iamaldrin

You will need to select a microcontroller first :P

The code was provided so as to let you reverse-engineer it for your own robot. So basically you can just copy/paste it, but you still need to read through and try to understand it.

I recommend buying a microcontroller with built-in USB, if that's what you need. The Axon series is just one example.

Offline iamaldrinTopic starter

  • Jr. Member
  • **
  • Posts: 19
  • Helpful? 0
Re: wavefront.c admin
« Reply #4 on: August 15, 2011, 07:13:58 PM »
I would like to use axon as well, problem is, it's not available here in Philippines. Here's is the uC that we will use:  http://a8.sphotos.ak.fbcdn.net/hphotos-ak-ash4/262968_237345376304141_100000860416105_697367_3518665_n.jpg

I'll be adapting the codes to C#, and I need help in understanding other parts of the codes.
« Last Edit: August 15, 2011, 07:15:50 PM by iamaldrin »

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: wavefront.c admin
« Reply #5 on: August 15, 2011, 08:26:08 PM »
You sent me a facebook link of which I don't have permissions to see ;)


btw, I ship to the Philippines for like ~$27. Or I can do UPS for ~$80, if you don't trust your local post service.

 


Get Your Ad Here

data_list