Society of Robots - Robot Forum
Software => Software => Topic started by: greywanderer012345 on August 14, 2012, 01:39:27 AM
-
I wrote some code similar to admin's to send pulses to my servos, however, I used _delay_us() instead of a for loop. This method has been great so far, and I can get very accurate speeds as I desire. However, until now I have been feeding this delay function a constant. I just wrote the rest of my program how I wanted it, but found that the servos constantly rotated at full speed clockwise. Upon investigation I have found that the delay produced by _delay_us() is much longer than the delay I'm asking for.
This code works:
PORTD|=0b00000001;
_delay_us(1000);
PORTD=0b00010000;
This code does not
Ldelay=1000;
PORTD|=0b00000001;
_delay_us(Ldelay);
PORTD=0b00010000;
When stepping through the debugger, the latter code produces a seemingly random delay of 5-8ms. I am sending the correct datatype (double) to the function. I know there are other ways to send a pulse to the servos, but I want to make this work. Is the _delay_us() function broken? Would I likely get the same result going a level down and using _delay_loop_2() ?
It took me about an hour to narrow down the problem. I don't understand why a function is treating a variable any differently than a constant of equal value. Thanks for any help.
-
What microcontroller platform/toolchain/libraries are you using?
The one thing that jumps out at me is that I've never seen delay_us/delay_ms functions take doubles/floats... It's always some form of an integer.
-
I'm using an atmega328p, AVRStudio4, with the same setup as described for the $50 robot tutorial. I started out using int as the datatype, and changed it to double after viewing the util/delay.h documentation here.
http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html (http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html)
-
Ahh I missed the "similar to admin's" part initially.
Indeed, you're right about it asking for a double... I'm curious whether it compiles when you make Ldelay an integer?
I've not done the $50 robot myself, so I'm not familiar with its code. My inclination were I to do the project, though, would be to check out Webbotlib's support for the $50 robot, as that should save you many hassles.
-
I've answered in your other (double) post ;) at http://www.societyofrobots.com/robotforum/index.php?topic=15926.0 (http://www.societyofrobots.com/robotforum/index.php?topic=15926.0)