Shop OBEX P1 Docs P2 Docs Learn Events
variable space — Parallax Forums

variable space

Robert25Robert25 Posts: 53
edited 2006-03-08 21:10 in BASIC Stamp
OK folks... quick question.... When uploading a program from the PC to my BS2 robot, I get an error message stating there is no more variable space left.·

Any hints?

Comments

  • GadgetmanGadgetman Posts: 2,436
    edited 2006-03-05 17:00
    How many variables have you defined, and how large are they?
    (Feel free to post that part of your code)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't visit my new website...
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-03-05 17:17
    Robert -

    There is only a finite amount of variable space on any PBASIC Stamp.·The amount of space for variables is limited to 26 bytes. It appears as though you may have exceeded this maximum. Not to worry!·Go through your program and check for the following:

    Replace any WORD var with a BYTE var if that is all you need. Savings =·1 BYTE.

    Replace any BYTE var with a NIB var if that is all you need. Savings = 4 BITs. Typical of this kind of replacement is a loop counter which only needs to count (say) from·0 to 12, when a BYTE would permit 0-255, which is unnecessary.

    Replace any BYTE or NIB var with a BIT var if only ON / OFF is required.

    Due to the way the PBASIC Stamp allocates variable space, you can end up with more space than you expect when you consolidate and condense your WORD, BYTE, NIB and BIT usage as shown above.

    Reuse variables rather than allocating new ones. One variable named (say) Loop_Cnt could be used for ALL independent, non-nested FOR ... NEXT loops throughout the program, rather than using Loop_Cnt1, Loop_Cnt2, etc.

    Rename variables if you wan to reuse them, but they need a more appropriate name, such as follows:

    Initial_Value VAR BYTE

    Revised_Value VAR Initial_Value

    The PBASIC Stamp syntax is perfectly able to use an interim (unnamed) workspace for inline calculations. Force it to do so, rather than pre-allocating interim workspace variables unless absolutely necessary. Sometimes this may mean the creative use of parentheses in arithmetic expressions.

    When all else fails, show us your entire·program, and we can generally make some creative suggestions for reducing the amount of allocated variable space.

    Regards.

    Bruce Bates


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-05 17:39
    You can also view your user variable space by clicking on the Memory Map Button on the Toolbar.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Robert25Robert25 Posts: 53
    edited 2006-03-05 19:01
    Thanks for the feedback, and I actually started going through the variable definitions to reduce the size as you indicated.· This apparently works for now....·

    However, assuming I am now very close to the max space allowable on the bs2, and knowing this is only the start of building a more complex program, how do I get more variable space?

    I've read that some duties can be offloaded to a co-processor.· Can more space be used there?

    I also read that memory in the stamp chips is allocated in blocks and there are some programming tools enabling you to access the extra memory.· Is this true and if so, what are those tools?
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-05 19:03
    No; what you'll need to do is learn to conserve memory. You may in fact be using several temporary variables, but never at the same time -- these could be consolidated. If you post your code you'll get more specific suggestions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Robert25Robert25 Posts: 53
    edited 2006-03-05 19:10
    stay tuned... have to switch computers to post the code...
  • Robert25Robert25 Posts: 53
    edited 2006-03-05 19:20
    OK attached is the code (still in development)

    Any help or comments would be appreciated.·

    Jon,· please keep in mind this is on an Arrick Arobot.

    As long as folks are perusing the code, I am having some logic problem with the headlight routine.
  • Fe2o3FishFe2o3Fish Posts: 170
    edited 2006-03-05 19:54
    Robert25,

    Alas, the wife wants to go for a ride so I don't have much time to peruse yer code.
    Still, I did load it into my BS Editor and you still have extra RAM (variables) unused.

    You can save almost 200-bytes of EEPROM (program memory) by replacing redundant
    phrases (e.g. "I'll head that way." and "seems to be clear.") with just one instance in
    your DATA statements and add an extra BASIC command or two to have those
    concatenated at the appropriate places.

    FWIW, I had no problems downloading your code into my BoE other than I didn't have
    any of the peripherals attached.

    That's all for now. Good luck!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Rusty-
    --
    Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
    **Out yonder in the Van Alstyne (TX) Metropolitan Area**
    Microsoft is to software what McDonalds is to gourmet cooking
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-05 20:06
    I can see at least a few variables that can be reused, and have a direction variable defined as a Byte that could be a bit. Granted, this is a fairly large program that seems to have been built-up from pieces of others (which is fine), so what you have to do is go through and analyze what is actually being used and where and if you can consolidate variables. For example, can you have one variable called 'result' (a Word) that can be used for both the Ping sensing and the light sensing since you act immediately on those respecitve sensors and don't really need to save the variable?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • John R.John R. Posts: 1,376
    edited 2006-03-05 21:25
    As a hopefully helpful comment:

    If you come from a "computer programming" background, where you have (relatively speaking) unlimited memory for program and variables, you need to realize that the microprocessor is a whole different ball game. Because of limited resources (memory and processor time, etc.) you have to do things with a microprocessor that might be considdered "poor practices" in a different environment.

    As and example I would not be very happy with a programmer working for me on a PC or Unix based project who used a common variable for different purposes. It makes the software harder to maintain, and while it works now could lead to a conflict at a latter point in time. You would also tend to frown on some tricky "if test" that might be more efficient, but harder to read for future (potentially less clever) programmers. You might tend to break up the statement into multiple tests or even nested statements. Less efficient, but with current processing and RAM, it makes the total cost of the software lower (long range maintenance is reduced).

    On the other hand, if the project is on a microprocessor, I would lean the other way, and be unhappy of the save variable weren't reused, saving room for possible future enhancements.

    Kind of like going back to the "old" days, when 640k of RAM on a PC was "a deluxe system". You have to make compromises and do things that in a different environment wouldn't make sense.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10
  • Robert25Robert25 Posts: 53
    edited 2006-03-05 21:35
    Jon,

    Thanks for looking. I will review the program and try to uncover places where a variable can be reused... good point.

    I am concerned about the future though. My plans are to keep expanding the capabilities (e.g., put in the PSC controller for additional servo controls etc.) I know I will quickly run out of space at this rate. Can you suggest what my alternatives might be?

    Speaking of the PSC... ( I know probably a different post) but I'll ask it here anyway. I was toying around to get a feel for it and the programming. I ran the "find the PSC" routine and it worked fine... reply was version 1.4. However, when I tried the ramp program, nothing happens to any servo even though the PSC red led is on and the green led blinks.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-05 22:03
    Please move PSC-related questions to separate thread and keep this one on the topic of variable-space conservation.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • stamptrolstamptrol Posts: 1,731
    edited 2006-03-06 12:58
    Robert25,

    For your future variable needs; while you should always use the resources efficiently, as the other posters have suggested, the day will come when you need more space. Just switch to the BS2sx which will give you 8 times the variable space, in the form of 8 separated program slots. As well, there is a common scratchpad area accessible from from each program slot. Runs 2.5 times faster, too!

    Tom Sisk
  • NewzedNewzed Posts: 2,503
    edited 2006-03-06 13:08
    Unless I am mistaken, the BS2SX has the same amount of variable space as all the other Stamps - 6 I/O bytes and 26 variable bytes.· Each of the program slots just use exactly the same variable list, arranged in exactly the same order.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Do you have a Stamp Tester yet?
    http://hometown.aol.com/newzed/index.html

    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-06 15:10
    That is correct, the variable space is actually the same.· The EEPROM space is multiplied times and you have 63 bytes of ScratchPad RAM you can use for data as well.· You cannot use it as direct variable storage but it can be implemented to work as such.· Kind of like work space.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Robert25Robert25 Posts: 53
    edited 2006-03-06 15:50
    OK so the way I understand it, I can change the stamp to the SX version and then through some programming skill, I can add several more varialbles. I assume this programming will be in the form of directing the area in memory to store the extra variables etc. The SX version has more memory and runs faster (that is cool). So, the extra memory in the SX is used for larger programs in general? Is it a direct pin for pin replacement for the BS2?

    On a similar thought, one other possibility to this issue that I was thinking about was to adapt Herbert to go wireless; I have a 802 home network running and thought that the extra space and higher functions that I was thinking of adding (video, etc) could be handled by the computer on the system and local functions handled by the onboard stamp. I wouldn't know where to start with this option although I've been reading about PINK and other RF transceiver projects. Anyone have a best way to start this up???
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-03-06 16:01
    Robert,

    ·· I still think the original advice you got about condensing your variable and code space is your best option right now.· I have seen and helped reduce programs that use all variable space and program space down to usually 2/3 of the variables (in one case lesss than half!) and less than half the code space.·

    ·· I have released a few programs which make great use of BASIC Stamp resources by sharing I/O lines, re-using variables and using subroutines and data statements to reduce the number of times I use certain instructions.· Some demo programs I have done intentionally waste space in the interest of being clear, but I could easily take the same program (I have one that fills the BS2 EEPROM) and get it down to around half of that size with full functionality.··In this case I used a·lot of SEROUT and DEBUG statements, but I could've just as easily setup a subroutine to handle all of that and put all of the text blocks into data statements.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Robert25Robert25 Posts: 53
    edited 2006-03-06 16:23
    Chris,

    I really appreciate you help (and all of the others as well).· My intro into computer programming years ago was with basic in a PC.· Thus the earlier comment about the "philosophy" of programming in PBasic in a uController vs. basic in a PC I really understood.· That being the case, I know I could probably reduce program and variable space by getting more streamlined (this will definetly test my programming skill).·

    But one of your comments brings up another question.· In my program, I use DATA statements to store the EMIC text.· Also, the debug statements are there only as temporary to help 'debug" the program and will be removed eventually.· I don't think there is much more room in eeprom space either (according to the memory map).· How can I effectively use this space?
  • Robert25Robert25 Posts: 53
    edited 2006-03-06 17:01
    Ah... One other thought...

    Isn't there a way of connecting two STAMPS together??? Could I get say an SX chip as the main and use the BS2 for other tasks???
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-06 17:13
    That question should really be posted as a separate thread -- the answer is of course, yes, and there's a Nuts & Volts article that shows how.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Robert25Robert25 Posts: 53
    edited 2006-03-06 17:17
    Yes... I thought I read that somewhere....

    When ready, I will post it. Right now, I will try trimming the program with everyones great suggestions.

    Jon, as always, thanks for the help.
  • GadgetmanGadgetman Posts: 2,436
    edited 2006-03-06 22:20
    Robert25 said...
    Chris,
    But one of your comments brings up another question. In my program, I use DATA statements to store the EMIC text. Also, the debug statements are there only as temporary to help 'debug" the program and will be removed eventually. I don't think there is much more room in eeprom space either (according to the memory map). How can I effectively use this space?

    You may want to look at a BS2P24 as not only does it have 8 progam-spaces, 127Bytes of Scratchpad RAM and is faster than the BS2sx, but it also allows you to use othervise unused program-spaces to store DATA.
    Also, I believe the SERIN/SEROUT commands can work with the Scratchpad RAM instead of the usual variables, which might just help you out a bit if you're running out of variable space...

    And if you don't need the speed, the BS2Pe has double that number of program-spaces...
    (you can only run programs from the first 8, the last 8 are for DATA only)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't visit my new website...
  • verobelverobel Posts: 81
    edited 2006-03-08 21:10
    from another thread..

    I was reading in the Basic Ref. Manual ...another approach is to use DATA in conjunction with READ and WRITE (to EPROM). So you could set a block of say 10 bytes aside at the start of the program .. with individual pointers like: temperature, speed, pressure.. (10 different variables) and then use them as required in the program. [noparse][[/noparse]could even program arrays like this]

    READ temperature VAR1 ' read the 'external variable' in to a temp variable (that is reused often)
    RESULT = VAR1 * CALIBRATION + OFFSET
    WRITE RESULT temperature ' store the result.. for use later or elsewhere
Sign In or Register to comment.