Breaking out of subroutine?

Started by Michael Kemper March 2, 2005

I'm a bit of a newb, so here I go!

I'm working on a simple robot that uses sonar to avoid objects. The
sonar is mounted on a "turret servo" that looks around and sends the
bot in the direction of the greatest distance using select case.

I have a "backup system" consisting of some mechanical switches
mounted at the front of the bot. There is a subroutine running a do
loop, waiting for a closed switch signal to back the bot up and turn
it 90 degrees. This is where I need help:

My sub main() starts with my sonar turret looking around and storing
distances. What I really want to do is use a goto command in the
*subroutine* to reference the beginning of the *sub main()*, but I
realized the goto and label have to be in the same sub.

Is there any way to do this? I'm a bit new to this, so I imagine
there are many possibilties I am unaware of. Maybe my idea of
keeping my switch backup code in a subroutine is a bad one!

Thanks!

Mike Very basic idea of my code:

Sub main()

calltask "switch",switchstack

Do
Turret look right,front,left and record distance
Go in direction with greatest distance
Loop

End Sub

Sub Switch()

Do
If input from pin7 is 1 then
Stop
Turn around
[***This is where I would like to tell program to start at
beginning of sub main()***]
End If
Loop

End sub




One simple approach...

(Look in the code below.)

BR
Ingvar

--- In , "Michael Kemper" <kempermo@y...>
wrote:
>
> I'm a bit of a newb, so here I go!
>
> I'm working on a simple robot that uses sonar to avoid objects.
The
> sonar is mounted on a "turret servo" that looks around and sends
the
> bot in the direction of the greatest distance using select case.
>
> I have a "backup system" consisting of some mechanical switches
> mounted at the front of the bot. There is a subroutine running a
do
> loop, waiting for a closed switch signal to back the bot up and
turn
> it 90 degrees. This is where I need help:
>
> My sub main() starts with my sonar turret looking around and
storing
> distances. What I really want to do is use a goto command in the
> *subroutine* to reference the beginning of the *sub main()*, but I
> realized the goto and label have to be in the same sub.
>
> Is there any way to do this? I'm a bit new to this, so I imagine
> there are many possibilties I am unaware of. Maybe my idea of
> keeping my switch backup code in a subroutine is a bad one!
>
> Thanks!
>
> Mike > Very basic idea of my code:

dim turnaround as boolean

>
> Sub main()

turnaroundse

>
> calltask "switch",switchstack
>
> Do

if turnaround then
turn robot around
turnaroundse
else

> Turret look right,front,left and record distance
> Go in direction with greatest distance

endif

> Loop
>
> End Sub
>
> Sub Switch()
>
> Do
> If input from pin7 is 1 then
> Stop

turnaround=true

> End If
> Loop
>
> End sub



Michael,

A couple of thoughts come to mind.

You can monitor this input on a separate task although I'm not sure you
want your control logic (e.g. backing up) in the same task. In terms of
monitoring the switch, you could also use a hardware interrupt via the
WaitForInterrupt system library function or you may prefer to allocate
this to a "panic switch" that shuts down the BX and motors. There are
several ways to communicate to the main task that the switch is now
active including
1. Using a boolean module global flag, read only for the control logic,
read write for the monitor task (Ingwar's suggestion uses this approach)
2. Using a semaphore
3. Using a queue

The control logic loop (which should probably be your main task) is
always going to have this type of flow. There is not an easy way to get
into the middle of this flow. Almost all GUI programs are written using
this same kind of basic control flow.

Initialize
Do Forever
Monitor Inputs
Decide What to Do
Execute
End Do

The trick is to make this control loop as fast as possible so that it
can respond to the latest inputs (be they sensors, wheel encoders or
other kinds of interrupts). The sonar turret input could be written as
another task so it is independent of the main control loop. In this case
you might consider the switch input as part of the main task instead as
it is so straightforward.

The motor control logic could be a separate task as well especially if
there is a feedback loop that ensures the motors run in the intended
direction. Some people offload this logic onto a specialized control chip.

Mike

>I'm a bit of a newb, so here I go!
>
>I'm working on a simple robot that uses sonar to avoid objects. The
>sonar is mounted on a "turret servo" that looks around and sends the
>bot in the direction of the greatest distance using select case.
>
>I have a "backup system" consisting of some mechanical switches
>mounted at the front of the bot. There is a subroutine running a do
>loop, waiting for a closed switch signal to back the bot up and turn
>it 90 degrees. This is where I need help:
>
>My sub main() starts with my sonar turret looking around and storing
>distances. What I really want to do is use a goto command in the
>*subroutine* to reference the beginning of the *sub main()*, but I
>realized the goto and label have to be in the same sub.
>
>Is there any way to do this? I'm a bit new to this, so I imagine
>there are many possibilties I am unaware of. Maybe my idea of
>keeping my switch backup code in a subroutine is a bad one!
>
>Thanks!
>
>Mike >Very basic idea of my code:
>
>Sub main()
>
>calltask "switch",switchstack
>
> Do
> Turret look right,front,left and record distance
> Go in direction with greatest distance
> Loop
>
>End Sub
>
>Sub Switch()
>
> Do
> If input from pin7 is 1 then
> Stop
> Turn around
> [***This is where I would like to tell program to start at
>beginning of sub main()***]
> End If
> Loop
>
>End sub >