CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

restarting loop
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
switchblademaster
Guest







restarting loop
PostPosted: Tue Dec 01, 2009 10:53 am     Reply with quote

so I have a problem implementing a code. I want to restart the loop I am currently in.......if a certain condition becomes true. However, the compiler see it as redundancy. How can I work around this:

void ultrasonic()
{
if (i=1)
{
start routine to avoid obstacle;

if (obstacle is detected)
{
ultrasonic(); //this part is seen as redundancy, how can //i work around this this
}
}
}
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

Re: restarting loop
PostPosted: Tue Dec 01, 2009 10:59 am     Reply with quote

switchblademaster wrote:
so I have a problem implementing a code. I want to restart the loop I am currently in.......if a certain condition becomes true. However, the compiler see it as redundancy. How can I work around this:

void ultrasonic()
{
if (i=1)
{
start routine to avoid obstacle;

if (obstacle is detected)
{
ultrasonic(); //this part is seen as redundancy, how can //i work around this this
}
}
}


You probably mean
Code:

if (i == 1)


otherwise no wonder your compiler sees the rest of your code as gibberish.
switchblademaster
Guest







PostPosted: Tue Dec 01, 2009 11:00 am     Reply with quote

yea i mean ( i==1) sorry about that
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Tue Dec 01, 2009 11:38 am     Reply with quote

Why don't you post a complete test program?
switchblademaster
Guest







PostPosted: Tue Dec 01, 2009 12:01 pm     Reply with quote

Code:
#include <18f4520.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232 (DEBUGGER)

#USE STANDARD_IO (A)

#define MOVE_FORWARD  0
#define MOVE_LEFT     1
#define MOVE_RIGHT    2
#define STOP          4
#define ROTATE_LEFT   5
#define ROTATE_RIGHT  6

#define servo_center  7
#define servo_left    8
#define servo_right   9

#define servo PIN_D5
#define  A1 PIN_D0
#define  A2 PIN_D1
#define  A4 PIN_D2
#define  A3 PIN_D3
#define instruction_time (20000000/4)
#define velocity_cm 34442.4


int32 timer; // travel time for sonar
float distance; // disctance between ultrasonic sensor and obstacle
int32 pin = PIN_C1; // differenct pin for each ultrasonic sensor
int16 act_dist;
int16 overflow_count;
int16 MAX_PWM = 255;
  int steer, rotate, i;

void obstacle();
void LFR_STEER();
void ping_servo();

void LFR_STEER()
   {
      switch(steer)
         {
            case MOVE_FORWARD:
               set_pwm1_duty(MAX_PWM);  //PWM for right motor
               set_pwm2_duty(MAX_PWM); //PWM for left motor
               break;
           
           
            case MOVE_LEFT:
               //left_motor();
               set_pwm1_duty(MAX_PWM);  //PWM for right motor
               set_pwm2_duty(MAX_PWM); //PWM for left motor
               //printf("%u desired \n",value);
               break;
           
            case MOVE_RIGHT:
              //right_motor();
              set_pwm1_duty(MAX_PWM);  //PWM for right motor
              set_pwm2_duty(MAX_PWM);    //PWM for left motor
              //printf("%u desired \n",value);
              break;
           
            case STOP:
              output_low(A1); //stop left motor
              output_low(A4);
              break;
           
            case ROTATE_LEFT:
              set_pwm1_duty(200);  //PWM for right motor
              set_pwm2_duty(200);  //PWM for left motor
              delay_ms(2000);
              break;
                 
            case ROTATE_RIGHT:
               set_pwm1_duty(200);  //PWM for right motor
               set_pwm2_duty(200);  //PWM for left motor
               delay_ms(2000);
               break;
         } 
   }

void ping_servo()
{
     switch (rotate)
      {
         case servo_center:        //center servo
            output_high(PIN_D6);
            break;
         
         case servo_left:     //rotate 90 degree left
            output_high(PIN_D7);
            break;
         
         case servo_right:     //rotate 90 degeree right
            output_high(PIN_D6);
            break;
      }
}

void obstacle()
{
if (i=1)
   {
         steer = STOP;
         LFR_STEER();
         delay_ms(1000);
         steer = MOVE_FORWARD;
         LFR_STEER();
         delay_ms(500);
               
        //ultrasonic();
         
        if(act_dist <= 15)
            {
               steer = MOVE_FORWARD;
               LFR_STEER();
               delay_ms(1000);
               printf("%i steer1\n",steer);
            }

         steer = STOP;
         LFR_STEER();
         delay_ms(500);
         
         steer = ROTATE_LEFT;
         LFR_STEER();
         delay_ms(500);
         
         rotate = servo_right;
         ping_servo();
         printf("%i rotate1\n",rotate);
        // ultrasonic();
         
         if(act_dist <= 15)
           { obstacle();}   //HERE IS WHERE THE PROBLEM IS
         
         steer = STOP;
         LFR_STEER();
         delay_ms(500);
         
         steer = MOVE_FORWARD;
         LFR_STEER();
         delay_ms(1000);
         
         
         steer = STOP;
         LFR_STEER();
         delay_ms(500);
         
         steer = ROTATE_LEFT;
         LFR_STEER();
         delay_ms(1000);
         
       //  ultrasonic();
                 
         steer = MOVE_FORWARD;
         LFR_STEER();
         
         if (input(PIN_A0) == 1)
            {
               steer = ROTATE_RIGHT;
               LFR_STEER();
               i=0;
            }
   }   
}

void main()
{
   while(TRUE)
   {

      setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
      enable_interrupts(int_timer1);
      enable_interrupts(global); // enabling global interrupt controls
      output_low(pin); // pin kept low for 1 ms
      delay_ms(1);
      output_high(pin); // pin kept high for 10us for pulse
      delay_us(10);
      output_low(pin); // pin brought back to low
     
      while(!input(pin)); // wait for signal to go high
      set_timer1(0); // set timer1 to zero and turn it on
      overflow_count = 0; // initialize overflow_count identifier
     
      while(input(pin)); // wait for signal to go low
      disable_interrupts(global); // disable global interrupts
      timer = get_timer1();
      printf("%lu time\n",timer);
      distance = timer*.0000004*343;
      printf("%f distance1\n",distance);
      act_dist = distance * 200;
      printf("%lu distance2\n",act_dist);
      delay_ms(200);
      if (act_dist <= 15)
      {
       i=1;
       obstacle( );
      }

   }
}
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Tue Dec 01, 2009 12:22 pm     Reply with quote

Do you still get the "code has no effect" error after fixing the "i == 1" bug right after "void obstacle ()" ?
switchblademaster
Guest







PostPosted: Tue Dec 01, 2009 12:30 pm     Reply with quote

nope............i just get a redundancy error.
To the effect of you cant call a loop while within the same loop.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Tue Dec 01, 2009 12:45 pm     Reply with quote

After changing the line to if (i==1), the code is not even compilable. I just get this:

Executing: "C:\Program Files\PICC\Ccsc.exe" +FH "deleteme.c" +DF +LN +T +A +M +Z +Y=9 +EA
*** Error 69 "deleteme.c" Line 206(1,2): Recursion not permitted [obstacle]
1 Errors, 0 Warnings.
Halting build on first failure as requested.
BUILD FAILED: Tue Dec 01 13:26:56 2009

Also, inside your obstacle function you have two instances of this statement:

Code:

if(act_dist <= 15)

which looks like a bug. Can you fix that?


Last edited by mkuang on Tue Dec 01, 2009 12:47 pm; edited 1 time in total
switchblademaster
Guest







PostPosted: Tue Dec 01, 2009 12:47 pm     Reply with quote

Recursion not permitted [obstacle]

that is the error i want to try and remove. The thing is remove that one line and the entire thing will compile
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Dec 01, 2009 12:48 pm     Reply with quote

Quote:
i just get a redundancy error
Do you mean "recursion not permitted"? Recursive function call is in fact impossible with PCH compiler, basically due to the limited PIC 18 stack. However, I don't understand at all the purpose of recursive function call in your code. It seems to me, that you are trying to achieve something different but are missing elementary means of C programming. Unfortunately, I don't understand your primary intention from your post.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 01, 2009 12:49 pm     Reply with quote

From the CCS manual:
Quote:

When compared to a more traditional C compiler, PCB, PCM, and PCH
have some limitations. As an example of the limitations, function
recursion is not allowed
. This is due to the fact that the PICĀ®
has no stack to push variables onto, and also because of the way the
compilers optimize the code.
Guest








PostPosted: Tue Dec 01, 2009 12:58 pm     Reply with quote

Sorry I mean a recursion error...............yea that is it
it is not allowed but does anyone have any ideas how to work around this. I am thinking about breaking up that code into parts. I just want to know ppl thoughts on it.
Guest








PostPosted: Tue Dec 01, 2009 3:47 pm     Reply with quote

Hi,

Sorry if this is a quick&stupid answer - haven't checked your code in detail.

Seems to me that main() checks if your too close to something, if so goes to obstacle() to try avoiding it. But there the same check is repeated?
Why not do simple evasive action & go back to main() and let *it* re-call obstacle() if needed?

Regards:

/BdeB
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

Re: restarting loop
PostPosted: Tue Dec 01, 2009 4:06 pm     Reply with quote

switchblademaster wrote:
so I have a problem implementing a code. I want to restart the loop I am currently in.......if a certain condition becomes true. However, the compiler see it as redundancy. How can I work around this:


Wouldn't this accomplish it?
Code:

void ultrasonic()
{
   while (i==1) {
      start routine to avoid obstacle;

      if (obstacle is detected) {
         i=1;
      } else {
         i=0;
      }
   }
}

You don't even need the 'i=1' line but it reads similar to your code.

Mark S.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Dec 01, 2009 6:11 pm     Reply with quote

You _cannot_ call a function when inside that same function.
Code:
void functioncall(void)
{
  functioncall();
}


This is not allowed and that is what you are trying to do. I highly discourage this but you could use a goto statement that will put you at the begining of the function. Goto's are highly discouraged, but possible, when writing code for PICs.

You could return a value to wherever your obstacle() function is called from and check for that value. If it it shows that the function needs to be called again then just have it loop around and call obstacle() again. Maybe something like:

Code:
main()
{

  while(obstacle())
  {
    ;
  }

}

int1 obstacle(void)
{
  code stuff.....

  if(act_dist <= 15)
  {
    return_number = 1;
  }

  if(normal code sucess.......)
  {
    return_number = 0;
  }

  return(return_number);
}


or something like that. It's just an idea.

Clear as mud?

Ronald
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group