Todays goals:

Are as described in the lesson 5 exercises notes:

  • Exercise 1 – Self-balancing robot with light sensor
  • Exercise 2 – Choice of parameters on-the-fly
  • Exercise 3 – Self-balancing robot with color sensor
  • Exercise 4 – Self-balancing robot with gyro sensor

Additional “Optional” Goals

  • None this week

The Plan

Because of the thorough guide described in the lesson 5 exercises notes, no further planning was necessary.

Then we did the exercises as explained.

Exercise 1 – Self-balancing robot with light sensor


In this exercise we will rebuild Max as described in Philippe Hurbain’s NXTway. This makes Max a two wheeler. By using the code provided by Brian Bagnall, Max should have the ability to balance on only his two wheels for some seconds. We will investigate the software provided and try to find the best environment for Max to balance in.


Using the trial and error principle we adjust all the variables until Max is able to stand for a short period of time e.g. 2-3 sec, and repeating this process in different environments.

Because of the bluetooth calibration program that we made in Lesson 4 – Bluetooth calibration, we were able to values needed to get a viable result much faster as we could change them instantly.


The program is based on the by Brian Bagnall, the only changes that needed to be made to the code was implementing the bluetooth calibration code.

The final code can be found here, but requires the calibration program to run.


The trial and error method helped giving us a rough understanding on how the different parameters affects Max’ balancing performance. These variables are used in different parts of the calculations to get the desired output for Max’ motors:

  • Kp – This parameter is used for the proportional error. Basically the more the measured value is from the desired target, the more power is added to Max’s motors.
  • Ki – This parameter is used for the integral error. Meaning that the more Max stays tilted to one side, the more power gets added to the motors.
  • Kd – This parameter is used for the derivative error. Meaning that the bigger the difference between the last read error and the currently read error, the bigger the derivative error – meaning more power is added to the motors.
  • Scale – The scale parameter is a factor that is used for dividing the calculated value of the the earlier errors, and thereby adjusting the overall output of Max’ motors.
  • Offset – This is the desired value for the sensor to read. If the read value is lower or higher than the offset, then Max is tilting either forward or backwards.

As seen in the video, Max is not very convincing at balancing on his own. There are several different things that influence this.

  • The ideal parameters might not have been found after all (if they even exist when using a light sensor).
  • There are windows in the ceiling which allow for uncontrolled changes in the lighting. Since we are using a light sensor for balancing Max, this can cause problems with the parameters we use.
  • Max’ physical parts might be a factor in this, for instance with parts bending, backlash and unequal motor output. If the construction is not rigid when the motors run and the wheels start bending it can cause issues with the parameters we are using.

Exercise 2 – Choice of parameters on-the-fly


Once again gloat in our previous success, because as a bonus exercise in lesson 4 we made Max able to be calibrated with a PC over a Bluetooth connection. This meant we could calibrate the parameters on-the-fly.


A video from Lesson 4 showing the calibration program in use can be seen here.


As described in exercise 1, the calibration program was slightly modified to include the new parameters.


With the Java interface and the calibration program running on Max, it was easier and much faster to alter parameters on the fly and making it easier to adjust to different settings.

Exercise 3 – Self-balancing robot with color sensor


To test what difference it will make when using a color sensor to have Max balance on two wheels by swapping the light sensor with a color sensor.

We will slightly modify the program and process we used for the light sensor experiment, to now use the color sensor instead which means we will be using the same equation and calibrate the parameters until Max reaches a standstill.

Afterwards, we will rebuild Max as described in NXT Segway with Rider. When he is build we will do the same experiment as before to see how the structure affects his balancing abilities.


As in exercise 1, we used a white surface (which was the one that gave us the best results with the light sensor) and through trial, error and our calibration program, we managed to fine-tune Max to balance on two wheels.

We also changed the wheels from a convex shaped wheel to a flat wheel, which apparently made Max less affected by the construction bending. The different tyres are seen in Figure 1

LEGO tyres Figure 1: Flat tyres to the left; medium, convex in the middle; and large, convex to the right.

We also created the Segway With Rider experiment – which was tested in the same way as before, but seeing as the type of tire had an effect on the results, meant that we started by making a mini experiment where we tried three different types of wheels with the Rider setup and same variables, except for the offset, which needed to be adjusted to the change in height because of the wheels’ different sizes, as seen in the following figure 2.

Max with different wheels Figure 2: Small wheels to the left; medium wheels in the middle; and large wheels to the right.

The different wheels had an obvious effect on Max ability to balance as his balance got worse when the larger wheels were used. This is expected, as the distance covered per rotation becomes greater, meaning that this could probably be corrected through the variables. One thing that we did experience with the medium and the large wheels was that it started twisting as seen in the following videos: Max balancing on medium wheels can be seen here. Max balancing on large wheels can be seen here.


The program is the same as used in the last exercise, only with the light sensor changed with the color sensor and can be found here.


At first we had severe problems with power drain when both motors are switching rapidly between moving forwards and backwards. This is also noted in the datasheet for the motor controller. Under design notes LB1836M there is described that shifting direction may cause a misoperation due to line oscillation. To circumvent this problem, we changed the motor from Port B to Port A, since these have separate H-Bridges, effectively doubling the allowed power.

We managed to make Max balance for a whopping 20 second with the first construction as seen in this video. Unfortunately, we were too happy with our results that we accidentally closed the GUI with all the parameters on the laptop, so we cannot describe the values here. At least we still have the video!

The Segway with Rider experiment was a great success as well. We managed to make Max stand and drift a bit, as shown in this video, for several minutes. This time, though, we did have the values as seen in figure 3:

Values for Segway with Rider Figure 3: Values for Segway with Rider.

The reason for Max being better at balancing with the Rider construction might be because of the placement and angle of the color sensor when Max is on his pivot point. When Max is pivoting in the Rider construction, the color sensor is facing directly at the surface, which (as seen in previous exercises with the light and color sensors) gives the most reliable results, whereas with the initial construction when Max would be at his pivot point, the color sensor would be at a slight angle.

Exercise 4 – Self-balancing robot with gyro sensor


By using the HiTechnic Gyro Sensor we want to achieve a similar result as in the earlier exercises, but since the technology is so different from the light and color sensor, it means that we cannot just change the code used from before on a Gyro Sensor instead. The thing we can do, though, is reuse the same calculation as before. This requires a calculation of the angle of Max based on the data from the gyro sensor and then use this to calculate the error between the measured value and the desired value.

We will also need to perform a small mini experiment to see if the placement of the gyro has anything to do with the measurements e.g. at the center of rotation or above the center of rotation.

Another thing that we have to take into account is that the gyro sensor is imprecise in its measurements, meaning that Max will eventually start to either go forward or backwards. To compensate for this, we need to create a low-pass filter as described by HiTechnic here.

Lastly a small change to the calibration GUI is needed to allow for the alteration of the EMAOFFSET variable, that controls the low-pass filter.


We started by doing our mini experiment, making Max able to calculate the angle based on the data from the Gyro sensor. With this, we could then see what effect the placement would have on the values, as the calculation method would be the same.

We therefore placed the gyro in three different positions as seen in the following figure 4:

Segway rider with gyro in different positions Figure 4: Gyro placed: Left: At center of rotation. Middle: Above center of rotation. Right: On top of Max.

After running the code, we could see that the values and reactions that we got were all similar to each other. This could indicate that the placement of the gyro sensor does not have any effect on its measurements as long as the axis of measurement is facing the right way.

Why the placement has no effect is seen in figure 5.

Position of gyro experiment. The light grey box is the gyro sensor, and the dark grey is Max Figure 5: Position of gyro experiment. The light grey box is the gyro sensor, and the dark grey is Max.

As seen in figure 5, the angle between the solid red line and the dotted red line is 45 degrees – same as with the green and blue line. This shows that as long the gyro sensor pivots around the same point, the angle will always be the same.

With the information that the position of the gyro is not relevant, we could now work on getting Max to balance on two wheels using the gyro data. First, we implemented the low-pass filter to counteract the drifting caused by the gyro sensor as described by HiTechnic.

gOffset = EMAOFFSET * val + (1-EMAOFFSET) * gOffset;

We then implemented the new variable in our calibration GUI as seen in figure 6.

The modified GUI for gyro calibration The modified GUI for gyro calibration

We then tweaked the variables until a satisfactory result was reached.


There was not made a lot of changes to the program except the addition of the EMAOffset and the initial calibration of the gyro sensor.

The final code can be found here and the adjusted GUI can be found here.


As seen in this video, Max was not able to sustain his balance but was able to react correctly to the changes in his position. The reason for his lack of maintaining balance was that we were not able to find the correct variables and not completely correct for the drifting of the gyro. There are several reasons for this:

  • Since the motors and the gyro use the same battery source, the power consumption of the motors affect the readings of the gyro.
  • It was observed that when sending new EMAOffset changes via Bluetooth, the sensor values of the gyro sensor were affected and caused the values to change. This could mean that using power on Bluetooth communication will also affect the readings of the gyro.



Backlash in the construction will affect Max Noob’s performance. It is difficult to have robot stand still when the construction itself cannot.

The lighting conditions have a big effect on how well Max can balance with the light and color sensors. In a well-lit room during daytime, natural lighting contains infrared light which can interfere with the light and color sensor’s readings. This can hinder Max in staying in balance for more than one second.


Making Max balance on two wheels can be done with a lot of different sensors. In our case, the light and color sensor excel because the precision of their measurements were consistent, making it a matter of tweaking the variables to make Max balance. The gyro sensor, however being the more obvious choice, was harder to implement because of the problems with inconsistent measurements, e.g. drift.

Other than the sensor itself, the construction of Max and placement of the sensor does have an impact as seen with the rider experiment when using the light and color sensor. This, however, is where the gyro excels, since – as seen in our mini experiment – positioning of the gyro sensor has no impact.