PDA

View Full Version : Offset to variable howto ?



RFsolution
- 27th September 2008, 20:40
Hi all

I have the following problem

I read an ADC 10bit but would like to add an offset and keep the 10bit resolution

When the ADC value is smaller than the offset value I got a wrong indication on the LCD
substraction is going negative
any idea how i can solve this ?

eaxample:
offset: 200
if ADC = 512 - offset = 312 => no problem
if ADC = 180 - offset = -20 => wrong value

So i would like to keep the 10bit resolution but just shift it with the offset value ?


Thanks

skimask
- 27th September 2008, 22:16
See {I} {S} DEC {1..10} in the LCDOUT (and others) section of the manual.

Jerson
- 28th September 2008, 02:48
I read an ADC 10bit but would like to add an offset and keep the 10bit resolution
eaxample:
offset: 200
if ADC = 512 - offset = 312 => no problem
if ADC = 180 - offset = -20 => wrong value

This is not addition !!

JF

RFsolution
- 28th September 2008, 11:42
Ok you are right it is not addition but I mean

the 10 bits 1024 represents a position from a potmeter
in order not to change the fysical position i would like to add or substract an offset
but finnaly need to keep the 10bit resolution for further calculation
But I dont now how to do that

any help welcome

Jerson
- 28th September 2008, 12:29
Ok, I think I understand it now. You want to move the physical position of the pot in code.
You will be able to offset the reading. But the part that you've discarded due to the offset will be lost, so you cannot expect the full 10bits once you do this.

JF

Acetronics2
- 28th September 2008, 12:49
Ok you are right it is not addition but I mean

the 10 bits 1024 represents a position from a potmeter
in order not to change the fysical position i would like to add or substract an offset
but finnaly need to keep the 10bit resolution for further calculation
But I dont now how to do that

any help welcome

Hi, RF

Could you be more precise about what you want to do ...

The physical position of WHAT ???

i.e. read a pot with ADC , and from this reading show the pot angle, then place a servomotor's arm ... or what ???

Skimask's answer is one of the numerous possibilities ... consider it !!!

Alain

RFsolution
- 28th September 2008, 14:27
Hi Alain and others

Well I read a potmeter or other position encoder which is 10bit for full resolution
or equal to 1 single turn

Later I will convert this to Degrees (which is no problem)
But i would like to keep the maximum resolution to 10bit
but shift in the software the actual position instead of moving the potmeter mechanicaly

example

0 deg = 0V, 5V = to 360 (1024) or 10bit

imagine that the true 0 degree is at a value of 120 (decimal) and I dont want to move
the mechanical position of the potmeter but would like to see 0 as a decimal value
SO i would like to implement an offset of 120 decimal
whitout touching the 1024 10bit resolution for 1 revolution

I hope this is more clear

Walter

Melanie
- 28th September 2008, 14:45
So why not ADD an offset of 120 (using your example)...

That way zero will be in reality -120, 120 will be zero, and 240 is actually +120 ???

This way you will never go below zero, because zero is your REAL reference point, and your offset value is actually your mechanical zero.

Simple maths prior to displaying the figures will have it display whatever you want it to display.

RFsolution
- 29th September 2008, 00:16
thank you melanie

Well this is what i want to do and would show on the display:

I read the Real position from the encoder as a 10bit word (0-1024) as the encoder is absolute
i might want to add or substract an offset so that how the encoder mechanicly is fixed
i still can correct it by the offset but need to keep 1024 as resolution for 1 revolution

So i want to display or have new variables with:
the position +/- it's offset but between 0 and 1024
Conversion of the position +/- offset to 0 - 360 deg (keep the 10bit resolution)

I dont see how I can do that

Can you point me out ?

skimask
- 29th September 2008, 03:51
thank you melanie

Well this is what i want to do and would show on the display:

I read the Real position from the encoder as a 10bit word (0-1024) as the encoder is absolute
i might want to add or substract an offset so that how the encoder mechanicly is fixed
i still can correct it by the offset but need to keep 1024 as resolution for 1 revolution

So i want to display or have new variables with:
the position +/- it's offset but between 0 and 1024
Conversion of the position +/- offset to 0 - 360 deg (keep the 10bit resolution)

I dont see how I can do that

Can you point me out ?

So, you add your offset...
If it's above 360, subtract 360...
If it's below 360 (i.e. negative, somewhere above 65000, if using words), add 360...
Get the picture yet?

