Software > Software

About the values used in the DELAY_CYCLES command in the photovore_v1.h

(1/1)

paulobrasko:
Hello there!

I am a newbie but after done some “homework” I looked back to the photovore_v1 source code and I have questions in the following portions of the code.

in the header (.h) file…

void delay_cycles(unsigned long int cycles)
{
   while(cycles > 0)
      cycles--;
}
//***************************************

void servo_left(signed long int speed)
{
   PORT_ON(PORTD, 0);
   delay_cycles(speed);
   PORT_OFF(PORTD, 0);//keep off
   delay_cycles(200);
}

void servo_right(signed long int speed)
{
   PORT_ON(PORTD, 1);
   delay_cycles(speed);      
   PORT_OFF(PORTD, 1);//keep off
   delay_cycles(200);
}

and in the c file...

delay_cycles(500);//a small delay to prevent crazy oscillations


The questions are:

a) Why do we have the command “delay_cycles(200) after turning port D to zero? Why is it necessary?

b) From my readings on servo-motor specs, I believe that the signal that we are sending to the motor, such as 1.5 ms of high voltage, must be “modulated” in a 50 ms signal. Therefore, if we send a 1.0 ms of high voltage making the motor to go full left position, then we need to make the next 49 ms with low voltage and then after it, and repeat this sequence over and over again if you want to keep this position. If we apply 2.0 ms of high voltage, then we need only 48 ms. Assuming that the above statement is correct, then here comes my question:

b1) the servo_right and servo_left have the same last line (delay_cycles(200)). I believe that they should be different to account for the differences in the length of time (48 ms and 49 ms explained above). Is it right?

b2) also, there is a delay_cycles(500) in the last line of the while(1) loop.
Adding up for those delays for any turn, such as left turn, we get the following numbers: one servo will have a total of delay cycles of 745 and the other 725, what gives a total of ~32 and 31 milliseconds respectively. The question then is: where is the rest of milliseconds that the microprocessor should have to wait to add up the 50 ms modulation accounted in this code?

c) Last but not the least…  to calculate the number of cycles to be delayed, the code states the following:

23/.992*(time in milliseconds) = number of cycles

I could not figure out from where the “23” and the “0.992” came from. Could any one of you give me a reasonable explanation? Thanks.


Well that is it. If you know any of the above questions, please give me a feedback.

Regards,

Paulo Brasko

Admin:

--- Quote ---Why do we have the command “delay_cycles(200) after turning port D to zero? Why is it necessary?
--- End quote ---
Its not required in that particular location, but a servo can only be sent a signal every 10-20ms. If you send signals more often, the servo can become damaged. I put that there just as a safety for beginners.



--- Quote ---From my readings on servo-motor specs, I believe that the signal that we are sending to the motor, such as 1.5 ms of high voltage, must be “modulated” in a 50 ms signal. Therefore, if we send a 1.0 ms of high voltage making the motor to go full left position, then we need to make the next 49 ms with low voltage and then after it, and repeat this sequence over and over again if you want to keep this position. If we apply 2.0 ms of high voltage, then we need only 48 ms.
--- End quote ---
Refer to my previous sentence. I've managed to get a Futaba servo down to 10ms, but it really depends on the servo you are using. I never fully tested the HS-311.


--- Quote ---b1) the servo_right and servo_left have the same last line (delay_cycles(200)). I believe that they should be different to account for the differences in the length of time (48 ms and 49 ms explained above). Is it right?
--- End quote ---
it doesn't really matter. really, you want to minimize the 'off' time for a servo. if you wait too long to send another command, the servo will turn off and not produce any torque.


--- Quote ---also, there is a delay_cycles(500) in the last line of the while(1) loop.
--- End quote ---
Just another catch-all thing I added to help beginners. Not really needed, but can fix a few potential problems caused by going 'too fast' on hardware.


--- Quote ---Last but not the least…  to calculate the number of cycles to be delayed, the code states the following:

23/.992*(time in milliseconds) = number of cycles

I could not figure out from where the “23” and the “0.992” came from. Could any one of you give me a reasonable explanation? Thanks.
--- End quote ---
I hooked it up to an oscilloscope. There is a way to calculate it too, but it varies with attached hardware so oscope's are quick/reliable/easy to do.

paulobrasko:
thanks for your reply.
Have a happy new year!

Navigation

[0] Message Index

Go to full version