Input and Subroutines issues

Hi,
I hope you can help. I’m coming to the end of a rather complicated build and have hit a brick wall with the Script side of things.
The part I am trying to get working is a Tank Sponson. Yeah I know, why am I building a Tank?! Because I happen to like them! :sunglasses:

The effect I am close but failing to achieve, is for the gun to traverse through a sequence and when the sound unit sends a signal to the servo controller, a gun recoil and flash sequence happens. Then it carries on traversing. It would have been easier possibly if there was an option for a random traverse motion, but I understand from what I have read that this is not an easy option with this setup.

Anyhow, good news is I got all the hardware to work and talk to one another, phew! Basically it was a case of buy bit, scratch head when it didn’t work, look at Forum… purchase the missing parts. :smiley:
I may have played with my first computer in the 80s, but I’m not to proud to admit this has all been a learning curve for me. Challenging the mind and getting something to work still gives me a buzz.

The Parts I am using is as follows…

Pololu RC switch with Small Low-Side MOSFET. (This converts the signal from the Sound Unit to something the Servo Controller can understand.)
Six-channel Micro Maestro Servo Controller. (This is the main unit into which the rc switch sends its signal.)
Pololu basic relay carrier 5v. (Used to fire the Strobe Light for gun flash… which works! Yes!!)
Servos (Traverse, Recoil, Elevation, Gun Smoke.)

So far I have managed to get the Gun traversing and I can get the Sound unit to activate the Recoil sequence… but I can’t get it to do both! It is one or the other. I seemed to be close and last night everything I did just made things worse or at best made no change whatsoever.

Today I have decided to to do the sensible thing and just ask for help.

This is the last Script I tried…

# Sponson Movement LH

start:
begin

3968 5 servo
6062 4 servo
4809 3 servo
4594 1 servo

0 get_position 100 greater_than
if
recoil
0 get_position 100 less_than
if
traversing
endif endif
repeat

sub traversing

begin
  3000 4594 4809 0 6062 3986 frame_1..5 # Frame 0
  3000 5024 frame_1 # Frame 1
  3000 5279 frame_1 # Frame 2
  3000 4750 frame_1 # Frame 3
  3000 5122 frame_1 # Frame 4
  3000 5984 frame_1 # Frame 5
  3000 5279 frame_1 # Frame 6
  3000 5690 frame_1 # Frame 7
  3000 5103 frame_1 # Frame 8
  3000 4555 frame_1 # Frame 9
  3000 4437 8000 frame_4_5 # Frame 10
  3000 5886 3968 frame_4_5 # Frame 11
repeat

sub frame_1..5
  5 servo
  4 servo
  3 servo
  2 servo
  1 servo
  delay
  return

sub frame_1
  1 servo
  delay
  return

sub frame_4_5
  5 servo
  4 servo
  delay
  return

sub recoil

  8000 5 servo
  4680 4 servo
  6368 3 servo
  0 1 servo
  100 delay
  goto start

Helen x

This is the model actually running last weekend :sunglasses:https://www.facebook.com/673233936126477/videos/845596288890240/?fref=nf


Hello.

That looks like a really cool project! Thank you for sharing it.

It sounds like you can get the traversing motion and the gun recoil motion, but you want to be able to interrupt the traversing subroutine to run the gun recoil one and return to traversing from where it left off. If this is the case, that kind of behavior could be tricky to achieve without flaws or drawbacks, and it might take a lot of trial and error to get correct. The Maestro scripting language does not support interrupts, so to mimic that behavior you could make the traversing subroutine do many small servo movements and check for the input signal that triggers the gun recoil subroutine between each small movement. If the input signal from your sound module is there, run the gun recoil routine and return to the traversing routine, if it is not triggered, continue to the next traversing movement. If you try modifying your script to do this, and have problems, you can post what you have so far here (along with a description of what it is doing), and I would be glad to take a look at it.

Alternatively, you might consider using a separate microcontroller, such as an Arduino or one of our Arduino-compatible A-Star controllers, to process the input signals and send TTL serial commands to the Maestro since it is easier to do slightly more complex programming like this in the Arduino programming language. If you choose this option, the Maestro library for Arduino might be helpful.

-Brandon

Thank you for the quick response Brandon, glad you like the little project. :slight_smile:
I have put the 3D Printed boxes on Thingiverse and will put some photos up here of the final wiring setup, that way I can help others just as this site has helped me. http://www.thingiverse.com/HelentheCAD/designs

Actually that has been really helpful, I can kinda see now why I had some success at times and then with a tiny alteration had nothing. I think I will try the smaller movements first. If that fails then you just gave me the idea of giving the traversing servo its own Servo Controller.

