Developer

Ardupilot 2.5 preview part 5

Merry Christmas everyone.I wanted to talk about a new aspect of 2.5 that enables coders to easily program missions through a simple API. The main idea is that many common events are sent to functions in the event.pde file to be handled by you in any way you see fit. You can do anything and access any variable in the system - within reason. The events.pde file can be swapped out with a different mission and uploaded to the the hardware.The first and most important event is the switch_event. This event is called when the 3-position switch on the radio has changed. The function set_mode(MODE) is called and the argument is one of the following flight modes:MANUAL - complete radio controlSTABILIZE - uses IR sensors to stabilize the aircraftFLY_BY_WIRE - allows the user to "steer" the aircraft using autopilot control loopsAUTO - exactly what you thinkRTL - Fly home nowLOITER - Circle around current location (set when aircraft arrives at home during RTL)An example of setting the current waypoint back to the index 1 is shown when the switch is in manual position.
void switch_event(byte switchPosition){        switch(switchPosition)        {                case 1: // First position                set_mode(MANUAL);                // this is handy function to restart your mission:                reset_waypoint_index();                break;                case 2: // middle position                set_mode(AUTO);                break;                case 3: // last position                set_mode(STABILIZE);                break;        }}
The next most important event is a waypoint event. There are a few basic events called (more will be added as necessary.) In the example below Loiter mode is turned on when we load WP 3. The variable elapsedTime is set to 0 ( elapsedTime is a user defined timer that increments in milliseconds.) If you want to stop Loitering after a set number of seconds, see the next example below.
void waypoint_event(byte event){        switch(event)        {                case EVENT_WILL_REACH_WAYPOINT:                        // called just before wp_index is incemented                        Serial.print("Reached WP:");                        Serial.println(wp_index,DEC);                        break;                case EVENT_SET_NEW_WAYPOINT_INDEX:                        // called just after wp_index is incemented                        Serial.print("Now going to WP:");                        Serial.println(wp_index,DEC);                        break;                case EVENT_LOADED_WAYPOINT:                        // called just after wp is loaded from memory                        Serial.print("Loaded WP index: ");                        Serial.println(wp_index,DEC);                        print_current_waypoint();                        // custom loitering code                        if (wp_index == 3){                                set_mode(LOITER);                                elapsedTime = 0;                        }                        break;                // called when the pattern to be flown is automatically restarted                case EVENT_LOOP:                         Serial.println("Looped WP Index");                        print_current_waypoint();                        break;                  }}
The mainLoop_event is called with every execution of the control loop. Here is a good place to check the elapsedTime variable to escape Loiter mode and continue on with the mission.
// called AFTER every control loopvoid mainLoop_event(void){        if (control_mode == LOITER){                if (wp_index == 2 && elapsedTime > 120000 ){ // 2 minutes                        elapsedTime = 0;                        // our waypoints index is not altered during LOITER                        // All we need to do is reload the waypoint                        load_waypoint();                        // and return to Autopilot mode!                        set_mode(AUTO);                }        }}
Other events include:void gps_event(void)void low_battery_event(void)void failsafe_event()More will be added as necessary, and of course, you can add your own.JasonNext installment - The Mission Simulator
E-mail me when people leave their comments –

You need to be a member of diydrones to add comments!

Join diydrones

Comments

  • Developer
    Exactly.
  • Moderator
    So if I want 'Manual' , 'RTL' & 'Waypoints Nav' from my switch then I must change this in the events tab?
  • No, no, I think what you're doing is great! Mine was mostly an idea & sw design comment for code construction. I think you're providing an excellent framework for people to add their specifics. I'll throw out an example tho... my psuedocode here reflects my uplink, but in-line code works just as well (like setting a flag based on arrival or exit from a waypoint... or something)

    parseCmd( cmd) {
    switch (cmd_type) {
    case CMD_VEHICLE: // again, like manual/auto, stabilize, etc.
    // stuff goes here
    case CMD_MISSION: // platform stuff... route & waypoint related things
    // maybe it's add a waypoint or an upload of waypoints or a whole new route
    // alternately, in your event processing, you could switch to a pre-programmed alternate route
    // maybe it's a "target" waypoint, associated with an index into the waypoint list (w/another cmd to loiter or photo on arrival)
    case CMD_SENSOR:
    // what to do when you get where you're going
    // point and click or slew camera turret or.... heck, if it's a UGV, activate chemical sensor or activate your esb missile launcher!!! : )
    }
    }

    then in your event loop you tie it all together, e.g.,
    // is a given event flag set, am i at the event location, execute the event
    // like, point your camera at this target location once you've arrived at a target wp (or begin a loiter), or...

    without an uplink, folks could swap out event code based on the 'mission' they're about to fly. Easier with an uplink, but you gotta monitor your code bloat more... MAN this is fun. : )
  • Developer
    We're planning on having better uplink capabilities with Mega. That's a while off though. Can you be more specific on the events you're looking for? The only reason I don't have more events is that I'm not sure what people realy want.
  • Great stuff Jason. I haven't moved off of 2.0, but have been integrating components from later versions as they're made available. THANKS! It's awesome to see things become stable enough to move from the basics to 'mission type' programming. Are you considering event 'types', like: Platform (like manual/auto, failsafe, battery); Misson or Route (waypoint stuff, loiter), and Sensor (execute a sensor task, which could be tied to mission/route events like a waypoint or waypoints or a loiter)? A cool thing would be, if you have a command uplink, the GCS could upload a sensor task and associate it with a route event : ) 'course, this could also be pre-programmed. Thanks again, nice work (ALL of your 2.5 stuff)
  • Admin
    :)) Jason , way to go. Santa has come , Pretty soon , some good development road map document has to be put together( I am getting lost quite often without reading all the older posts to know what is the new one applies to) , so that every one knows where things are and what are the different tracks taking you to. Some thing like a birds view for atleast main components like , Ardupilot , Ardustation software, GCS , UAV DB , GSC integration option ( GSC having option of choose either Ardu pilot and UAV DB to start with). GCS with comm to hardware ardustation to control/feed back for antenna tracking etc. Sorry if there is one already. :)
  • Merry merry indeed, thank you! Have a good one and may you and your families enjoy the day.
  • 3D Robotics
    Best. Christmas present. Ever.
This reply was deleted.