View unanswered posts | View active topics It is currently Sat Nov 22, 2014 8:51 pm






Reply to topic  [ 14 posts ] 
Debug my code, No errors, but the execution is incorrect 
Author Message
Rookie

Joined: Sat Nov 01, 2008 3:08 pm
Posts: 4
Post 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 200 times
Sat Nov 01, 2008 3:28 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sat Nov 01, 2008 4:33 pm
Profile
Professor

Joined: Fri Sep 19, 2008 1:22 am
Posts: 200
Post 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.

_________________
Jeff McBride
Benson Robotics Club


Sat Nov 01, 2008 5:12 pm
Profile
Rookie

Joined: Sat Nov 01, 2008 3:08 pm
Posts: 4
Post 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 191 times
Sun Nov 02, 2008 2:30 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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:

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sun Nov 02, 2008 6:23 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post 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

_________________
| 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 Nov 02, 2008 9:27 am
Profile WWW
Rookie

Joined: Sat Nov 01, 2008 3:08 pm
Posts: 4
Post 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).


Sun Nov 02, 2008 6:09 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post 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

_________________
| 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 Nov 02, 2008 6:20 pm
Profile WWW
Rookie

Joined: Sat Oct 25, 2008 3:47 pm
Posts: 38
Post 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?


Sun Nov 02, 2008 6:22 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3289
Location: Rotterdam, The Netherlands
Post 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

_________________
| 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 Nov 02, 2008 6:30 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Sun Nov 02, 2008 6:50 pm
Profile
Rookie

Joined: Sat Oct 25, 2008 3:47 pm
Posts: 38
Post 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 ?


Mon Nov 03, 2008 4:22 am
Profile
Rookie

Joined: Sat Nov 01, 2008 3:08 pm
Posts: 4
Post 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.


Mon Nov 03, 2008 10:30 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Jan 31, 2007 3:39 am
Posts: 299
Location: San Diego, California. USA
Post 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);
}

_________________
Mmmm Legos B-)

My Robot Projects:
http://www.freewebs.com/robotprojects/


Tue Nov 04, 2008 7:43 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 14 posts ] 

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.