Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a method to make a robot ride in a specific shape. During the riding he must detect collision and stop if he has detected an obstruction.
When I would use a loop for this, it would only detect collision at the start of the method, and not during the rest of the commands. How can I make this loop apply to every movement without making a loop for every single movement?

What I have tried:

5 stands for 5 centimers detection length

if(!Detect.obstacle(5))
{
forwardLong();
Drive.toTheRight();
forwardShort();
Drive.toTheLeft();
forwardLong();
Drive.toTheLeft();
forwardShort();
Drive.toTheLeft();
forwardLong();
Drive.toTheLeft();
forwardShort();
Drive.toTheRight();
forwardLong();
Drive.toTheRight();
forwardShort();
}
else
{
BoeBot.wait(5);
state = 1;
}
Posted
Updated 30-Nov-17 1:35am

The way I'd do it is to use a set of commands.
This could be as simple as a string of character pairs: one for operation, one for duration.
Perhaps like this:
FLDRFSDLFL...
Where "F" is for "forward", "L" is for "Long"; "D" is for "Direction", "R" is for "Right"; "F" is for "forward", "S" is for "Short"; "D" is for "Direction", "L" is for "Left"; ... and so on.
Then have your loop process a pair of characters: if it gets "FL" it calls forwardLong; if it gets "DL" it calls Drive.toTheLeft After each command, it checks for an obstruction before moving on to the next command.

This also means that you don't have to rewrite your code for each shape - just change the string you pass it.
 
Share this answer
 
Comments
________________ 30-Nov-17 7:43am    
This is good idea, but if create one more programming language. :-)
OriginalGriff 30-Nov-17 7:53am    
No, you're just providing data which controls the actions to a fixed program, in the same way that the contents of a database control what is displayed on an online shopping website. As the products and their prices change, the data is changed, not the program, but the webpage is always up to date.
________________ 30-Nov-17 8:56am    
Agree. But this is next step for topicaster - after understanding the OOP. :-)
Your question is not very clear. But I suggest to write a method for a single move passing distance and direction as parameters and returning the collision state:
Java
boolean MoveOne(boolean longMove, boolean leftDirection)
{
    // Optionally wait and check again here upon collision
    if (IsCollision())
        return false;
    if (longMove)
        forwardLong();
    else
        forwardShort();
    if (leftDirection)
        Drive.toTheLeft();
    else
        Drive.toTheRight();
    return true;
}
You can then define an array of movements and use a loop to call that function for each item pair:
Java
// Tip: Define constants for the corresponding moves to increase readability
boolean moveShape[][] = { 
    { true, false}, { false, true} // , ...
};
for (int i = 0; i < numberOfMoves; ++)
{
    if (!MoveOne(moveShape[i][0], moveShape[i][1]))
    {
        break;
    }
}
 
Share this answer
 
Java

I suggest to look at bot moves as objects (I have no java environment now, so fix the syntax) :

You should define the class like

Java
class SingleMove
{
    public SingleMove(boolean short, boolean left)
    {
    Short = short;
    Left = left;
    }
        public boolean Short;
        public boolean Left;
    }

public void Move()
{
       if (Short)
    {
       forwardShort();
    }
    else
    {
      forwardLong();
    }

if (Left)
{
  Drive.toTheLeft();
}
else
{
  Drive.toTheRight();
}


}


}

Than you can create array with preferred algorithm of movement:

ArrayList [] Allmoves = new Arralylist;

Allmoves.Add(new SingleMove(True, False));

...

Allmoves.Add(new SingleMove(True, False));
Allmoves.Add(new SingleMove(False, False));

Then your action code will be like :

Java
for(int ii = 0 ; ii <  Allmoves.GetLenght(); ii++)
{
   if(!Detect.obstacle(5))
    {
        Allmoves[ii].Move();
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900