ROBOTC.net forums
http://robotc.net/forums/

Debug my code, No errors, but the execution is incorrect
http://robotc.net/forums/viewtopic.php?f=1&t=873
Page 1 of 1

Author:  afruff23 [ Sat Nov 01, 2008 3:28 pm ]
Post subject:  Debug my code, No errors, but the execution is incorrect

Hi, I have written a program to maneuver my NXT-controlled hovercraft. The hovercraft uses 4 light sensors placed at the front of the hovercraft with equal spacing in between in order to track a black tape. The motion is controlled by 3 fans: 2 side fans which face outwards for turning and 1 fan that faces backwards for thrust. You can test this code (like I did) on a car by commenting out everything (the attacfehd file already has the necessary parts commented out) that references motor A and using a two motor design with 4 light sensors. If you do this, then you can see that the program just goes through the code once ("Running") and then says ("Done") which is not what should happen. The motors are never even moved.

The compiler gives no errors.

I attached the code.

Attachments:
SourceCodeFINAL.c [4.58 KiB]
Downloaded 237 times

Author:  Ford Prefect [ Sat Nov 01, 2008 4:33 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

hi,
after having started all other tasks, the task main() terminates, and thus all the other tasks are terminated again, too. ;)

you have to keep the task main alive, e.g. by adding
while (true) {}
as the last command line.

HTH!

