View unanswered posts | View active topics It is currently Sat Aug 23, 2014 5:48 am






Reply to topic  [ 31 posts ]  Go to page Previous  1, 2, 3  Next
Hitechnic IR link with multitasking 
Author Message
Rookie

Joined: Mon Jun 11, 2012 9:28 pm
Posts: 37
Post Re: Hitechnic IR link with multitasking
Thanks, Spiked.
This looks like a rather complex way to do something that should really be simple. I am not sure why it it is so inefficient to multitask! It is working quite well for me, apart from this strange delay.

Any tips?

Thanks for all the help, guys!


Wed Jul 18, 2012 8:31 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Hitechnic IR link with multitasking
"fetch me a beer from the fridge" sounds like something really simple, but its achievement is still a few years away in a practical sense. The ideas I presented before would be considered beginner level complexity. Welcome to programming.

Multitasking is something that is hard to code correctly, when it works ideally. PCs have had multitasking for years, but very very few people use it when they program, for a reason. The RobotC implementation is severely limited, and that makes it even more hard. You better have experience and understand exactly what is happening to even attempt it with RobotC. Thus the recommendation to stay away from it. Honestly, if you don't understand the reasons to avoid it, you don't understand it enough to use it. It is an advanced programming concept, not a shortcut.

Your code loops, with at least a 2 second delay every time. How long does it take your motors to move? The task that moves motors may completely miss a position change if it is longer than 2 seconds. And at best case can change no faster than once every 2 seconds. The way the 2 (time AND waitForIdle) gets mixed, is probably what is causing the delay.

Bottom line, dont use multitasking here, pretend there is no such thing. You need to figure out the logic to make it work in a single loop.

A few debugging lines of code wouldn't hurt. use WriteDebugStreamLine to show the time nProgramTime and what is being done at that point. RobotC is better than the rest for this reason, use it.

_________________
Mike aka Spiked3
http://www.spiked3.com


Thu Jul 19, 2012 12:03 am
Profile
Rookie

Joined: Mon Jun 11, 2012 9:28 pm
Posts: 37
Post Re: Hitechnic IR link with multitasking
Thanks, Spiked.
I will definitely attempt to go over my code again and try to reduce the tasks.
Just out of curiosity, I am interested in knowing why it is so dangerous to use, and in what cases it should be used.


Thu Jul 19, 2012 8:09 am
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 565
Post Re: Hitechnic IR link with multitasking
sqiddster wrote:
Thanks, Spiked.
I will definitely attempt to go over my code again and try to reduce the tasks.
Just out of curiosity, I am interested in knowing why it is so dangerous to use, and in what cases it should be used.


This is actually something that comes up quite a bit when I'm talking to different people on the subject. Basically, most things that can be done with multitasking can also be done easier and safer (less chance of lockups or skipped code) with conditional structures (meaning, 'if statements', 'while loops', etc) and/or functions combined with proper coding techniques. Multitasking is nice because it can split the CPU time between two different tasks (go figure) but generally it's easier to simply check whatever sensors or values you want to in a conditional loop and structure your code so it keeps looking for the conditions (calling a function, for example).

It's one of those 'give me a goal that needs to use multi-tasking, and I'll give you three different ways to complete the objective without it' type deals. You can use timers and loops to set how long a section of code runs, and put it inside a loop with other code to flip-flop between the two (run code section 1 for 20 seconds, then run code section 2 for 30 seconds, repeat until a condition is met). It's safer (don't have to worry about locking up the CPU or time-slices) and generally easier (a loop, a condition, the code you want to run, and possibly functions to make it easier on to follow the code) to use.

I may be wrong on some of these points, I'm no pro when it comes to multitasking (feel free to correct me if anything I say is wrong or questionable, I won't take it personally) but it always seems like there's a way to get the job done quicker and easier.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Thu Jul 19, 2012 11:35 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Hitechnic IR link with multitasking
in most versions of C, when you call a function, variables declared within that function are given a private copy of that variable.
for example
Code:
void foo(int a)
{
  int b;
  b = a * a;
  print(a,b);
}


So if foo is called from multiple tasks, the function will work as expected. In robotC, that is not the case. If foo is called by more than 1 task, the first task, might, keyword might, depending on the timing, print the results of the call from the second task, as the variable is not private, and is instead used by all. This is why your code receives a warning about calling a function where this might happen. You can program to make sure that doesn't happen, for example only starting one of the 2 tasks at a time, so robotC allows you to do it, but you would need to handle the concurrency (running only 1 of the tasks at a time in the intended order).

(this is supposed to change soon, as an improvement to robotc, but there still can be other similar problems you need to consider, for example a set of global variables. What happens when only 2 out of 3 get changed and your task is 'interrupted'?)

