# Robot Class - Check my Code Please

Hello Geniuses,

This code is for a Differential Drive Mobile Robot Class.
The robot should change each of its left and right wheel velocity from [-1.0 to 1.0] .
// I used nested for loop for this.

In each specific velocity value for left and right wheel, the robot will go in a circular trajectory.
I need to calculate the diameter of this circle by calculating the distance between the first point and current point of its trajectory (then stop at the longest distance).

I have wrote the code – but it doesn’t work.

``````class Robot {

float diameter;
float distance;
int   index = 1;  //index for number of outputs
int condition;       //if (Nl == Nr or Nl = -Nr) = 0;

float [] velocityLeft  = {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0};
float [] velocityRight = {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0};

float Nl;    //Right wheel velocity
float Nr;    //Left  wheel velocity
float theta = radians(0); //Angle with -x-axis

ArrayList<Float> positionX = new ArrayList<Float>();
ArrayList<Float> positionY = new ArrayList<Float>();

float L = 6;       //Distance between two wheels

float vx;
float vy;
PVector velocity = new PVector(vx, vy); //linear velocity
PVector location; //DDMR current location
float omega;      //Angular velocity

float initialX;
float initialY;

Robot (){
initialX = 300;
initialY = 300;
location = new PVector (initialX, initialY); // Initialize the location.
}

void display(){

for (int iterationLeft = 0; iterationLeft < velocityLeft.length; iterationLeft ++)
{
for (int iterationRight = 0; iterationRight < velocityRight.length; iterationRight ++)
{
condition = 1;
diameter  = 0;
distance  = 0;

Nl = velocityLeft[iterationLeft];
Nr = velocityRight[iterationRight];

if (positionX.size() > 0){
for (int iterationDelete = 0; iterationDelete < positionX.size(); iterationDelete ++)
{
positionX.remove(iterationDelete);
positionY.remove(iterationDelete);
}//end (for) iterationDelete
}//end (if) statement

if (Nl == Nr)
{
condition = 0;
print (index + ": Diameter = Infinity - Straight Movement - " + "Left Speed = " + Nl + " - Right Speed = " + Nr + "\n");
index ++;
} else if (Nl + Nr == 0)
{
condition = 0;
print (index + ": Diameter = 0 - In place turning " + "Left Speed = " + Nl + " - Right Speed = " + Nr + "\n");
index ++;
}

while (condition == 1)
{

//Save the all location values of x and y in an ArrayList

theta = theta + omega;

//Calculate the distance between the current position and the first position
int lastIndex = positionX.size()-1;
distance = dist(positionX.get(0), positionY.get(0), positionX.get(lastIndex), positionY.get(lastIndex));

if (diameter <= distance)
{
diameter = distance;
}//end of (if) statement

if (diameter > distance)
{
print(index + ": Diameter= " + diameter + " - Left Speed = " + Nl + " - Right Speed = " + Nr + "\n");
condition = 0;
index++;
}//end of (if) Statement

}//end of (While) Loop
}//end (for) iterationRight loop
}//end (for) iterationLeft loop
}//end void Display()
}//end class Robot
``````

Can you please be more specific? What do you mean when you say it doesn’t work? Have you tried debugging your code? Which line of code is behaving differently from what you expected?

1 Like

The first line in (while) loop

`positionX.add(location.x);`

``````crashed in event thread due to null
java.lang.NullPointerException
at processing.mode.java.runner.Runner\$2.run(Runner.java:590)
``````

I got also the following message

``````An OutOfMemoryError means that your code is either using up too much memory
because of a bug (e.g. creating an array that's too large, or unintentionally
If your sketch uses a lot of memory (for instance if it loads a lot of data files)
you can increase the memory available to your sketch using the Preferences window.
``````

Well, your error message says it all: it sounds like you’re creating an array that’s too large. The `ArrayList` class uses an array internally, so my guess is you’re adding too many elements to your `ArrayList`. Try printing out the size of that `ArrayList` just before that line. What do you expect the size to be? What is the size actually?

I got it thanks.
So, if i want to remove all the elements from the ArrayList in the way that ArrayList will add new elements from the index
I was using

``````//Remove Elements
ArrayList.remove(index);
``````

ArrayList becomes larger and larger !!!

Then you want myList.clear()

From the ArrayList reference page

``````public void clear ()