Re: Threading problems

Olivier Michel <[email protected]> Mon, 29 Mar 2004 13:32:56 +0200
Newsgroups gmane.comp.cybernetics.webot.contest
Organization Cyberbotics Ltd.
Message-ID <[email protected]>
Hi,

It seems the following threaded program works fine on my Windows XP 
machine with Webots 4.0.20 (see attachment). What error (if any) do you 
get ?

-Olivier

Anders Lyhagen wrote:

>Hi,
>
>The problem occurs when you make a call (from a thread) to any method 
>in the webots API (camera_get_image, servo_set_position, robot_step 
>etc).
>I solved the image processing the way you mentioned but I would like to 
>thread some movements as well, however, that is
>impossible (with XP). Might install Linux though..
>
>Best,
>Anders L
>
>  
>
>>Hi,
>>
>>I could reproduce this problem and tryed to fix it but it is not 
>>easy...
>>Strangely enough, this problem occurs only under Window. The program
>>appear to work fine under Linux... Actually under Windows the same
>>OpenGL context cannot be shared among different threads, instead
>>different OpenGL context should be used in each thread which cause some
>>problems with initialization and cleanup which is done by the main 
>>thread...
>>
>>It would be possible to create a special subclass the java Thread that
>>would work with camera, but it would complicate the programming API.
>>
>>Instead, I would recommand you that only the main Thread should do the
>>image acquisition, i.e., camera_enable() and subsequent
>>camera_get_image() while you can distribute the image processing on
>>other Threads. The int array receiving the image should be global
>>variable to be shared between threads. Of couse, this requires some
>>basic Thread synchronization to work well.
>>
>>-Olivier
>>
>>Olivier Michel wrote:
>>
>>    
>>
>>>Anders Lyhagen wrote:
>>>
>>>
>>>
>>>      
>>>
>>>>Hi,
>>>>
>>>>Does anyone know why this code yields a "cannot activate openGL 
>>>>context"
>>>>error?
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>I recently made the Webots API MT-safe, but this was not enough to fix
>>>that problem. It seems that the OpenGL context is not inherited in
>>>subsequent Java Threads. I will try to sort that out as it is 
>>>becoming a
>>>recurrent question... Thanks for the source code which is very simple
>>>and shows nicely the problem.
>>>
>>>-Olivier
>>>
>>>
>>>
>>>      
>>>
>>>>import com.cyberbotics.webots.Controller;
>>>>public abstract class Judoka0 extends Controller {
>>>>  static int camera;
>>>>
>>>>  public static void reset() {
>>>>      camera = robot_get_device("camera");
>>>>  }
>>>>
>>>>  public static void main() {
>>>>      camera_enable(camera, 64);
>>>>
>>>>      Sensors sensors = new Sensors();
>>>>      sensors.setPriority(Thread.MIN_PRIORITY);
>>>>      sensors.start();
>>>>
>>>>      while(true)
>>>>          robot_step(64);
>>>>  }
>>>>
>>>>  static class Sensors extends Thread {
>>>>       public void run() {
>>>>          while(true) {
>>>>              int[] img = camera_get_image(camera);
>>>>              try {
>>>>                  thread.sleep(200);
>>>>              }
>>>>              catch(Exception e){}
>>>>          }
>>>>      }
>>>>  }
>>>>}
>>>>
>>>>Best regards,
>>>>Anders.L
>>>>        
>>>>
> 
>
>
>  
>


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/webots-contest/

<*> To unsubscribe from this group, send an email to:
     [email protected]

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
Judoka0.java (text/x-java, 728 B)
import com.cyberbotics.webots.Controller;
import java.io.*;

public abstract class Judoka0 extends Controller {
  public static void die() {
    System.out.println("die method called");
  }
  static int joint;
  public static void reset() {
    joint=robot_get_device("back_1");
  }
  public static void main() {
    servo_enable_position(joint,64);
    Motion motion = new Motion();
    motion.setPriority(Thread.MIN_PRIORITY);
    motion.start();
    while(true) robot_step(64); // run one step
  }
  static class Motion extends Thread {
    public void run() {
      float pos=0;
      while(true) {
        servo_set_position(joint,pos);
	pos+=0.05;
	try {
	  Thread.sleep(200);
	} catch(Exception e) {}
      }
    }
  }
}