Shop OBEX P1 Docs P2 Docs Learn Events
Calling a Sub routine in spin? HOWTO??? — Parallax Forums

Calling a Sub routine in spin? HOWTO???

BotdocterBotdocter Posts: 271
edited 2010-04-07 16:18 in Propeller 1
I need some help on how to use subroutines but i can't seem to find any clear information ( at least not understandable for me) on it.

I have a dutycycle that i want to call with something like:

TURNLEFT := TRUE
repeat 450000

I attached my spin file. Have a look at line 54 and below

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
1 Parallax Propeller Robot Control Board
1 Memsic MX2125 accelerometer/ tilt
1 Parallax Ping))) ultrasonic sensor

a few motors and a whole lot of chaos!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-04-06 20:55
    You have many things wrong with your program. Please review the Propeller Manual and the Propeller Education Kit tutorials, particularly the description and examples of the PRI / PUB declarations, method calls, and the assignment operator (":="). Remember that assignments are different from testing for equality ("=="). Also note that local variables (in PRI / PUB declarations) are temporary. They disappear when the method returns to its caller.

    Your program uses PWM.Duty calls. This is a method call to another object. If you included that object as part of your main program, you would leave out the "PWM.". If you don't need parameters, you can leave out the parenthesized parameter list and just give the name of the method. The "#" is only used in Spin for access to constants in another object.

    Post Edited (Mike Green) : 4/6/2010 9:01:08 PM GMT
  • BotdocterBotdocter Posts: 271
    edited 2010-04-06 21:01
    I see. I read a lot in the manual and especially the part about pub but that didn't make anything clear to me.. english isn't my native language and i am also dislectic.
    Could you maybe show me a little example code of how i should use it?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-06 22:09
    pub and pri are used to define a subroutine. The difference is the scope. Subroutines defined as pub are public and can be used from other spin-files. pri subroutines are only visible inside of the spin-file they are defined in.
    This difference is only important if you want to write an object for the object exchange.
    Let's start with a new program ... I tend to name the first function main, as the first function is the one which executed by the propeller after boot:
    pub main
      ' here comes the code
    

    This is the easiest way to define a function ... no parameters ... no local variables ... no return value name
    Now let's say you want to define another function to do some initialization:
    pub main
      Init
     
      'some other code ...
     
    pub Init
      ' setup the pins
      dira := %11000011
    

    Again no parameters ... so the call is easy, just say "Init" wherever you want to call the subroutine. There is no need to use "return" in a subroutine, as it will automatically return to the caller when the code of the function ends (in this case dira:= is the last instruction and Init will return after that).
    Now let's say you want to pass a parameter to the function:
    pub main
      Init( 10 )
     
      ' some other code ...
     
    pub Init( pinNo )
      dira := |<pinNo
    

    Now the "Init" is defined another way ... it has a list (in this case it's only a list with one name) of parameters that are expected. Wherever you call "Init" you have to provide a value for·each parameter. This value is now available inside of "Init" by using the name of the parameter.
    Now let's say "Init" is doing some more things and can be successfull or unsuccessfull. Then it would be nice for the caller to know if it was called successfully or not:
    pub main | ok
      ok := Init( 10 )
     
      ' some more code ...
     
    pub Init( pinNo )
      ' check the pin
      if ina[noparse][[/noparse] pinNo ]
        ' ok, it's high
        return 1
      else
        ' not ok, it's low
        return 0
    

    As you can see nothing has changed for "Init" declaration. That's because each function will return a value whether you use it or not.·In "main"·declaration a variable name has been added. This is a so called local variable ("ok"). It is only visible inside of main. In this case it will store the·value returned by the function "Init". And "Init"·will return a value depending on the pin passed as parameter.

    Local variables can be declared in any function. But be aware that each local variable needs an additional LONG on the stack. So, later on when you run functions in another COG you have to provide a stack which is big enough. As long as you stay in the one COG which runs the "main" the available stack is as big as it could be - it's the whole not used memory.

    The return value of a function is also a local variable and has the default name "ret". So, instead of doing "return 0" or "return 1" you could also write "ret:=0" or "ret:=1" in the code above and let the function return automatically at the end of the code.

    You can also rename that default name of the return variable if you want. If I remember that correctly it's:
    pub Init( pinNo [b]):ok_nok[/b]
      ' check the pin
      if ina[noparse][[/noparse] pinNo ]
        ' ok, it's high
        ok_nok := 1
      else
        ' not ok, it's low
        ok_nok := 0
    

    Hope that helps ... and nobody else wrote something meanwhile ;o)
  • BotdocterBotdocter Posts: 271
    edited 2010-04-06 22:32
    Thank you for the quick reply. I think its clearer now. Allthough i may have to read it a few more times.

    I think this would be how i would call one of my subs:

    pub init : TURNLEFT ( ret (pc+1))

    right?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
  • Mike GreenMike Green Posts: 23,101
    edited 2010-04-07 00:14
    What you just posted is incorrect, but it's impossible to tell you what would be correct unless you describe what you're trying to do.

    In your original code, you have "IF TURNLEFT := TRUE". That's a valid Spin statement, but I don't think it will do what you want and I'm not really sure of that either.
  • BotdocterBotdocter Posts: 271
    edited 2010-04-07 01:49
    I just need to have a base system for my bot. So in the code im writing i just want to use words like forward or turnleft, turnright and reverse.

    So if i have a sensor reading, i just need to say;

    if sensorreading is < 40
    Turnleft := true OR sonething like goto subroutine- play it- and return to the next line.

    so in fact the code that i have to make it turn left, is a subroutine and i need to call it somehow

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
  • mparkmpark Posts: 1,305
    edited 2010-04-07 02:43
    Maybe you're more used to saying something like "GOSUB Turnleft". In Spin, just drop the GOSUB.

    PUB Init
      ' ... other code
    
      if sensorreading is < 40
        Turnleft
    
      ' ...
    
    



    and
    PUB Turnleft
    ' code to turn left
    ' ...
    
    
  • BotdocterBotdocter Posts: 271
    edited 2010-04-07 03:01
    It worked like a charm! Thank you so much! I couldn't find a sollution to this. ( this explains it) hahaha

    thanks again ya'll

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!

    Post Edited (Botdocter) : 4/7/2010 1:18:12 PM GMT
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2010-04-07 14:45
    Hi Botdoctor,

    You might find the Spin tutorial built into the Help file to be a good place for foundation information. Also, the PE Kit Labs book and example code that Mike Green mentioned are included with the Propeller Tool software installer, and the Labs book is also available through the Help menu.

    In the PE Labs, Chapter 5 on Methods and Cogs will go into more detail with what you are working on, especially the illustrations on pages 69-71.

    Take care,
    -Steph
  • BotdocterBotdocter Posts: 271
    edited 2010-04-07 16:18
    Steph Lindsay (Parallax) said...
    Hi Botdoctor,

    You might find the Spin tutorial built into the Help file to be a good place for foundation information. Also, the PE Kit Labs book and example code that Mike Green mentioned are included with the Propeller Tool software installer, and the Labs book is also available through the Help menu.

    In the PE Labs, Chapter 5 on Methods and Cogs will go into more detail with what you are working on, especially the illustrations on pages 69-71.

    Take care,
    -Steph

    ah thank you! I do read the manual but didn't know about the pe and examples.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1 Parallax Propeller Robot Control Board
    1 Memsic MX2125 accelerometer/ tilt
    1 Parallax Ping))) ultrasonic sensor

    a few motors and a whole lot of chaos!
Sign In or Register to comment.