Sejway issues

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

Moderators: 99jonathan, roger, imaqine

Sejway issues

Postby ruperty » Sat Sep 14, 2013 12:16 am

I am trying out the Sejway from Maximum Lego NXT (2007), and am encountering some problems.

It seems to balance some of the time (though runs away), for a second or two, but often the motors just seem to freeze for about half a second, seemingly around the balancing point. I notice a previous post on this, motor freezes, to do with large changes in the power. Doesn't the change in direction represent such a change?

My implementation has a couple of differences to the original, the hardware has the rechargeable battery pack, and I have updated the code slightly due to the latest version of lejos. I also notice that there is a new edition of the book, so have I missed something with the various changes?

Here's the code I am using:

Code: Select all
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.LightSensor;
import lejos.nxt.MotorPort;
import lejos.nxt.NXTMotor;
import lejos.nxt.SensorPort;

public class Sejway {
   
   // PID constants
   static final int KP = 28;
   static final int KI = 4;
   static final int KD = 33;
   static final int SCALE = 18;
   
   // Global vars:
   int offset;
   int prev_error;
   float int_error;
   
   LightSensor ls;
   NXTMotor mb ;
   NXTMotor mc ;
   public Sejway() {
      ls = new LightSensor(SensorPort.S2, true);
      mb = new NXTMotor(MotorPort.B);
      mc = new NXTMotor(MotorPort.C);
   }
   
   public void getBalancePos() {
      // Wait for user to balance and press orange button
      while (!Button.ENTER.isDown()) {
         // NXTway must be balanced.
         offset = ls.readNormalizedValue();
         LCD.clear();
         LCD.drawInt(offset, 2, 4);
         LCD.refresh();
      }
   }
     
      public void pidControl() {
         while (!Button.ESCAPE.isDown()) {
            int normVal = ls.readNormalizedValue();
           
            // Proportional Error:
            int error = normVal - offset;
            // Adjust far and near light readings:
            if (error < 0) error = (int)(error * 1.8F);
           
            // Integral Error
            int_error = ((int_error + error) * 2)/3;
           
            // Derivative error:
            int deriv_error = error - prev_error;
            prev_error = error;
           
            int pid_val = (int)(KP * error + KI * int_error + KD * deriv_error) / SCALE;
           
            if (pid_val > 100)
               pid_val = 100;
            if (pid_val < -100)
               pid_val = -100;
           
            // Power derived from PID value:
            int power = Math.abs(pid_val);
            power = 55 + (power * 45) / 100; // NORMALIZE POWER
            mb.setPower(power);
            mc.setPower(power);
           
            if (pid_val > 0) {
               mb.forward();
               mc.forward();
            } else {
               mb.backward();
               mc.backward();
            }
         }
      }
      public void shutdown() {
         // Shutdown LightSensor, motors
         mb.flt();
         mc.flt();
         ls.setFloodlight(false);
      }
     
      public static void main(String[] args) {
         Sejway sej = new Sejway();
         sej.getBalancePos();
         sej.pidControl();
         sej.shutdown();
      }
}
Regards,
Rupert
ruperty
Novice
 
Posts: 71
Joined: Sun Oct 14, 2007 11:21 am

Re: Sejway issues

Postby gloomyandy » Sat Sep 14, 2013 6:55 am

Hi,
I notice that you are using the light sensor to balance the robot. I don't think you will get a very stable balance using this. Most of the balancing robots you see will be using a gyro. So I don't think there is anything wrong, it will just not work so well, and to get it working you will have to fine tune the settings a lot, especially if you are using different batteries etc. Yes the motors turning off is something you see a lot with balancing robots when you are trying to get things working!

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

Re: Sejway issues

Postby ruperty » Sat Sep 14, 2013 9:28 am

Thanks. What is it that is causing the motors to turn off? Is it the (large) changes in the power setting?

I tried with a constant power,

power = 55;
rather than
power = 55 + (power * 45) / 100;


where pid_val is only used for direction. The balance control actually seemed better, and I didn't notice motor freeze. What happpens with the speed of the motors when changing direction, I presume they must go through zero from 500ish forward to 500 backward? Why doesn't this cause motor freeze but power change does?

The book suggests the tilt sensor, should this also work? I assume it means the HiTechnic Accel sensor (aka Accelerometer / Acceleration / Tilt). I guess this would also need the initial balance offset calibration.

What is the advantage of using the gyro sensor? Is it that it would not need the balance calibration, as the target would be zero (velocity)?
Regards,
Rupert
ruperty
Novice
 
Posts: 71
Joined: Sun Oct 14, 2007 11:21 am

Re: Sejway issues

Postby gloomyandy » Sat Sep 14, 2013 11:29 am

Don't try and use an accelerometer I don't think you will get it to work. There are a lot of threads on various forums giving the reasons why. A gyro is just a much better sensor for this purpose as it measures exactly what you need to deal with (speed of rotation) and produces readings that are consistent. Using a light sensor does not produce a linear relationship between rate of rotation and light intensity and is subject to all sorts of outside interference. The motor shutdown is caused by the motor drivers becoming overheated. The maximum current draw for a motor is when it is stalled, for a balancing robot the motor spends a lot of time in an almost stalled condition, so it is very easy to cause an overload.

The most recent version of the book presents a much better balancing robot using a gyro. The code for this is now a standard part of leJOS (look in the samples).

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

Re: Sejway issues

Postby ruperty » Tue Sep 17, 2013 7:33 pm

Thanks, I've tried the Segoway class,

Code: Select all
public static void main(String[] args) throws InterruptedException {
      NXTMotor left = new NXTMotor(MotorPort.B);
      NXTMotor right = new NXTMotor(MotorPort.C);
      GyroSensor g = new GyroSensor(SensorPort.S4);
      Segoway seg = new Segoway(left, right, g, SegowayPilot.WHEEL_SIZE_NXT1);
      Button.waitForAnyPress();
      seg.interrupt();
   }


and it works very well. I would be interested to know how it finds the balance point initially. My reading of the code is that the variables (gyroSpeed, gyroAngle, motorPos, motorSpeed) in the balance equation start out at zero, but that doesn't tally with the calibration movements if leant against the wall. Any clue to what is happening during the calibration to get the initial values?
Regards,
Rupert
ruperty
Novice
 
Posts: 71
Joined: Sun Oct 14, 2007 11:21 am


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff