Self-balancing robot...

A video of Ramin's version of a self-balancing bot using the Gyro sensor has been posted... Ramin has also offered to share the code used to create it.


Anonymous said…
Hi nice job. Can you publish more detail,
Anonymous said…
Impressive! How do one get a peek at the source?
RawMean said…
You can get the RobotC code from my website:

I have added some comments to make it more readable.


Gaurav said…
This comment has been removed by a blog administrator.
Anonymous said…
Very impressive. Would the code be available in NXT-G?
RawMean said…
I find NXT-G somewhat difficult to work with (not flexible enough), but the RobotC code is very simple and you should be able to convert it to NXT-G yourself.

Ethan Steckmann said…
SOme one has allready made a segway with nxt-g then someone else improved on it.

link 1 is the mod and link 2 is the orignal



if any one gets it so that you can drive the nxt around using nxt-g rather then just having it balance.

2-balance with nxt-g-done
3-balance with nxt-g and drive around-waiting.

lets see who can do step 3

Anonymous said…
I'd love to have a look at the actual progam that was used in the video. Unfortunately, the program above won't work, because it's reading the "Sensor Value" and compairing that to 600, however the Sensor Value is not on that scale. I assume it should read "Sensor Raw".
RawMean said…
The URL to the "actual" code is what's given above. The output of my gyro sensor (i.e., SensorValue(GyroSensor)) when stationary is ~600.

What is the output of yours when stationary.

I suggest you comment out the following line, then th eprogram will measure the output of the gyro for 3 second and uses that value as the bias (there is comment in the code that explains this).

GyroBias = 600.5;

Hope this helps,

Anonymous said…
Sorry, Ramin. Maybe I'm missing something. The value returned by SensorValue for my gyro sensor is ~43. The raw value returned is ~592.

I don't know if it's possible to configure RobotC so it returns the "raw" value, when you request the "SensorValue", but that may be what I need to do.

When I changed the "SensorValue" functions to "SensorRaw" in the above program, it works.

Can you tell me what I'm missing?

Anonymous said…

That is some crazy math! Way beyond my capabilities. Thanks for the comments so i can understand what is going on.

Very impressive!
RawMean said…

not sure why the output of your gyro sensor is ~43. See the following code from It suggests that the offset should be around 600 as well.

const tSensors GyroSensor = (tSensors) S1; //gyro sensor//

#define offset 598 //offset value may vary depending on sensor & temperature
long Gyro_value;

task main()
Gyro_value=SensorValue(GyroSensor)-offset; //read the gyro sensor

HiTechnic said…
The HiTechnic Gyro sensor gives a value close to 600 when at rest. This value may vary from sensor to sensor and will change due to temperature and other factors so you may see a value somewhere in the range of ~595 to 605. It is best to calculate the base output each time the program is run by reading the sensor 10 or 20 times and then averaging the output. Note that during this "calibration" the gyro sensor must be kept absolutely still.
Steve -
rade95 said…

any chance to publish the remote control portion of the code? thanks a lot.

-- ray
RawMean said…

The complete code is in the URL above. There is no remote control. The robot is completely autonomous and avoids obstacles on its own using the sonar sensor.

rade95 said…

That is very very cool. I will give it a try. Thanks a lot. You've done an amazing job.

-- ray
Anonymous said…

First of all, thanks for sharing your code with us. Second, I have try it and unfortunatelly it doesn't work for me. Maybe could be the physical differences between my lego implementation and yours or perhaps something else.

I have been debugging a bit and I've found some strange things:

1. When I debug the x_dot variable (linear speed) it show a very high value (tipically 450). It's like the speed change only from 1 to 9!?.

2. The theta value doesn't reflect the real tilt value and grows very fast.

The results is that the f_pwr grows also very far and the robot falls down almost inmediatelly.

It would be helpful to know which procedure you use to adjust the k1,k2,k3,k4 variable.

Thanks in advance for your answer.

rade95 said…
I am getting similar results as Mon. I changed SensorValue function to SensorRaw, so now the gyro reading is in 604 range, but the robot just spins its wheels. It does not balance at all. If anyone has any idea how to fix Ramin's code, please e-mail me at Thanks.

// ray
rade95 said…
Hi again,

First I want to apologize to Ramin, because his original code works just fine. My problem was that my gyro was facing the wrong way, so since it is a single axis gyro it didn't work. Now it does, because it is facing the right way. The code works just fine with both SensorValue and SensorRaw functions. Although the robot balances well for over 5 minutes, there are 2 small issues that would require fine tuning. First the jitter. The robot balances but jitters little too much. I see that in Ramin's demo his Bot didn't jitter quite as much. Ramin, if you can advise what parameter to change to reduce the jitter, I would greatly appreciate it. I tried several but didn't quite fix it yet. The second is velocity while balancing. My Bot has no velocity during balancing, so I need to make the Bot move forward slowly. Easy. Here is the link to my youtube video showing the Bot balance and sorry for the long (boring) intro.

My next thing is to add PSP-Nx based remote instead of an ultrasonic sensor, so I can move the Bot forward backward and make turns using a PS2 game pad.

Thanks Ramin again for an excellent code you wrote.fcaqtu
Anonymous said…
Hey Ramin,

Great code. I've been trying it out but I have one problem. After I've update the bias it seems to balance but after about 30 seconds the motors seem to lock up so the robot freezes and than just face plants probably due gravity. Any advice you have would be greatly appreciated. Thanks in advance.

- Tony
Unknown said…
hey guys I'm the guy that made the modified version of the balancing robot Ethan talk about. Well if you didn't know I am now working on the gyro version with nxt-g so if you might want to look at the code send me an email at (
Albert said…
Hi! Very nice job!

But sorry, the url you put doesn't work. If you can repair it, thanks a lot!

Anonymous said…
Hello. Impressive work. I was wondering if you Ramin or anybody else have the control variables. How the K gain values were obtained. THanks
Anonymous said…
Hi, Ramin!

Very nice code. Works like a breeze for me.

Could you, please, help me with some doubts?

Could you explain in more detail the meaning of the k1, k2, k3, k4 and k_e constants?
How did you arrive at the values you used?

Could you explain a bit about the Runge-Kutta (RK) integration?
I see only two terms: one that uses angular velocity at tn and one that uses it at tn+h/2. Shouldn't there be at least one term that uses the angular velocity at tn+h?

Thank you!
Unknown said…
for begun i'm sorry for my poor english i'm not very strong in this language. A big Thank for the code it's a very very nice job. i have problem for controlled my robot can you help me? i'm working with robotC and if you want to see videos of the robot write tipso100 on youtube
nuno bernardo said…
Hi Ramin!

Incredible piece of code, tried it and works like a charm!
Anyway i was going thru it trying to remove the Sonar Sensor part of the code and still make it work but i had no luck so far.. seems that without that specific part the program just stops working.. can you help me out with this? Cheers!
Anonymous said…
This comment has been removed by a blog administrator.
Saša said…
link is not working. I have tried several times in last week. Can you please update your link.

Thank you, that you are willing to share.
Unknown said…
Hi Ramin
I have a problem with the link. I want to know if u can send me the code it well greatful if u send me the fast as you can
Thanks you , I will wait for your message

Popular Posts