1

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?

  • Are we talking about `java.lang.Thread`s here? Because you aren't supposed to call `wait()` and `notify()` on thread objects at all. – biziclop Nov 19 '16 at 22:36
  • 1
    Methods are on objects, whether that object represents a thread shouldn't stop it from neing called by another thread. And why split out functionality into 2 threads if one has to stop the other before it can work? I don't know anything about robotics but this makes me question how things are organized. – Nathan Hughes Nov 19 '16 at 22:46

1 Answers1

0

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.