aratti
- 29th September 2008, 12:35
Why not using an offset switch? when you press it, you jump to a reading routine put the value into a word variable that will be use as the offset. In this way the offset will never be greater then the true value.

RFsolution
- 1st October 2008, 23:27
Hi Melani and others

Well the problem is as followed:

The application which is reading the encoder via a serial port expects a 10bit value
where 0 is 0 and 1024 is 1 resolution (or 360deg)
So this application does the mathematics for conversion

So the meaning of my offset value is to avoid that i have to move the encoder physicaly to its true zero point

So i would like to keep the resolution of 1024 but shift it around (if i may call it shifting)
in a way that i will keep a 10bit value

I dont see how to do that
maybe you might have some help

Thanks in advance

Walter




So why not ADD an offset of 120 (using your example)...

That way zero will be in reality -120, 120 will be zero, and 240 is actually +120 ???

This way you will never go below zero, because zero is your REAL reference point, and your offset value is actually your mechanical zero.

Simple maths prior to displaying the figures will have it display whatever you want it to display.

skimask
- 1st October 2008, 23:48
Either we're not getting it (not likely) or you're not getting it (more likely)...
If you're converted/offset value is out of range after the calculations, then put it back in range.
Example...
You had read 300 degrees, now you add an offset of 100 degrees, that gives you 400 degrees, well, that's no good, 'cause 400 degrees is out of range, but we both know 400 degrees is actually 40 degrees past 360 degrees, so subtract 360.
Further example...
You had 10 degrees, now you need to offset that by -50 degrees, which will give you -40 degrees, again, that's no good because -40 degrees isn't in the range of 0 to 359 degrees, so add 360 degrees to -40 will give you 320 degrees which is the answer you want.
Yes, the examples above are in degrees. If 360 degrees = 1024 counts, then add or subtract 1024 counts from any answer that's out of range.
If value => 1024 and value < 64512 then subtract 1024...
If value => 64512, then add 1024...
Question for you...Where did I get that value of 64512?

Here's another thread talking all about this same sort of thing...
http://www.picbasic.co.uk/forum/showthread.php?t=6650&highlight=wind

RFsolution
- 2nd October 2008, 07:37
Hi Skimask

Well i will give it a closer look today,

Well I have to say that i'm not a programmer so please excuse me if i ask rather
easy questions for your skills !!! hihi

Well the answer for my question should be 16bit or 65536 - 1024 = 64512

You see i'm learning !!!

RFsolution
- 2nd October 2008, 17:13
Hi skimask and others

here is what i do:
loop:
Offset = 400 ' example add 400 as offset
gosub read_position ' get the position: 10bit 0=0deg, 1024 =360deg
gosub set_offset
goto loop

set_offset:
position = position + offset
if position => 1024 & position < 64512 then
Position_cor = position - 1024
endif
if position => 64512 then
position_cor = position + 1024
endif
return

if i display the original position and the corrected position a get a strange result
i will have a look at that

skimask
- 2nd October 2008, 17:22
Ok then...Let's have a look-see at the whole program...
And it would really help if you'd explain strange result.
Just saying the words 'strange result' could mean that your PIC just spit a banana out of pin 1. Who knows... Qualifies as a strange result though, dontcha think?
I think I know what your root problem is...but I want to see the program first before I help you figure out what the deal is.

RFsolution
- 2nd October 2008, 18:12
Ok played a bit following your help,

and this code works, actually it was quit simple but did not found it =(

Assuming that the given offset is positive but that's enough as you can go 360 degrees in total:


loop:
Offset = 400 ' example add 400 as offset
gosub read_position ' get the position: 10bit 0=0deg, 1024 =360deg
gosub set_offset
goto loop

set_offset:
position_temp = position + offset
if position_temp => 1024 then
Position_cor = position_temp - 1024
endif
if position_temp <= 1024 then
position_cor = position_temp
endif

Seems to work very well

Thanks for your help,and nice to learn from others

Walter

skimask
- 2nd October 2008, 20:53
position_temp = position + offset
That was one of my solutions, but since I couldn't see the program, the whole program, and nothing but the program, I wasn't sure.
The other answer would've been whether or not you were defining your variables as BYTEs or WORDs.

RFsolution
- 3rd October 2008, 09:36
Hi skimask

I was thinking about the following feature:

imagine the encoder is mounted in the opsit way
so the the real 10bit value is increasing while it should decrease

Any idea how to reverse this