/********************************************************************************************* * example.c : Generic autonomous agent for Netmaze 0.96 * Thomas Voegtlin , may 2001 * ********************************************************************************************** * * initialization : * A call to init_netmaze returns a structure * that contains sensory and motor information * * netmaze = (struct shared_struct*) netmaze_init(argc,argv); * ********************************************************************************************** * * sensory inputs are : * * float netmaze->Retina[]; //the visual fields. 4 float/pixel, RGBA * float netmaze->RangeFinder[]; //range finder info: 1 float * float netmaze->HorizRangeFinder[]; //horizontal line in the middle of the visual field * int netmaze->Collisions[256]; //tactile info * int netmaze->Fitness; //internal state (pain if low - dead if zero) * ********************************************************************************************** * * Retina and RangeFinder contain (netmaze->gamewidth)*(netmaze->gameheight) pixels * HorizRangeFinder contains (netmaze->gamewidth) pixels * ********************************************************************************************** * * Collision sensors are placed around the body of the agent: * * * 2 1 0 255 * . . . . . . . . * . . * . ----------- . * . / \ . * . / \ . * . / /|\ \ . * . | / | \ | . * . | | | . * . | | | . * . | | | . * . \ | / . * . \ | / . * . \ / . * . ----------- . * . . . . . . . . * 127 128 * * ********************************************************************************************** * * Motors : * * a motor action is a boolean combination of the following integer constants: * JOY_LEFT : turn left * JOY_RIGHT : right turn * JOY_UP : go forward * JOY_DOWN : go back * JOY_BUTTON : shoot * * ********************************************************************************************** */ #include "netmaze.h" struct shared_struct* netmaze; int choose_action() { int motor=0; /*motor action*/ int sensor; for(sensor=0;sensor<128;sensor++) { if(netmaze->Collisions[128+sensor]>0) { /* if obstacle on the right then turn left*/ printf("collision %d\n",128+sensor); motor |= JOY_LEFT; break; } if(netmaze->Collisions[127-sensor]>0) { /*if obstacle on the left then turn right*/ printf("collision %d\n",127-sensor); motor |= JOY_RIGHT; break; } } motor|=JOY_UP; /*We also want to move forward*/ return motor; } int main(int argc,char **argv) { int i,j; float a,b; /***************************************************** * Initialization of Netmaze's environment and threads */ netmaze = (struct shared_struct*) netmaze_init(argc,argv); /***************************************************** * Main loop - */ while(!netmaze->exitprg) { /*here stimuli are read*/ netmaze_read_sensors(); /* for(i=0;igamewidth;i++) for(j=0;jgameheight;j++) { a = netmaze->Retina[ i+j*netmaze->gamewidth ]; b = 1-netmaze->RangeFinder[ i+j*netmaze->gamewidth ]; } */ netmaze_write_motors(choose_action()); } }