My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
SpecialEvents  
Updated Aug 23, 2011

Triggering events, such as a camera, automatically

ArduPilot has the ability to trigger events when certain conditions are met, such as hitting a waypoint. The way you do this is to add them to the events.pde file. You'll need to a little comfortable with programming for this, but we've provided some sample events in that file (commented out with //, so they won't run unless you uncomment them) for your guidance:

Here they are.

In this first one, an egg drop event, the servo on channel 5 moves to -45 when the plane hits the 5th waypoint. This one is inside the waypoint event routine, so ignore the two cases on either side:

void waypoint_event(byte event)
{
	switch(event)
	{
		case EVENT_WILL_REACH_WAYPOINT:
			// called just before wp_index is incremented
			Serial.print("MSG Reached WP:");
			Serial.println(wp_index,DEC);
			break;
			
		case EVENT_SET_NEW_WAYPOINT_INDEX:
			// called just after wp_index is incremented

			/*
				// Dead Simple Egg drop example:
				// This doesn't take into account height and airspeed, etc.
				if(wp_index = 5){
					servo_out[CH_RUDDER] = -45;
				}
				
			*/
			break;

		case EVENT_LOADED_WAYPOINT:
			//Serial.print("Loaded WP index:");
			//Serial.println(wp_index,DEC);
			print_current_waypoints();
			reset_I();			
			break;			
	}
}

In this second example, the plane will drop the egg at Waypoint 2, but only at least two minutes after launch:

// called after every single control loop
void 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);
		}
	}
*/

			/*
				// Complex Simple Egg drop example:
				// This doesn't take into account height and airspeed, etc.
				if(wp_index == 5){
					servo_out[CH_RUDDER] = -45;
				}
				
			*/
}

Finally, in this third example, the plane drops the payload if it is within range of the target:

void mediumLoop_event(void)
{
	/*
	float temp  = (float)(current_loc.alt - home.alt) * .01;
	egg_dist = sqrt(temp / 4.903) * (float)ground_speed *.01;

	if(wp_index == 3){
		wp_radius = 10;
		if(wp_distance < egg_dist){
			servo_out[CH_RUDDER] = PAYLOAD_OPEN;
		}
	}else{
		wp_radius = 20;	
		servo_out[CH_RUDDER] = PAYLOAD_CLOSED;
	}
	*/
}

Sign in to add a comment
Powered by Google Project Hosting