Go to Post Some clever fellow a few years back made a little scooter based on this principal and made so much on the idea he can afford the finest denim in the land. - phrontist [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #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.)
*/
  #2   Spotlight this post!  
Unread 17-12-2012, 23:26
apalrd's Avatar
apalrd apalrd is offline
More Torque!
AKA: Andrew Palardy (Most people call me Palardy)
VRC #3333
Team Role: College Student
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Auburn Hills, MI
Posts: 1,347
apalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond repute
Re: New autonomous outline tutorial

Maybe I'm completely missing the point, but why not just call the commands in a list, procedurally? Like, if you had functions drive_straight(float dist, float speed) and fire_ball(void) you could just do

Code:
void autonomous_1() 
{
drive_straight(10,30);
fire_ball();
wait(500);
fire_ball();
drive_straight(-10,30);
}
And you would make more functions for more autonomous modes. It seems like a more elegant and execution-efficient solution.


I'm not saying there aren't reasons for storing commands in a list. In fact, I myself wrote a system which is by all metrics complex in LabVIEW to do a similar thing. However, I was trying to gain the ability to store the command vector in a text file, and load it on demand. Had I only wanted to write the commands in LabVIEW, I would have just called the command blocks in sequence (which I did successfully in our 2010 season, which suffered only from a very long on-field iteration time due to LV compile time issues).
__________________
Kettering University - Computer Engineering
Kettering Motorsports
Williams International - Commercial Engines - Controls and Accessories
FRC 33 - The Killer Bees - 2009-2012 Student, 2013-2014 Advisor
VEX IQ 3333 - The Bumble Bees - 2014+ Mentor

"Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function." ~ John Carmack
  #3   Spotlight this post!  
Unread 18-12-2012, 00:14
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
Re: New autonomous outline tutorial

Quote:
Originally Posted by apalrd View Post
Maybe I'm completely missing the point, but why not just call the commands in a list, procedurally? Like, if you had functions drive_straight(float dist, float speed) and fire_ball(void) you could just do

Code:
void autonomous_1() 
{
drive_straight(10,30);
fire_ball();
wait(500);
fire_ball();
drive_straight(-10,30);
}
And you would make more functions for more autonomous modes. It seems like a more elegant and execution-efficient solution.


I'm not saying there aren't reasons for storing commands in a list. In fact, I myself wrote a system which is by all metrics complex in LabVIEW to do a similar thing. However, I was trying to gain the ability to store the command vector in a text file, and load it on demand. Had I only wanted to write the commands in LabVIEW, I would have just called the command blocks in sequence (which I did successfully in our 2010 season, which suffered only from a very long on-field iteration time due to LV compile time issues).
The reason for this is to be able to spawn them in separate threads... I originally wrote this today just for fun.
__________________
/*
* 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.)
*/
  #4   Spotlight this post!  
Unread 18-12-2012, 00:47
apalrd's Avatar
apalrd apalrd is offline
More Torque!
AKA: Andrew Palardy (Most people call me Palardy)
VRC #3333
Team Role: College Student
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Auburn Hills, MI
Posts: 1,347
apalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond repute
Re: New autonomous outline tutorial

I see. You are able to launch the next command before the previous command finishes.


That makes a lot of sense. However, when do you really need to do that? I spent a very long time trying to get my script system to allow asynchronous command execution and decided that it was too difficult of a tradeoff in code complexity. I found that there was no reasonable case in which it was impossible to write autonomous code with only one autonomous commanded action executing at a time, with the blocks I wanted to write.

The tradeoff is always between simplicity in execution and flexibility in design.
__________________
Kettering University - Computer Engineering
Kettering Motorsports
Williams International - Commercial Engines - Controls and Accessories
FRC 33 - The Killer Bees - 2009-2012 Student, 2013-2014 Advisor
VEX IQ 3333 - The Bumble Bees - 2014+ Mentor

"Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function." ~ John Carmack
  #5   Spotlight this post!  
Unread 18-12-2012, 02:35
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
Re: New autonomous outline tutorial

Quote:
Originally Posted by apalrd View Post
I see. You are able to launch the next command before the previous command finishes.


That makes a lot of sense. However, when do you really need to do that? I spent a very long time trying to get my script system to allow asynchronous command execution and decided that it was too difficult of a tradeoff in code complexity. I found that there was no reasonable case in which it was impossible to write autonomous code with only one autonomous commanded action executing at a time, with the blocks I wanted to write.

The tradeoff is always between simplicity in execution and flexibility in design.
The other reason I would do something like this(Which I have not yet been able to try) is to allow errors. As you see if one of the functions returns false the rest do not execute.
__________________
/*
* 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.)
*/
  #6   Spotlight this post!  
Unread 18-12-2012, 11:40
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,567
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: New autonomous outline tutorial

Have you looked at the Command based programming model introduced in 2012?
  #7   Spotlight this post!  
Unread 18-12-2012, 14:31
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
Re: New autonomous outline tutorial

Quote:
Originally Posted by Joe Ross View Post
Have you looked at the Command based programming model introduced in 2012?
No I have not. Where could this be found?
__________________
/*
* 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.)
*/
  #8   Spotlight this post!  
Unread 18-12-2012, 15:01
Brian Selle's Avatar
Brian Selle Brian Selle is offline
Mentor
FRC #3310 (Black Hawk Robotics)
Team Role: Engineer
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Texas
Posts: 162
Brian Selle has a spectacular aura aboutBrian Selle has a spectacular aura aboutBrian Selle has a spectacular aura about
Re: New autonomous outline tutorial

Quote:
Originally Posted by jacob9706 View Post
No I have not. Where could this be found?
The 2012 WPILib Cookbook explains how it works. Read the whole document first but CommandGroups described on page 50 may be of interest.

http://firstforge.wpi.edu/sf/docman/...tation/doc1297
  #9   Spotlight this post!  
Unread 18-12-2012, 15:23
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: New autonomous outline tutorial

Quote:
Originally Posted by jacob9706 View Post
No I have not. Where could this be found?
http://www.chiefdelphi.com/forums/sh...79&postcount=7

http://www.chiefdelphi.com/forums/sh...88&postcount=1

http://www.chiefdelphi.com/forums/sh...d.php?t=109719

http://www.chiefdelphi.com/forums/sh...1&postcount=25


Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:08.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi