Autonomous Java Robot

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

Post Reply
Edutainment
Posts: 423
Joined: Tue Feb 20, 2007 8:33 pm
Location: Ontario, Canada

Autonomous Java Robot

Post by Edutainment »

In my Computer Sciences class, they're teaching us Java with a little tile-based Robot called Karel. We're doing basic things like making him pick up a series of beepers without having to type out every action. It's pretty basic, so I'm doing something cooler.

'Tis an autonomous (independent, self-operation, automatic, whatevz) robot. It can so far (not very far... it's quite simple) detect when it's about to collide with a wall and evaluate which way to go. That's basically it so far. So it's already autonomous, but stupid, and slow (it spins around 360 before it actually makes a decision where to go).

I call it AutoBot. Here is the code:

(There's no turnRight, that's why turnLeft is repeated so much)

Code: Select all

import becker.robots.*;

/**
 * This is an autonomous robot. He can operate on his own, though he does not exhibit free will.
 * 
 * @Author Edutainment */

public class AutoBot extends Robot
{
   /**
    * Construct a new AutoBot.
    * 
    * @param aCity city name
    * @param avenue x coordinate of initial position
    * @param street y coordinate of initial position
    * @param aDirection initial direction: NORTH, EAST, SOUTH, or WEST
    */
   public AutoBot(City aCity, int avenue, int street, Direction aDirection)
   {  super(aCity, avenue, street, aDirection);
   }
   
   /**
    * When AutoBot senses a wall, he will check if his left, and then right, sides are clear.
    * These booleans will store than info. */
   boolean right;
   boolean left;
   
   /**
    * Surveys surroundings. Stores info in above booleans.
    */
   public void survey(){
     this.turnLeft();
     if(this.frontIsClear()==true){
       left = true;
     } else {
       left = false;
     }
     this.turnLeft();
     this.turnLeft();
     if(this.frontIsClear()==true){
       right = true;
     } else {
       right = false;
     }
     this.turnLeft();
     this.decide();
   }
   
   /**
    * This service takes said booleans, and uses the information to decide which way to go.
    */
   public void decide(){
     if(right==true && left==false){
       this.turnLeft();
       this.turnLeft();
       this.turnLeft();
     } else if(left==true && right==false){
       this.turnLeft();
     } else {
       this.turnLeft();
       this.turnLeft();
   }
   }
   
   /*
    * This is the service called in the main program to run AutoBot autonomously.
    */
   public void run(){
     while(true){
       if(this.frontIsClear()==true){
         this.move();
       } else {
         this.survey();
       }
     }
   }
}
So far, when it detects a wall, it checks which sides are not blocked, and then goes either, right, left or back the way it came if both sides are blocked. But I don't know how it should decide which way to go if both it's right and left sides are open. I could just make it go either way first and then it will end up coming back if that way's blocked but that's dumb :lol:.

Ideas?

BTW, if you wanna try it, you can get the Robots library at http://www.learningwithrobots.com and lots of example robots.
Post Reply