Multithreading

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Multithreading

Postby Shadowstep » Sat Feb 04, 2012 5:28 pm

Hay I was wondering I am making this robot that needs to drive and at the same time do some other stuff but I don't understand anything about multithreading, I just need the robot to look for sensor press and for distance at the same time it is driving

Code: Select all
package org.lejos.example;

import lejos.nxt.*;

/**
 * Example leJOS Project with an ant build file
 */
public class HelloWorld {
        public static boolean rotate = false; 
         public static boolean halt = false;
         public static boolean auto = true;
         
         class MyClass {
         
         
         }
         
    public static void main(String[] args)
        {
           
             // Main class
             
            if (auto = true){
           
                /**
             * Autodrive
             */
                autodrive();
           
           
           
           
            }else{
            /**
             * guided drive
             */
           
           
           
           
            }
           
           
      
   }
        public static void headcheck()
        {
//            Rotate op motor.B input op 2
            boolean allowrotate = false;
            boolean direction = true;
            int intdegrees = 0;
            if (allowrotate = true) {
                //rotate
                Motor.B.resetTachoCount();
                if (direction = true) {
                                //rotate and check left
                                       direction = false;
                                       while (intdegrees <= 15) {
                                       Motor.B.rotateTo(intdegrees);
                                       intdegrees = intdegrees + 5;
                                   }       
                            } else {
                                //rotate and check right
                                       direction = true;                                   
                                       while (intdegrees >= -15) {         
                                       Motor.B.rotateTo(intdegrees);
                                       intdegrees = intdegrees - 5;
                                   }
                        }       
            } else {
            //no rotate
            }         
        }
         public static void forward()
        {
           while (rotate = true){
           Motor.A.forward();
           Motor.C.forward();
           }       
        }
         public static void backward()
        {
           while (rotate = true){
           Motor.A.backward();
           Motor.C.backward();
           }     
        }
       
         public static void left()
        {
           while (rotate = true){
           Motor.A.backward();
           Motor.C.forward(); 
           }     
        }
         public static void right()
        {
           while (rotate = true){
           Motor.A.forward();
           Motor.C.backward();
           }         
        }
           public static void stopall()
        {
           while (rotate = true){
           Motor.A.stop();
           Motor.C.stop();
           }       
        }
        public static void pressuresensora()
        {
            TouchSensor pa = new TouchSensor(SensorPort.S4);
           while (!pa.isPressed()){
            stopall();   
           }       
        }
        public static void pressuresensorb()
        {
           TouchSensor pb = new TouchSensor(SensorPort.S1);
           while (!pb.isPressed()){
            stopall();       
           }           
        }
        public static void afstand()
        {
           UltrasonicSensor us = new UltrasonicSensor(SensorPort.S2);
            int afstand;
            afstand = us.getDistance();
            LCD.drawInt(afstand, 1, 1);
            if (afstand < 10){
            stopall();           
            }
        }     
        public static void autodrive()
        {
           
            while(auto = true){
             
               
               
           
           
           
           
           
            }
           
        } 
       
}


this is what I have, now I only need a way to put this multithreading in my public void autodrive and then call al the other functions there while driving.

I would appreciate all help caus it's for my thesis or whatever you call it in english (in dutch it's eindwerk.)

regards Shadowstep
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Multithreading

Postby Shadowstep » Sat Feb 04, 2012 5:31 pm

The last thing that says if auto = true bla bla bla bla bla .... that's caus I am going to make it like this so it can be remote controlled or can auto drive in a room
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Multithreading

Postby roger » Sat Feb 04, 2012 5:39 pm

You might want to look at the API for DifferentialPilot, particularly the non-blocking versions of the rotate and travel methods.
Roger
roger
Moderator
 
Posts: 357
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: Multithreading

Postby Shadowstep » Sun Feb 05, 2012 8:49 am

Ok I will have a look at that, but how does that help with the multithreading?

kind regards
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Multithreading

Postby Shadowstep » Sun Feb 05, 2012 8:50 am

And do I need a compas sensor for that or not? caus I don't have one.
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Multithreading

Postby Xfel » Sun Feb 05, 2012 12:09 pm

If you want to use real Multithreading, just look for a tutorial on Java multithreading like this one. The LeJOS threading model has the same API as the Java SE one for most features.

However, Multithreading can produce many synchronisation errors. so, it is better to use non-blocking tasks and do custom multiplexing. The easiest solution for that is the arbitrator system.
Xfel
New User
 
Posts: 11
Joined: Mon Jan 30, 2012 3:29 pm

Re: Multithreading

Postby Shadowstep » Sun Feb 05, 2012 12:41 pm

WOW :o this suppresion stuf etc was what I was looking for :D that is much better then multithreading in my case :D
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Multithreading

Postby Rickz2020 » Mon Feb 06, 2012 9:32 pm

Multithreading can be very useful and will allow your robot to perform a number of tasks at the same time.

It could be reading sensor data, sending it, reading data coming into the robot from a connection - EG bluetooth, moving around, doing calculations... I can go on :D

The easiest way to use a thread is like this:

Code: Select all
      // start a new thread to read the sensor data.
      // @param readSensors_thread is just the name given to our example thread.
      // Create the new thread
      Thread readSesnsors_thread = new Thread(new Runnable() {
         @Override
         public void run() {
            //Whatever happens between the brackets of run() is done so on a separate thread...
            //While the escape button is not pressed:
            while (!Button.Escape.isPressed()) {               
                  LCD.clear();
                  LCD.drawString("I'm a new thread... na naa na naaa naaaa!, 0, 0);
                  //Add a 5ms delay otherwise the screen will be cleared too quickly and you'll see nothing.
                  Delay.msDelay(5);
                  LCD.refresh();      
            }
         }
      });
      //Start the thread :D
      readSesnsors_thread .start();


Don't be afraid to use multithreading, it's your friend :D

Regards,

Rich.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Multithreading

Postby Shadowstep » Tue Feb 07, 2012 4:07 pm

first clear explenation on how multithreading works ... I BOW FOR YOU!! really thanx mate :)
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Multithreading

Postby Rickz2020 » Tue Feb 07, 2012 7:18 pm

You're welcome.
Some of the explanations online can be pretty confusing until you get the hang of it. Glad I could help.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.

Re: Multithreading

Postby skoehler » Tue Feb 07, 2012 8:25 pm

Rickz2020 wrote:Don't be afraid to use multithreading, it's your friend :D


Multithreading may result in the necessity of synchronization, at least mutual exclusion, and when you realize that busy waiting is not the top-notch thing to do, new challenges like using conditions await you. Knowing the rules is essential, otherwise you just end up with race conditions, which means that you have a problem that works sometimes, and sometimes doesn't - or even worse: a program that works most of the time, but not always.
With multithreading, the basic mentality of "it runs OK, so my code must somewhat correct" becomes even more wrong.
skoehler
leJOS Team Member
 
Posts: 1387
Joined: Thu Oct 30, 2008 4:54 pm

Re: Multithreading

Postby Shadowstep » Sun Feb 12, 2012 11:23 am

Code: Select all
package org.lejos.example;
import lejos.nxt.*;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.robotics.subsumption.Behavior;
import lejos.robotics.subsumption.Arbitrator;



/**
 * Example leJOS Project with an ant build file
 */
public class HelloWorld  {
       
        Thread readSesnsors_thread = new Thread(new Runnable() {
        @Override
        public void run() {
          //code voor thread1
           
           
        }
       });
        Thread readSesnsors2_thread = new Thread(new Runnable() {
        @Override
        public void run() {
          //code voor thread2
           
           
        }
       });
        Thread readSesnsors3_thread = new Thread(new Runnable() {
        @Override
        public void run() {
          //code voor thread3
           
           
        }
        });
        Thread readSesnsors4_thread = new Thread(new Runnable() {
        @Override
        public void run() {
          //code voor thread4
           
           
        }
       });
       
       
       

       }

ok now I have this, but when I type the rearsensor1_thread.start(); it gives an error package readSensors4_thread does not exist

<identifier> expected and I don't know what to do. any help? and I will take a look at the other thing you mentiond later, but I will first try this caus I don't know much about java yet it's my first time I have to code something a bit bigger the I'm used to.
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm

Re: Multithreading

Postby Shadowstep » Sun Feb 12, 2012 11:41 am

never mind I got it, I did not know you could not program directly into the main class thingie :p
Shadowstep
New User
 
Posts: 22
Joined: Sun Jan 15, 2012 9:24 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff