NXT arduino Balancing PID PLEASE HELP

Hi guys, I come here for some advice, I will appreciate if some nice people will answer me on my post.

I build a balancing robot from arduino and nxt motors, everything works great just PID not so much. I am trying to tune it already one week and the best results which i got are:

but still happens that the robot sometimes falls down and is not in stability. I used I think all ways of tuning Ziegler Nichols also . I found from graph period of Oscillation (0.67s) and calculated all values. But it did not work properly.

Sometimes happens that he is standing properly, afterward he tilts little bit in front but he correct it, after he tilts more backwards and he has to correct it much more but he still do it, and after he tilts again forward but he falls already. So basically he creates oscillation which is increasing. It looks to me like that I term of PID is changing to slow. Can it be?

Or do you have some advice how can I improve PID constants?

I want to ask you guys to some advice, I have to present this robot soon and I need little better results.

I am really thankful for all your answers. All my code is free in GitHub(youtube video description)

Thank you so much Juraj!

Guys I am really bagging you for help! I cant move on! If you would give me some document where I could find some info. I searched on internet a lot but everything what I found did not help!

I need someone who has experiences and knows what to do, I am sure here are lot of this people!

Thank you

If it''s oscillating then the derivative term is probably too low.

Thank you, I appreciate your reply!

The problem is that it doesn't oscillate fast, I mean it tilts in front, it corrects it, but it doesn't catch already the opposite tilt.


I tried make D higher but it comes just to much bigger osculations around setpoint. It is quit stable around setPoint now but when it tilts little more than it falls.

What do you think, can not it be that when I can be max 255 that it is to much and it cant change direction so fast?

Maybe you're just running out of power - are you getting anywhere near full power when it remains balanced?

Do you know what the control frequency is relative to the frequency of the wobble? You need to be able to measure and control it far faster than it responds. That's quite a small device and will be inherently hard to balance. I suggest you make it taller, so that it is slower to respond and easier to balance, at least until you have got something that can balance. You can always reduce the height later once your balancing code is working better.

Thank you for ideas.

I think the power is not the problem because sometimes it is able to balance properly. I am using 9V battery, true is that it use all capacity of battery in around 20 min.No idea why.

Yes sure I will try to make it taller, is made from lego so it will be easy task.

What i was thinking also as you said, the response is slow, but I have no idea where can be the mistake or how can I improve it.
The rate for PID i choose 30ms what has to be enough and I tuned off PID for motors so there should be no delay. MPU6050 uses interrupts from DMP so it has to be also enough fast. I was testing it with that GUI which is see-able on video how fast the motors can response on speed change from positive to negative numbers and i was completely satisfied. I am sending to GUI data every 40ms so Serial.println does not make it slower.
Maybe could you check my code and maybe also MotorNXT library which i made and give me some ideas how could I make it faster to response?

Thank you so much for your time!

I am using 9V battery, true is that it use all capacity of battery in around 20 min.No idea why.

Probably something to do with a capacity of less than 700mAh

I am using completely normal 9V alkaline battery. This motors need exactly 9v. Maybe i can use lager one and use some transistor to reduce to 9v or do you have some better idea? But than the robot would be to heavy and i am afraid that the motors would not be able balance it anymore.

You'd be better off with six AAs than a PP3

I think the power is not the problem because sometimes it is able to balance properly.

In order to maintain stability you need to have enough power in reserve. You need to find out how much power it has in reserve i.e. what the range of PWM output values is - if it's getting anywhere near to 100% power then it would be very vulnerable to any further disturbances.

I am writing in gui also output values and mostly they are around +-255.But this cause PID.I am using L293d so I can use all range of PWM.and when it balance the values are jumpingwith huge diferences.

Maybe i have to say i dont know exactly what you mean with range of power and if i have something in reserve could you please describe it to me more close what ypu mean?

Thank you so much for your help and time! :stuck_out_tongue: