Go to Post The more time we spend there, and the more investment we have in the team, the more rewarding it is. - ILAMtitan [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-02-2012, 19:33
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
How to use tasks

I read the task section in the WPILib manual and the class help file, but when I'm having difficulty getting anything working. The example I'm giving has everything extra cut out. (spelling and code punctuation might not be right)

Code:
class 4174robot : public InterativeRobot
{
public:
4174robot();
...(Interative Robot Overrides)...
void TestTaskFunction();
...
private:
Task *task1;
...
};
4174robot:4174robot()
{
task1 = new Task("testtask", (VOIDFUNCPTR) TestTaskFunction();
}
...
4174robot:Teleopinit
{
task1->start;
}
...
4174robot:Disabledinit
{
task1->stop;
}
...
4174robot:TestTaskFunction()
{
std::cout<<"task has been started"<<endl;
}
It complies fine (sometimes), but as I watch it on the WTX console, it just says something along the lines of "task failed." whey I start teleop.

Last edited by agartner01 : 07-02-2012 at 20:13.
Reply With Quote
  #2   Spotlight this post!  
Unread 07-02-2012, 21:21
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How to use tasks

Why do you need to use task? Are you comfortable with multi-tasking programming? To deal with multiple tasks, you need to understand the concept of protecting share resources when accessing them from different tasks (e.g. using semaphores). If you are not familiar with the concept, you may want to avoid using task.

Having said that, our team is using task to offload the vision processing so it doesn't bog down the main robot task. We have done some performance tests and found out that analyzing a frame takes about 150-200 msec. So if you are doing vision processing in your main robot task, you may need to set the safety expiration longer than the time required to process a frame.
__________________
Reply With Quote
  #3   Spotlight this post!  
Unread 07-02-2012, 21:33
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: How to use tasks

1. The main reason I want to use a task is so that I can tell one system to do something while continuing to do something else.
2. No, I'm not really comfortable with multi-tasked programming. But what I'm trying to do won't be subject to multiple tasks trying to access the same systems. The task that I create will be the only one accessing the systems.
Reply With Quote
  #4   Spotlight this post!  
Unread 07-02-2012, 21:34
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How to use tasks

Code:
class 4174robot : public IterativeRobot
{
public:
...(Interative Robot Overrides)...
    Task *task1;
 
    4174robot();
    ~4174robot();
 
...
};
 
static void TestTaskFunction(void)
{
    std::cout<<"task has been started"<<endl;
}
 
4174robot::4174robot()
{
    task1 = new Task("testtask", (VOIDFUNCPTR) TestTaskFunction);
}
 
4174robot::~4174robot()
{
    delete task1;
}
...
4174robot::Teleopinit()
{
    task1->Start();
}
...
Disabledinit()
{
    task1->Stop();
}
Note that once you enabled TeleOp, it will print the line "task has been started" and the task will end.
__________________

Last edited by mikets : 07-02-2012 at 22:17.
Reply With Quote
  #5   Spotlight this post!  
Unread 07-02-2012, 21:38
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: How to use tasks

Alright, thanks alot... I'll try it tomorrow. I wish they would have put more of this in the WPILib Manual.

In addition to that, I noticed that you added the destructor. In a lot of other [FRC] examples, it seems like it is pretty much ignored. Just good coding practices or something more?

Last edited by agartner01 : 07-02-2012 at 21:43.
Reply With Quote
  #6   Spotlight this post!  
Unread 07-02-2012, 21:51
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How to use tasks

Quote:
Originally Posted by agartner01 View Post
1. The main reason I want to use a task is so that I can tell one system to do something while continuing to do something else.
You can achieve this without using tasks. We use something call Cooperative multi-tasking. It is not using real tasks, instead we have a periodic function for each subsystem. In our main robot loop, we call the periodic functions of each subsystem. So all subsystems get a chance to run. However, since it is "cooperative", there are rules to follow when writing such "tasks". You must not have any loop in these functions and you cannot use any Wait statements. If you need to wait, you use a state machine to remember the state and exit the function. You will continue your processing on the next loop when you will be called again. It works really well for us and you don't have to worry about all the gotcha's of real multi-tasking programming.
Quote:
Originally Posted by agartner01 View Post
2. No, I'm not really comfortable with multi-tasked programming. But what I'm trying to do won't be subject to multiple tasks trying to access the same systems. The task that I create will be the only one accessing the systems.
You will be surprise. It may not be obvious that you are accessing shared resources. Sometimes, you may be accessing it indirectly through calling another function or even the WPI library. That's why when there is a problem, it's very hard to debug.
__________________
Reply With Quote
  #7   Spotlight this post!  
Unread 07-02-2012, 21:59
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: How to use tasks

I wasn't aware of the "cooperative multitasking" think. I'm just not sure how to go about it...

And replace all the absolutes (won't, never, etc) with shouldn't and such. I really do have absolutely no idea what I'm talking about...
Reply With Quote
  #8   Spotlight this post!  
Unread 08-02-2012, 15:16
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: How to use tasks

Now I get this ->
Attached Thumbnails
Click image for larger version

Name:	image[1].png
Views:	24
Size:	247.4 KB
ID:	11801  
Reply With Quote
  #9   Spotlight this post!  
Unread 08-02-2012, 15:19
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How to use tasks

Ah, try this instead.
Code:
static void TestTaskFunction(void)
{
    std::cout<<"task has been started"<<endl;
}
 
4174robot::4174robot()
{
    task1 = new Task("testtask", (FUNCPTR) TestTaskFunction);
}
__________________
Reply With Quote
  #10   Spotlight this post!  
Unread 08-02-2012, 15:26
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: How to use tasks

Alright that works... I don't get it (FUNCPTR vs VOIDFUNCPTR), but it works.

Then I was like, why am I getting no output on my console. Then I remembered that the windriver console only works for the current task. (or something like that)

Last edited by agartner01 : 08-02-2012 at 15:29.
Reply With Quote
  #11   Spotlight this post!  
Unread 08-02-2012, 15:29
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How to use tasks

Quote:
Originally Posted by agartner01 View Post
Alright that works... I don't get it (FUNCPTR vs VOIDFUNCPTR), but it works.

Then I was like, why am I getting no output on my console. Then I remembered that the windriver console only works for the current task.
No, not true. we printed debug output from a different task. I don't know if the picture showed the whole code but it doesn't seem to have the task1->Start() call. I don't think the task get started automatically when created. You need to explicitly start it.
__________________
Reply With Quote
  #12   Spotlight this post!  
Unread 08-02-2012, 15:35
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How to use tasks

BTW, FUNCPTR and VOIDFUNCPTR are custom types. I don't have Wind River in front of me at the moment so I can't find the definitions of FUNCPTR and VOIDFUNCPTR for you. But I think VOIDFUNCPTR defines a function that returns void and FUNCPTR defines a function that returns an integer. Apparent, the task function should be returning an integer. Probably wouldn't matter but more correctly:
Code:
static int TestTaskFunction(void)
{
    std::cout<<"task has been started"<<endl;
    return 0;
}
 
4174robot::4174robot()
{
    task1 = new Task("testtask", (FUNCPTR) TestTaskFunction);
}
BTW, in WPILib\task.h, the Task class constructor is defined as:
Code:
Task(const char* name, FUNCPTR function, INT32 priority = kDefaultPriority, UINT32 stackSize = 20000);
So it is expecting the type FUNCPTR.
__________________

Last edited by mikets : 08-02-2012 at 15:38.
Reply With Quote
  #13   Spotlight this post!  
Unread 08-02-2012, 15:40
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: How to use tasks

Quote:
Originally Posted by mikets View Post
No, not true. we printed debug output from a different task. I don't know if the picture showed the whole code but it doesn't seem to have the task1->Start() call. I don't think the task get started automatically when created. You need to explicitly start it.
yeah, I have it later on. I'm using wtx console in windriver, not netconsole. If I put a mechanical task in the funcion it works, but not a cout. I'm trying printf now.
Reply With Quote
  #14   Spotlight this post!  
Unread 08-02-2012, 15:41
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 671
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: How to use tasks

Oh yeah, cout doesn't work, either printf or cerr will work.
__________________
Reply With Quote
  #15   Spotlight this post!  
Unread 08-02-2012, 15:50
agartner01 agartner01 is offline
Captain + Control Sys & Design
FRC #4174
Team Role: Engineer
 
Join Date: Feb 2012
Rookie Year: 2012
Location: Hector MN
Posts: 109
agartner01 is an unknown quantity at this point
Re: How to use tasks

huh... printf dosen't work either. Are you using the wtx console in windriver?
Reply With Quote
Reply


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 03:01.

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