Saturday, November 27, 2010

Compare motor controllers of LeJOS and Lego firmware

In this post I'm going to compare two motor controllers: from Lego Mindstorms firmware and from LeJOS class library. I use LeJOS in my robot programming, because it much more powerful than other programming languages I used (NXT-G, NXC). But I found a strange behavior of the motor controller in my last project and wanted to study how it works and compare it to PID-controller from default Lego firmware.


I ported Lego PID-controller to Java, made the motor's emulator using motor characteristics measured by Philo. Then I prepared a set of tests for different use-cases. I tested LeJOS development snapshot, revision 4037.

No load run
In this case we test speed up curve, speed stability and changing rotation direction. The motor rotates backwards for 5 seconds with speed of 600 degree/second, then starts to rotate forwards.
Running without external load
We can see that LeJOS controller has bigger overshot on changing direction but reaches the target speed more quickly.

Running with external load 0.1 N*m
In this case we apply constant external load to the motor. This load allows the motor to rotate with the specified speed 600 deg/sec. The load is applied in one direction.
Run with external load 0.1 N*m

Both controllers work well.


Running with external load 0.3 N*m
In this case we apply high load that does not allow the motor rotating with the specified speed 600 deg/sec.
And controllers' behavior is different. The load is applied in one direction as in the previous test.
Running with external load 0.3 N*m
 We can see that LeJOS motor does not powered at the first moment after start, but after some period of time starts working, but after changing direction again loss the power. Then after some period it can start working, but after one second of running it stops finally. LeJOS controller has so called "stall detector" that stops the motor if the regulation error is too big. Unfortunately, we cannot turn off  this detector using LeJOS motor controller interface. I turned it off in he source code and got the following diagram.
Running with external load 0.3 N*m without stall detection

We can see that LeJOS controller still works. Lego controller has big overshot at the first moment but switches direction without delay, which LeJOS controller has.


Variable load
Now we can look into how the controllers can stabilize the rotation speed under variable external load. I use sine-modulated load from 0.05 to 0.15 N*m with different frequency. In this case the external load is bidirectional like the dry friction force.
Running with variable external load of low frequency
Running with variable external load of low frequency and low speed
We can see that both controllers can keep the specified speed under low-frequency modulated external load. LeJOS controller works better.
Running with variable external load of medium frequency
Running with variable external load of medium frequency and low speed
We can see that in this case Lego controller works much worse than LeJOS one.

Holding the position against external load

In this chapter we will test locking the motor axle against external load. Lego and LeJOS controllers use different approaches to keep axle position. Lego controller keeps the rotation speed to zero, but LeJOS controller uses some kind of PWM controller to keep the current position. Therefore Lego controller does not keep the current position so strictly as LeJOS one, and the offset depends on the load value. In this test we apply the load force in one direction.

Lego FW locking diagram


LeJOS locking diagram
LeJOS controller has a bug in lock method implementation that prevent us to use custom locking power. It always use locking power 30 percent.

I also tested locking mechanism against variable external load.
Lego locking diagram. Variable external load.

LeJOS locking diagram. Variable external load
It seems that Lego approach works better because position oscillations are smaller.

Smooth acceleration
Smooth acceleration one of features of LeJOS motor controller. Lego firmware have similar feature that allow to control acceleration an deceleration explicitly. But I found this approach not convenient and implemented smooth acceleration in my Java version of Lego controller.
I found that LeJOS implementation have a serious bug that makes this feature much less useful that it could be.
I use two scenarios for testing this feature:
The first scenario:
        motor.smoothAcceleration(true);
        motor.setAcceleration(300);
        motor.setSpeed(300);
        motor.forward();
        <wait>
        motor.setSpeed(600);
        <wait>
        motor.setSpeed(100);
        <wait>

Lego implementation of smooth acceleration

LeJOS implementation of smooth acceleration
LeJOS diagram shows that LeJOS controller resets the smooth acceleration data at speed change.

The second scenario:

        motor.smoothAcceleration(true);
        motor.setAcceleration(50);
        motor.setSpeed(600);
        motor.forward();
        <wait>
        motor.setAcceleration(300);
        <wait>
        motor.setSpeed(100);
        <wait>
        motor.setSpeed(600);
Lego implementation of smooth acceleration
LeJOS implementation of smooth acceleration
We see the same behavior at acceleration change. It seems LeJOS controller resets its state when speed or acceleration is changed. Also we can see that LeJOS interprets the specifies acceleration value incorrectly, because LeJOS controller's acceleration curve has less inclination than in my Lego-based implementation. Ff we set acceleration value of 300 degree/sec/sec we expect that after one second period of time the rotation speed will be 300 degree/sec, but LeJOS controller does not provide such behavior.

The third scenario:
        motor.smoothAcceleration(true);
        motor.setAcceleration(300);
        motor.setSpeed(speed);

        motor.backward();
        <wait>
        motor.forward();
        <wait>



In this scenario I tested how the motor controller handles changing direction. I implemented a special control algorithm that can change direction smoothly. It seems that LeJOS motor controller does not handle this case correctly.

Conclusion
This comparison shows that LeJOS motor controller provides great control of the motor, in most cases it is better than Lego FW controller. But LeJOS controller has a few bugs that prevent its usage in some cases. So I currently try to implement alternative variant of motor controller.
I have some suggestions to LeJOS motor controller authors:
1. fix smooth acceleration behavior
2. allow to turn off the stall detector
3. fix the bug in lock method that prevent me from changing the lock power.

No comments:

Post a Comment