Issue with ultrasonic in behavior programming

Post your NXJ projects, project ideas, etc here!

Moderators: roger, 99jonathan, imaqine

Issue with ultrasonic in behavior programming

Postby abcde13 » Mon Jul 09, 2012 2:46 pm

Hi guys. So, I'm new to lejos and all, but I've taken an AP Computer Science class in java, so the syntax and threading stuff isn't that new. Just maybe the robotics aspect of it. I've got a problem right now using the ultrasonic sensor in my behavior. For some reason, when I print the sonar.getDistance() to screen, it gives me a value of 255, but when I move something close to it, the number climbs through 300 to 800, before hitting a random value like 145 (not the right number) and returning to 255. Here is the code for the behavior and the main class with the arbitrator:

import lejos.nxt.*;
import lejos.util.*;
import lejos.robotics.subsumption.*;

Code: Select all
//Behavior "Scan"
//Search for objects in vicinity. Stop when something is in sight.

public class Scan implements Behavior {
   private UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S4);
   private boolean suppressed = false;
   private int i = 0;
   private int finding;

   public void suppress() {
      suppressed = true;
   }

   public boolean takeControl() {
      return(sonar.getDistance() > 25 );
   }

   public void action() {
      suppressed = false;
      if(Button.RIGHT.isDown()) {
                System.exit(1);
          }
      Motor.A.setSpeed(180);
      Motor.C.setSpeed(180);
      Motor.A.forward();
      Motor.C.forward();
      while(!suppressed) {
         if(Button.RIGHT.isDown()) {
                System.exit(1);
             }
         Thread.yield();
         LCD.drawInt(sonar.getDistance(), 5, 3);
      }
      Motor.A.stop();
          Motor.C.stop();
          System.exit(1);
    }
}


*The if statements were place so that I could exit and not have to pull the battery out if the program became stuck in an infinite loop.

import lejos.nxt.*;
Code: Select all
import lejos.util.*;
import lejos.robotics.subsumption.*;


//SwarmFind
//Meant to cycle through behaviors to help swarm gather around desirable object.

public class SwarmFind {
   public static void main (String[] args) {
      Behavior scanner = new Scan();
      Behavior[] behaviors = {scanner};
      Arbitrator arby = new Arbitrator(behaviors);
      arby.start();
   }
}


Called SwarmFind because this is going to be part of a bigger swarm project.

Now, I've tested it with all the ports, different wires. This program I quickly whipped up:

Code: Select all
import lejos.nxt.*;
import lejos.util.*;
import lejos.robotics.subsumption.*;

public class testUS {
      public static void main(String[] args) {
         UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S4);
         while(sonar.getDistance() > 25) {
            LCD.drawInt(sonar.getDistance(), 5, 0);
            Motor.A.forward();
            Motor.C.forward();
         }
         Motor.A.stop();
         Motor.C.stop();
         System.exit(1);
      }
}


works like a charm, with the correct distance and stopping and all.

So, can anyone figure out what I'm doing wrong and explain it please? Thanks.

Oh, I have lejos 0.9.1 on a Windows 7, if that's any help.
abcde13
New User
 
Posts: 7
Joined: Mon Jul 09, 2012 2:37 pm

Re: Issue with ultrasonic in behavior programming

Postby gloomyandy » Mon Jul 09, 2012 5:13 pm

When displaying numbers on the lcd screen you should probably use the version of drawInt that takes a field width. That way you will not end up with parts of old numbers being left on the screen. So with your current code if getDistance returns 255 followed by say 33 you will see 255 and then 335 because the last 5 of 255 was not cleared from the display. use something like drawInt(sonar.getDistance(), 5, 5, 3) instead this will always use a field width of 5 to display the number and so no parts of the old value will be left behind.

Also there is no need to program your own way to abort a program (unless you really want to), simply hold down both enter and escape (the two squarish keys), and you program will be aborted...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3004
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Issue with ultrasonic in behavior programming

Postby abcde13 » Mon Jul 09, 2012 6:35 pm

Well, shows why I'm new to lejos. OK, I'll try drawInt with a field with parameter.

And I'll remember that for aborting a program. It was get annoying having to put those if's everywhere.

--

Awesome, so now, the right digits are showing up. And my behavior programming works.

Thanks for your help, gloomyandy
abcde13
New User
 
Posts: 7
Joined: Mon Jul 09, 2012 2:37 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff