Shop OBEX P1 Docs P2 Docs Learn Events
store data and retrieve — Parallax Forums

store data and retrieve

oldhippyoldhippy Posts: 36
edited 2014-12-09 13:05 in BASIC Stamp
I have been trying to find a way to store serial data on a BS2 and retrieve it to get the average of the data. Any Suggestions?

Comments

  • davejamesdavejames Posts: 4,047
    edited 2014-12-04 13:44
    The WRITE and READ command descriptions in the Help will provide the basic information on saving/retrieving data from the Stamp's EEPROM.

    Or are you asking the "how-to" on grabbing a serial stream and stuffing it into memory?
  • oldhippyoldhippy Posts: 36
    edited 2014-12-04 18:11
    I can see the data stream on the debug screen and need to capture about 5 seconds worth then average it. and send the result to an LCD
    I will try to see what i can do with WRITE AND READ
  • davejamesdavejames Posts: 4,047
    edited 2014-12-04 19:55
    Depending on the number of bits appearing in the 5-second window, you might consider streaming the data into an array. Then push that set of array data in EEPROM for future post processing.
  • DomanikDomanik Posts: 233
    edited 2014-12-04 21:02
    If you can create a 16 bit reg (ALU and another GP Reg to catch the carries) it would be possible to sample every 80ms for 5.12 seconds to capture 64 samples. If you add each incoming sample to a 16 bit register (or 2 regs plus housekeeping) every 75ms to 79ms within an n =1 to 64 loop, you’ll have a total in the reg pair. Now rotate right shift the register by 6 places and viola there’s your average (almost). A right shift 6 is the same as dividing by 64. You might need to left shifted the carry reg 2 and OR with the lower reg for the average. Don't wrap around LSBs, they need to go into the bit bucket.

    The wait 75ms to 79ms will depend on how often your samples come along and how important it is to be right on 5 seconds. Whether you sample for 4.8 seconds or 5.3 won’t matter as long as you have 64 samples to divide by 64. Simple, accurate and no memory would be involved.
    My disclaimer-- I've never tried this and never worked with a BS2---
    Dom..
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-12-05 07:45
    If you're trying to deal with data in real-time I would not recommend using EEPROM since it has a limited write-cycle lifetime. Depending on how much data you're talking about you could store it in an array and average the array. Even so you have a total of 26 bytes of RAM available for variables on the BS2, so if this data is larger strings you may have to use a BASIC Stamp Model such as the BS2p series. Can you describe what the data looks like and what information out of it you're looking for?
  • 72sonett72sonett Posts: 82
    edited 2014-12-05 09:49
    Store the incoming serial data on a memory stick or on a smart card and process it later.
  • oldhippyoldhippy Posts: 36
    edited 2014-12-07 20:32
    The data is from a Maxbotix 7369 sonic sensor serial I am sensing water levels in a sloshing tank and need an average of the min and max
  • oldhippyoldhippy Posts: 36
    edited 2014-12-07 20:34
    I am trying to get the average of the min and max serial data (in mm) from a 5 second length reading
  • Mike GreenMike Green Posts: 23,101
    edited 2014-12-07 21:30
    So, how many samples do you need to save over your 5 second interval and how big are the samples (8 bits, 16 bits, ?)
  • oldhippyoldhippy Posts: 36
    edited 2014-12-09 08:28
    The serial data format is 9600 baud, 8 data bits, no parity, with one stop bit (9600-8-N-1).
  • Mike GreenMike Green Posts: 23,101
    edited 2014-12-09 09:51
    I was asking about the number of samples in your 5 second sampling interval, not the serial data rate. I went to the datasheet for the sensor to find out that the samples are provided in six character groups with an "R", then 4 digits, then a carriage return character. Each sample ranges from 0000 to 9999 which would fit in two bytes. These samples are sent about one every 120-150ms which comes out to about 40 samples in a 5 second interval. Your best bet would be to use a Stamp model with scratchpad RAM like any of the BS2p series and store the samples in the scratchpad RAM using GET/PUT. The BS2 Stamps are marginal in receiving serial data at 9600 Baud, but the BS2p series are faster and should be fine at that data rate. You'd use the SERIN formatters to wait for the "R", read a 4 digit decimal number and store it in a 16-bit variable, then accept and otherwise ignore the carriage return.

    SERIN <pin>,<Baud code>,[wait("R"),DEC4 sample]

    The PUT statement can write a single 16-bit value as two bytes

    PUT <address>,WORD sample

    After reading 40 samples, your program can go back and process the saved values. You could compute the max and min for the 40 samples "on the fly" as well. Remember that the PUT address has to be an even number since each sample takes two bytes to store.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2014-12-09 13:05
    If you are using an original BS2, I think it would be fine to keep the average, min and max computed on the run in ordinary RAM variables. No need for an array. Something like this:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    idx VAR BYTE
    range VAR BYTE
    accum0 VAR WORD
    accum1 VAR BYTE
    mymax VAR WORD
    mymin VAR WORD
    
    baudmode CON $54  ' 9600 true
    
    DO
      accum0 = 0
      accum1 = 0
      mymax = 0
      mymin = 9999
      SERIN 1, baudmode, [wait(CR)]  ' sync with the stream, 
      FOR idx = 1 TO 32
        SERIN 1, baudmode, [DEC range]    ' capture 32 decimal values
        accum0 = accum0 + range                ' average
        IF accum0 < accum1 THEN accum1 = accum1 + 1   ' double precision
        mymax = mymax MIN range     ' track min and max
        mymin = mymin MAX range
      NEXT
      range = (accum0 >> 5) + (accum1 << 11)   ' double precision divide by 32
      DEBUG CR,"average=", DEC range
      DEBUG "  max=", DEC mymax
      DEBUG "  min=", DEC mymin
      PAUSE 1000
    LOOP
    

    The DEC modifier waits for decimal digits, so it will bypass the "R" at the start of the string and it will close with the CR at the end of the number. At a sample rate of 6.67 Hz, 32 samples take 4.8 seconds.

    I think the DEC modifier by itself can work at 9600 baud. Here there is no WAIT or SKIP modifier in front of the DEC, so no slow call back to the interpreter. It it doesn't work, there is another approach using the STR modifier that definitely will work.

    An alternative to a straight average would be a low-pass filter. The Maxbotics sensor itself lowpass filters the streaming data.
Sign In or Register to comment.