Deprecated: Function eregi() is deprecated in /public/sites/ on line 51
Self-balancing Segway NXTway-Gyro bot & wireless PS2 controls | My NXT projects
Home Segway NXTway-Gyro Bot
Bookmark and Share

Who is online?

We have 42 guests online
Where do visitors come from?
Locations of visitors to 

this page

Visitors in 2009:

Visitors in 2008:

page counter:

PDF Print E-mail
Written by   
Friday, 13 March 2009 00:00

Self-balancing Segway NXTway-Gyro bot & wireless PS2 controls

In one of my first NXT projects I rebuild the NXTway design by Philippe E. Hurbain's. This two-wheel bot was self-balancing using a light sensor. Other people came up with the idea to balance such a robot with a Gyro sensor: Ryo Watanabe made his NXTway-G, Ramin made his self-balancing robot, Yorihisa Yamamoto ( made his NXTway-GS and Engscope made his Jegway. Just because I am fascinated by the fact that a two-wheel bot can stay up (like a human!) I wanted to make my own self-balancing robot with a wireless remote joystick controller.

Here is a video of my remote controlled NXTway-gyro performing a simple test over a tilt surface with small NXT wheels:

Click here to watch in normal quality/limited bandwidth
Running my latest NXC program my bot is quite stable and runs smooth.....

The orginal Segway PT (personal transpoter) is a two-wheeled self balancing electric vehicle invented by Dean Kamen. The Gyroscopic sensor is used to detect tilting.

The Gyro sensor
A Hitechnic Gyro sensor contains a single axis gyroscopic sensor that detects rotation and returns a value representing the number of degrees per second of rotation. The Gyro Sensor can measure up to +/- 360° per second of rotation. The rotation rate can be read up to approximately 300 times per second. The Gyro sensor is used to measure size of the angel in a very short interval (let say 100 times per second). This way it can determine if the bot is falling, in which direction and how fast it is falling.

This video shows how the NXTway-Gyro bot is self-balancing (without PSP remote control):

Click here to watch in normal quality/limited bandwidth

The bot is falling back- and forward. But the bot can be much more stable like this as I will show you in other video's.
The basic functions of a Seyway bot

The orginal LEGO Mindstorms NXT-G software is quite slow and it may not be fast enough to program your bot as a Segway with remote control. My NXTway-Gyro Segway self-balancing NXT Mindstorms robot is using a Hitechnic Gyro sensor with NXC software based on JanB's program (software that is possibly related to RobotC source code made by Ramin (gyro_rk4_v4.c).  It is a very small NXC program is doing the following tasks:
- Determine the Gyro bias (offset), because the gyro sensor value can be influenced by temperature and manufacturing tolerances.
- Prevent the robot from falling over (using by the body tilt control).
- Prevent the robot from driving away (using the position control).
- Allow the robot to balance for long time (using the gyro drift compensation).
- Set the friction compensation.
- Optional: read the joystick values of the PSP controller.

Gyro Bias offset
Before you can use the value of the Gyro sensor you need to determine the Gyro Bias offset. This is different for every Gyro sensor and can be influenced by temperature. You must held the the sensor absolute still for a few seconds and do a couple of dozen Gyro measurements. The Gyro Bias is the sum of the measurements divide by the amount of sample you made.

line    statement                          comment
 1 timer1 = CurrentTick();                 //set timer1 equal to the system clock tick.
 2 GyroBiasCount = 0;
 3 while (CurrentTick() < (3000+timer1)) { //loop until 3000 ms have past.
 4     Gyro_value = SensorRaw(S3);         //read out the Gyro sensor on port 3#.
 5     Wait(150);
 6     GyroBiasCount = GyroBiasCount + 1;  //count the times a sample is made.
 7     gn = gn + Gyro_value;               //sum the Gyro values.
 8     PlayTone(TONE_B7, 5);               //play a sound to give notice of Gyro Bias process.
 9 }
10 GyroBias = gn / GyroBiasCount;         
//calculated sum of values divide by times measured.

 A sound is played, to notice you that the bot must be held still. Gyro measurements are made every 150ms.

