RCX Rotation Sensor problem

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

Moderators: imaqine, 99jonathan, roger

New User
Posts: 5
Joined: Fri Jan 10, 2014 6:20 pm

RCX Rotation Sensor problem

Postby JanH95 » Sun Dec 21, 2014 12:04 pm

Hey there!

I have a problem with my RCX rotation sensor: The sensor measures wrong rotation speeds an the measurements are very jumpy, so if i connect the sensor with one of the regulated NXT Motors with constant speed, the measurements fluctuate for example between 15 an 30 RPM, although the Motor Speed is setted to 360 degrees per second (= 60 RPM).
Of course i divide the rotation speeds of the sensor by 16.
Is this bug known or is it possible, that my RCX rotation sensor ist not working well?

Here is my program for the NXT (of course I have a second program for my Computer to control the NXT via Bluetooth):

Code: Select all

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.nxt.*;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.addon.*;

public class Mustang {
   NXTRegulatedMotor mB = new NXTRegulatedMotor(MotorPort.B);
   RCXRotationSensor sensor1 = new RCXRotationSensor(SensorPort.S1);
   BTConnection connection;
   DataInputStream dis;
   DataOutputStream dos;
   boolean abort = false;
   boolean forward = true;
   int command;
   int gear = 1;
   public static void main(String[] args)
      new Mustang();
   public Mustang()
   public void setButtonListeners()
      //Zurueck-Button bricht das Programm ab und startet den NXT neu
      Button.ESCAPE.addButtonListener(new ButtonListener(){
         public void buttonPressed(Button button)
         public void buttonReleased(Button button)

    * Verbindet den NXT mit einem Computer. Bei Erfolg ertoenen zwei Beeps.
   public void connect()
      connection = null;
      dis = null;
      dos = null;
      LCD.drawString("Waiting for Connection",0,0);
      connection = Bluetooth.waitForConnection();
      dis = new DataInputStream(connection.openDataInputStream());
      dos = new DataOutputStream(connection.openDataOutputStream());
    * Sendet den aktuellen Status als Folge mehrere Integer-Zahlen an den PC
   public void sendStatus()
      // Keine negativen Zahlen senden!
      try {
         //Batteriespannung senden
         //Drehzahl senden
         int rotationSpeed = sensor1.getRotationSpeed();
         LCD.drawString(""+rotationSpeed+"  "+rotationSpeed/16,0,2);
         if (rotationSpeed > 0)
      } catch (IOException e) { e.printStackTrace(); }
    * Empfängt und bearbeitet Signale des Computers
    * @return
   public void receiveMessages()
       * 1: Programm beenden
       * 2: Status senden
      while (command != 1)
         command = 0;
         while (command == 0 || command == -1)
            try { command = dis.read(); } catch (IOException e) { e.printStackTrace(); }
         LCD.drawInt(command, 0, 0);
            case 2: this.sendStatus(); break;
            case 11: mB.forward(); break;
            case 12: mB.flt(); break;
            case 13: mB.backward(); break;
            case 14: mB.setSpeed(mB.getSpeed()+10); break;
            case 15: mB.setSpeed(mB.getSpeed()-10); break;
            default: break;

Thank you for your help!!

Best regards,

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

Re: RCX Rotation Sensor problem

Postby gloomyandy » Sun Dec 21, 2014 1:27 pm

A few things...

I'm not convinced that you should be dividing the speed by 16, the driver seems to already be doing that.

The sensor driver returns the rotation speed in degrees/second so you should be getting a reading that is close to 360. Note however that the sensor calculates this speed by timing the difference between two sensor readings which are 22.5 degrees at a time. For a rotation speed of 360 degrees per second this is a time of 1000/360*22.5 = 62.5mS this measurement is performed by a thread so there is the possibility that the timing is only accurate to approx +/-3mS or so. This would mean a reading of 59 to 66 mS which would result in speed values of 381 to 340 degrees per second. If you are not getting readings in this sort of range it may be that either there is a problem with the sensor, or perhaps it does not work well at these speeds. You could investigate further by using slower motor speeds, and by reading the actual tacho counts from the sensor and see how this compares.

If you still don't understand what is going on then please post some actual readings showing the motor speed, the actual tach count from the sensor and the speed reading, take these readings say ever 30 seconds or so.

As always it is often worth looking at the source of the sensor driver to understand what is going on. You can see the source here:
https://sourceforge.net/p/lejos/code/HE ... ensor.java

Return to “NXJ Hardware”

Who is online

Users browsing this forum: No registered users and 4 guests