Author:  Jeff McBride [ Sat Nov 01, 2008 5:12 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

It is a good idea to always include a wait in any infinite loop like that.

while (true)
{
wait10Msec(1);
}

That makes more CPU cycles available for your other tasks.

Author:  afruff23 [ Sun Nov 02, 2008 2:30 am ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

Ford Prefect wrote:
hi,
after having started all other tasks, the task main() terminates, and thus all the other tasks are terminated again, too. ;)

you have to keep the task main alive, e.g. by adding
while (true) {}
as the last command line.

HTH!

So is it fixed now? See the attached file.

Jeff McBride wrote:
It is a good idea to always include a wait in any infinite loop like that.

while (true)
{
wait10Msec(1);
}

That makes more CPU cycles available for your other tasks.


The code I attached is the whole program. I don't think I'll run into CPU cycle issues. If I do, I will keep that in mind.

Attachments:
SourceCodeFINAL.c [4.59 KiB]
Downloaded 239 times

Author:  Ford Prefect [ Sun Nov 02, 2008 6:23 am ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

afruff23 wrote:
So is it fixed now? See the attached file.

you won't expect ME to check YOUR program with MY suggested improvement, do you? :poke:

Author:  mightor [ Sun Nov 02, 2008 9:27 am ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

afruff23,

I must admit, I read the title of this thread and thought "Hrmm, here's one to ignore." This kind of attitude will really get you nowhere fast. The folks on this forum are quite a helpful bunch of knowledgeable programmers. However, your assumption that it is their *job* to help *you* is rather mistaken. They're here on a strictly volunteer basis, with the exception of the actual employees of the RobotC company. There is no "please" or "I'd really appreciate your help on this...", or something to that effect in your OP. Remember, you catch more flies with honey than vinegar.

Have a nice day,
Xander

Author:  afruff23 [ Sun Nov 02, 2008 6:09 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

Ford Prefect wrote:
you won't expect ME to check YOUR program with MY suggested improvement, do you? :poke:


No, that's not what I was expecting at all. Your suggestion was a little vague to me, so I asked you if the changes I made were what you suggested. You said to add a while(true){} to the last execution line. I didn't know if this meant to put that as a standalone line of code or to place the "startTask" commands within it. I am fully aware that I will have to test the execution of the code myself.

Thanks for the help.

mightor wrote:
afruff23,

I must admit, I read the title of this thread and thought "Hrmm, here's one to ignore." This kind of attitude will really get you nowhere fast. The folks on this forum are quite a helpful bunch of knowledgeable programmers. However, your assumption that it is their *job* to help *you* is rather mistaken. They're here on a strictly volunteer basis, with the exception of the actual employees of the RobotC company. There is no "please" or "I'd really appreciate your help on this...", or something to that effect in your OP. Remember, you catch more flies with honey than vinegar.

Have a nice day,
Xander


I never said it was anybody's job to help me. I am quite aware of the fact that people on these forums are volunteers (I have been to many forums of a similar nature like Rockbox.org). I'm sorry that I forgot to include some "thanks" and "pleases"; I wrote this message in haste and it is hard to communicate emotion over the internet (which is why you do not presume to know someone's emotions without direct text quotes).

Author:  mightor [ Sun Nov 02, 2008 6:20 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

afruff23 wrote:
I never said it was anybody's job to help me. I am quite aware of the fact that people on these forums are volunteers (I have been to many forums of a similar nature like Rockbox.org). I'm sorry that I forgot to include some "thanks" and "pleases"; I wrote this message in haste and it is hard to communicate emotion over the internet (which is why you do not presume to know someone's emotions without direct text quotes).

All the more reason to take your time and reflect on how your words could be perceived by others as not everyone on this forum is a native English speaker. Oh well, I guess now you know that rushing a message on a board you're new on is never a good idea :)

As you become more familiar with this place, you'll find us to be more than willing to help you out with even the most challenging programming or hardware problems.

I think they call this baptism by fire, but welcome to the forum :)

Regards,
Xander

Author:  Jetro [ Sun Nov 02, 2008 6:22 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

Ford Prefect wrote:
hi,
after having started all other tasks, the task main() terminates, and thus all the other tasks are terminated again, too. ;)

you have to keep the task main alive, e.g. by adding
while (true) {}
as the last command line.

HTH!

I'm not sure which post to quote, so I've picked this one. I've downloaded the altered code that includes this statement in the last line, and the very first thing RobotC tells me is
Quote:
*Warning*:Possible programming error. Infinite loop (unconditional branch to itself) detected.

I know 'main' is the first task that's started, but does a program necessarily end when main finishes? If main has set another task in motion shouldn't that task continue until it runs its natural course and not suddenly stop when 'main' ends?

Author:  mightor [ Sun Nov 02, 2008 6:30 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

Jetro wrote:
I know 'main' is the first task that's started, but does a program necessarily end when main finishes? If main has set another task in motion shouldn't that task continue until it runs its natural course and not suddenly stop when 'main' ends?

The program always exits when the main task dies. That's by design. As mentioned in an earlier post, it would be better to have something like:
Code:
while (true) {
  wait1Msec(1);
}

You can make the wait time as long as you want, it doesn't matter. Make it 20-100ms or so, whatever you like :) You can safely ignore the infinite loop warning, unless that's not what you want, of course.

Xander

Author:  Ford Prefect [ Sun Nov 02, 2008 6:50 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

Quote:
If main has set another task in motion shouldn't that task continue until it runs its natural course and not suddenly stop when 'main' ends?

yes, I agree, that's the logical and "natural" point of view, and that's the way e.g. also NQC works.
But RobotC it's different, it won't be the last time where you'll have to recognize this.

Ignore the compiler warning, and go ahead.

Much luck!

Author:  Jetro [ Mon Nov 03, 2008 4:22 am ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

thanks to both for your explanation!

afruff23, does you code now work as expected ?

Author:  afruff23 [ Mon Nov 03, 2008 10:30 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

Jetro wrote:
thanks to both for your explanation!

afruff23, does you code now work as expected ?

Yeah, it works fine now. I added a wait 1 millisecond comman within the infinite loop to get rid of the compiler warning.

Author:  starwarslegokid [ Tue Nov 04, 2008 7:43 pm ]
Post subject:  Re: Debug my code, No errors, but the execution is incorrect

Are you sure main will terminate everything? I don't want to throw a monkey wrench into things, but this piece of code does not stop execution even though main has reached the end of its code.

Let me talk to Dick and see what the intent is for program termination, because there may be a bug.

Scott

Code:
task test()
{
    while (true)
    {
         PlayTone(2000, 100);
    }
}


task main()
{
  StartTask(test);
}

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/