Main part program - balancing task
Here is the main part of the NXC program. It contains only the basis functions for a Segway bot:

    statement                                                                      comment
 11 while(true){                                  //begin of a continuously loop
12   ng=SensorRaw(Gyro sensor port)-gn; nw+=ng;  //body angle in radians/sec
 13   rp=MotorTachoCount(Motor port);             //wheel position
 14   rg=rp-lrp; lrp=rp; rwi+=rg;                 //wheel speed
 15   if (abs(rwi)>15){gn-=sign(rwi); rwi=0;}     //Gyro drift compensation
 16 //md=(nw+ng*10+rp*4+rg*200)>>4;               //pd control NXT small wheels
 17   md=(nw+ng*12+rp*5+rg*360)>>5;               //pd control RCX large wheels
 18   md+=sign(md)*48;                            //default friction compensation = 48
 19   SetOutput(Motor port A & B,                 //PID motor control
          md,                                     //calculated correction
 20   Wait(8);                                    //loop about 100 times/sec
 21   }

The program is a continuously loop. It runs about 100 cycles per second.

Body angel
In line 12 the body angel is calculated just by adding the Gyro value to all the previous Gyro values.

Wheel turning
In line 13/14 the wheel speed is calculated just by subtracting the turning of the wheel by the previous turning of the wheel (measured in the last cycle).

Different wheels & PD control
In line 16 or 17 the motor power is calulated with PD (proportional and derivative) control. If you wish to use small NXT wheels (part no:55976/54087) instead of large RCX wheels (part no: 2903/2902) you can move the comment '//'  from line 16 to line 17.

Friction compensation
An other value you can change is the friction compensation in line 18. If you use batteries instead of a (NXT) LiIOn-Accu, the motors get a higher voltage. You may need to reduce this value a bit.

In the video above I use a value of 44. When you decrease the friction compensation to 42 the bot will become more instable and moves further for- and backward:

If you decrease the friction value to 38 it will fall:

If you increase the friction value to 46 the bot is much more stable:

But if you increase the value to 52 it move to quick for- and backward and it will fall:

The optimum value of my bot seems to be 48:

You can download the program without PSP remote control here: nxtway-gyro.nxc (last update 2009/09/18 - adapt to firmware 1.28 and higher).

PSP remote joystick control
For the remote control I used the Mindsensors PSP-Nx-v3 controller with a SONY playstation 2 (PS2) wireless joystick. A Mindsensors PSP-Nx-v3 wireless Playstation 2 controller controls your NXT robot with a PS2 joystick. This is an interface for a wireless SONY Playstation 2 Controller which you may already have. The controller has two analog joysticks and 16 buttons, using this interface, values of all 16 buttons and joysticks can be read by the NXT over the wireless link.

psp controller task
A simple task called psp_controller (line 22 - 40, see below) runs 5 times a second. It reads out and displays the joystick values. It runs until the finshes its last statement in line 39. The whole task takes about 35 ms.

To control the NXTway-gyro bot I use the left joystick. The Y-axe value (line 24 and 34) controls the bot's forward/backward movement. The X-axe value (line 25 and 35) controls the sidewards direction. The raw joystick values can go from -100 to +100. I reduce these values a bit (line 34/35) to prevent that the bot will react to quick and fall over.

line    statement                           comment
22  task psp_controller()
23  {    PSP_ReadButtonState(SensorPort, ADDR, currState);
24       move = (currState.l_j_y);           //read out left joystick - Y-axe
25       turn =  - currState.l_j_x;          //read out left joystick - X-axe
26       // Display the joystick values on the NXT screen
27       message = "y:       x:          ";
28       messageA = NumToStr(move);
29       messageB = NumToStr(turn);
30       message = StrReplace(message, 3,  messageA);
31       message = StrReplace(message,12 , messageB);
32       TextOut(0, LCD_LINE7, message, false);
33       //return the values to the bot balancing task
34       m=move / 16;                        //divide joystick y-axe by factor
35       t=turn / 5;                         //divide joystick x-axe by factor
36       // During the psp_controller task, statements of the balancing task are executed slower.
37       // The processor of the NXT is serving 2 tasks at the same time after all.
38       // Therefore, at the end of the psp_comtroller task, the cuclus delay is set back from 4 ms to 7 ms.

39       t1=7;                              //set cyclus delay back to 7 ms
40    }

To add the direction control to the bot, the following lines in the main balancing part (the balancing task) are added/modified. The variables 'm' & 't' are the joystick values. The variable 'count' is a counter that counts the number of cycles, so we can launch the psp_controller task every 20 cycles.

line    statement                        comment
13   rp=(MotorTachoCount(Motor port A)+MotorTachoCount(Motor port B))/2; 
14a  M = (M*75+m*25)/100;          //To let the bot lowly get used to the for- and backward movements 
14b  rg=rp-lrp; lrp=rp; rwi+=rg-M; //wheel speed + joystick forward value
19a  SetOutput(OUT_A,              //PID motor control motor A
               md-t,               //balance correction - joystick turn value.
19b  SetOutput(OUT_B,              //PID motor control motor B
               md+t,               //balance correction + joystick turn value.

20a  if (count==20)                //ones in 20 cycles (= 5 times per second)
20b     {
20c       count=0;
20d       // During the task psp_controller the balancing task runs slower.
20e       // Therefore the cuclus delay is set from 7 ms to 4 ms.

20f       t1=4;                     // set cyclus delay to 4 ms
20g       StartTask(psp_controller);// set joystick values m & t
20h     }
20i  count+=1
20j  Wait(t1);                     //Delay to slow down the loop to about 100 times/sec (default = 7ms)

In line 14a the forward/backward movement is calculated so it will always increase or decrease slowly.

The NXTway-gyro bot turns by moving the wheels in opposite directions (line 19a and 19b), depending of the variable 't'. This shouldn't change the body angel and therefor the wheel position measured in line 13 depends on both wheels. The forward/backward movement M is added in line 14b.

During the psp_controller task, the balacing task runs a bit slower. This is because the NXT processor must process two tasks at the same time. It is important that the balacing task runs 100 cycles per second no matter what. So we have to adjust the Wait(8) statement in line 20 while the psp_controller task is running.  The variable t1 determine the number of ms delay (line 20j).

Ones in 20 cycles, the psp_controller task is launched (line 20g) and the t1 delay is set to 4 ms (line 20f). At the end of the psp_controller task t1 is set back to 7 ms (line 39).  Notice that in the orginal program (nxtway-gyro.nxc) the delay in line 20 was 8 ms, but because of the number of lines added to this program (to make joystick control possible) a delay of 7 ms is enough.

Watch my NXTway-gyro controlled by a PS2 joystick:

Click here to watch in normal quality/limited bandwidth

Download the NXTway-Gyro program with SONY PSP joystick controls here: nxtway-gyro-psp.nxc (last update 2009/09/18 - adapt to firmware 1.28 and higher). Don't forget to download the PSP-Nx-lib.nxc library, available at Mindsensors.

Here is a video with my NXTway-gyro performing on a tilt surface:

You can see that the bot has no problem to run over slope table.

Programming in NXC
You may not have experience with NXC language for LEGO NXT mindstorms. I suggest you to try it out!
NXC (Not eXactly C) is similar to C and contains the same functions as LEGO Mindstorms NXT-G (and more)  to control your NXT. The character based program language is very flexible, easy to learn and way much faster. The source size of a NXC program (which exist on your PC) is more then 1000 times smaller as a similar NXT-G program and the executable size (the compiled version which run on our NXT) is about 10 times smaller as a NXT-G program. NXC fully supports NXT sensors made by LEGO, Mindsensors, Hitechnic, Codatex and Vernier, but it also support homebrew sensors.
You can run NXC programs on the standard LEGO NXT-G firmware, but to have full bennefit of the NXC language you must flash your NXT with a NXC/NBC enhanced firmware (real easy!). On this firmware you can still run your NXT-G programs. It is fully compatible with the standard LEGO firmware! (And you can always flash your NXT with the original LEGO NXT-G firmware.)
NXC comes with the Bricx Command Center (BricxCC, a professional programming and development tool for your NXT, both made by John Hansen. It run on every Windows (95, 98, ME, NT, W2K, XP, Vista) PC. John Hansen and Danny Benedettelli also wrote very usefull NXC documentation. And (unlike RobotC) NXC and BricxCC are for free!

Note for NXT 2.0 users
The hardware of NXT 1.x (set #8527) and NXT 2.0 (set #8574) Bricks are 100% identical. It's the NXT Brick firmware and the LEGO Mindstorms NXT PC software that are different. Firmware level 1.28 or higher is known as NXT 2.0 firmware. You can install NXT 1.x firmware (levels up to 1.27) and NXT 2.0 firmware (firmware levels starting from 1.28) on a NXT 1.x as well as a NXT 2.0 Brick. (Yes, if you upgrade the NXC/NBC  firmware on your NXT 1.0 brick, you will have a NXT 2.0 brick!!!) If you use the LEGO Mindstorms NXT PC software 2.0 you must run 1.28 firmware (or higher) on you Brick (the LEGO firmware and the NXC firmware use the same firmware levels). The new NXT 2.0 is compatible with old programs: both NXC and NXT-G 1.x programs run on NXT Bricks with 1.28 firmware or higher.
Here you can download the latest NXC/NBC BricxCC with the 1.28 (NXT 2.0) firmware: Install this firmware from the BricxCC GUI.
And here you can get the orginal LEGO 1.28 firmware (NXT2.0): (in case you want to restore the 2.0 NXT brick in it's original state. Install the LEGO firmware from you LEGO Mindstorms NXT PC software.

Lego Digital Designer
Jean-Pierre Lammers, New Albany (Ohio, USA), a student of David Herman (teacher at New Albany High School), performed a great job. He "reverse-engineered" my NXTway-gyro model with Lego Digital Designer (LDD), a free LEGO 3D CAD tool that let you build with bricks on your computer. With LDD (in Building Guide mode, using the Builing Guide Player) you get a building instruction which you can use to rebuild my NXTway-gyro.

If you want to look at the model from any point of view or you want to rebuild it, you can download Jean-Pierre's LDD file here: nxtway-gyro-psp.lxf

Build this bot
If you want to build this NXTway-gyro bot you need a LEGO NXT Mindstorms set (9797-1, 8527-1 or 8547-1) and a Hitechnic Gyro sensor. If you want to control it using a PS2 joystick, you need a Mindsensors PSP-nx-v3 controller.

Check the complete Self-balancing Segway NXTway-Gyro NXT robot high resolution photo gallery at Google Picasa...

Copyright © 2014 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.
Powered by WebRing.