Acceleration Sensor different in pccomm?

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc

Moderators: roger, 99jonathan, imaqine

Acceleration Sensor different in pccomm?

Postby rctdeclan » Mon Oct 10, 2011 1:17 pm

Hi guys,

I tried the AccelDemo example and it returned nice values.

The problem is, now I'm trying to poll the (HT) accelerometer values within the NXT, and it returns weird numbers.

(The AccelDemo had numbers from 0-200 and 800-1000 while the NXT one spastically goes from 0 to 9000)

Any suggestions why this happens?

-Declan :?
rctdeclan
New User
 
Posts: 3
Joined: Mon Oct 10, 2011 1:12 pm

Re: Acceleration Sensor different in pccomm?

Postby gloomyandy » Mon Oct 10, 2011 2:03 pm

Hi,
What code are you using to test with on the NXT? What version of leJOS are you running? What exactly are you seeing as the output? How are you displaying the results? What are you doing to the sensor when you get these results?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3003
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Acceleration Sensor different in pccomm?

Postby rctdeclan » Mon Oct 10, 2011 5:05 pm

Hi

What code are you using to test with on the NXT?


The following (I just pasted everything, go down to where the comment "HERE'S THE CODE" is.)

Code: Select all
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.MotorPort;
import lejos.nxt.NXTMotor;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
import lejos.nxt.SoundSensor;
import lejos.nxt.TouchSensor;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.addon.AccelHTSensor;
public class CPConAccel {
   public static CPConAccel instance;
   static String VERSION = "1.0.0";
   
   BTConnection btc;
   DataInputStream dis;
   DataOutputStream dos;
   
   NXTMotor mot;
   AccelHTSensor a;
   TouchSensor left;
   TouchSensor right;
   SoundSensor sound;
   
   int yVal,zVal;
   byte yByte,zByte;
   int soundVol;
   byte touch = 0;
   
   public CPConAccel()
   {
      LCD.drawString("CP CONTROLLER",0,0);
      LCD.drawString("Version "+VERSION, 0, 1);
      LCD.drawString("YADA YADA YADA",0,2);
      LCD.refresh();
      SetupSensActuators();
      waitForConnection();
      Step();
      Disconnect();
   }

   public void SetupSensActuators()
   {
      mot = new NXTMotor(MotorPort.A);
      a = new AccelHTSensor(SensorPort.S1);
      left = new TouchSensor(SensorPort.S2);
      right = new TouchSensor(SensorPort.S3);
      sound = new SoundSensor(SensorPort.S4);
   }
   
    public void waitForConnection()
    {
       LCD.drawString("Waiting for connection",0,4);
       Sound.beepSequence();
      LCD.refresh();

        btc = Bluetooth.waitForConnection();
        dis = btc.openDataInputStream();
       dos = btc.openDataOutputStream();
      LCD.clear();
      LCD.drawString("Connected to PC",0,4);
      Sound.twoBeeps();
      LCD.refresh();   
    }
    Byte i;
   
    public void Step()
    {
       //data specification:
      //in bytes: length,null,x,y,soundVol,touchSensors (7 bytes)
       //TODO USE MULTI THREADING? (ONE FOR VALUE READING AND ONE FOR BLUETOOTH I/O)
       while (true)
       {
          //UPDATE ACCEL VALUES (HERE'S THE CODE)
          yVal = a.getYAccel();
          yByte = (byte) (0.635*(yVal-((yVal>500) ? 1024 : 0))); //skim down values from (800..1024, 0...200) to (-127...127)
          zVal = a.getZAccel();
          zByte = (byte) (0.635*(zVal-((zVal>500) ? 1024 : 0)));
          soundVol = sound.readValue();
          if (left.isPressed()) touch+=1;
          if (right.isPressed()) touch+=2;
          LCD.drawInt(yByte, 0, 0);
          LCD.drawInt(yVal, 5,0);
          LCD.drawInt(zByte, 0 ,1);
          LCD.drawInt(zVal, 5,1);
          LCD.refresh();
          try
          {
             //READ BT
             if (dis.available() > 0)
             {
                i=dis.readByte();
                if (i==1) //FEEDBACK: TURN MOTOR.
                   Motor.A.rotate(1000);
                if (i==2) //FEEDBACK: EXIT PROGRAM
                   break;
                dis.skipBytes(dis.available());
             }
             //WRITE BT
             dos.writeByte(yByte);
             dos.writeByte(zByte);
             dos.writeByte(soundVol);
             dos.writeByte(touch);
             dos.flush();
          }
          catch (IOException e)
          {
             LCD.drawString("BT Exception", 0,6);
             break;
          }   
          touch = 0;
          try {
            Thread.sleep(1000);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            LCD.drawString("Thread Exception", 0,6);
            break;
         }
       }
    }
   
    public void Disconnect()
    {
       btc.close();
    }
   
   public static void main(String[] args)
   {
      instance = new CPConAccel();
   }
}



What version of leJOS are you running?


0.9.0 beta

What exactly are you seeing as the output?


When I run the AccelDemo on the pc, I see nice (y and z) values in the range of 800...1000 and 0...200

When I run the CPConAccel on the NXT, the direct sensor values (variables yVal and zVal) start of just fine, but then they reach values like 9000.

How are you displaying the results?


I'm using LCD.drawInt(); to draw the sensor values directly to the LCD.


What are you doing to the sensor when you get these results?


I'm tilting it slowly over the Y and Z axes, I go down to 90 degrees and then return back.

Thanks for the response!

-Declan
rctdeclan
New User
 
Posts: 3
Joined: Mon Oct 10, 2011 1:12 pm

Re: Acceleration Sensor different in pccomm?

Postby gloomyandy » Mon Oct 10, 2011 5:29 pm

Hi,
Are you sure that you are not seeing parts of previous readings being left of the screen. The 3 argument version of drawInt does not touch any of the characters outside of actual string, so if you write...
119
19
9
to the screen you will see
999
after the final 9 has been written. Try changing the call to use the 4 argument version which uses a field width value. So change...

LCD.drawInt(yByte, 0, 0);
LCD.drawInt(yVal, 5,0);
to...

LCD.drawInt(yByte, 4, 0, 0);
LCD.drawInt(yVal, 4, 5,0);
Also I'm a little puzzled as to processing you are doing on the sensor values. What is it that you are doing with the scaling line and why do you need to do it?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3003
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Acceleration Sensor different in pccomm?

Postby rctdeclan » Mon Oct 10, 2011 7:43 pm

Oh, that seems very logical, how stupid of me :D

I'm using the accelerometer's values and send these via bluetooth to a game written in C#. It connects via a COM port.
Cool idea, right?

Thanks for the help,

EDIT: Here's a demo video for those interested:
http://youtu.be/Hmoco8e9OL8

-Declan
rctdeclan
New User
 
Posts: 3
Joined: Mon Oct 10, 2011 1:12 pm


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff