Page 1 of 3

Re: Picaxe servo control

Posted: Fri May 25, 2018 4:06 pm
by philipy
tom_tom_go wrote: Fri May 25, 2018 3:51 pm Humour me, try a different pin as when I check the 18M2 chip the outputs can do different things so it might be that what you are trying to do that output cannot do that (try the C.x outputs).

Also, are you using a seperate power supply for both chip and servo with a common ground? I have to do this with my Arduino although might not be applicable to the Picaxe chips.
Tom,
Yes, I've already tried three different pins with the same non-result. Plus I will need to use all of the outputs eventually so I have to find a solution rather than just work around.

No, I'm using the one power supply, although I have had that in the back of my mind.
The regulated output into the Picaxe board measured at 4.94v. The servo operates quite readily at 4.5v on the servo tester and the stall current is said to be approx 450ma and the Darlington output is rated up to 500ma. It is a wee bit tight, I know, but since it doesn't even flick, I think there must be something else going on.

Re: Picaxe servo control

Posted: Fri May 25, 2018 5:56 pm
by metalmuncher
philipy wrote: Fri May 25, 2018 3:36 pm The 'pulsout' command sends a pulse of either 100 or 200 ms to the servo to turn it left or right ( nominal mid point is 150). The servo takes 120ms to do a 60deg sweep, so will need several pulses to do the full left to right, hence it goes round the loop several times and the counter keeps track. The number of loops will obviously need to be refined when I can actually get it to move at all!!
This sounds a little bit odd to me, almost like you're trying to drive a stepper motor?

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. I would try and get that working first, before doing any input at all. Just send a 1.5ms pulse, then wait 18.5ms, then another 1.5ms pulse... The servo should go to mid travel, and if you push the horn it will fight back. Then at least you know your circuit is working. Then go on to sweeping the servo through different positions, input, etc.

If it's any help, this is the Arduino code I used to make sure the numbers I was saying worked:

Code: Select all

#define SERVO_MICROSECONDS 1500 // 1500us desired position
#define SERVO_PIN 11            // pin the servo signal will come out of

void setup() {                    // this happens once at power on
    pinMode( SERVO_PIN, OUTPUT ); // set the servo pin to output
}

void loop() {                                        // this happens forever
    digitalWrite( SERVO_PIN, HIGH );                 // turn on the servo pin
    delayMicroseconds( SERVO_MICROSECONDS );         // wait 1500us
    digitalWrite( SERVO_PIN, LOW );                  // turn servo pin off
    delayMicroseconds( 20000 - SERVO_MICROSECONDS ); // wait the remainder of the 20ms frame
}

Re: Picaxe servo control

Posted: Fri May 25, 2018 6:39 pm
by Southern188
Philip

I think the Picaxe experts will ask for a circuit diagram or sketch. I run my servos direct from the Arduino/ ESP8266 GPIO lines, so not sure why you need a darlinton drive?

Michael.

Re: Picaxe servo control

Posted: Fri May 25, 2018 7:17 pm
by philipy
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.

Re: Picaxe servo control

Posted: Fri May 25, 2018 7:21 pm
by philipy
Southern188 wrote: Fri May 25, 2018 6:39 pm Philip

I think the Picaxe experts will ask for a circuit diagram or sketch. I run my servos direct from the Arduino/ ESP8266 GPIO lines, so not sure why you need a darlinton drive?

Michael.
Michael,
The Picaxe 18M2 project board comes with a Darlington chip incorporated. As I understand it, the Picaxe itself is only capable of outputting 50mA which isn't enough for a servo.

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: