Offset to variable howto ?


Closed Thread
Results 1 to 19 of 19
  1. #1

    Default Offset to variable howto ?

    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

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    See {I} {S} DEC {1..10} in the LCDOUT (and others) section of the manual.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RFsolution View Post
    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

  4. #4


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Question Crystal ball needed

    Quote Originally Posted by RFsolution View Post
    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7


    Did you find this post helpful? Yes | No

    Default

    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

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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.

  9. #9


    Did you find this post helpful? Yes | No

    Default

    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 ?

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RFsolution View Post
    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?

  11. #11
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    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.

  12. #12


    Did you find this post helpful? Yes | No

    Default

    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



    Quote Originally Posted by Melanie View Post
    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.

  13. #13
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    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/show...highlight=wind

  14. #14


    Did you find this post helpful? Yes | No

    Default

    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 !!!

  15. #15


    Did you find this post helpful? Yes | No

    Default

    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

  16. #16
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    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.

  17. #17


    Did you find this post helpful? Yes | No

    Default

    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

  18. #18
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by RFsolution View Post
    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.

  19. #19


    Did you find this post helpful? Yes | No

    Default

    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

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. HSEROUT full 3 digit display of variable howto ?
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th October 2008, 13:53
  4. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  5. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts