Page 1 of 1

G-Tune Mode (ZERO PIDs)

Posted: Mon Jun 02, 2014 8:53 pm
by MHefny



This topic is about PID auto tuning algorithm that can tune your Quadcopter from PIDs ZERO as a start value.
Introduction
Adjusting PIDs is a problem for many of Quadcopter’ pilots, it would be nice to get your quadcopter to adjust PIDs for itself. I made some investigation in this area, and found many complex algorithms that are related to Adaptive PIDs, and Fuzzy Logic. Although my background is engineering but I don’t like complex math when you can make simple solutions :)
I started with multiwii version 2.3, I chose it because its code is not that complex, while it has many GUI tools and debug facilities to trace, and it has one of the largest community that can test and verify the results. Anyway I have developed a new mode that I called G_TUNE, which enables quadcopter to calculate flyable PIDs values for ACRO mode. i.e. PIDs for GYRO.
How to Use
G_Tune mode starts by resetting all Gyro' PIDs for Roll, Pitch & Yaw to Zero’s. Then pilot needs to takeoff !! yes it will take off as the algorithm will start instantly to correct errors and update PIDs parameters accordingly. Pilot should try not to make aggressive maneuvers. That is because the error in multiwii is calculated as (RC_Stick - Gyroreading) as stick position acts as rotation speed target i.e. the target rpm, so moving stick suddenly generates false errors.
The algorithm mainly works as our human eye. what you do is that if you find your quad tilt you give thrust to motor to adjust this tilt.


Caution
1- This is a test mode that I tested only on my quadcopter with Simonk ESC, 10x4.5 props and Turnigy 1100 KV. It should work on other configuration, but I have not try it myself.
2- Moving ELE & ALE sticks just before changing from G_TUNE to ACRO mode could corrupt the values, so please try to fly as stable as possible and switch to ACRO while sticks are almost released for best results.
3- Switching back from GYRO to G_Tune mode in AIR could be dangerous -however the correction is very fast and it normally works- because PIDs are reset to ZEROs in AIR.

Re: G-Tune Mode (ZERO PIDs)

Posted: Tue Jun 03, 2014 1:34 am
by ezio
Very interesting. Looking forward for more.

Re: G-Tune Mode (ZERO PIDs)

Posted: Tue Jun 03, 2014 1:57 am
by MHefny
EZ :) Thank you for your great APP

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Jun 04, 2014 8:23 am
by Hamburger
Intersting. Can we see code please?

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Jun 04, 2014 4:20 pm
by shikra
+1 cool development

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Jun 04, 2014 4:27 pm
by Crashpilot1000
Your G-spot tuning looks to be working fine. Can you outline the working principle? Can it run all the time i.e do some inflight tuning all the time?

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Jun 04, 2014 5:21 pm
by giiba
You're teasing us with a demo and no code!

Looking forward to trying this out myself.

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Jun 04, 2014 10:08 pm
by MHefny
Thank you for your interest in the G_TUNE.
I am working more on the algorithm by trying it on my own firmware HefnyCopter.
It needs some tuning, as I notice some dependent on loop time.
I will publish it when it is solid on different platforms.

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Jun 04, 2014 10:17 pm
by copterrichie
That makes total sense about the loop time, it has been a pain in the .... for sometime now.

Re: G-Tune Mode (ZERO PIDs)

Posted: Mon Jun 09, 2014 8:04 am
by o_lampe
Wouldn't it be better to start with PIDs preset to values known to be working and start autotune from there?
Or start with 50% value?

OT-question: Do you have public access to GPS satellites in egypt?

Re: G-Tune Mode (ZERO PIDs)

Posted: Mon Jun 09, 2014 4:56 pm
by MHefny
The idea is to start with no settings, to help ppl. that are not in interest in setting their quad to fly.

yes we do have GPS in Egypt.

Re: G-Tune Mode (ZERO PIDs)

Posted: Tue Jun 10, 2014 9:29 am
by o_lampe
MHefny wrote:The idea is to start with no settings, to help ppl. that are not in interest in setting their quad to fly.


You are right, when your code set the values to zero.

Re: G-Tune Mode (ZERO PIDs)

Posted: Sun Jul 06, 2014 3:30 am
by dominicclifton
Any update on this? I think many on are are keen to see the code regardless of it's state of completion - code is never really finished anyway and can always be changed later when people get time so don't be afraid of posting it.

Re: G-Tune Mode (ZERO PIDs)

Posted: Tue Aug 05, 2014 12:21 am
by ny_racer_xxx
Looks very promising, please post a sketch when it ready to test...

CR

Re: G-Tune Mode (ZERO PIDs)

Posted: Thu Aug 07, 2014 2:24 pm
by MHefny
Hi Guys,

Sorry I took a long time, however I believe I have made some good progress. and now I am ready to reveal the code :)



kindly access details on here http://technicaladventure.blogspot.com/2014/08/zeropid-tunes-for-multirotors-part2.html

or in DIYhttp://diydrones.com/profiles/blogs/zero-pid-tunes-for-multirotors-part-2?xg_source=msg_appr_blogpost

Regards

Re: G-Tune Mode (ZERO PIDs)

Posted: Mon Aug 11, 2014 9:40 pm
by Hypermobile
Looks Great, another great improvement on MultiWii :!: :!: :D

i'll give it a try on my new quad in a while

Re: G-Tune Mode (ZERO PIDs)

Posted: Tue Aug 12, 2014 12:02 am
by ny_racer_xxx
Thank you for posting the sketch!!!

I haven't loaded it up on my quad yet...

Does G-Tune work automatically in Acro mode, or is it set to an Aux switch?

CR

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Aug 13, 2014 4:44 am
by MHefny
@ny_racer_xxx it should be engaged using AUX :)

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Aug 13, 2014 6:14 pm
by mxspode
Nice, Mr. Miniature is at it again. ;)

I will be testing. Not that I can't adjust PID's on my own, I just don't have the patience for it.

Rob...

Re: G-Tune Mode (ZERO PIDs)

Posted: Wed Sep 17, 2014 9:23 pm
by acessford
Has anyone successfully tried this auto tune? Im having difficulty tuning a new build, and would love to try this.

Re: G-Tune Mode (ZERO PIDs)

Posted: Sun Oct 19, 2014 7:37 pm
by izeman
maybe someone can help me how to use it. the instructions are VERY short ;)
once installed on my FC i set GTUNE to be activated by a switch on my remote. works so far. when i engage the switch all PIDs of ROLL, PITCH and YAW are set to zero.
the instructions doesn't say if ACRO, LEVEL or ANGLE have or have NOT to be engaged. i guess it has to be ACRO (so no other modes enabled)?!

Code: Select all

ALT I value has to be set to 0.1
LEVEL P value has to be set to 1.0
LEVEL I value can be set between 0.1 and 0.2
LEVEL D value has to be set to 10

correct so far? do i have to take off and land in ACRO mode and then re-read the value from usb or with an app by bluetooth?
then i should be able to fly with those calculated values?
problem i have: if i try to lift off the copter wants to flip immediately even with very very little throttle gain. it's very hard to fly.
any advice for me?

Re: G-Tune Mode (ZERO PIDs)

Posted: Sun Oct 19, 2014 9:02 pm
by giiba
I dug around last night as I was having the same trouble. Mhefney doesn't explain usage in the part two post he links. I dug and found part 1 (not easy via the mobile sites):

http://technicaladventure.blogspot.ca/2 ... s.html?m=1

I haven't had time to actually test it but a little down the page there is a flowchart of how to use the mode. I think that will do it. I will report back later.

Re: G-Tune Mode (ZERO PIDs)

Posted: Sun Oct 19, 2014 11:33 pm
by giiba
So I tried this methodology just now and I didn't get positive results... Just flipping over as soon as the throttle was high enough.

Any advice on usage Mhefny?

Re: G-Tune Mode (ZERO PIDs)

Posted: Mon Oct 20, 2014 1:28 am
by ny_racer_xxx
I spent I don't know how many hours screwing with bradwii and autotune, I never did get it going....

Just an FYI, what I did finally to test it, I took a 10' A-frame ladder and ran bungee front to back, and zip ties the frame to it.... no matter what I did, it flapped around like a flounder on hot black top....

CR

Re: G-Tune Mode (ZERO PIDs)

Posted: Mon Oct 20, 2014 4:13 pm
by izeman
good to know that it's not only me having issues :) i really appreciate ppl doing r&d and spending their time programming new stuff. but a solid HOW-TO is also a lot of work. it's not easy for a pro to know what a noob or someone not able to follow those programming steps needs to know to use the software.
so an easy step by step instructions like:

Code: Select all

install gtune software
attach battery
arm
set gtune mode and acro (nothing else activated)
fly for x minutes
land
deactivate gtune?!
re-read values ...
jadajada ...

Re: G-Tune Mode (ZERO PIDs)

Posted: Thu Oct 23, 2014 5:13 am
by giiba
Okay... one problem is that large chunks of the code added at the bottom of MultiWii.cpp for G-Tune is commented out. Dunno why.

Lines 1297 to 1343:

Code: Select all

#if defined (G_TUNE)
    if((f.GTUNE) && f.ARMED)
    {
        calculate_ZEROPID (axis, imu.gyroData[axis]); //error);
/*
      debug[0]= rc;
      debug[1]= abs(error) ;

      int16_t diff_P = (int16_t)(abs(error) - abs(OldError[0][i]));
      int16_t diff_I = (int16_t)(abs(error) - abs(OldError[1][i]));

      if (diff_P > 1)
      {
        if (conf.pid[axis].P8 < 100) conf.pid[axis].P8 +=1;
      }
       if (diff_P < -1)
      {
        if ( conf.pid[axis].P8 > 0)   conf.pid[axis].P8 -=1;
      }
     
      if (diff_I > 1)
      {
        if (conf.pid[axis].I8 < 100) conf.pid[axis].I8 +=1;
      }
       if (diff_I < -1)
      {
        if ( conf.pid[axis].I8 > 0)   conf.pid[axis].I8 -=1;
      }
     

      //if ((abs(rc) < 50) && (rcCommand[THROTTLE] > MINTHROTTLE + 250))
      if ((abs(error) < 50) && (rcCommand[THROTTLE] > MINTHROTTLE + 250))
      {
        AvgPID[axis].P8 = (AvgPID[axis].P8 + conf.pid[axis].P8) / 2;
        AvgPID[axis].I8 = (AvgPID[axis].I8 + conf.pid[axis].I8) / 2;
      }
       debug[2] = diff_P;
       debug[3] = conf.pid[1].P8;

     OldError[0][i] = ( 3 * OldError[0][i] + (int16_t)abs(error)) / 4;
     OldError[1][i] = ( 19 * OldError[1][i] + (int16_t)abs(error)) / 20;

*/
   }


#endif

Lines 1365 to 1406:

Code: Select all

  #if defined (G_TUNE)
 
 if((f.GTUNE) && f.ARMED)
  {
      calculate_ZEROPID (YAW,error);
/*    int16_t diff_P = (int16_t)(abs(error) - abs(OldError[0][YAW]));
      int16_t diff_I = (int16_t)(abs(error) - abs(OldError[1][YAW]));
     
      if (diff_P > 1)
      {
        if (conf.pid[YAW].P8 < 100) conf.pid[YAW].P8 +=1;
      }
       if (diff_P < -1)
      {
        if (conf.pid[YAW].P8 > 0)   conf.pid[YAW].P8 -=1;
      }


      if (diff_I > 1)
      {
        if (conf.pid[YAW].I8 < 100) conf.pid[YAW].I8 +=1;
      }
       if (diff_I < -1)
      {
        if (conf.pid[YAW].I8 > 0)   conf.pid[YAW].I8 -=1;
      }


      //if ((abs(rc) < 50) && (rcCommand[THROTTLE] > MINTHROTTLE + 250))
      if ((abs(error) < 50) && (rcCommand[THROTTLE] > MINTHROTTLE + 250))
      {
        AvgPID[YAW].P8 = (AvgPID[YAW].P8 + conf.pid[YAW].P8) / 2;
        AvgPID[YAW].I8 = (AvgPID[YAW].I8 + conf.pid[YAW].I8) / 2;
      }
     
      OldError[0][YAW] = (3 * OldError[0][YAW] + (int16_t)abs(error)) / 4;
      OldError[1][YAW] = (19 * OldError[1][YAW] + (int16_t)abs(error)) / 20;
  */
  }


  #endif


Notice the main code in both those sections is removed, leaving only the zero PID part. No wonder I get no results but crashing. I've fired up the code with the commenting removed, and it seemed to be learning, but it may be my micro quad (~14g) with fast motors (14,000 kv) is too much for the algorithm to handle. Needs more testing.




Second, I may have made sense of the flow chart. Proceed as follows:

A) ensure all special flight modes are disabled on Tx [ACRO]

1) Plug in flight battery [DISARMED, GTUNE off]
2) Switch on Gtune mode [DISARMED, GTUNE on]
3) Arm the copter [ARMED, GTUNE on]
4) Lift off slowly, avoid stick inputs
5) Eventually the copter should fly well... when done:
6) Land, turn off Gtune [ARMED, GTUNE off]
7) Disarm copter [DISARMED, GTUNE off]

However as my copter obviously doesn't like Gtune mode (looooooooooooooots of crashing to try it out) I cannot confirm this process works. Attached is a substitute MultiWii.cpp with only the commenting changed.


Good Luck!

Re: G-Tune Mode (ZERO PIDs)

Posted: Thu Oct 23, 2014 8:51 am
by izeman
thanks. looking good. my code IS learning somehow. but i can't really produce results. and PIDs produced are very high (P=15+) and I~0. i will try with your code changes. i wished the author of GTUNE would pass by and give some input. i think this has great potential and it would help many noobs to find flyable settings to start off tuning.