Shop OBEX P1 Docs P2 Docs Learn Events
bs2 acceleromter datalogger memory stick — Parallax Forums

bs2 acceleromter datalogger memory stick

yoda152yoda152 Posts: 18
edited 2011-02-07 12:04 in BASIC Stamp
Hi im working on a project what i require to use an accelerometer and then output the data to an memory datalogger ( usb memory stick) i require to take a number of samples. I have run out of ram in using just one bs2, so i have reduced the buffer to a lower value from 15 to 6 to fit in the other variables words. what is the point of the buffer? should i use two bs2?

is it possible to use 2 bs2 with one conected to an acceleromter and then out put the data to the other one and then out put to a memory data logger im asking due to the fact that i have ran out of ram

any help would be very much appreciated. thank you in advance

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-02-06 08:32
    As you have noticed, the Stamps have limited memory for variables, 26 bytes only. The sample program for the datalogger uses quite a lot of this, particularly for a buffer. When you try to combine the datalogger example with other sample programs, you often run out of memory. The answer is to share the variable space between the two sections of your program. Most of the memory used for the datalogger is not needed in between the writing of data to the datalogger. Have a look at what's called an "alias" in the Stamp Manual. Look it up in the index and read that section with its examples.

    It's difficult to give you any kind of specific suggestions without your source code. Use the "Reply" button rather than "+ Reply to Thread" and you'll see a button for including attachments with your message. Attach your source program file that way.

    It is possible to use two BS2 connected together for this sort of thing. It's expensive and unnecessary most of the time.

    You can use a Propeller instead. In that case, you can do datalogging directly to an SD card rather than using the USB memory stick datalogger although you can use that with the Propeller as well.
  • yoda152yoda152 Posts: 18
    edited 2011-02-06 08:52
    I have the source code attached

    when i try to run the program it some time just stops i dont know why. and i have the serout comand (like the one from the example program)to the usb stick and when i check to see what is on my memory stick i see that i only have the y and z condinates from the accelerometer hence missing the X

    thanks for your help

    '
    [ I/O Definitions ]
    TX PIN 8 ' Transmit Data --> 27937.4 (RXD)
    RTS PIN 9 ' Request To Send --> 27937.6 (CTS)
    RX PIN 10 ' Receive Data <-- 27937.5 (TXD)
    CTS PIN 11 ' Clear To Send <-- 27937.2 (RTS)
    Dio PIN 15 ' data to/from module
    Clk PIN 14 ' clock output
    CS PIN 13 ' active-low chip select
    '
    [ Constants ]
    Baud CON 84 ' Serial Baud Rate 9600 bps (BS2)
    XAxis CON 0 ' adc channels
    YAxis CON 1
    ZAxis CON 2
    VRef CON 3
    Cnt2Mv CON $CE4C ' counts to millivolts
    ' 0.80586 with **
    GfCnv CON $3852 ' g-force conversion
    ' 0.22 with **

    '
    [ Variables ]
    buffer VAR Byte(8) ' Input Buffer
    index VAR Byte ' Index Variable
    ioByte VAR Byte ' Input/Output Storage
    'counter VAR Byte ' counter
    flag VAR Nib ' Event Status Flag
    flag2 VAR Nib ' Event Status Flag 2
    'ace VAR Byte
    axis VAR Nib
    rvCount VAR Word ' ref voltage adc counts
    axCount VAR Word ' axis voltage adc counts
    gForceX VAR Word
    gForceY VAR Word
    gForceZ VAR Word
    gForce VAR Word ' axis g-force
    dValue VAR Word ' display value
    dPad VAR Nib ' display pad
    '
    [ Initialization ]
    DEBUG CLS, "Memory Stick Datalogger Demo V1.0", CR, CR, "Initializing..."
    PAUSE 200 ' Allow Time To Settle
    HIGH TX ' Initialize Transmit Line
    LOW RTS ' Take Vinculum Out Of Reset
    PAUSE 600 ' Allow Time To Settle
    DEBUG "Done!", CR, "Synchronizing..."
    DO
    SEROUT TX\CTS, Baud, ["E", CR] ' Sync Command Character
    GOSUB Get_Data ' Get Response
    PAUSE 250
    LOOP UNTIL ioByte = $0D ' Wait For Carriage Return
    DO
    SEROUT TX\CTS, Baud, ["e", CR] ' Sync Command Character
    GOSUB Get_Data ' Get Response
    PAUSE 250
    LOOP UNTIL ioByte = $0D ' Wait For Carriage Return

    '
    [ Program Code ]
    Main:
    DEBUG "Done", CR, "Switching to Short Command Mode..."
    SEROUT TX\CTS, Baud, ["SCS", CR] ' Switch To Short Command Mode
    GOSUB Get_Data ' Purge Receive Buffer
    DEBUG "Done!", CR, "Waiting for Memory Stick..."
    Check_Drive:
    DO
    SEROUT TX\CTS, Baud, [CR] ' Prompt Device For Status
    GOSUB Get_Data ' Purge Receive Buffer
    IF buffer(0) = ">" THEN ' Check For Ready Prompt
    EXIT ' If Ready Then Exit Loop
    ELSEIF buffer(0) = "N" AND buffer(1) = "D" THEN
    DEBUG "." ' Device Ready But No Memory Stick
    ELSEIF buffer(0) = "D" AND buffer(1) = "D" AND flag = 0 THEN
    DEBUG "Connected!", CR, "Accessing..."
    flag = 1 ' Memory Stick Ready
    ELSE
    DEBUG "."
    ENDIF
    PAUSE 250 ' Command Retry Delay
    LOOP
    DEBUG "Ready!", CR
    Open_File:
    DEBUG "Opening Data File..." ' First Delete File
    SEROUT TX\CTS, Baud, [$07, $20, "datafile.txt", CR]
    GOSUB Get_Data ' Purge Receive Buffer
    ' Then Create File
    SEROUT TX\CTS, Baud, [$09, $20, "datafile.txt", CR]
    GOSUB Get_Data ' Purge Receive Buffer

    DEBUG "Open!", CR, CR, "Ready to Write Data...", CR
    flag2 = 1
    HIGH 6
    Write_Data:

    PAUSE 30

    IF IN2 = 0 THEN ' Are we in sampling mode?

    IF IN5 = 1 THEN ' Rotary encoder
    IF flag2 = 1 THEN
    flag2 = 0
    FOR axis = XAxis TO ZAxis ' loop through each axis
    GOSUB Get_H48C ' read vRef & axis count
    dValue = rvCount ' display vRef count
    dValue = axCount ' display axis count
    IF (axCount >= rvCount) THEN
    gForce = (axCount - rvCount) ** GfCnv ' positive g-force
    ELSE
    gForce = -((rvCount - axCount) ** GfCnv) ' negative g-force
    ENDIF
    PAUSE 10
    IF axis = 0 THEN gForceX =gForce
    IF axis = 1 THEN gForceY =gForce
    IF axis = 2 THEN gForceZ =gForce

    'PAUSE 1 ' Wait 1 ms
    ' " " + (gForce.BIT15 * 13),
    NEXT
    'PAUSE 10 ' Wait 1 ms
    DEBUG "X"," " + (gForceX.BIT15 * 13),DEC1 (ABS(gForceX) / 100), ".", DEC2 ABS(gForceX), ' Display Results
    "Y"," " + (gForceY.BIT15 * 13),DEC1(ABS(gForceY) / 100), ".",DEC2 ABS(gForceY) ,"Z"," " + (gForceZ.BIT15 * 13),
    DEC1 (ABS(gForceZ) / 100), ".",DEC2 ABS(gForceZ) ,CR ' Display Results
    SEROUT TX\CTS, Baud, [$08, $20, $00, $00, $00, $0D,CRSRDN,
    ","," " + (gForceX.BIT15 * 13),DEC1 (ABS(gForceX) / 100), ".", DEC2 ABS(gForceX),
    ","," " + (gForceY.BIT15 * 13),
    DEC1 (ABS(gForceY) / 100), ".",DEC2 ABS(gForceY),
    ","," " + (gForceZ.BIT15 * 13),DEC1 (ABS(gForceZ) / 100), ".",DEC2 ABS(gForceZ) ,CR, LF ,CR]
    PAUSE 5 ' Write Results/Delay
    GOSUB Get_Data ' Purge Receive Buffer
    GOTO Write_Data
    ELSE
    DEBUG "."
    PAUSE 5
    GOTO Write_Data
    ENDIF

    ELSEIF IN5 = 0 THEN
    flag2 = 1
    DEBUG "."
    PAUSE 5
    GOTO Write_Data
    ELSE
    PAUSE 5
    GOTO Write_Data
    ENDIF
    ELSE
    PAUSE 100
    GOTO Close_File
    ENDIF

    Close_File:
    DEBUG "Closing Data File...Program Complete!"
    SEROUT TX\CTS, Baud, [$0A, $20, "datafile.txt", CR]
    GOSUB Get_Data ' Purge Receive Buffer
    STOP

    '
    [ Subroutines ]
    Get_Data:
    index = 0 ' Reset Index Pointer
    DO ' Receive Data
    SERIN RX\RTS, Baud, 100, Timeout, [ioByte]
    buffer(index) = ioByte ' Add Received Byte To Buffer
    index = index + 1 ' Increment Index Pointer
    IF index > 14 THEN Timeout ' Check For Overflow
    LOOP
    Timeout:
    RETURN

    ' Reads VRef and selected H48C axis through an MCP3204 ADC
    ' -- pass axis (0 - 2) in "axis"
    ' -- returns reference voltage counts in "rvCount"
    ' -- returns axis voltage counts in "axCounts"
    Get_H48C:
    LOW CS
    SHIFTOUT Dio, Clk, MSBFIRST, [%11\2, VRef\3] ' select vref register
    SHIFTIN Dio, Clk, MSBPOST, [rvCount\13] ' read ref voltage counts
    HIGH CS
    PAUSE 1
    LOW CS
    SHIFTOUT Dio, Clk, MSBFIRST, [%11\2, axis\3] ' select axis
    SHIFTIN Dio, Clk, MSBPOST, [axCount\13] ' read axis voltage counts
    HIGH CS
    RETURN
    '
  • yoda152yoda152 Posts: 18
    edited 2011-02-06 09:02
    should i use a PropStick USB instead how much ram does this contain or is it an issue conecting an accelerometer

    sorry i dont know much about microcontrollers
  • Mike GreenMike Green Posts: 23,101
    edited 2011-02-06 09:44
    First of all, you need to use the advice you're given. Don't "cut and paste" your program into a message. It loses formatting and, for long programs like yours, it makes it difficult to examine, particularly if you have several versions over time. Attach it to a message as a file. Have you looked at the description of "alias"? It's important if you're going to use Stamps.

    Your program has two phases (other than initialization): 1) Read a set of values from the accelerometer 2) Write them to the memory stick

    You need to make a list of all the variables in your program and mark each one as to how its used:

    1) Used temporarily while reading the accelerometer values vs. Used to hold the results of reading from the accelerometer

    2) Used temporarily while writing to the memory stick vs. Used to hold values from one episode of writing to another (like flag).

    Temporary variables can be used again (maybe with a different name / alias) in the other section(s) of your program thus saving space without affecting function.

    Maybe what you need to do is spend some time learning about microcontrollers. There are some good tutorials. Start with "What's a Microcontroller?"

    They're downloadable from here and available translated into a variety of languages.
  • yoda152yoda152 Posts: 18
    edited 2011-02-06 11:01
    Hi mike

    i have used alias on the accelerometer and have proved usfull thank you very much

    on the other side it proves harder to reduce the datalogger ram? do have any ideas

    iv attached the two files

    I have read through the book Whats a microcontroller and i am curently on basic stamp syntax and reference manual

    I am trying to add the two programs together but it is proving hard to get the ram down.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-02-06 11:28
    If you look carefully at the sample datalogger code, you'll see that really none of the variables are needed in-between writing data to the datalogger file. "flag" is not really used and "flag2" is used to keep track of what's to be written ... you don't have to use it. "buffer", "index", and "ioByte" are used during the writing process to check the error response, but are not needed afterwards. That's at most 18 bytes out of 26. The accelerometer sample program uses only 7 bytes of variable space. If you add the two together, you're still below the maximum of 26 bytes.
  • yoda152yoda152 Posts: 18
    edited 2011-02-06 14:37
    Hi

    I have connected the 2 programs together but there is a problem with the accelerometer in that it is giving a value of 0.46 X , 5.23 for Y and Z is working correct in that its value is 1.05. lines 195 to 197 are giving the problem but when i use line 160 to 163 (X) and (171- 174 Y)the accelerometer is correct for all axis

    because i need to out put more bits of data(eg -0.01, 0.05,1.05) for each axis do i need a bigger buffer

    see attachment datalogger2.2
  • yoda152yoda152 Posts: 18
    edited 2011-02-07 12:04
    hi Mike

    I was wonering if you could help me
    i got the accelerometer working now but when i try to output data to the datalogger (memory stick) the data is all over the place and part of the data is missing eg 0.0# where # is a number for the y axis.
    i have the file attached. the numbers are over lapping.
    do i need a bigger buffer because i need to out put more data
    aswell as this the program is only running through twice at most i dont know why when i have serout command active

    thank you in advance
Sign In or Register to comment.