To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.legosOpen lugnet.robotics.rcx.legos in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / legOS / 2705
2704  |  2706
Subject: 
Re: HOWTO Demo.c
Newsgroups: 
lugnet.robotics.rcx.legos
Date: 
Wed, 10 Jul 2002 21:05:58 GMT
Viewed: 
2097 times
  
To solve your problems with demo.c, you sent me your source. There are some
problems with it, but I think they are intresting enough to say something
about them.

The big problem you get with your program is caused by your main function:

----------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
pid1=execi(&task_swapper,0,NULL,1,DEFAULT_STACK_SIZE);
return 0;
}
--------------------------------------------------------------------------------------

This causes all your problems. The reason is, that you exec the task swapper
process, but then YOU EXIT YOUR PROGRAM!
There was a bug in the reaper, some legOS versions ago. The reaper is the
procedure that should clean the memory used by a program after the end of
the program. The reaper didn't do that and that caused a memory leak every
time you had run your user program. Nowadays, the reaper behaves as
expected and frees all user memory. So in your program, the tasks started
by the task_swapper will try to use memory formerly allocated by your main
routine beeing now void. This could work, sometimes, but normaly your
program wil crash badly (what your program does).

You can do two things now. First, put all the task_swapper functionality in
main and sweep out the task_swapper. If you want to keep the program logic,
you may introduce a new to-be-watched variable:

-------------------------------------------------------------------------------------
...
int ready;

/*
button press functions
*/

wakeup_t button_press_wakeup(wakeup_t data) {
return PRESSED(dbutton(),data);
}

wakeup_t button_release_wakeup(wakeup_t data) {
return RELEASED(dbutton(),data);
}

wakeup_t done_it( wakeup_t data )
{
   return( ready == 1 );
}

int main(int argc, char *argv[])
{
   ready = 0;
   pid1 = execi(&task_swapper,0,NULL,1,DEFAULT_STACK_SIZE);
   wait_event( done_it, 0 );
return 0;
}

-----------------------------------------------------------------------------------

now you must insert

   ready = 1;

as the last statement in your task_swapper function. Main() will wait for
ready to become 1 (via the done_it wakeup function) and exits then.

This will make your program work, mostly. There is a subtle bug in your
touch_sensor function. Your write the following:

-----------------------------------------------------------------------------------

void touch_sensor()
{
cputs("touch"); /*indicate which function we are in*/
msleep(500);
motor_a_speed(100);
motor_c_speed(100);
while(1)
{
motor_a_dir(fwd);
motor_c_dir(fwd);
cputs(TOUCH_1); /*output sensor value*/
if(TOUCH_1!=0) /*if sensor has been touched*/
{
motor_a_dir(rev);
motor_c_dir(rev);
sleep(1);
}
}
}

------------------------------------------------------------------------------------

you cputs a non-string variable. You better do an lcd_int( TOUCH_1) or drop
this statement completly. But then, you can't stop touch_sensor by pressing
the view button most of the time. If you change the cputs( TOUCH_1) to
msleep(100) or yield(), your program will run as you expected.

Hope it helps, regards,

Michael



Message has 1 Reply:
  Re: HOWTO Demo.c
 
Brilliant! Brilliant! It works! Many Thanks to Micheal Obenland and also to Steve Hassenplug Best Regards Shehryar "Michael Obenland" <obenland@t-online.de> wrote in message news:Gz1xAM.Inn@lugnet.com... (...) some (...) ---...--- (...) ---...--- (...) (22 years ago, 11-Jul-02, to lugnet.robotics.rcx.legos)

Message is in Reply To:
  HOWTO Demo.c
 
Need Help, if someone has faced such a problem or knows the fix please do reply. In the legOS Howto their is a demo code demo.c which has some task switching examples I am having trouble with the following piece of code int task_swapper() { (...) (22 years ago, 9-Jul-02, to lugnet.robotics.rcx.legos)

3 Messages in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR