### Author Topic: cycles timing question  (Read 7474 times)

0 Members and 1 Guest are viewing this topic.

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### cycles timing question
« on: August 16, 2008, 11:16:12 PM »
23/.992*(time in milliseconds) = number of cycles

From where do we get the 23/.992 ?

Also, how would this formula change if the microcontroller was running at 16mHz

Btw I am writing a function for the Axon that is mS based instead of cycles , just to make it easier for me. I might write a function for angle also , once I have the mS done . Easier for me to go from mS to angle , then from cycles to angle .

Thanks
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

• Supreme Robot
• Posts: 11,657
##### cycles timing question
« Reply #1 on: August 17, 2008, 11:17:31 AM »
Quote
From where do we get the 23/.992 ?
My oscilloscope.

Quote
Also, how would this formula change if the microcontroller was running at 16mHz
If you are going from 1 MHz to 16 MHz, it should be:
23/.992*(16MHz/1MHz)*(time in milliseconds) = number of cycles

AVRlib has these functions that will already work on the Axon:
delay_ms(1000);
delay_us(1000);

However, I remember that AVRlib had some bug that if you put a variable in it like this:
delay_us(variable);
that it won't work if the variable went above 255. So thats why I created the cycles method for servos. Plus, on the \$50 Robot, timer interrupts are limited.

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### cycles timing question
« Reply #2 on: August 17, 2008, 12:40:17 PM »
can AVRlib deal with fractions? ]

If I write a  macro for mS, can I do
Code: [Select]
`servo(1.5)`
Or can it not handle the 1.5?

Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

• Supreme Robot
• Posts: 11,657
##### cycles timing question
« Reply #3 on: August 17, 2008, 04:53:50 PM »
Quote
If I write a  macro for mS, can I do
Code: [Select]
`servo(1.5)`Or can it not handle the 1.5?

I just tried both of these:
Code: [Select]
`PORT_ON(PORTE, 6);delay_ms(1.9);PORT_OFF(PORTE, 6);delay_ms(1);`and
Code: [Select]
`PORT_ON(PORTE, 6);delay_ms(1);PORT_OFF(PORTE, 6);delay_ms(1);`
and got a frequency of 471 Hz for both.

This also shows that the ATmega640 clock isn't perfect (5.8% error), as it should have been 500Hz. The \$50 Robot clock will not be perfect either.

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #4 on: August 17, 2008, 07:48:35 PM »
However, I remember that AVRlib had some bug that if you put a variable in it like this:
delay_us(variable);
that it won't work if the variable went above 255. So thats why I created the cycles method for servos. Plus, on the \$50 Robot, timer interrupts are limited.

does that mean that this wont work? Or is the bug fixed already?

#define servo(port,number,position)   (PORT_ON(port,number), delay_us(position), PORT_OFF(port,number))

I made it time based instead of cycles based - its just easier for me .
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### pomprocker

• Supreme Robot
• Posts: 1,430
• Sorry miss, I was giving myself an oil-job.
##### Re: cycles timing question
« Reply #5 on: August 18, 2008, 10:42:50 AM »
just use 1500uS instead of 1.5mS

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #6 on: August 18, 2008, 03:25:58 PM »
just use 1500uS instead of 1.5mS

yea that was what I was planning to use but admin said there was a bug for using variables above 255 in delay_us.  I wanted to know if the macro would work since its not really a variable , but rather passing numbers - i think.

Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### pomprocker

• Supreme Robot
• Posts: 1,430
• Sorry miss, I was giving myself an oil-job.
##### Re: cycles timing question
« Reply #7 on: August 18, 2008, 03:34:37 PM »

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #8 on: August 18, 2008, 09:03:44 PM »
will this work :

Code: [Select]
`Set up code:'set up servo macro#define servo(port,number,position)   (PORT_ON(port,number), delay_us(position), PORT_OFF(port,number))'hardware define:#define servo1( position ) servo(PORTE,6,position)`
Then when I want to control the servo :
Code: [Select]
`servo1(1500)`
The only reason I can see for it not to work is because of that bug Admin mentioned. But I was wondering if one of the more seasoned programmers can help me out.

Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### pomprocker

• Supreme Robot
• Posts: 1,430
• Sorry miss, I was giving myself an oil-job.
##### Re: cycles timing question
« Reply #9 on: August 18, 2008, 11:42:10 PM »
instead of using software based pwm, why don't you use the pwm capabilities built into the avr?

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #10 on: August 19, 2008, 05:24:22 AM »
instead of using software based pwm, why don't you use the pwm capabilities built into the avr?

Because there are only 9 PWM channels but I have a lot of servos
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### Ro-Bot-X

• Contest Winner
• Supreme Robot
• Posts: 1,431
• Store: RoBotXDesigns.ca
##### Re: cycles timing question
« Reply #11 on: August 19, 2008, 05:45:04 AM »
Arduino has a ServoTimeTimer1 that works like you described: Servo1(1500). Check it out here: http://www.arduino.cc/playground/ComponentLib/Servotimetimer1
Check out the uBotino robot controller!

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #12 on: August 19, 2008, 06:21:25 AM »
thanks RobotX but I think the code is pin specific ,

Will that code I posted above work?
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### JesseWelling

• Expert Roboticist
• Supreme Robot
• Posts: 707
• Only You Can Build A Robot!
##### Re: cycles timing question
« Reply #13 on: August 19, 2008, 06:50:34 AM »
I'm working on a configurable PWM C code that is somewhat object oriented and utilizes hardware timers but It's not quite working yet. Any one who wants to look at it can but to make it compile you'd have to take out a bunch of debug stuff. And if any one can find the bug before I can let me know. Once I get this working I need to make it a more general solution (For any F_CPU) and then Add support for more than one timer.

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #14 on: August 20, 2008, 05:28:29 AM »
What I want to do is to be able to control the servos the same way that the SSC-32 controls it.
Its command to control the servos are :
Code: [Select]
`'RA,                    RK,      RH,       LA,                LK,                 LH,        speed#0 P1427      #1 P1292   #2 P1292  #16 P1573    #17 P1708    #18 P1708   T500`Where RA is right ankle,RK is Right Knee, RH is Right Hip, etc.
The pulse is in uS as you can see by the " P1427" which is a pulse for 1427 uS.

I want to control the servos from the Axon in a way similar to this . I want a macro that does servo1(1427) . Can anyone help me to write this macro? I still don't know if a bug exists for having a variable larger than 255 in a delay_us macro.

And I'd rather not use UART from the Axon to control the SSC-32 , I want it all done on the Axon.
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### pomprocker

• Supreme Robot
• Posts: 1,430
• Sorry miss, I was giving myself an oil-job.
##### Re: cycles timing question
« Reply #15 on: August 20, 2008, 11:52:40 AM »
can you write your own function

Code: [Select]
`void pulse(uint8_t microseconds) {     while (microseconds > 0) {          delay_us(1);          microsecs--;     }  // end while loop} // end pulse function `
« Last Edit: August 20, 2008, 11:53:17 AM by pomprocker »

#### paulstreats

• Supreme Robot
• Posts: 1,381
##### Re: cycles timing question
« Reply #16 on: August 20, 2008, 05:29:03 PM »
The way I got around it in Pic18's running a 20mhz clock was like the sample below.

Basically you call the function and pass the degree that you want the servo to turn to as the parameter (0 = far right. 180 = far left on servo. 90= servo centered). so i would write move_servo(90) to center the servo.

The thing is that its a freak occurence that running a 20mhz clock produces the correct servo angle when the angle degree is multiplied by 4 (in the PIC because it delays according to instruction cycles). But maybe it can be modified to your needs...

Code: [Select]
`void move_servo(unsigned int duty){ SERVO1 = 1; Delay10TCYx(duty); Delay10TCYx(duty); Delay10TCYx(duty); Delay10TCYx(duty); SERVO1 = 0;}`
There is obviously a small loss in precision by having to call the delay 4 times but its not going to be huge.

Doing it this way means that nothing else can happen while the servo is being pulsed. If you are wanting to do that then you need some serious speed.

Try to find the common denominator between the delay functions and the servo angles and you will eventually come up with something that will work for you.

• Supreme Robot
• Posts: 11,657
##### Re: cycles timing question
« Reply #17 on: August 22, 2008, 08:56:41 AM »
You could just write a quickie function that converts milliseconds to cycles . . .

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #18 on: August 22, 2008, 08:58:30 AM »
You could just write a quickie function that converts milliseconds to cycles . . .

I'm confused

Didn't you say that the Axon cannot do 23/.992 since it will round up and will end up being 23/1

I want it accurate also.
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

• Supreme Robot
• Posts: 11,657
##### Re: cycles timing question
« Reply #19 on: August 22, 2008, 09:07:00 AM »
23/.992*(time in milliseconds) = number of cycles

Lets say you want 1.5ms, then you get 34 cycles.

Or 1.1ms, 25. Or 2ms, 46.

This is already way more accurate than your servos will be, trust me

The Axon runs 16 times faster than the \$50 Robot, so you'll get 16x higher accuracy on the cycles method.

#### pomprocker

• Supreme Robot
• Posts: 1,430
• Sorry miss, I was giving myself an oil-job.
##### Re: cycles timing question
« Reply #20 on: August 22, 2008, 09:52:13 AM »
I'm no expert but I was reading on AVRfreaks that servo delays are for n00bs, and that the proper thing to do would be to use timers. The higher resolution the timer the better.

• Supreme Robot
• Posts: 11,657
##### Re: cycles timing question
« Reply #21 on: August 22, 2008, 10:46:42 AM »
Nope not for n00bs. It has a lot to do with your processing requirements, if you have other interrupts running, how many servos you plan to use, how many timers you have, etc.

For example, if you only have 4 servos, thats only a ~25ms delay per second. Hardly worth the frustration of implementing timer interrupts.

Now if you are making a 25 servo biped with tons of sensor processing, well thats another story . . .

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #22 on: August 22, 2008, 03:22:14 PM »
I did what pomprocker suggested. Thanks!!!

Heres the macro:
Code: [Select]
`void pulse(uint8_t microseconds) {     while (microseconds > 0) {          delay_us(1);          microseconds--;     }  // end while loop} // end pulse function `

and heres the modified servo define code:
Code: [Select]
`//define the servo function macro#define servo(port,number,position)   (PORT_ON(port,number), pulse(position), PORT_OFF(port,number))`
To call the macro I just do :
Code: [Select]
` servo_1(1500)  // where 1500 is the pulse in microseconds.`

Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

• Supreme Robot
• Posts: 11,657
##### Re: cycles timing question
« Reply #23 on: August 22, 2008, 03:55:03 PM »
Looks exactly like my cycles code, but with a timer delay inside of it . . . I not sure which is more accurate, probably the only way to know is by comparing both with an oscope . . .

Code: [Select]
`void pulse(uint8_t microseconds) {     while (microseconds > 0) {          delay_us(1);          microseconds--;     }  // end while loop} // end pulse function `
my cycles code (with your variables):
Code: [Select]
`void pulse(uint8_t microseconds) {     while (microseconds > 0) {          microseconds--;     }  // end while loop} // end pulse function `

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #24 on: August 22, 2008, 04:06:46 PM »
one second

the servos are turning but no matter what I give them 1000 - 2000 they will always turn the most extreme 2000 point. This reminds me of my PULSOUT problems the PIC microcontroller where the oscillator speed caused issues.

I'll get back to you guys once I try a solution.
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,653
• narobo.com
##### Re: cycles timing question
« Reply #25 on: August 22, 2008, 04:10:28 PM »
found the issue :

Exactly the same problem with my PIC . Dividing the amount in microseconds by 16 solves the problem. I think delay_us might be faulty.

This works

Code: [Select]
`void pulse(unsigned long int microseconds) {     microseconds=microseconds/16;     while (microseconds > 0) {          delay_us(1);          microseconds--;     }  // end while loop} // end pulse function `
Btw I changed it to unsigned long int b/c its easier for me to understand the code at a glance
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### dunk

• Expert Roboticist
• Supreme Robot
• Posts: 1,086
##### Re: cycles timing question
« Reply #26 on: August 22, 2008, 05:41:54 PM »
so i'm no C expert but you guys have me thinking about C data types...

Airman: when you first tried to use the "uint8_t" type to define "microseconds" you were limiting that variable to 1 bite = 8 bits = a maximum value of 255.
the type "unsigned long int" you use later is possibly a little excessive allowing 4 bites = max value of 4294967295. but hey, you have plenty of memory on the AVR right?

Admin: when you were talking about the bug with delay_us accepting  a max value of 255 near the start of this thread, delay_us in AVRlib is written to accept the type "unsigned short" which i  believe is the same as "unsigned short int" should be good for 2 bytes...
this strikes me as a compiler issue rather than a AVRlib issue.
what compiler do you use?
i'm guessing you could chance AVRlib's timer.c to make delay_us use type "int" instead of "short int" as a workaround.

dunk.

#### pomprocker

• Supreme Robot
• Posts: 1,430
• Sorry miss, I was giving myself an oil-job.
##### Re: cycles timing question
« Reply #27 on: August 22, 2008, 05:58:50 PM »

• Supreme Robot
• Posts: 11,657
##### Re: cycles timing question
« Reply #28 on: August 22, 2008, 06:11:16 PM »
Quote
so i'm no C expert but . . . the type "unsigned long int" you use later is possibly a little excessive allowing 4 bites = max value of 4294967295.
Isn't it 65535? And isn't it spelled 'byte'? Don't you work for google?

Quote
Admin: when you were talking about the bug with delay_us accepting  a max value of 255 near the start of this thread, delay_us in AVRlib is written to accept the type "unsigned short" which i  believe is the same as "unsigned short int" should be good for 2 bytes...
this strikes me as a compiler issue rather than a AVRlib issue.
what compiler do you use?
i'm guessing you could chance AVRlib's timer.c to make delay_us use type "int" instead of "short int" as a workaround.
I should reclarify that I first read and I *think* even experienced this problem about 1.5-2 years ago. I don't really remember anymore, shoddy memory from the days I was an AVR noob . . . but yea I think if I change it to 'long int' it should work . . . Im using the gcc compiler. I'll try it out when I have time and add it to the next Axon software upgrade if it works.

#### dunk

• Expert Roboticist
• Supreme Robot
• Posts: 1,086
##### Re: cycles timing question
« Reply #29 on: August 22, 2008, 06:20:00 PM »
Quote
Isn't it 65535
65535 would be 2 bytes.
4 bytes = max value of 0xFFFFFFFF = 4294967295.

Quote
And isn't it spelled 'byte'?
bahh. fix your website's spilling chacker.

dunk.
« Last Edit: August 22, 2008, 06:22:24 PM by dunk »