Shop OBEX P1 Docs P2 Docs Learn Events
BS2 Quadrature Reseting — Parallax Forums

BS2 Quadrature Reseting

StevenGStevenG Posts: 9
edited 2010-05-06 14:11 in BASIC Stamp
I've been playing with making a program that uses the quadrature encoder to measure distance and convert it to real units to the .01 decimal place (Sending the data out the RS232 port). I have been running into an issue where the BS2 resets itself after every increment, resending the header. I still have some work on the code, but am having a hard time getting past this.

I am very sure that it isn't a power supply issue and am running off of the BOE's Voltage regulator.

Any help is much appreciated.

' {$STAMP BS2}
' {$PBASIC 2.5}

'Constants (Program)
MaxDiff               CON       1000      'Value represents a larger value than the encoder can change between readings
DirUp                 CON       -1         'Value represents direction up
DirDown               CON       1        'Value represents direction down
'TimeInterval          CON       5000      'Value represents milli-seconds
COUNTS_PER_FOOT       CON       240       'Value represents the encode counts per foot
MAXVAL                CON       65535     'Value represents the encoders maximum possible reading
MINVAL                CON       0         'Value represents the encoders minimum possible reading

'Constants (Quadrature)
QPOS                  CON      $08        'Query Position
QSPD                  CON      $10        'Query Speed
CLRP                  CON      $28        'Clear Position
SREV                  CON      $30        'Set Orientation as Reversed
STXD                  CON      $38        'Set TX Delay
QuadAdd               CON      2          'Address for the quadrature reader
CommPin               CON      12         'Communication bus pin
BaudValue             CON      32         'For 19.2kbps

'Variables
NewReading            VAR       Word      'Value represents the new reading from the quadrature encoder
OldReading            VAR       Word      'Value represents the old reading from the quadrature encoder
Direction             VAR       Word      'Value represents the direction
Delta                 VAR       Word      'Value represents the delta between NewReading and OldReading
Depth                 VAR       Word      'The current depth
DepthR                VAR       Word      'The current depth remainder
DepthDec              VAR       Word      'The current depth decimal

'Initialization
PAUSE 1000                                'Let things settle
SEROUT CommPin, BaudValue, [noparse][[/noparse]CLRP]         'Clears any prior position info
GOSUB ReadEncoder                         'Get initial encoder reading
OldReading = NewReading                   'Clear OldReading
Depth = 0                                 'Clear Location
Direction = 1                             'Assume positive
GOSUB SendHeaders                         'Send Initial Header for Debug
GOSUB Main                                'Go to Main routine

Main:
  GOSUB ReadEncoder
  IF NewReading <> OldReading THEN NewLocation
  GOSUB CalcDepth
  GOSUB SendDepth
  GOSUB SendAll
  OldReading = NewReading
  'PAUSE 500
GOTO Main

'Subroutines
ReadEncoder:
  SEROUT CommPin, BaudValue, [noparse][[/noparse]QPOS + QuadAdd]                             'Assume encoder can have values from 0 to 65535 when read
  SERIN  CommPin, BaudValue, [noparse][[/noparse]NewReading.HIGHBYTE, NewReading.LOWBYTE]    'Assume encoder values increase if depth increases        'Assume encoder values decrease if depth decreases
  'SEROUT 16,16468,[noparse][[/noparse]LF]
  'SEROUT 16,16468,[noparse][[/noparse]"             "]
  'SEROUT 16,16468,[noparse][[/noparse]CR]
  'SEROUT 16,16468,[noparse][[/noparse]DEC NewReading]
RETURN

RolloverDelta:
  'SEROUT 16,16468, [noparse][[/noparse]LF]
  'SEROUT 16,16468, [noparse][[/noparse]CR]
  'SEROUT 16,16468, [noparse][[/noparse]DEC Delta]
  IF Direction = DirDown THEN
    Delta = (MAXVAL - OldReading) + 1 + (NewReading - MINVAL)
  ELSE
    Delta = (OldReading - MINVAL) + 1 + (MAXVAL - NewReading)
  ENDIF
RETURN

NewLocation:
  IF NewReading > OldReading THEN
    GOSUB DirectionUp
  ELSE
    GOSUB DirectionDn
  ENDIF
RETURN

DirectionUp:
  Delta = NewReading - OldReading
  IF Delta < MaxDiff THEN
    Direction = DirUp
  ELSE
    Direction = DirDown
  ENDIF
  GOSUB RolloverDelta
RETURN

DirectionDn:
  Delta = OldReading - NewReading
  IF Delta < MaxDiff THEN
    Direction = DirUp
  ELSE
    Direction = DirDown
  ENDIF
  GOSUB RolloverDelta
RETURN

CalcDepth:
  IF Delta => COUNTS_PER_FOOT THEN
    DO WHILE Delta >= COUNTS_PER_FOOT
    Depth = Depth + Direction
    Delta = Delta - COUNTS_PER_FOOT
    LOOP
  ENDIF
  GOSUB DSDecimal
RETURN

DSDecimal:
  DepthDec = (Delta * 100) / COUNTS_PER_FOOT                  'This calculates the depth to the .01 decimal place
RETURN

'RS232 Output Routines

SendDepth:
  SEROUT 16,16468,[noparse][[/noparse]CR]
  SEROUT 16,16468,[noparse][[/noparse]SDEC Depth]
  SEROUT 16,16468,[noparse][[/noparse]"."]
  SEROUT 16,16468,[noparse][[/noparse]DEC DepthDec]
RETURN

SendHeaders:
  'SEROUT 16,16468,[noparse][[/noparse]CR]
  'SEROUT 16,16468,[noparse][[/noparse]LF]
  SEROUT 16,16468,[noparse][[/noparse]"Depth"]
  SEROUT 16,16468,[noparse][[/noparse]"  Dir  "]
  SEROUT 16,16468,[noparse][[/noparse]"  CPF"]
  SEROUT 16,16468,[noparse][[/noparse]"  Delta"]
  SEROUT 16,16468,[noparse][[/noparse]"    NR"]
  SEROUT 16,16468,[noparse][[/noparse]"      OR"]
  SEROUT 16,16468,[noparse][[/noparse]"   DepthR"]
  SEROUT 16,16468,[noparse][[/noparse]LF]
RETURN

SendAll:
  SEROUT 16,16468,[noparse][[/noparse]"     "]
  SEROUT 16,16468,[noparse][[/noparse]DEC Direction]
  SEROUT 16,16468,[noparse][[/noparse]"     "]
  SEROUT 16,16468,[noparse][[/noparse]DEC COUNTS_PER_FOOT]
  SEROUT 16,16468,[noparse][[/noparse]"    "]
  SEROUT 16,16468,[noparse][[/noparse]DEC Delta]
  SEROUT 16,16468,[noparse][[/noparse]"     "]
  SEROUT 16,16468,[noparse][[/noparse]DEC NewReading]
  SEROUT 16,16468,[noparse][[/noparse]"    "]
  SEROUT 16,16468,[noparse][[/noparse]DEC OldReading]
  SEROUT 16,16468,[noparse][[/noparse]"    "]
  SEROUT 16,16468,[noparse][[/noparse]DEC DepthR]
RETURN

END


Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2010-05-04 23:48
    Can you tell us about the encoder? I haven't seen too many that are read with a serin command.

    To keep resending the header info, you are definitely resetting which would only be caused by low supply voltage, cycling the reset pin of the Stamp chip or cycling the dtr pin of the programming port.
    What is the Stamp talking to? Is the receiving device opening and closing it's serial port?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • StevenGStevenG Posts: 9
    edited 2010-05-04 23:58
    I should have included this in the original post...

    Parallax Position Controller Kit (#27906)
    Listening to the stamp using Hyperterminal on the BOE programming port

    I didn't know that the Stamp would reset if the dtr pin if the programming port was cycled. I will have to out a scope on that.
    The Stamp does seem to reset when the "call" button is pressed on Hyperterminal.
  • stamptrolstamptrol Posts: 1,731
    edited 2010-05-05 13:07
    OK, that narrows it down considerably.

    The easiest way to avoid that issue during communication is to build a serial cable with only pins 2, 3, 5 connnected.

    Won't work for programming, but doesn't reset the chip when the PC serial port is opened.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • StevenGStevenG Posts: 9
    edited 2010-05-05 13:35
    I tried connecting only Pins 5 and 2 of the programming port since I am only sending information and not receiving any. I did get communication but I'm still having the same issue. Every time the encoder is incremented the Stamp goes through it's initialize routine again and appears to start the program completely over.
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-05-05 13:49
    You need both pins from the PC -- the BS2 hardware uses the 'TX' signal from the PC to get the voltage to make the 'RX' signal back to the PC.

    What ARE you using for a power-supply?

    Another problem -- Your "IF ... THEN NewLocation" is a GOTO NewLocation.· But you 'return' from NewLocation with a RETURN.· Doing this will reset the BS2.
    So, you need "IF ... THEN GOSUB NewLocation" at the very least.

    I also wouldn't use a "GOSUB MAIN" -- I believe the BS2 only has a 4-deep GOSUB/RETURN stack, you don't want to use one up with GOSUB MAIN that you never RETURN from.

    Post Edited (allanlane5) : 5/5/2010 1:54:26 PM GMT
  • StevenGStevenG Posts: 9
    edited 2010-05-05 14:26
    Well lookie there....

    Changed the GOSUB Main to a GOTO Main
    Changed the GOTO NewLocation to a GOSUB NewLocation

    The BS2 no longer resets itself!!

    Looks like I have some calculation problems to fix now...


    THANKS!!!!
  • stamptrolstamptrol Posts: 1,731
    edited 2010-05-05 19:07
    Good analysis, allanlane5!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • StevenGStevenG Posts: 9
    edited 2010-05-06 14:11
    Ok so on to the next problem...

    Everything is working correctly except for one thing. I am having a problem when the "DepthR" variable goes from 0 to 65535. What is happening is once it goes to 65535 the DepthR gets divided by the COUNTS_PER_FOOT and adds that to the "Depth". I need to make some sort of rollover in the "DepthR" variable.

    Any help is much appreciated...
Sign In or Register to comment.