AXE024 servo board for Model Tank turret rotation - help please

AllyCat

Senior Member
Hi Jen

Yes, my code was just intended to show how the various forms of the FOR loop will behave. Note that STEP can be any value, positive or negative (it's one of the few cases where the PE/Compiler "understands" a negative number).

SERTXD sends whatever is written within the brackets (numbers and/or text strings) to the "Terminal Emulator". The TE (window) can be arranged to open automatically after a program is downloaded, or manually (PICAXE : Terminal , pull-down menu). It's similar to DEBUG, but faster, and allows you to choose just the data values that you're trying to follow/analyse. So the window could show a "list" of the values (which might be) sent to control a servo.

Cheers, Alan.
 

jensmith25

Senior Member
Thanks Alan,

I found the simulator in the windows software pretty handy as I could see that the servo code was stepping through correctly.

This is what I've got now so it goes from centre, to one side, round to the other and then back to centre.

Code:
'*** Program constants
' Turret position
	symbol turretposition = b1


main: servo 1, 135
pause 2000
for turretposition = 135 to 200 step 1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 200 to 80 step -1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 80 to 135 step 1
	servoPos 1, b1
	pause 10
next b1

goto main
 

jensmith25

Senior Member
I've got the servo working quite nicely but it does a bit of a jump at the start of the programme where it's set to go to it's start position, even if I switch off the power when it's in the centre. Is there a way to prevent this?

Video showing the servo working including the jump at the start: http://youtu.be/u5HdPvRQl7E (hope the link works ok as it's set to 'unlisted' so it's not public.

Code is as follows:
Code:
'*** Program constants
' Turret position
	symbol turretposition = b1


main: servo 1, 135
pause 2000
for turretposition = 135 to 200 step 1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 200 to 80 step -1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 80 to 135 step 1
	servoPos 1, b1
	pause 10
next b1

goto main
I need to adjust the numbers for the angles a bit but otherwise it's working ok.
 

AllyCat

Senior Member
Hi Jen,

Does the servo "kick" immediately when power is first applied (to it and the PICaxe at the same time?), or when the PICaxe Program starts running (and outputting the servo control pulses), perhaps one or two seconds later? It's one of the subtle (and unforeseen) differences between a servo and a stepper (which certainly wouldn't kick at start-up).

A solution might lie in using the PICaxe to control (switch) the power line to the servo, only after the positioning pulses start being generated. That would need at least one external transistor, preferably in the positive rather than the ground line (i.e. a PNP giving "high side switching") driven from a spare PICaxe pin. But before attempting that, can you create some test code which delivers a constant servo output pulse (at whatever best appears to correspond to the turret centre position). Then try disconnecting and reconnecting either the supply or the ground servo connections and see if the kick can be avoided.

Cheers, Alan.
 

jensmith25

Senior Member
Hi Alan,

It is an immediate kick when I switch the power on which supplies both the PICAXE and the servo.

How do I create the constant servo output pulse? Just ask it to stay at 135?
 

AllyCat

Senior Member
Hi Jen,

Yes. The servo output is a "background" task, so should continue when the program is inactive, e.g. using a DO : LOOP , or a very long pause.

You might also put a few seconds pause at the start of the test program, to confirm that it really is the power being applied to the servo that is causing the kick.

Cheers, Alan.
 

jensmith25

Senior Member
Hi Alan,

I tried the test. I made it wait for 5 seconds and then move to centre (135)

What it now does is jump clockwise, wait for 5 seconds and then rotate to centre or 135.

It would seem that the jump is therefore related to switching on the power. If the power is disconnected it jumps further clockwise when power is reattached, the same amount each time and then goes back to 135 after the 5 seconds.
 

AllyCat

Senior Member
Hi Jen,

What it now does is jump clockwise, wait for 5 seconds and then rotate to centre..
Does it jump to a position that could be used as a centre position? i.e. is is far enough from the end to still permit the angular rotation desired? I believe the "normal" range of a servo is 75 to 225 which would make the centre nominally 150. What (approximate) number corresponds to the position to where the servo jumps at power-on?

If the "default" (power-on) position is too near to an endstop (or if you want the ability to define the "centre" position purely within the software) then you will probably have to arrange the PICaxe to control the power supply to the servo (as I mentioned above), in addition to the position. Then, the PICaxe can start its program running and generate some sort of "correction" signal (I don't know what that might be) when it switches the power on to the servo.

IMHO the first solution is preferable, if possible. ;)

Cheers, Alan.
 

jensmith25

Senior Member
Thanks Alan. I did wonder if what it might be doing is jumping to what it thinks is the centre based on the test. I will try it again with 150 as the centre and see what happens.
 

JimPerry

Senior Member
Looks like an old clock - it will always go to centre (midnight) when powered up- so move the "hand" to centre then adjust code to move as required :confused:
 

jensmith25

Senior Member
Need more help - added a switch

I added a switch to my circuit on pin 3 as directed in the instructions but I can't get it to work and must be doing something wrong in the code but I can't figure out what! I'm sure it's something very simple....

I tried this and the switch just doesn't do anything - the servo just moves as per the programme.

Code:
#Picaxe 08M2

'*** Program constants
' Turret position
	symbol turretposition = b1
	
init:
' set servo to centre
servo 1, 150

main: 
if pinC.3 = 1 then move
goto main

move:
pause 2000
for turretposition = 150 to 200 step 1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 200 to 110 step -1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 110 to 150 step 1
	servoPos 1, b1
	pause 10
next b1

goto main
So then I tried adding this to the code if pinC.3 = 0 then init and now the servo doesn't move at all other than the initial jump when power is applied.

Code:
#Picaxe 08M2

'*** Program constants
' Turret position
	symbol turretposition = b1
	
init:
' set servo to centre
servo 1, 150

main: 
if pinC.3 = 0 then init
if pinC.3 = 1 then move
goto main

move:
pause 2000
for turretposition = 150 to 200 step 1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 200 to 110 step -1
	servoPos 1, b1
	pause 10
next b1

pause 2000
for turretposition = 110 to 150 step 1
	servoPos 1, b1
	pause 10
next b1

goto main
What am I missing. I've tried looking through the manuals and as far as I can see I've done what it said but I'm obviously missing something....

I've attached a photo of the board with the switch leads - just in case. It's the two red wires and it's a push button switch.

AXE024 board.jpg

Thanks.
 

hippy

Technical Support
Staff member
It all looks correct so perhaps try a simpler program. Download and keep the download cable connected, the program should show "1" on the terminal display when the button is pushed, "0" when it isn't -

Code:
#Picaxe 08M2
#Terminal 4800
#No_Data
Do
  SerTxd( #pinC.3 )
  Pause 500
Loop
 

jensmith25

Senior Member
Thanks Hippy, I'll try that tomorrow.

It's not the servo because it's running fine. Just the switch isn't having any effect.
 

edmunds

Senior Member
Sorry, if it comes too late, but here is a link to one of the smallest full function servos in the world.

http://www.mikromodellbau.de/Shop/artikeldetails.php?aid=946

As a 1:87 model builder, I have worked with a number of those also in picaxe driven projects. Servo is, no doubt, a better option for what you want to achieve. While stepper motors are great for many applications (especially the small ones from cd drives and printers), they do require end-stops or at least one to reference and a lot more code and extra components. I have not tried it yet, but I'm sure you can make the servo turn very slow if need be if you send your own pulses with pulsout command, rather than servo command or increment and pause the servo command in a for loop.

Edmunds
 
Last edited:

jensmith25

Senior Member
It all looks correct so perhaps try a simpler program. Download and keep the download cable connected, the program should show "1" on the terminal display when the button is pushed, "0" when it isn't -

Code:
#Picaxe 08M2
#Terminal 4800
#No_Data
Do
  SerTxd( #pinC.3 )
  Pause 500
Loop
I've run the code and the switch is working fine. Seems the opposite way round to what I expected - when it's pushed in it's 0 and out is 1 but it's working as it should in the terminal for the test programme.

It's still not working with the servo though. I press the switch and the servo just carries on as normal.

Is there anything else I can try or might be doing wrong? I noticed in one of the manuals that a switch press can sometimes be read as several presses - could this be the issue? I'd still expect something to happen though with the servo.
 

Technical

Technical Support
Staff member
You do realise that in the program in your earlier post the switch input is only checked once every 7 to 8 seconds or so (3 x 2 second pauses + the servo movement pauses) - or in other words you may need to hold the switch down for at least 8 seconds before any change is noticed?
 

jensmith25

Senior Member
You do realise that in the program in your earlier post the switch input is only checked once every 7 to 8 seconds or so (3 x 2 second pauses + the servo movement pauses) - or in other words you may need to hold the switch down for at least 8 seconds before any change is noticed?
Ah, no I didn't know that. It now appears to work if I wait.

What do I add to the code so it does it immediately?
 

AllyCat

Senior Member
Hi Jen,

What do you expect "it" to do immediately? You only have code to start it moving.

If you want a "toggle" action (press once for on, again for off) then you need to code that, and indeed you may then have issues with contact bounce, unless it's coded correctly (usually a pause and re-test is sufficient).

To make the switch more responsive, one method is to test it regularly, e.g. within a sub-loop having shorter pauses, and also within the servo movement loop. The more advanced method is to use an interrupt routine, or you can read the switch in a parallel task (multi-tasking).

Cheers, Alan.
 

jensmith25

Senior Member
Hi Alan,

I want it to start moving when the switch is pressed 'on' and return to centre when the switch is pressed 'off'. I think I just twigged about sub-routines.

Can you show me what I'm supposed to write in the code? Do I use gosub or goto or is the interrupt better?
How do you do a pause and retest?
 

AllyCat

Senior Member
Hi Jen,

The "efficient" method is to use an interrupt, but I wouldn't advise attempting that until you're really comfortable wih subroutines. Interrupts are basically a complex form of subroutine, and even subroutines can have "issues" if you don't return from them correctly.

The important thing to understand is that the PICaxe normally only does one thing at a time, so for example, if it's sweeping the barrel right then that's all it will do. If you want to be able to stop that happening, then you need to repace its Pause (or shorten the period) with a test of the switch. For example if pinC.3 = 0 then {goto} returntocentre . Here, returntocentre is a simple label, not a subroutine, but note that that its following code needs to "know" the present barrel position, so that it can return smoothly to the centre. In this example, contact bounce shouldn't be an issue.

To test the switch during a pause, then the equivalent of Pause 2000 might be:
Code:
for b1 = 1 to 200
   if pinc.3 = 0 then {goto} returntocentre
   Pause 9             ; the switch test is approximately equivalent to Pause 1
next b1
Cheers, Alan.
 

Hemi345

Senior Member
This is how i'd approach it:
warning- psuedo code:
Code:
symbol buttonOption = b12
symbol btn1 = pinC.0
buttonOption = 0
setint %0000001, %00000000  'arm interrrupt for c.0 and interrupt when pin is low

init routine: 'buttonOption = 0
   servo 1,150

main:
 select case buttonOption
 case 0
   gosub init
 case 2
   gosub moveTurret
   end select
   goto main

moveTurrent: 'buttonOption = 2
  for 
   'move code here
   if buttonOption = 0 then exit 'check if buttonOption is still 2, if it's not, exit loop --- need this in each loop to exit what it's doing.
  next 
  return

interrupt:
   if btn1 = 0 then interrupt ;debounce button
   buttonOption = buttonOption + 2 MAX 3 % 3 'buttonOption will either be 0 or 2
   setint %0000001,%0000000 'rearm interrupt
   return
again this is just psuedo code and isn't matched up to the hardware you're using but might give you an idea on where to start using an interrupt.
 
Last edited:

jensmith25

Senior Member
Thanks for your suggestions. I've now got it checking the switch every 500ms with pause 500. I'm concerned about leaving the 08M2 powered all of the time and draining the batteries. Does a pause use less power than other instructions?
I suspect it's the same but just thought I'd ask.
 

Hemi345

Senior Member
lbenson has a good thread power tweaks. Forum search for: lbenson blinking
You can use the sleep command to save power with the PICAXE but the servo will be the biggest power draw even doing nothing. For reference, I'm using a HiTec HS-422 servo in a project and since idle power is 8mA, I'm using a high-side load switch (TPS27082L) to control the power to it so I can shut it off when it's not in use.
 

AllyCat

Senior Member
Hi Jen,

Yes, a Pause uses much the same power as most other instructions, but NAP and SLEEP (see Manual 2) use almost none.

However, I suspect that the Servo will use more power (even when not moving) than the PICaxe (even continuously at a raised clock rate). If you want a good "Shelf life" then you must use a switch in series with all the electronics.... Or use the PICaxe to control the direct power to the servo (not its control pin) with a transistor (or small relay), then long-term "sleeps" or a "suicide" shut-down (cutting off its own power supply).

Cheers, Alan.
 

jensmith25

Senior Member
Thanks guys.

I'll see if my customer wants a second on/off switch or just to switch out the batteries more often. Other than that it's ready to go. Thanks everyone for all your help. No doubt I'll be back when it's my next project! :)
 
Top