go away spammer

Author Topic: _delay_us acting up when given a variable  (Read 2328 times)

0 Members and 1 Guest are viewing this topic.

Offline greywanderer012345Topic starter

  • Robot Overlord
  • ****
  • Posts: 133
  • Helpful? 2
_delay_us acting up when given a variable
« 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:
Code: [Select]
PORTD|=0b00000001;
_delay_us(1000);
PORTD=0b00010000;

This code does not
Code: [Select]
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.
« Last Edit: August 14, 2012, 01:48:34 AM by greywanderer012345 »

Offline Gertlex

  • Supreme Robot
  • *****
  • Posts: 763
  • Helpful? 24
  • Nuclear Engineer · Roboticist
    • Index of Oddities
Re: _delay_us acting up when given a variable
« Reply #1 on: August 14, 2012, 10:16:47 AM »
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

Offline greywanderer012345Topic starter

  • Robot Overlord
  • ****
  • Posts: 133
  • Helpful? 2
Re: _delay_us acting up when given a variable
« Reply #2 on: August 14, 2012, 01:19:15 PM »
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

Offline Gertlex

  • Supreme Robot
  • *****
  • Posts: 763
  • Helpful? 24
  • Nuclear Engineer · Roboticist
    • Index of Oddities
Re: _delay_us acting up when given a variable
« Reply #3 on: August 14, 2012, 02:58:24 PM »
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

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: _delay_us acting up when given a variable
« Reply #4 on: August 16, 2012, 10:31:37 AM »
I've answered in your other (double) post  ;) at http://www.societyofrobots.com/robotforum/index.php?topic=15926.0
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here