View previous topic :: View next topic |
Author |
Message |
switchblademaster Guest
|
restarting loop |
Posted: Tue Dec 01, 2009 10:53 am |
|
|
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
|
Re: restarting loop |
Posted: Tue Dec 01, 2009 10:59 am |
|
|
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
otherwise no wonder your compiler sees the rest of your code as gibberish. |
|
|
switchblademaster Guest
|
|
Posted: Tue Dec 01, 2009 11:00 am |
|
|
yea i mean ( i==1) sorry about that |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Dec 01, 2009 11:38 am |
|
|
Why don't you post a complete test program? |
|
|
switchblademaster Guest
|
|
Posted: Tue Dec 01, 2009 12:01 pm |
|
|
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
|
|
Posted: Tue Dec 01, 2009 12:22 pm |
|
|
Do you still get the "code has no effect" error after fixing the "i == 1" bug right after "void obstacle ()" ? |
|
|
switchblademaster Guest
|
|
Posted: Tue Dec 01, 2009 12:30 pm |
|
|
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
|
|
Posted: Tue Dec 01, 2009 12:45 pm |
|
|
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:
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
|
|
Posted: Tue Dec 01, 2009 12:47 pm |
|
|
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
|
|
Posted: Tue Dec 01, 2009 12:48 pm |
|
|
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
|
|
Posted: Tue Dec 01, 2009 12:49 pm |
|
|
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
|
|
Posted: Tue Dec 01, 2009 12:58 pm |
|
|
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
|
|
Posted: Tue Dec 01, 2009 3:47 pm |
|
|
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
|
Re: restarting loop |
Posted: Tue Dec 01, 2009 4:06 pm |
|
|
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
|
|
Posted: Tue Dec 01, 2009 6:11 pm |
|
|
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 |
|
|
|