In your code, I saw examples where 1 task would set the value of a variable, then the other task would act on that. Skip the variable (and tasks), just do the action.
Code:
// idea conveyed in straight robotc, your code uses different PF functions so will be different
if (a)
{
  nMotorEncoderTarget[a] = open;
  power[a] = 30 * direction;
}


In the case of a move to an encoder, the NXT will use tasks itself to move while your code keeps on running. let it do the work for you. You don't need to wait for it to complete. Although you may want to wait before you start the next action.

Be careful of timing. Always assume it will be inaccurate. When you move something up for 7 seconds, then down for 7 seconds, the move will not be between the same 2 positions. it takes more power to move up (in theory) so the up move will weaken the batteries and move slower, than the move down. Use touch sensors instead, or encoderTargets, if available. In some cases, timing is 'good enough', for example when physical stops exist, but think about it before you make that decision.

If you used touch sensors or encoder targets (or timing is 'good enough'), the elevator up/down looked independent of everything else, and would be the one place you could probably keep as a task.

_________________
Mike aka Spiked3
http://www.spiked3.com


Thu Jul 19, 2012 11:46 am
Profile
Rookie

Joined: Mon Jun 11, 2012 9:28 pm
Posts: 37
Post Re: Hitechnic IR link with multitasking
Thanks, John and Spiked.

In regards to the motor timing, I know all about this, don't you worry. In that one place I use timing, it has a clutch system so that accuracy won't be a problem.

I'll let you know how I do with my code!


Thu Jul 19, 2012 3:17 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Hitechnic IR link with multitasking
FWIW, I have a previous post on the subject of multi-tasking.
viewtopic.php?f=11&t=3341&p=16533&hilit=pizza#p16533
This may help you to write multi-tasking code without using RobotC tasks.


Thu Jul 19, 2012 8:29 pm
Profile
Rookie

Joined: Mon Jun 11, 2012 9:28 pm
Posts: 37
Post Re: Hitechnic IR link with multitasking
Well, I went over the code and got it all down to one task using timers and trigger variables. Works well now.
Thanks!

I should be able to post the project soon.


Sat Jul 21, 2012 8:04 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Hitechnic IR link with multitasking
Great to hear a success story - Post your new code, I want to see :) Any pics would be cool too.

_________________
Mike aka Spiked3
http://www.spiked3.com


Sat Jul 21, 2012 10:54 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3192
Location: Rotterdam, The Netherlands
Post Re: Hitechnic IR link with multitasking
Can't wait to see what you've made :)

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jul 22, 2012 1:19 am
Profile WWW
Rookie

Joined: Mon Jun 11, 2012 9:28 pm
Posts: 37
Post Re: Hitechnic IR link with multitasking
Thanks again, guys.

Here's the code, spiked said he was interested...

There are probably some bad practices still in there, but no more multitasking.

As for pics/description, you will just have to wait and see. Let's just say I am quite pleased to be using a 2D boolean array. Come on, how many times do we get to use a 2D boolean array?

Pics/description should be up in a week or two.


Attachments:
GridV2.c [5.09 KiB]
Downloaded 180 times
Sun Jul 22, 2012 12:14 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Hitechnic IR link with multitasking
I didn't see anything that jumped out at me as a 'bad practice'. Actually, it looked pretty good. I liked your use of comments, not overkill, but informative.

remember to ctl-a ctl-alt-f occasionally to format your indents.

I'm glad you got it working..

Don't be afraid to try tasks again later. There are times when they are a good thing. Hopefully you understand a little more about the dangers when using in them.

_________________
Mike aka Spiked3
http://www.spiked3.com


Sun Jul 22, 2012 2:22 pm
Profile
Rookie

Joined: Mon Jun 11, 2012 9:28 pm
Posts: 37
Post Re: Hitechnic IR link with multitasking
Here it is:

viewtopic.php?f=15&t=4721&p=19894#p19894


Tue Jul 31, 2012 1:14 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post Re: Hitechnic IR link with multitasking
sqiddster wrote:


Excellent! My LEGO club did something similar but bigger last year for BrickCon (search ballmageddon on youtube).

I see the idea of using servo travel v. time/speed for the gates worked out :)

_________________
Mike aka Spiked3
http://www.spiked3.com


Tue Jul 31, 2012 4:52 pm
Profile
Rookie

Joined: Mon Jun 11, 2012 9:28 pm
Posts: 37
Post Re: Hitechnic IR link with multitasking
Oh, wow, that was you guys! I thought I remembered seeing something like that, I think I was indirectly inspired ;)


Tue Jul 31, 2012 5:12 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 31 posts ]  Go to page Previous  1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.