Feature detector/ threads and differential pilot stop()

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

Moderators: roger, 99jonathan, imaqine

Feature detector/ threads and differential pilot stop()

Postby nms886 » Fri Feb 17, 2012 8:53 pm

I'm wondering what the best way to run my ultrasonic sensors. As of now I have three sensors in their own classes and they extend thread. I'm new to multithreading as well as the feature detector. This is my code so far. I have a few questions about it: first Will this work XD? I know threads will end unless there is a loop, but don't know if having the feature detector will keep the thread alive without a loop. Should I just poll the sensor from the loop and forget the feature detector? Also if I can keep the feature detector what will int range equal if there is a null reading?

Code: Select all
package me.team1.nxt;

import lejos.nxt.SensorPort;
import lejos.nxt.UltrasonicSensor;
import lejos.robotics.objectdetection.Feature;
import lejos.robotics.objectdetection.FeatureDetector;
import lejos.robotics.objectdetection.FeatureListener;
import lejos.robotics.objectdetection.RangeFeatureDetector;


public class FrontSensor extends Thread implements FeatureListener{
   Drive drive = new Drive();
   Decide decide = new Decide();
   int range, truerange, count;
   Boolean scan = false;
   
   public void scanON(){
      scan = true;
      truerange = 0;
      count = 0;
   }
   
   public int truerange(){
      return truerange;
   }
   
   public Boolean scan(){
      return scan;
   }
   
    public void run() {
       FrontSensor listener = new FrontSensor();
       UltrasonicSensor us = new UltrasonicSensor(SensorPort.S2);
       RangeFeatureDetector fd = new RangeFeatureDetector(us, 255, 1);
       fd.addListener(listener);
          while (true) {
            try {
               
              }catch(Exception e){}
            }
    }

   public void featureDetected(Feature feature, FeatureDetector detector) {
      range = (int)feature.getRangeReading().getRange();
      if(scan){
         if(range > truerange) truerange = range;
         count++;
         if(count == 9) scan = false;
      }
      
      if((drive.roomcheck() == true || drive.moving() == true) & range < 23){ drive.motorStop(); drive.goOFF(); drive.movingOFF(); decide.stopON();}
   }
   
}


My final question is lets say my differential pilot class activates pilot.travel(45); and that and a sensor reading of <23 signals the
Code: Select all
if((drive.roomcheck() == true || drive.moving() == true) & range < 23){ drive.motorStop(); drive.goOFF(); drive.movingOFF(); decide.stopON();}
line and calling for pilot.stop(); in my drive class. Will it stop the motors even though the travel command is not compete and is not pilot.travel(45, true). I didn't want it to return because the next lines of code in the drive class depend on the vehicle being 45 units forward or everything turning off through the if statement. Im sorry if any of this is hard to understand. Thank you. Randy
nms886
New User
 
Posts: 1
Joined: Fri Feb 17, 2012 7:56 pm

Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff