Shop OBEX P1 Docs P2 Docs Learn Events
Notice: RCTime won't work for Pot — Parallax Forums

Notice: RCTime won't work for Pot

Kirk FraserKirk Fraser Posts: 364
edited 2011-08-23 19:28 in Propeller 1
Simulating the RCTime command from the BS2 cannot accurately measure a 10K pot because the maximum value changes significantly when the circuit heats up. No need for reply, this result is for anyone searching for RCTime in the future. Here's the code:
PUB RCTIME (Pin):Duration | ClkStart, ClkStop
{{
   Reads RCTime on Pin
   returns discharge time in 12 bit form
   actually 69-4086 cold, 69-2593 hot   
}}      
   dira[Pin]~~                  ' Set as output
   outa[Pin]:=1                 ' Set high
   Pause(10)                    ' Allow to charge
   DIRA[Pin]~                   ' Set as input
   ClkStart := cnt              ' Save counter for start time
   waitpne(1 << pin, |< Pin, 0) ' Wait for opposite state to end
   clkStop := cnt               ' Save stop time    
   Duration := (clkStop - ClkStart) * 468 / 10_000

To get a position value from a Potentiometer, consider using the Pot as a voltage divider feeding an A/D chip like MCP3208, thanks to Daniel Harris. That will be discussed in another thread.

Comments

  • mparkmpark Posts: 1,305
    edited 2011-08-12 07:48
    Which part of the circuit is heating up?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-12 08:21
    Your claim / notice is not correct. Simulating RCTIME does work and works correctly. If you want this technique to be accurate in the face of large temperature changes, you have to use a capacitor and resistor (pot) that are stable over the expected temperature range. We're not talking expensive or difficult-to-find parts. You just have to choose the part for the job at hand.

    Switching to using a pot as a voltage divider feeding an A/D chip doesn't necessarily fix your problem. It will help near the mid-range position because the pot resistance on either side of the wiper will change by similar amounts and the ratio will remain similar. Nearer the ends of the pot's range, the ratio will shift with temperature changes and you'll have significant errors because the resistance changes proportionately with temperature and there's more resistance on one side of the wiper.
  • ctwardellctwardell Posts: 1,716
    edited 2011-08-12 08:40
    Mike Green wrote: »
    It will help near the mid-range position because the pot resistance on either side of the wiper will change by similar amounts and the ratio will remain similar. Nearer the ends of the pot's range, the ratio will shift with temperature changes and you'll have significant errors because the resistance changes proportionately with temperature and there's more resistance on one side of the wiper.

    This doesn't sound correct Mike. The total resistance of the POT may drift, but in a voltage divider scenario, with the POT connected to the supply rails and the center tap being the output, the output should remain consistent assuming you are feeding a high impedance input.

    C.W.
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-12 09:07
    My original post is correct. My RCTime program is correct. The problem is Potentiometers are unreliable in the configuration documented in the BS2 help menu for RCTime. Pots are extremely sensitive to:
    1) Shaft nut tightness for panel mount models.
    2) Grounding of metal objects attached or nearby.
    3) Temperature, in my case, the difference between room temperature at 5AM and noon.

    Beyond that I am no expert, so I hope the voltage divider configuration works. My goal is to achieve rotary position sensing that is reliable. I am aware of some other options like using a trimmer capacitor or optical encoder. I am not aware of a small size optical encoder that can deliver 12bits. I've heard laser encoders are very expensive and haven't seen anything on building your own.

    I haven't yet tried to reconfigure the BS2 diagram to a full voltage divider - it may be faster than continuing to work on the Serin and Serout software for the A/D chip.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-08-12 09:17
    A "garden variety" pot, 270 degree rotation, can be "touchy" at its extremes, but such is not for demanding applications.
    There should be a low-ohm resistor in series with the pot.
    For those demanding applications, a multi-turn (10-turn) pot would be a good idea.
    Sometimes people expect a lot from a pot.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-12 09:47
    It all depends on the quality and construction of your potentiometers. They are not inherently unreliable. None of the things you've mentioned are inherent to either potentiometers or the RCTIME form of measurement.

    I can well imagine that a cheap potentiometer could have problems with shaft nut tightness. I have trouble with the notion of so much sensitivity to grounding nearby, particularly with a 10K pot. I could see it with a 1 megOhm pot, but 10K is a fairly low impedance and I've not seen that kind of sensitivity before. Depending on the length of the wires to the pot, you may need to use a twisted pair for that wiring.

    Pots are not necessarily sensitive to ambient temperature. It depends on their construction. Thin film resistances are usually less sensitive to temperature although there are wire-wound pots that are constructed to minimize their temperature sensitivity.

    Some types of capacitors are very temperature sensitive and you need to choose the type of dielectric and the capacitor construction properly for the kind of temperature stability you need. The manufacturer's specs will tell you the temperature coefficient.
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-12 09:56
    PJ
    I've not seen a small enough 10 turn pot. Think of fitting it into a space the size of your finger knuckle.

    "Garden variety" pots were a mistake for that reason too. The pot which can fit is
    http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=P12426CT-ND

    Mike,
    Perhaps not all pots have all 3 problems at the same time. Those are accumulated experiences.
    Thanks for the tip on choosing the correct capacitor.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-08-12 10:21
    If you suffer variations in absolute readings -- for whatever reason -- you can always use RCTIME to read a pot ratiometrically:

    attachment.php?attachmentid=83974&d=1313169520

    With this configuration, you can measure either the charge time or the discharge time -- or both -- for each leg.

    -Phil
    260 x 154 - 1016B
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-08-12 10:45
    I think you're expecting a lot from a pot.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-08-12 11:23
    attachment.php?attachmentid=83975&d=1313173310
    458 x 219 - 30K
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-12 11:36
    Phil,
    Wow! That's one more on the list of things to try. Thank you.

    Of course the degree of warmth causing variations is relative - it's about 65 to 75 I guess with fewer operations while at 65 and more attempts at 75. Neither hot or cold in the absolute sense.
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-22 11:26
    The ratiometric solution for some reason doesn't report any values using the same code as above driven by this method:
    PUB RCT_PWM | x
    {{
      Set up and run a RCTime-PWM loop.
      Sense 10K Pot and output relative PWM duty.
      Show value on serial terminal.
    }}
        Debug.start(31, 30, 0, 115200)     'Debug Startup 
        waitcnt(clkfreq*3+cnt)         'For Debug Startup
        PWM_Set(8,255,1000)         ' PWM Startup
        repeat
          x := RCTime(13)           ' Measure Pot Value
          Debug.Str(string(13,"Pot value A: "))
          Debug.dec(x)              ' Display Pot Value
          Debug.Str(string(" B: "))
          Debug.dec(RCTime(14))              ' Display Pot Value  
          'PWM-dir(8, 9, x)          ' Output PWM to two pins
    

    with the exception, some values are reported when ground and Prop input pins are shorted by a continuity test, which are of course wildly irratic.

    Please suggest how to get values to report Pot changes like for other circuits. I'm using 220 resistors between the Pot and Prop and a standard 104 capacitor in Phil's circuit.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-08-22 11:44
    In the AP-16+ we use 10K and 0.01uF caps for the PRE and POST delay settings -- works like a charm with the attached code. Note that I shift off the lower bits (noise) and truncate to 500 for my purposes.
    pub readpot(p)
    
    '' Reads RC circuit on pin p
    '' -- takes about 2ms
    '' -- assumes 10K + 0.01uF
    
      ctra := (%01000 << 26) | p                                    ' ctra in pos detect mode 
      frqa := 1
    
      outa[p] := 1                                                  ' charge cap
      dira[p] := 1
      waitcnt((clkfreq >> 10) + cnt)                                ' ~1ms @100MHz
    
      phsa := 0                                                     ' clear counter
      dira[p] := 0                                                  ' allow discharge
      repeat while (ina[p] == 1)
      ctra := 0                                                     ' disable ctra
      
      return (phsa >> 4) <# 500                                     ' return discharge time
    
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-22 12:18
    Thank you Jon, it gives much more consistent results than the code in my first post. I added a zero to the 500 in your last line and that looks like it works too.

    Now on to get the result to PWM...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-08-22 12:42
    Kirk,

    Try this program with the differential circuit:
    CON
    
      _clkmode      = xtal1 + pll16x
      _xinfreq      = 5_000_000
    
      APIN          = 0
      BPIN          = 1
    
    OBJ
    
      sio   : "FullDuplexSerial"
    
    
    PUB  start | atime, btime
    
    
      sio.start(31, 30, 0, 9600)
      repeat
        atime := rctime(APIN, BPIN)
        btime := rctime(BPIN, APIN)
        sio.tx(1)
        sio.dec(atime)
        sio.tx(11)
        sio.tx(14)
        sio.tx(7)
        sio.dec(btime)
        sio.tx(11)
        sio.tx(14)
        sio.tx(14)
        sio.dec(atime + btime)
        sio.tx(11)
    
    PUB rctime(pin0, pin1) : time
    
      dira[pin1]~
      dira[pin0]~~
      outa[pin0]~~
      waitcnt(cnt + clkfreq / 20)
      dira[pin0]~
      outa[pin1]~
      dira[pin1]~~
      time := -cnt
      waitpeq(0, 1 << pin0, 0)
      time += cnt 
    

    It measures the RC time on both sides of the pot. Notice the longer charge time. That's because you're charging the cap through more than just the 220R resistors.

    -Phil
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-22 14:12
    Phil,

    On my setup your code does something but I can't recognize what. The Serial Terminal posts a gibberish wrap with no noticable change when turning the pot. I tried some tweaks like changing the _xinfreq to 100_000_000 which is supported by my 6.25MHz crystal but to no avail. Thanks for trying to help.

    Kirk
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-08-22 14:34
    Kirk,

    Well, use this, then:
    CON
    
      _clkmode      = xtal1 + pll16x
      _xinfreq      = 6_250_000
    

    _xinfreq is the frequency of the crystal, not the system clock.

    -Phil
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-22 15:53
    Phil,

    Thanks for the education on _xinfreq. It does work, but the total varies by at least 3 in the second digit without moving the pot wiper. Somehow the theory behind Jon's code is more stable. Can you explain why?

    There's also something unexpected about the _clkfreq which failed the serial communication when set to 6_250_000 with the Baud rate set to 115200. At that rate 100_000_000 works. So looking into the FullDuplexSerial object there's a line bit_ticks := clkfreq / baudrate which useage may redefine the meaning of clkfreq even if it's crystal frequency for determining milliseconds in rxtime(ms)?

    Kirk
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-08-22 16:00
    PhiPi,
    I wasn't on about ambient temp variation, just that we're talking about a bloody pot. Eh, maybe it's one of those newfangledy, non-mechanical, infinite cycle, gold standard pots.
    What do you reckon the accuracy of RCTIME is, anyway.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-08-22 18:42
    Kirk,

    Jon's code eliminates any uncertainty from variable hub access times inherent in Spin programs. It will give you the most consistent results for a single resistance but does not do the ratiometric measurement that mine attempts in order to eliminate temperature variations. But how much precision do you need? +/-30 out of, what? 56000?, is only .052%.

    PJ,

    I once had a digital bathroom scale that I disassembled and traced the circuitry for. It used a General Instruments PIC NMOS processor. The A-to-D conversion from the strain gage bridge was done via an RC-time conversion, since those early PICs did not have built-in ADCs. Yet, it produced very accurate and consistent results.

    -Phil
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-22 20:20
    Phil,

    My desire has always been for 12 bit precision, one position in 4096 but I couldn't come close before. I don't know if I'll have to add ratiometric to Jon's readpot program. Today's temperature change wasn't enough to cause any effect. Thank you.

    Kirk
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2011-08-23 10:19
    Hi Kirk, This is a d
  • Kirk FraserKirk Fraser Posts: 364
    edited 2011-08-23 19:28
    Hi Tracy, Yes, I'm still pursuing the same goal as years ago. It's been so long I don't remember getting the TI chip you recommended working - I remember we got the serial IO interface working but I don't remember if the results were good. (I don't know how to generate SerIn and SerOut on a Prop.) I think the results were poor which is why I tried RCTime on the Stamp.

    In intervening years I got additional help to flesh out the opAmp circuit you designed and found it failed because for some reason the IRL530 needs PWM instead of a constant voltage level. PWM appeared to work on BS2 before I burned two of them out with poor bi-level ground design so I switched to the cheaper Props after wasting time with a Microchip microcontroller. So PWM is my next quest. Anyway the readpot program by Jon McPhalen earlier on this thread should replace all attempts to use RCTime for pot reading on a Propeller.

    Kirk
Sign In or Register to comment.