### Author Topic: strange ADC / rprintf prob with WebbotLib  (Read 1626 times)

0 Members and 1 Guest are viewing this topic.

• Supreme Robot
• Posts: 11,653
##### strange ADC / rprintf prob with WebbotLib
« on: August 28, 2009, 10:18:32 AM »
I have a strange problem. I'm probably doing something dumb, but can't figure it out.

This code works:
Code: [Select]
r1cur=a2dConvert8bit(r1current);
rprintf("%d\r\n",r1cur);
r2cur=a2dConvert8bit(r2current);
rprintf("%d\r\n",r2cur);
r4cur=a2dConvert8bit(r4current);
rprintf("%d\r\n",r4cur);
rprintf("get data %d %d %d\r\n",r1cur,r2cur,r4cur);

It correctly outputs:
Quote
42
0
1
get data 42 0 1

This does not work:
Code: [Select]
r1cur=a2dConvert8bit(r1current);
r2cur=a2dConvert8bit(r2current);
r4cur=a2dConvert8bit(r4current);
rprintf("get data %d %d %d\r\n",r1cur,r2cur,r4cur);

It outputs the same value for r1cur, r2cur, and r4cur no matter what. The actual value appears a bit random.

I can't figure out why it only works when I add in extra rprintfs . . . .

This is my initialization:
Code: [Select]

int r1cur;
int r2cur;
int r4cur;

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,132
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #1 on: August 28, 2009, 11:54:58 AM »
As  a matter of interest: have you tried using ADC 1,2,3 rather than 8,9,10 - just humour me !

Also: with the code that 'does not work' I'm a bit confused as to what you see. You say 'It outputs the same value....' - do you mean the same value is repeated for all 3 readings - and that one value changes?

Incidentally - just as with AVRlib - you dont need to say '\r\n' you cant just say '\n' as it gets xlated to '\r\n'. Will save you 2 bytes of memory each time.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

• Supreme Robot
• Posts: 11,653
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #2 on: August 28, 2009, 12:03:58 PM »
Quote
have you tried using ADC 1,2,3 rather than 8,9,10 - just humour me !
Nope, doesn't fix it.

Quote
with the code that 'does not work' I'm a bit confused as to what you see. You say 'It outputs the same value....' - do you mean the same value is repeated for all 3 readings - and that one value changes?
I mean that whatever value it outputs, all of them are outputting the same value. Strange, since each is attached to a different sensor. For example:
Quote
get data 5 5 5

Quote
Will save you 2 bytes of memory each time.
lol I'm not that OCD!

• Supreme Robot
• Posts: 11,653
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #3 on: August 28, 2009, 12:35:07 PM »
Still playing around with it . . . using this code:

Code: [Select]
r1cur=a2dConvert8bit(r1current);
rprintf("%d\r\n",r1cur);
r2cur=a2dConvert8bit(r2current);
r4cur=a2dConvert8bit(r4current);
rprintf("get data %d %d %d\r\n",r1cur,r2cur,r4cur);

It'll generate a result where r1cur is printed out currectly, but r2cur and r4cur are not (always the same value).

Quote
41
get data 41 5 5

Then if I run this code:
Code: [Select]
r1cur=a2dConvert8bit(r1current);
r2cur=a2dConvert8bit(r2current);
rprintf("%d %d\r\n",r1cur,r2cur);
r4cur=a2dConvert8bit(r4current);
r5cur=a2dConvert8bit(r5current);
rprintf("get data %d %d %d\r\n",r1cur,r2cur,r4cur,r5cur);

It'll give me results where r1cur and r2cur are always the same, and r4cur and r5cur are always the same:

Quote
41 41
get data 41 41 5 5

But if I put a rprintf for each individual value, they are all different values (like in the first post).

totally confused . . .

• Supreme Robot
• Posts: 11,653
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #4 on: August 28, 2009, 12:50:33 PM »
Playing around with more code . . . still confused.

Both code snippets result in all output values being exactly the same. As if there is no difference.

Code: [Select]
Code: [Select]
r1cur=a2dConvert8bit(r1current);

• Supreme Robot
• Posts: 11,653
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #5 on: August 28, 2009, 02:07:25 PM »
PROBLEM SOLVED

Code: [Select]
r1cur=a2dConvert8bit(r1current);
delay_us(2);
r2cur=a2dConvert8bit(r2current);
delay_us(2);
r4cur=a2dConvert8bit(r4current);
rprintf("get data %d %d %d\r\n",r1cur,r2cur,r4cur);

I never had this problem with AVRlib . . . Webbot, can you fix this in your WebbotLib? Basically force a 2us delay after each ADC read (not sure what the optimal delay is).

thanks airman00!

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,132
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #6 on: August 28, 2009, 02:51:10 PM »
Ok, no probs.

But will do some 'data sheet checking' as well since I hate adding delays per se as they just make your code grind to a halt.

AVRlib uses an interrupt to signal 'conversion complete' but as far as I can tell this is not used by the rest of their code. Consequently I have excluded it in my lib (no point having meaningless interrupts). But it may be that this interrupt service handlerr just 'wastes time' and achieves the 2us delay.

Will report back.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

• Supreme Robot
• Posts: 11,653
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #7 on: August 31, 2009, 01:40:44 PM »
I'm still having another problem with ADC.

My code:
Code: [Select]

When I apply 5V to the signal pin on ADC 12:
channel 11 and channel 0 both give 255, while channel 12 gives random value

When I apply 5V to the signal pin on ADC 11:
channel 0 both gives 255, while channel 11 and 12 both give random values

When I apply 5V to pins 13-15, channels 0, 11, and 12 all give random values

Applying 5V to pin 0, channel 12 goes high . . .

I'm confused!

(its not cross-pin voltage leaking, I already checked)

• Supreme Robot
• Posts: 11,653
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #8 on: August 31, 2009, 02:16:26 PM »
It gets even stranger. It appears like the output is out of phase.

For channels 0->7, whatever I apply to pin X, the voltage outputs on pin X+1.

For example, if I put 0V on pin 2, it'll say pin 1 is 0V.

And of course for applying 0V to pin 0, it changes output 12 to 0 . . .

Its just whack!

Also of note, your a2d.h has this:
Code: [Select]

While mine has this:
Code: [Select]

I tried using my code in your a2d.h and compiling, but that didn't change anything . . .

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,132
##### Re: strange ADC / rprintf prob with WebbotLib
« Reply #9 on: September 01, 2009, 11:46:02 PM »
Think I've emailed you on this ... but anyway I've fixed the problem in the next release which I'm in the process of making.

The problem was caused by the routine to detect if the a2d conversion was complete. It was back-to-front. So if you asked to do ADC on channels 0,1, and 2 then it would return immediately with a random value and then return the 'real' ADC reading at a later date - ie for ADC#2 for example. My elementary test only used one ADC channel and that worked ok because the correct value was returned eventually - its only when you have multiple ADC reads that things got jumbled up.
So previous posts regarding adding a '2us' delay sort of helped the problem to get hidden. But nows its fixed.

The ADC channel numbers I use are straight out of the ATmel data sheet. The reason our channel 8 starts at 0x20 is because in between channels 0 and 7 there are a whole host of other modes for measuring voltage differences between sets of ADC pins. My lib still allows you to use these whereas your code maps ADC8-15 over the top of them . Thats where my ADC_NUMBER_TO_CHANNEL macro comes in - it makes my code do the same as yours does for the simple case whilst keeping the 'voltage difference' ability for anyone who may choose to use it by not using the ADC_NUMBER_TO_CHANNEL macro.

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

#### Webbot

• Expert Roboticist
• Supreme Robot
• Posts: 2,132