To late tonight, but will definitely be having a play tomorrow. Post progress in the evening.

Helen x

Hi,

Well I’ve not managed the time on the Scripting side of the Sponson as I’d hoped. I have though I think at least a glimmer of hope as far as getting this to work with one servo controller.

It looks like I might be able to do it with a Stack of numbers, that I then work down by using the Drop command to remove one each loop. The number in the Stack are used to move the Traversing Servo to set positions.
The Stack and one second loops appear to work ok, the servo movements look good.
The only issue I have with using a Stack of set numbers, is that I don’t know how or if, the Stack can be refreshed and become an endless loop. Any ideas?

Now at the moment I have the one second delay in the servo sub routine, this results in the Input that is only momentary being missed more times than it is picked up. When it spots the Input the Recoil sub routine works fine.
What I am wondering is if instead of putting in the delay the usual way, is it possible to delay the servo movements by Looping for one second the get_position (Input) part of the Script, so as to increase the chance of the Recoil subroutine being activated?

The problem with not knowing if these things are possible is that I don’t know if I’m close or further away from making it all work.

Hope you can help.

Helen x

Latest attempt…

# Sponson Movement LH
3968 5 servo
6062 4 servo
4809 3 servo
4594 1 servo

4426 4426 4426 5526 5526 4701 4701 4701 4701 5709 5709 5709 6240 6240 6240 5067 5067 5067 5067 5067 4426 4426 4426

begin
start:
0 get_position 100 greater_than
if
recoil
endif
traversing

drop
repeat

sub recoil
  8000 5 servo
  4680 4 servo
  6368 3 servo
  300 delay
  3968 5 servo
  6062 4 servo
  4809 3 servo
 return 

sub traversing
dup 1 servo
1000 delay
return

Hello, Helen.

I added code tags ([ code ][ /code ] - without spaces) to your post to make it easier to read; please use this method to post code in the future.

With your current code, you would probably need to add a label to jump to when you want to reload those numbers. Then use DEPTH to get the number of values left on the stack after the DROP command and compare it to 0. If the stack depth is 0, reload the stack again using the command GOTO to jump to “numbers”; otherwise, continue traversing and dropping values. Like so:

numbers:		# label to jump to when depth of stack equals 0
4426 4426 4426 5526 5526 4701 4701 4701 4701 5709 5709 5709 6240 6240 6240 5067 5067 5067 5067 5067 4426 4426 4426

BEGIN
  traversing		# move gun
  DROP
  DEPTH 0 EQUALS	# get depth of stack and compare it to 0
  IF
    GOTO numbers	# add list of numbers to stack if depth equals 0
  ENDIF
REPEAT

By the way, you might consider looking at the fourth example under the “Compressing the sequence” heading in the “Example Scripts” section of the Maestro User’s Guide.

You could replace the delay in your traversing subroutine with a WHILE loop so that the Maestro will continually check your input channel (channel 0) for 1000ms after setting the servo’s target position on channel 1. For example:

SUB traversing
  dup 1 servo
  1000			# 1 second timing
  # check input on channel 0
  BEGIN dup WHILE
    0 get_position 100 greater_than
    IF
      recoil		# fire gun if > 100
    ENDIF
    1 delay		# delay 1ms
    1 minus		# subtract 1 from count (1000ms)
  REPEAT
  DROP
RETURN

- Amanda

Fantastic Customer Support!!! <3

Thank You Amanda that worked first time of asking. I promise now to tidy my script up and post it without spaces. :slight_smile:
There are a few mechanical tweaks to do and I shall be able to post a full final post with parts and video. Should be later this week.

I have to admit that I was at an impasse, with two newly purchased Maestro units ready to go into the Sponsons to separate out the traversing form the recoil. These now will be used to make other parts come to life.

Brandon and Amanda, I can’t thank you enough, there is a big modelling show at Bovington Tank Museum in just over a week and with your help I can now show it to its full.

Helen x

I am glad that the code worked for you; thank you for letting us know.

By the way, your project looks really cool and ambitious, and we would love to see it in action. You might consider posting more about it (e.g. design, materials used, idea) in the “Share Your Projects” section of the forum. Also, it would be great if you could post a video of the tank’s demonstration at your event!

- Amanda

Hi Amanda,

Yes I can do that. More than happy to share what I have learned and pass it on.
A bit of background, some photos, a wiring diagram with parts list and the code you helped me with. May take a few weeks but I will put something up.

Thanks
Helen

Hi,

I’ve just posted in the ‘Share Your Projects’ section… https://forum.pololu.com/t/rc-tank-gun-movement/8521/2

Helen x