Jump to content
THIS IS THE TEST SITE OF EUROBRICKS! ×
THIS IS THE TEST SITE OF EUROBRICKS!

Recommended Posts

Posted

I couldn't find a topic on this subject (although search is not easy with a search engine not allowing 3-letter words like 'NQC'...)

Anyway, after dusting off the RCX I'm giving NQC a go and I seem to have a problem with a global variable. I can init a global variable and I can set its value in task main. However when I start a second task from main and I try to change the value of my global var in the second task then nothing happens...

So what I'm doing is this:

int x;

task main () {

x = 10;

start task_2;

}

task task_2() {

x = 20;

}

I would expect x to be 20 after running this but it's 10.

Any idea what I'm doing wrong?

Posted (edited)

I couldn't find a topic on this subject (although search is not easy with a search engine not allowing 3-letter words like 'NQC'...)

Anyway, after dusting off the RCX I'm giving NQC a go and I seem to have a problem with a global variable. I can init a global variable and I can set its value in task main. However when I start a second task from main and I try to change the value of my global var in the second task then nothing happens...

So what I'm doing is this:

int x;

task main () {

x = 10;

start task_2;

}

task task_2() {

x = 20;

}

I would expect x to be 20 after running this but it's 10.

Any idea what I'm doing wrong?

int x is declared globally so in task task_2() you are overwriting x from x = 10 to x = 20. We would expect to get 20 in this case. What's the programming language you are using? Can't you return a value from that function like int task_2() { return 20; }?

Edited by Ultimatemau
Posted

int x is declared globally so in task task_2() you are overwriting x from x = 10 to x = 20. We would expect to get 20 in this case. What's the programming language you are using? Can't you return a value from that function like int task_2() { return 20; }?

That's exactly the problem; I would expect the value to be changed to 20 but that doesn't happen.

I'm writing this in NQC in Bricx Command Center 3.3.

I've changed the program to do things a different way but I'd still like to know if this is a known issue or whether I'm doing something wrong...

Posted

That's exactly the problem; I would expect the value to be changed to 20 but that doesn't happen.

I'm writing this in NQC in Bricx Command Center 3.3.

I've changed the program to do things a different way but I'd still like to know if this is a known issue or whether I'm doing something wrong...

I have no experience with this program whatsoever, but could it be that the language is very sensitive,

and that using the word "task" (task_2) to declare the second function gives the problem.

Posted

It all looks OK. I don't have an RCX block to test and have loaned out my NXT.

Cheeky question, but is the code you've posted exactly the same as what you're trying to achieve? If you've simplified the example for posting then could there be a typo in your original?

The NQC Progrmaming Guide also mentions that there's a limit of 32 global variables, but doesn't say what the behaviour if exceeded is. Could you be hitting anything like that?

Posted

It all looks OK. I don't have an RCX block to test and have loaned out my NXT.

Cheeky question, but is the code you've posted exactly the same as what you're trying to achieve? If you've simplified the example for posting then could there be a typo in your original?

The NQC Progrmaming Guide also mentions that there's a limit of 32 global variables, but doesn't say what the behaviour if exceeded is. Could you be hitting anything like that?

Thanks for the replies.

Nothing wrong with that cheeky question. I have indeed simplified the code but I've checked the original code for typos (long live Notepad++). It's a small program that only uses a handful of variables so I'm nowhere near the limit of 32.

Posted

Thanks for the replies.

Nothing wrong with that cheeky question. I have indeed simplified the code but I've checked the original code for typos (long live Notepad++). It's a small program that only uses a handful of variables so I'm nowhere near the limit of 32.

Sorry for bumping this up:

Duq,

has this been resolved? I pulled out one RCX1.0, one RCX2.0, and a SCOUT PBrick (The RCX' are running the same firmware, firm0328.lgo. That is to my knowledge the last one TLC has released - the SCOUT just came with one firmware).

All PBricks do show the result "20" after running the program. Once the start task(2) is commented, "10" is indeed the result. I used the "Watching the brick" feature.

I have initialized the variable x ("int x=0;") - does that make any difference? Could timing be an issue? Sometimes multi tasking behaves a little weird on these bricks. A delay of 1 ms before starting task 2 may be worth a try.

Anyways, on my 3 PBricks your code runs fine.

Regards,

Thorsten

Posted

Thanks for testing that Thorsten. I'm planning to have another look at this next week. I'll post my updates here.

Posted

I've never programmed using NQC but I had a look at the guide

http://bricxcc.sourceforge.net/nqc/doc/NQC_Guide.pdf

Tasks aren't very well explained. But tasks are some form of multi-tasking. Perhaps you have some race condition here. How did you check that x was 10 or 20?

int x;

task main () {
x = 10;
start task_2;
// x Check here?
}

task task_2() {
x = 20;
}

If you checked where I put the comment (which seams logical as it is the main task. So if the check was placed there that code could very well be executed before task_2 had begun. If you want some linear flow you should use functions instead of tasks. If a function you call doesn't start any tasks you should have the contract that what happens in the function happens before the return of the execution to the caller (and what it continues to do).

So you can probably write it as

int x;

task main () {
x = 10;
myFunction();
// x will be 20 here
}

void myFunction() {
x = 20;
}

But as I said, I never (I think, perhaps I have in real time system course) programmed in NQC. Only using my programming skills to reason about the problem at hand.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...