|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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”);
}
Code:
typedef bool (Event*)(void); 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;
}
Code:
bool someEvent()
{
std::cout << “Event executed” << std::endl;
return true;
}
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);
}
|
|
#2
|
|||||
|
|||||
|
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);
}
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). |
|
#3
|
|||
|
|||
|
Re: New autonomous outline tutorial
Quote:
|
|
#4
|
|||||
|
|||||
|
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. |
|
#5
|
|||
|
|||
|
Re: New autonomous outline tutorial
Quote:
|
|
#6
|
||||||
|
||||||
|
Re: New autonomous outline tutorial
Have you looked at the Command based programming model introduced in 2012?
|
|
#7
|
|||
|
|||
|
Re: New autonomous outline tutorial
No I have not. Where could this be found?
|
|
#8
|
||||
|
||||
|
Re: New autonomous outline tutorial
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
|
||||
|
||||
|
Re: New autonomous outline tutorial
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 |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|