I am currently working on a robot. I use an EV3 lego brick. My goal is to travel from point A to B using a method travelTo(x,y) using the shortest path. This method is in a thread name Drive, which contains all the methods used to control the movements of my robot (turnTo(double angle), travel(double distance), travelTo(double x, double y), changeSpeed(int newSpeed)...). I have another thread called ObstacleAvoidance which is supposed to stop the thread Drive if there is an obstacle in front and start avoiding the obstacle using P Controller technique to follow the obstacle.
My problem is that my P Controller uses methods inside the Thread Drive. Therefore, when I see an obstacle I cannot do:
if (obstacle){
Drive.wait();
while(isAvoiding){
pControler();
}
}
Drive.Notify();
private void pController(){
//use methods inside the DriveThread
}
How can I go around this problem? In other words, how can I stop the current action of my robot, avoid the block and then continue what I was doing?
You can interrupt the Drive Thread using Thread.interrupt() if an obstacle is in the path. Then you can compute how to deal with the obstacle and then Drive again.
https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#interrupt--
You will have to catch the https://docs.oracle.com/javase/8/docs/api/java/lang/InterruptedException.html in the Drive thread and decide what to do then. Since i do not know how your robot works i would recommend to just stop the thread then. You can start another Drive threat after computing how to deal with the obstacle.