3

I am new to java and lejos so please don't blame me if i'm asking rather dumb questions.

I was trying to let the lego ultrasonic sensor scan a 360 degree area around my nxt by rotating it on a motor. Every 5 degrees it saves the distance to a .txt file.

My problem is that later when I read the file from my PC after uploading it with nxjbrowse.bat, it only contains the ASCII characters linked to the numbers that supposed to be saved there (0 - 255).

My code for the NXT:

package Dataloggers;

import java.io.; import lejos.nxt.;

public class USdistance {

int totalRotation = 360; int scanDensity = 5; UltrasonicSensor ultrasonicSensor = new UltrasonicSensor(SensorPort.S3); File distanceFile = new File("Distances.txt"); FileOutputStream fileStream = null; public static void main(String[] args) { @SuppressWarnings("unused") USdistance usD = new USdistance(); } public USdistance() { Motor.A.setAcceleration(2000); Motor.A.setSpeed(50); try { fileStream = new FileOutputStream(distanceFile); } catch (Exception e) { LCD.drawString("Can't make a file", 0, 0); Button.ESCAPE.waitForPress(); System.exit(1); } DataOutputStream dataStream = new DataOutputStream(fileStream); Motor.A.rotate(90); Motor.A.resetTachoCount(); Motor.A.backward(); do { if (-(Motor.A.getTachoCount()) % scanDensity == 0 ) { int distance = ultrasonicSensor.getDistance(); try { dataStream.writeInt(distance); fileStream.flush(); } catch (IOException e) { LCD.drawString("Can't write to the file", 0, 1); Button.ESCAPE.waitForPress(); System.exit(1); } } } while (-(Motor.A.getTachoCount()) < totalRotation); Motor.A.stop(); try { fileStream.close(); } catch (IOException e) { LCD.drawString("Can't save the file", 0, 1); Button.ESCAPE.waitForPress(); System.exit(1); } Motor.A.rotate(270); } }

Thanks in advance Rob

Rob
  • 51
  • 11
  • Can you give a snippet of the .txt file that your code produces? – Chris Laplante Sep 09 '12 at 16:29
  • It produces this: ㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㐀5㐴㐀3㌴㐀4㐴㌀6㌳㌀3㈳㌀2㈳㌀2㈳㌀2㘳㌀6㘳㌀6ㄳ㌀1ㄳ㌀1ㄳ㌀2㈳㌀4㐳㌀4㔶㘀7㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㈶㘀2㔲5㔲5㔲5㔲5㈲㈀3㔲5㔲5㔲5㌲㈀6㠲㈀㔵㈀㔵㈀2㈲㈀3㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㔲5㐵㔀3㈵㔀2㈵㔀2㈵㔀2㈵㔀2㌵㔀3㌵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵㈀㔵 – Rob Sep 09 '12 at 18:31
  • Is there any way to specify an encoding when you create the `File` or `FileOutputStream`? – Chris Laplante Sep 09 '12 at 18:34
  • by the way. If I don't plug in my sensor, ultrasonicSensor.getDistance() will get the value 255. Then the text file only contains a lot of 'ÿ' and the decimal code of this character is 255. – Rob Sep 09 '12 at 18:39
  • Definitely sounds like an encoding issue – Chris Laplante Sep 09 '12 at 18:49
  • Is it possible ultrasonicSensor.getDistance() returns a byte value instead of an int value, because if I replace dataStream.writeInt(distance) with dataStream.writeInt(255) it does work. – Rob Sep 09 '12 at 19:18
  • Doesn't look like it: http://lejos.sourceforge.net/p_technologies/nxt/nxj/api/lejos/nxt/UltrasonicSensor.html#getDistance\(\) – Chris Laplante Sep 09 '12 at 19:19
  • Then I still don't know what the problem is.. – Rob Sep 09 '12 at 21:09
  • I don't think the NXT is encoding the results incorrectly; I think Java is encoding the text file with the wrong encoding. – Chris Laplante Sep 09 '12 at 22:43
  • I solved my problem by using a 'BufferedOutputStream buffStream' than I only needed to use: 'buffStream.write(distance.getBytes());' and it works perfectly. But I still don't know why 'dataStream.writeInt(distance)' didn't work. – Rob Sep 10 '12 at 16:30
  • Not sure. Glad you solved it though. You should create an answer and mark it accepted. – Chris Laplante Sep 10 '12 at 16:52

1 Answers1

0

I solved my problem by using:

int distance = ultrasonicSensor.getDistance();
BufferedOutputStream buffStream = new BufferedOutputStream(new FileOutputStream(new File(distances.txt)));
buffStream.write(distance.getBytes());

By using this my text file looks like this: 41 41 0 0 0 0 0 0 40 40 40 40 40 39 39 39 39 39 39 40 40 40 40 instead of 㔵㈀㔵㈀㔵㈀㔵㈀㔵㐀5㐴㐀3㌴㐀4㐴㌀6㌳㌀3㈵㔀2㈵㔀2㈵㔀2㈵㔀2㌵㔀3㌵㈀㔵㈀㔵㈀㔵㈀

Anyway thanks for your replies @SimpleCoder

Rob
  • 51
  • 11