Question: I am trying to create a java application that uses jFuzzyLogic and simulates a robot traversing through a course with randomly generated walls. The walls
I am trying to create a java application that uses jFuzzyLogic and simulates a robot traversing through a course with randomly generated walls.
The walls are generated using PathGenerator.java. It creates a data file where each line contains a left bound and a right bound in integer form. Left bound max is -100 while right bound max is 100. The walls cannot be more than 200 units apart and cannot be closer than 25 units together.
PathGenerator.java will be the reaadings of the imaginary sensors on the robot. The task is to read a left bound and a right bound, apply it to the fuzzy control logic, return a heading, and have the robot make the appropriate corrections. This process will repeat until the course has been completed. 3000 ticks was suggested, though not required. The x coordinate is the only concern. We can also assume that for each tick, the robot can make the appropriate full corrections in positioning.
Initially the robot is placed randomly in-between the first data vectors bounds. For example is the left bound is -42, and the right bound is 39, the robot should be between those numbers. On each tick, the robot will compute from the heading returned how far it must move about the x axis. This means the only computation needed is the cosine of the heading added to the current position. It should output to the terminal via stdout a line such as the following,
-50 -32.46 27
The left value -50 is the left wall, the center value -32.46 is the robots position, and the right value 27 is the right wall.
The application should take two inputs, a data file and the .fcl file. Checks should be done to make sure the files were supplied and are correct.
I know this should be a simple program, 50 lines I would assume. I am just having trouble understanding exactly what to do.
PathGenerator.java code:
/**
Instructions:
Use this file to generate a data set that randomly creates your
path for the fuzzy controlled robot to follow.
Values will be between -100 and 100
Usage: java PathGenerator
*/
import java.io.*;
import java.util.*;
public class PathGenerator{
final static int SIZE = 200;
final static int SEPERATION = 25;
public static void main(String[] args){
/* Because people do not follow instructions */
if(args.length != 2){
System.err.println("Usage: java PathGenerator
System.exit((int)(Math.pow(2, (int)(Math.random()*128))));
}
/* Members */
BufferedWriter out = null;
int leftBound, rightBound, currentPosition;
int numTicks = 0;
Random random;
/* Open and read the user supplied file */
try{ out = new BufferedWriter(new FileWriter(args[0])); }
catch (IOException ioe) {
System.err.println("Cannot open file: " + args[0]);
System.exit((int)(Math.pow(2, (int)(Math.random()*128))));
}
/* Parse the int from args, and jic, catch excpetion */
try{ numTicks = Integer.parseInt(args[1]); }
catch (NumberFormatException nfe) {
System.err.println("This is not an integer: " + args[1]);
System.exit((int)(Math.pow(2, (int)(Math.random()*128))));
}
/* Set up an initial start bound */
random = new Random();
leftBound = random.nextInt(SIZE) - SIZE/2;
leftBound = (leftBound >= SIZE/2 - SEPERATION) ? SIZE/2 - SEPERATION : leftBound;
rightBound = random.nextInt(SIZE) - SIZE/2;
rightBound = (rightBound - SEPERATION <= leftBound) ? leftBound + SEPERATION : rightBound;
/* Now write current tick, then retick, then detick */
for(int i = 0; i < numTicks; i++){
/* Write to file */
try{ out.write(leftBound + " " + rightBound + " "); }
catch (IOException ioe){
System.err.println("Error writing to file");
System.exit((int)(Math.pow(2, (int)(Math.random()*128))));
}
/* Choose a new leftbound */
leftBound = (random.nextInt(SIZE) % 2 == 0) ?
leftBound - (random.nextInt(SIZE) % 4) :
leftBound + (random.nextInt(SIZE) % 4);
/* Choose a new rightbound */
rightBound = (random.nextInt() % 2 == 0) ?
rightBound - (random.nextInt(SIZE) % 4) :
rightBound + (random.nextInt(SIZE) % 4);
/* Restrict those sizes */
leftBound = (leftBound <= -SIZE/2) ? -SIZE/2 : (leftBound + SEPERATION >= rightBound) ? rightBound - SEPERATION : leftBound;
rightBound = (rightBound >= SIZE/2) ? SIZE/2 : (rightBound - SEPERATION <= leftBound) ? leftBound + SEPERATION : rightBound;
}
/* Clean up and exit nicely */
try{ out.close(); }
catch (IOException ioe){
System.err.println("Error closing file");
System.exit((int)(Math.pow(2, (int)(Math.random()*128))));
}
System.exit(0/(int)(Math.pow(2, (int)(Math.random()*128))));
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
