Picaxe servo control

Do you have a problem? Here is the place to appeal for help

Moderators: Big Jim, laalratty

User avatar
philipy
Driver
Driver
Posts: 1253
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants
Contact:

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 4:06 pm

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.

metalmuncher
Cleaner
Cleaner
Posts: 26
Joined: Sat Oct 20, 2012 4:15 pm

Re: Picaxe servo control

Post by metalmuncher » Fri May 25, 2018 5:56 pm

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
}

Southern188
Cleaner
Cleaner
Posts: 36
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

Re: Picaxe servo control

Post by Southern188 » 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.

User avatar
philipy
Driver
Driver
Posts: 1253
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants
Contact:

Re: Picaxe servo control

Post by philipy » 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.

User avatar
philipy
Driver
Driver
Posts: 1253
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants
Contact:

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 7:21 pm

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.

Southern188
Cleaner
Cleaner
Posts: 36
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

Re: Picaxe servo control

Post by Southern188 » Fri May 25, 2018 7:59 pm

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

metalmuncher
Cleaner
Cleaner
Posts: 26
Joined: Sat Oct 20, 2012 4:15 pm

Re: Picaxe servo control

Post by metalmuncher » Fri May 25, 2018 8:36 pm

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.

User avatar
gregh
Cleaner
Cleaner
Posts: 73
Joined: Sun Apr 01, 2018 5:44 am
Location: Sydney, Australia
Contact:

Re: Picaxe servo control

Post by gregh » Sat May 26, 2018 1:18 am

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 !
Greg from downunder.
The Sandstone & Termite's website: http://www.members.optusnet.com.au/satr/satr.htm

User avatar
philipy
Driver
Driver
Posts: 1253
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants
Contact:

Re: Picaxe servo control

Post by philipy » Sat May 26, 2018 1:45 pm

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.

User avatar
philipy
Driver
Driver
Posts: 1253
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants
Contact:

Re: Picaxe servo control - CRACKED IT

Post by philipy » Sat May 26, 2018 3:04 pm

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!).

User avatar
tom_tom_go
Chairman
Chairman
Posts: 3318
Joined: Wed Feb 23, 2011 3:08 am
Location: Kent, UK
Contact:

Re: Picaxe servo control

Post by tom_tom_go » Sat May 26, 2018 3:25 pm

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.

User avatar
ge_rik
Driver
Driver
Posts: 2035
Joined: Sun Oct 25, 2009 10:20 pm
Location: Cheshire
Contact:

Re: Picaxe servo control

Post by ge_rik » Sat May 26, 2018 3:39 pm

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
------------------------
Peckforton Light Railway - Blog Facebook Youtube

User avatar
tom_tom_go
Chairman
Chairman
Posts: 3318
Joined: Wed Feb 23, 2011 3:08 am
Location: Kent, UK
Contact:

Re: Picaxe servo control

Post by tom_tom_go » Sat May 26, 2018 3:47 pm

Going through other people's code is a good way to learn.

User avatar
Lonsdaler
Fireman
Fireman
Posts: 361
Joined: Tue May 20, 2014 9:50 am
Location: North Yorkshire

Re: Picaxe servo control

Post by Lonsdaler » Sun May 27, 2018 9:59 am

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:
Phil

I am not young enough to know everything anymore...

User avatar
gregh
Cleaner
Cleaner
Posts: 73
Joined: Sun Apr 01, 2018 5:44 am
Location: Sydney, Australia
Contact:

Re: Picaxe servo control

Post by gregh » 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. 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.
Greg from downunder.
The Sandstone & Termite's website: http://www.members.optusnet.com.au/satr/satr.htm

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests