Page 2 of 3

Re: Picaxe servo control

Posted: Fri May 25, 2018 7:59 pm
by Southern188
Philip

A standard servo has 3 wires, 0v, 5v and signal. The power required by the servo comes through the 5 volt line not the signal. The signal line can be driven directly via the GPIO of a Raspberry pi, Arduino or ESP8266 so I assume it is the same for a Picaxe. If you are using the darlington driver then the polarity will be reversed so it might not work.

Michael

Re: Picaxe servo control

Posted: Fri May 25, 2018 8:36 pm
by metalmuncher
philipy wrote: Fri May 25, 2018 7:17 pm
metalmuncher wrote: Fri May 25, 2018 5:56 pm
As far as I know, you need to send a 1 to 2 ms pulse every 20ms. The length of the 1-2ms pulse dictates the desired servo position. If you send a 1.5ms pulse every 20ms, the servo will attempt to position itself at mid travel fast as possible, rejecting any disturbances it can overcome.
Not quite sure why you think I'm sending a1.5ms pulse every 20ms? I'm sending either a 100ms or 200ms pulse to send it either left or right.
No, you should be sending a 1-2ms pulse every 20ms. That's what standard hobby servos expect, anything else they aren't going to respond to properly.

Re: Picaxe servo control

Posted: Sat May 26, 2018 1:18 am
by gregh
Before we get to the code, let's understand servos first.
The signal to control them is a positive pulse between 0.75 and 2.25ms long, followed by a 20ms 'off' or 0V 'pulse'.
In practice, I've found the off time can be anything from 10ms to 80ms with out affecting operation.
The important part is the short positive pulse duration.
0.75ms wll send the servo fully clockwise; 1.5ms will centre it and 2.25ms will be fully anticlockwise.The actual number of
degrees which it turns through for these values depends a lot on the servo itself. The one I'm using for testing turns about 180
degrees.

NOTE that you have to keep sending these signals all the time!! You don't just do it once.

let's just do a really simple test first. try this code... you will have to substitute your pin number instead of my C.1 (I only use 8 pin types)

Code: Select all

looper1:
PULSOUT C.1,150		'the 150 is 1.5ms
PAUSE 20		'20 ms 'off' time
goto looper1
You'll notice that you cannot turn the servo by hand - it is held in position.

You might like to try changing the PAUSE to 50 and confirm that it still works - but it is easier to turn by hand, signifying the
signal is not 'holding' it as well as 20ms does.

The next thing to know about servos, is that they turn as fast as they can in response to a change in the control signal pulse
length.

You can try this code to see this... (the 2 seconds is just a time I chose - it has no significance). I introduce a variable 'k' to
count the number of loops.

Code: Select all

symbol k=w0		'a counter
looper1:
'put it at one end for 2 secs   (100x20ms)
for k=1 to 100
pulsout C.1,75			'fully CW
pause 20
next k

'now swing to the other end for 2 secs
for k= 1 to 100
pulsout C.1, 225
pause 20
next k

goto looper1





In order to slow them down, we have to 'step' through the pulse lengths we send out. So if we start at 75 and then increase it
'slowly' the servo will turn slowly.

Code: Select all

symbol k=w0		

looper1:

'put it at one end for 2 secs   (100x20ms)
for k=1 to 100
pulsout C.1,75			'fully CW
pause 20
next k

'now swing to the other end SLOWLY
k=75
moveit:
inc k
if k>225 then looper1
pulsout C.1, k
pause 20
goto moveit





To go even more slowly, you can just repeat each pulsout, without changing the pulse duration.
............
moveit:
inc k
if k>225 then looper1
pulsout C.1, k
pause 20
pulsout C.1, k
pause 20

goto moveit



I hope these bits of code help you get it working. I'd suggest writing the code for just one servo and get it to do what you want,
and then adding the other servos.

I'm not sure why you need to 'remember' the previous state of the servo - you already know it because you are sending out the
pulses of that length. You don't need to know where you are just where you want to get to !

Re: Picaxe servo control

Posted: Sat May 26, 2018 1:45 pm
by philipy
Thanks for all your input chaps.
Unfortunately none of it has really helped so far!
I've spent about 3 hours this morning checking and re-checking but I can't even get Greg's first really basic loop to work, so I have to assume that in my ignorance I have done something really fundamentally stupid in putting the hardware together, but I simply cannot see what.

As I said in my first post, the system is basically OK because I can make an LED flash on demand and I can vary the flash rate etc, but servo's just do not want to know. I have tried them on 3 different output pins and I have tried two different servo's ( both of which are fine on a servo tester on 4.5v batteries).
The servo's have JST plugs wired brown-red-orange. Definitely the brown is wired to 0v, the red to +4.94v from the voltage regulator, and the orange is connected to the signal output on whichever channel I'm using.
The only thing is that, as I also said before, when I first power up the system the servo twitches ( so I know it is getting some power) and I've just discovered, by accident, that without the signal wire connected the initial twitching seems to be more pronounced, but after that it still does nothing.
When I realised that, I wondered if it is somehow relevant to Michael's comment above that, "If you are using the darlington driver then the polarity will be reversed so it might not work"? I'm not clear what you mean't by that? Do you really mean to swap the red & brown leads of the power supply i.e. connect the red to ground? - I don't like the sound of that.

Sorry to be a pain.

Re: Picaxe servo control - CRACKED IT

Posted: Sat May 26, 2018 3:04 pm
by philipy
Cracked it!!

After a lot more reading, I found the answer, which is basically to take out the Darlington and connect directly to the Picaxe output pins.
Somebody on a Robotics forum made the point that it is ONLY a signal that is taken from the Picaxe, not power to drive the servo. So I've removed the Darlington and Greg's second test program has been sitting here wiggling back and forth for the last 5 minutes.

Thanks for everyone's advice and assistance. I'll be back when I've hopefully got my program working the way I want ( and not before, also hopefully!).

Re: Picaxe servo control

Posted: Sat May 26, 2018 3:25 pm
by tom_tom_go
Well done Phil, it's a great feeling when you have achieved a goal with project work like this.

I would get your power sorted before you proceed so that servo and board have separate supplies with a common ground.

Re: Picaxe servo control

Posted: Sat May 26, 2018 3:39 pm
by ge_rik
Glad you got it sorted. I must admit that I looked at your code and couldn't see what the problem was - but then, trying to debug someone else's code is a bit like plaiting fog, so I decided to wait until the sun rose over Sydney.

Rik

Re: Picaxe servo control

Posted: Sat May 26, 2018 3:47 pm
by tom_tom_go
Going through other people's code is a good way to learn.

Re: Picaxe servo control

Posted: Sun May 27, 2018 9:59 am
by Lonsdaler
I'm glad you've got it sorted Philip. I'm afraid it's all left me in that fog that Rik can't plait! :lol:

Re: Picaxe servo control

Posted: Sun May 27, 2018 12:05 pm
by gregh
The Picaxe manual recommends using a 330 ohm resistor on the control output pin to a servo. Ie in series.
I don't know why. Maybe just as protection in case the (usually long) wire to the servo gets shorted. I've done with and without and it makes no difference to operation.
Glad you got the test working.

Re: Picaxe servo control

Posted: Sun May 27, 2018 12:12 pm
by philipy
gregh wrote: Sun May 27, 2018 12:05 pm The Picaxe manual recommends using a 330 ohm resistor on the control output pin to a servo. Ie in series.
I don't know why.
Thanks Greg. Yes, I heard that as well. Whilst searching for an answer to my problem yesterday, I came across numerous references to it on R/C and Robotics forums, and nobody seems to know why! I can't help wondering if it's another case of "it's on the 'net so it must be true"!
Having said that, the consensus seems to be that it can't hurt so do it anyway, which I have done.

On the basis of one quick test, I think I have two servo's doing what I want, but I'm saying no more until I'm sure.

Re: Picaxe servo control

Posted: Sun May 27, 2018 12:52 pm
by tom_tom_go
You are doing well Phil coding in this weather, it's too hot for me to go anywhere near my laptop!

Re: Picaxe servo control

Posted: Sun May 27, 2018 2:04 pm
by metalmuncher
philipy wrote: Sun May 27, 2018 12:12 pm
gregh wrote: Sun May 27, 2018 12:05 pm The Picaxe manual recommends using a 330 ohm resistor on the control output pin to a servo. Ie in series.
I don't know why.
Thanks Greg. Yes, I heard that as well. Whilst searching for an answer to my problem yesterday, I came across numerous references to it on R/C and Robotics forums, and nobody seems to know why! I can't help wondering if it's another case of "it's on the 'net so it must be true"!
Having said that, the consensus seems to be that it can't hurt so do it anyway, which I have done.

On the basis of one quick test, I think I have two servo's doing what I want, but I'm saying no more until I'm sure.
I think it's to protect the outputs on the microcontroller, in case the output accidentally gets shorted to ground or power. Say the output is on at 5V, and you short it to ground through a 330 ohm resistor, the current will be 5V/330ohms = 15mA, gives a nice safety net below the maximum rated 25mA sink/source current for an output pin on a PIC. Without the resistor, the current could be very high and blow output transistors inside the uC.

Re: Picaxe servo control

Posted: Wed May 30, 2018 12:15 pm
by philipy
After a short delay due to the need to do some gardening, fix a garden feature waterfall and other 'essential' non-railway activities, I can confirm that I have got my servo's working nicely and driving a spare Peco point on a piece of Plywood. :D
All I want now is for the weather to warm up, and dry up again, so that I can go down the garden and install the stuff!

Thanks once again for the advice and help, guys, and especially to Greg.

I think my basic problem stemmed from trying to do it in theory without being able to test out small steps along the way, and in doing so making everything far more complicated than it needed to be! :oops:

Re: Picaxe servo control

Posted: Wed May 30, 2018 12:48 pm
by tom_tom_go
Don't forget to post a video of it in action :thumbup:

Re: Picaxe servo control

Posted: Tue Jul 10, 2018 10:32 am
by philipy
tom_tom_go wrote: Wed May 30, 2018 12:48 pm Don't forget to post a video of it in action :thumbup:
Bet you thought I'd forgotten, or given up?? :lol:

Well, there are very good reasons why I've been quiet on this subject for the last 6 weeks and all will become clear in due course, but for now here are the points working. As always, it's difficult to do three things with one pair of hands, so it won't win any Oscars for cinematography...


https://youtu.be/NP97X1lkO94

Re: Picaxe servo control

Posted: Tue Jul 10, 2018 10:54 am
by tom_tom_go
Well done Phil, it's more effort getting points motorised above the base on the track so I can appreciate the time you have put into it.

Are you able to slow the movements down though in code?

Re: Picaxe servo control

Posted: Tue Jul 10, 2018 11:25 am
by philipy
tom_tom_go wrote: Tue Jul 10, 2018 10:54 am
Are you able to slow the movements down though in code?
I'm sure it can be done. In fact Greg did suggest how to do it back earlier in the thread but I haven't tried it yet, I was more concerned with getting it to work at any speed! I think I may need to just tweak the two throw limits anyway so I'll have a play when I do that.

Re: Picaxe servo control

Posted: Tue Jul 10, 2018 10:50 pm
by gregh
Very nicely done.
Always best to get the basics working first then to fine tune the speed or whatever.

Re: Picaxe servo control

Posted: Tue Jul 10, 2018 11:02 pm
by ge_rik
Impressive!! Nicely and neatly done!

Rik