Cannot read from Ultrasonic Sensor, even with long waits

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

Moderators: 99jonathan, roger, imaqine

Cannot read from Ultrasonic Sensor, even with long waits

Postby fritz2698 » Mon Nov 05, 2007 4:20 am

I put together a very simple program with the purpose of learning to work with sensors and motors before doing anything fancy. Using the NXT brick menu, I can see that the ultrasonic sensor is working, but my code cannot read distances from the sensor.

Environment:
-- Intel Macbook with OSX10.4
-- Using the bluetooth link
-- Running ICommand 0.6 and RXTX 2.1-7
-- Using eclipse for the build environment

Code and output is shown below. Coding longer wait times up to 10 seconds didn't clear up the communication errors that are displayed in the output. Any thoughts on getting this to work would be greatly appreciated.


package TestProject;

import java.lang.Object;
import icommand.nxt.*;
import icommand.nxt.comm.*;
// import icommand.robotics.*;

public class TestProj {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int millivolts;
int ctr, i;
int distance = 255;
int whatnot = 1024;
UltrasonicSensor sonic;

/****************************/
/* THIS MUST BE FIRST !!!! */
/****************************/
NXTCommand.open();

Motor.A.rotateTo(500);
ctr = Motor.A.getTachoCount();
System.out.println("Tacho = " + ctr);

millivolts = Battery.getVoltageMilliVolt();
System.out.println("Battery Test, mV = " + millivolts);

System.out.println("Tone Test.....");
Sound.playTone(500, 1000);

MySleep();
sonic = new UltrasonicSensor(SensorPort.S4);
MySleep();
whatnot = sonic.getScaleFactor();
System.out.println("ScaleFactor = " + whatnot);

MySleep();
whatnot = sonic.getFactoryScaleDivisor();
System.out.println("FactoryScaleDiv = " + whatnot);

MySleep();
whatnot = sonic.getFactoryZero();
System.out.println("FactoryZero = " + whatnot);

MySleep();
whatnot = sonic.getMeasurementInterval();
System.out.println("MeasurementInterval = " + whatnot);

MySleep();
distance = sonic.getDistance();
System.out.println("Distance = " + distance);

/***************************/
/* THIS MUST BE LAST !!!! */
/***************************/
NXTCommand.close();

}

public static void MySleep ()
{
try { Thread.sleep (2000); }
catch (InterruptedException e) { }
}

}


OUTPUT:::

Properties location: /Users/fritzmiller/Documents/MyLEGO/projects/LegoProj1/icommand.properties
NXTCOMM = /dev/tty.NXT-DevB-1
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Tacho = -34
Battery Test, mV = 6979
Tone Test.....
NXTCommand.LSGetStatus() error: Pending communication transaction in progress
ScaleFactor = 255
NXTCommand.LSGetStatus() error: Pending communication transaction in progress
FactoryScaleDiv = 14
NXTCommand.LSGetStatus() error: Pending communication transaction in progress
FactoryZero = 0
NXTCommand.LSGetStatus() error: Pending communication transaction in progress
MeasurementInterval = 1
NXTCommand.LSGetStatus() error: Pending communication transaction in progress
No bytes to be read in I2CSensor.getData(). Returning 0.
Distance = 0
Experimental: JNI_OnLoad called.
fritz2698
New User
 
Posts: 3
Joined: Tue Aug 21, 2007 3:29 am
Location: Virginia

NXTCommand.setVerify(true) fixes the problem

Postby fritz2698 » Mon Nov 05, 2007 12:28 pm

I found an earlier posting regarding this problem with reading from the Ultrasonic sensor with MAC's, and tried the above statement after the open command ..... eureka!
fritz2698
New User
 
Posts: 3
Joined: Tue Aug 21, 2007 3:29 am
Location: Virginia

Postby darkside40 » Tue Nov 13, 2007 1:45 pm

NXTCommand.setverify(true) did the trick.
darkside40
New User
 
Posts: 4
Joined: Thu Oct 25, 2007 5:57 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 4 guests

more stuff