Society of Robots - Robot Forum

Software => Software => Topic started by: ert481 on August 07, 2008, 10:19:35 AM

Title: delay function
Post by: ert481 on August 07, 2008, 10:19:35 AM
$50 robot

with the C code,  delay_cycles();

does the robot go forward and then when delay runs out it goes to the next thing?

or does the delay pause everything and when it runs then go to the next thing?

i want it to do the first thing that i said but when i program it it seems like it does the second thing i said.

if it does the second thing can you tell me how to make it do the first thing i said?   ::)

thanks
Title: Re: delay function
Post by: pomprocker on August 07, 2008, 11:14:20 AM
its emulating a pulse width required by the servos.

there is a PWM channel on the avr, but to make life simpler we just bring the voltage high on that pin, wait, then bring it low. thats a pulse width.

+5vdc being high, 0vdc being low.
Title: Re: delay function
Post by: ert481 on August 07, 2008, 11:55:32 AM
i get it but it doesn't answer the question i asked
Title: Re: delay function
Post by: pomprocker on August 07, 2008, 12:06:12 PM
you need to put your C code example in context with the rest of the code in order for me to tell whats its doing so i can help you.

You use the button above marked with the pound sign (#) to post code.
Title: Re: delay function
Post by: pomprocker on August 07, 2008, 12:14:11 PM
the delay function does a decremented count. to get your robot to go foward you would have to put it in a nested loop


Code: [Select]

while(1) {
     unsigned int i;

     for (i = 0; i < 500; i++)
          robot_go_straight();

}  // end while loop






that would make your robot go forward for a count of 500 cycles then go on to the next thing, but in my example there is no next thing so the while loop would start again and it would go forward another 500 cycles again and again.

Title: Re: delay function
Post by: izua on August 07, 2008, 12:49:42 PM
There is no technical difference between the cases you specified:
a)does the robot go forward and then when delay runs out it goes to the next thing?
b)or does the delay pause everything and when it runs then go to the next thing?

because you set pin outputs, the delay pauses everything, and when the delay runs out, it resumes. "go forward" is relative to that code. it may either set a pin, create a servo signal, etc.
Title: Re: delay function
Post by: Admin on August 09, 2008, 07:56:28 AM
The delay_cycles(); pauses everything, as in nothing changes.
Title: Re: delay function
Post by: ert481 on August 09, 2008, 02:47:01 PM
pomprocker, how would i add two things? can you give me the C code for that? do i do a whole nother while loop or put it in the same loop?  :)
Title: Re: delay function
Post by: pomprocker on August 09, 2008, 04:22:39 PM
when doing embedded programming, you always have to put your main code inside an infinite loop. whether it be
Code: [Select]
while (1){}
or
for (;;){}

because unlike a computer program which ends, and embedded program should never end until your flip the power switch to off.


Here is some code that would theoretically make it do something and wind up where it started

Code: [Select]

void main {

while(1) {
     unsigned int i;

     for (i = 0; i < 500; i++)
          robot_go_straight();

     for (i=0; i < 500; i++)
          robot_go_back();

}  // end while loop
}  // end main




You would have to add the robot_go_back function in SoR_Utils.h.
Just copy robot_go_straight and paste it. Change the name and reverse the values in servo left and right.
Title: Re: delay function
Post by: ert481 on August 09, 2008, 06:10:21 PM
thanks ill try it if it doesn't work ill hollar
Title: Re: delay function
Post by: ert481 on August 10, 2008, 04:23:59 AM
i tried it and it failed. these are the errors i get.

Photovore_v1.c:54: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

Photovore_v1.c:72: error: expected declaration or statement at end of input
Title: Re: delay function
Post by: ert481 on August 10, 2008, 05:38:07 AM
hey i got it to work. i just took out: void main  and it worked (yeah) :D