Back in June, Pieter suggested that we add a form of 'user hook' in to the arducopter code to simplify the use of user written additions or enhancements to the main branch off the arducopter code.

The problem we had was that every time Jason released new version of the code any changes you may have made for your own pet project would have to get laboriously ported across to the latest version of the code, or you stuck with an old version of the code and missed out on all of the latest and greatest bug fixes, tuning and additions.

We now have a first cut at a 'user hooks' solution included in the main branch of the arducopter code. In each main loop, in the variable declarations, and in the init_ardupilot() function there is now a USERHOOK section.

You can now define in your "APM_config.h" file

  • which user hooks you wish to use
  • what user functions you wish to run from these hooks
  • where your user variables are declared.

There are two files "UserCode.pde" and "UserVariables.h" in the arducopter code where you can write your own functions and declare your own variables. Now when a new version of the main code is released all you have to do to port your code over to the new version is copy over your "UserCode.pde" and "UserVariables.h" files and update the latest "APM_config.h" file to point to your user code.

Here is the relevant section from APM_Config.h"

// agmatthews USERHOOKS
// the choice of function names is up to the user and does not have to match these
// uncomment these hooks and ensure there is a matching function in your "UserCode.pde" file
//#define USERHOOK_FASTLOOP userhook_FastLoop();
#define USERHOOK_50HZLOOP userhook_50Hz();
//#define USERHOOK_MEDIUMLOOP userhook_MediumLoop();
//#define USERHOOK_SLOWLOOP userhook_SlowLoop();
//#define USERHOOK_SUPERSLOWLOOP userhook_SuperSlowLoop();
#define USERHOOK_INIT userhook_init();

// the choice of includeed variables file (*.h) is up to the user and does not have to match this one
// Ensure the defined file exists and is in the arducopter directory
#define USERHOOK_VARIABLES "UserVariables.h"

And in "UserCode.pde" you may have something that looks like this (this example is code for my i2c sonar)

// agmatthews USERHOOKS

void userhook_init()
    // put your initialisation code here

void userhook_50Hz()
    // put your 50Hz code here

And in "UserVariables.h" you could have something like this (again, my example i2c sonar code)

// agmatthews USERHOOKS
// user defined variables

APM_I2CSonar_Class     sonar; 


I imagine there are other places where user hooks may be useful, for example somewhere in leds.pde or in the logging code etc.

If you think this is useful or want to see other areas with user hooks let us know



Views: 3416

Reply to This

Replies to This Discussion

Hi Lance,

I am new to the code too and i don't have a clear answer. What i found so far is that you can simply write in your usercode gcs_send_text_P("message"); to send text messages (note that this messages will be stored in program memory, and should.

I also tried to send variable values using gcs_send_text_fmt(PSTR("%o"), variablename()); but the format was not well understood and i only recieved "????????????????????f" on the ground station.

You can see how these function work looking into the GCS_mavlink.pde, if you find how it works, please tell me.

Good luck 


Reply to Discussion


© 2020   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service