Shop OBEX P1 Docs P2 Docs Learn Events
Initialising an array — Parallax Forums

Initialising an array

MightorMightor Posts: 338
edited 2007-07-22 12:19 in BASIC Stamp
Hey there,

I'm looking to use an array to hold servo speed data. Is there a way to initialise it C style, e.g. something like this:

right_servo[noparse]/noparse = { 790, 715, 820, 680, 850, 650}
left_servo[noparse]/noparse = { 713, 790, 687, 817, 667, 829 }

I am aware of the DATA directive, but I'm keen to use an array for fast lookup purposes, I've found the EEPROM to be quite slow and that won't leave me with a lot of time to do more important things, like checking my sensors, etc.

Gr,
Mightor

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
| What the world needs is more geniuses with humility, there are so few of us left.

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-07-22 07:30
    Mightor -

    I know nothing about C, so is this fixed or variable data? In other words, will it be changed during the course of program execution?

    I don't know what sort of algorithm you are using to access the EEPROM tables, but that can have as much, or more, to do with the access speed as anything else.

    Rgards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-07-22 07:56
    If the values are constants and you use the array only one place in your program, you can use LOOKUP as a combination array constructor and indexer. If they're constants, but you want them in a variable array, you can define them in a DATA statement and READ them using a FOR...NEXT loop. Otherwise, you can just READ from EEPROM on a as-needed basis.

    PBASIC doesn't have any other array constructors.

    -Phil
  • MightorMightor Posts: 338
    edited 2007-07-22 07:58
    Bruce,
    Bruce Bates said...

    I know nothing about C, so is this fixed or variable data? In other words, will it be changed during the course of program execution?

    I don't know what sort of algorithm you are using to access the EEPROM tables, but that can have as much, or more, to do with the access speed as anything else.
    The data will not change. As for EEPROM speed, I found that with one of my previous projects that writing more than two words to the EEPROM would seriously bog down the servos. Does the same not also apply to reading or is this much faster?

    I am looking to read the data a bit like this (not necessarily working code):
    servo_right DATA Word 790, Word 715, Word 850, Word 650
    servo_left  DATA Word 713, Word 790, Word 667, Word 829
    
    FWD            CON      0
    FFWD           CON      2
    BAK            CON      1
    FBAK           CON      3
    
    MotorR         PIN      12
    MotorL         PIN      13
    
    SpeedR         VAR      Word
    SpeedL         VAR      Word
    
    ' Make the bot go forward:
    READ servo_right + FWD, SpeedR
    READ servo_left + FWD, SpeedL
    
    PULSOUT MotorL, SpeedR
    PULSOUT MotorR, SpeedL
    
    ' Make the bot go backwards very quickly:
    READ servo_right + FBAK, SpeedR
    READ servo_left + FBAK, SpeedL
    
    PULSOUT MotorL, SpeedR
    PULSOUT MotorR, SpeedL
    
    



    This seems a lot more code than for example something like this (this is not working code, of course):
    right_servo[noparse][[/noparse]] = { 790, 715, 850, 650}
    left_servo[noparse][[/noparse]] = { 713, 790, 667, 829 }
    
    FWD            CON      0
    FFWD           CON      2
    BAK            CON      1
    FBAK           CON      3
    
    MotorR         PIN      12
    MotorL         PIN      13
    
    ' Make the bot go forward:
    
    PULSOUT MotorL, right_servo[noparse][[/noparse]FWD]
    PULSOUT MotorR, left_servo[noparse][[/noparse]FWD]
    
    ' Make the bot go backwards very quickly:
    
    PULSOUT MotorL, right_servo[noparse][[/noparse]FBAK]
    PULSOUT MotorR, left_servo[noparse][[/noparse]FBAK]
    
    



    That way just looks cleaner and would be a lot faster; there is no EEPROM access and a lot less code. I suppose I could initialise the values in those arrays by one by one, but if there is a way around that, that'd be great [noparse]:)[/noparse]

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | What the world needs is more geniuses with humility, there are so few of us left.
  • MightorMightor Posts: 338
    edited 2007-07-22 08:02
    Phil,
    Phil Pilgrim (PhiPi) said...
    If the values are constants and you use the array only one place in your program, you can use LOOKUP as a combination array constructor and indexer.
    I see what you mean. I could indeed use the LOOKUP method wrapped in the servo control sub routine. I like that better than the EEPROM way. Thanks for the tip [noparse]:)[/noparse]

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | What the world needs is more geniuses with humility, there are so few of us left.
  • MightorMightor Posts: 338
    edited 2007-07-22 08:12
    The code would end up looking a bit like this:

    FWD            CON      0
    FFWD           CON      2
    BAK            CON      1
    FBAK           CON      3
    
    MotorR         PIN      12
    MotorL         PIN      13
    
    speed          VAR      Word
    speedR         VAR      Word
    speedL         VAR      Word
    
    ' Make the bot go forward one pulse:
    
    speed = FWD : GOSUB Do_Move
    
    ' Make the bot go backwards one pulse, very quickly:
    speed = FBAK : GOSUB Do_Move
    
    END
    
    Do_Move:
      LOOKUP speed, (790, 715, 850, 650), speedR
      LOOKUP speed, (713, 790, 667, 829), speedL
      PULSOUT MotorL, SpeedL
      PULSOUT MotorR, SpeedR
      return
    
    



    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | What the world needs is more geniuses with humility, there are so few of us left.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-07-22 12:19
    Mightor -

    Reading from a EEPROM is MUCH faster than writing to it.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sign In or Register to comment.