View Single Post
  #1   Spotlight this post!  
Unread 17-12-2012, 17:11
jacob9706 jacob9706 is offline
Registered User
AKA: Jacob Ebey
FRC #3574 (High Tekerz)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2010
Location: Seattle
Posts: 101
jacob9706 is on a distinguished road
Cool New autonomous outline tutorial

Hi everyone,

Today I will be showing you how to implement an easy autonomous system based on callbacks/events. If anyone has done JavaScript using the jQuery library you will be quite familiar with the concept of a callback. A callback is a function (or method depending on the language) that executes after a set of code is done running.

Here is a quick jQuery example...

Code:
$(document).load(loadFunction);

var loadFunction = function() {
	alert(“Loaded”);
}
What we will be doing today will be a little bit different. We will be creating a “list” of actions we want to preform one after another. This list will be represented as a vector of functions. The first thing we have to do is define a function pointer that returns a boolean and has nothing as its paramaters.

Code:
typedef bool (Event*)(void);
That line defines a function pointer called Event that takes nothing(void) and returns a boolean(bool).

The next step will be to define a function that will execute all of our function pointers called Event.

Code:
// Function that takes a vector of Even
bool runStringOfEvents(std::vector<Event> events)
{
	// Loop through every event
	for (unsigned int i = 0 i < events.size(); i++)
	{
		// Get current event from vector
		Event event = events.at(i);
		// Execute the event and get the return value
		bool success = event();
		// If it failed return false
		if (!success)
		{
			return false;
		}
	}
	// If all succeeded return true
	return true;
}
Now that the hard part is done we can create a function we will call two times through a list of events we pass to our runStringOfEvents function.

Code:
bool someEvent()
{
	std::cout << “Event executed” << std::endl;
	return true;
}
Now that we have our event function we can create a list in our program and execute the list of events.

Code:
int main(int argc, char** argv)
{
	// Create the vector to hold our events
	std::vector<Event> events;
	
	// Add the function to the list twice
	events.push_back(&someEvent);
	events.push_back(&someEvent);

	// Run the events
	runStringOfEvens(events);
}
__________________
/*
* Team 3574 Alumni
*
* 2011 - Highest Seeded Rookie
* 2011 - Rookie All-Star
* 2012 - Engineering Inspiration
* 2012 - Olympic Deans List Winner
* 2013 - Engineering Inspiration
* 2013 - Judges Award (For unique circular robot and the way the team works together.)
*/