motor setSpeed() changes AD sensor value

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

Moderators: 99jonathan, roger, imaqine

motor setSpeed() changes AD sensor value

Postby kirkpthompson » Mon Jul 26, 2010 3:11 am

Hi All. I found out something interesting that I wanted to share since I spent many hours debugging until I figured it out.
    NXT w/ LIon battery pack
    LeJos 0.85
    HiTechnic Gyro sensor NGY1044

I have a Gyro class that I have been working on that samples and calculates the bias value based on values that are "close" for a set number of consecutive samples. If the sensor is jostled (i.e. if the robot moves at all), the consecutive values will vary greater that 1 and the sampling is restarted until things are still. This allows me to dynamically determine the nominal bias/offset value to subtract from the raw sensor value to provide a reading relative to zero. I have found that the zero value shifts slightly over time so that is why I created this algorithm to provide a variable bias/offset.

Another class extends this one and provides a simple integrator to provide relative angular headings/vectors. It depends on a zero sensor reading when the sensor is still or drift will occur.

Everything was working ok until I coded to run a motor with a PID controller to test. The integrator would drift after setSpeed() was called. After many hours of banging my head, I finally isolated the cause: The sensor value (from the ADC) changes by 2 after setSpeed() is called. Here is my test case:
Code: Select all
package net.mosen.poc;

import lejos.nxt.ADSensorPort;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.SensorConstants;
import lejos.nxt.SensorPort;

public class setSpeedDebug implements SensorConstants{
    public setSpeedDebug() {

    public static void main(String[] args) {
        setSpeedDebug setSpeedDebug = new setSpeedDebug();
    private void doTest() {
        ADSensorPort port;
        Motor mot = Motor.A;
        port = SensorPort.S1;
        port.setTypeAndMode(TYPE_CUSTOM, MODE_RAW);
        while(!Button.ESCAPE.isPressed()) {
            LCD.drawString("raw: " + port.readRawValue() + " " , 0, 5);
            if (Button.ENTER.isPressed()){
    protected void doWait(long milliseconds) {
        try {
        } catch (InterruptedException e) {
          // do nothing

Observe the readings when the sensor is still. Press the Enter button and observe them drop by 2. I am guessing this has to do with voltage levels changing when setSpeed() is called.

What was happening is the "closeness" test failed because the new values after setSpeed() was called fell outside the "closeness" window I defined (1 unit shifts based on my previous empirical test data). The sensor was still but since the bias value didn't zero out the new shifted AD value, drift occurred. The solution was to recalc the bias after setSpeed(0) was called.

Anyways, thought I would share in case anyone is having weirdness in expected sensor readings.

Leg Godt!
User avatar
leJOS Team Member
Posts: 304
Joined: Wed Dec 05, 2007 1:27 am
Location: New Mexico, USA

Return to NXJ Software

Who is online

Users browsing this forum: 9guquaox and 1 guest

more stuff