Picaxe servo control

Do you have a problem? Here is the place to appeal for help
User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Picaxe servo control

Post by philipy » Fri May 25, 2018 11:50 am

Well, as I said on Greg's thread in response to Tom's request for info,(https://gardenrails.org/forum/viewtopic ... 86#p136686) I'm stumped!

Basically the plan was to use a "garage door opener" RC unit to operate the turnouts on my railway, via Picaxe 18M2 and Tower servos.

The Rc part works fine, and the Picaxe is receiving and obeying signals to flash an LED.
The Picaxe program to run the servos is testing out as I want it to, on the simulator part of the Picaxe software.
The servos are working fine using a servo tester, but they AIN'T working at all when connected to the Picaxe! ( I do get a short 'judder' from the servos when the power first applied though, so I know they are live).

Any suggestions gratefully received.

BTW I don't have access to an oscilloscope so can't check the actual pulse outputs.
Philip

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

Re: Picaxe servo control

Post by Lonsdaler » Fri May 25, 2018 12:20 pm

I can't help Philip, other than to suggest a couple of links.
There is a picaxe forum http://www.picaxeforum.co.uk and there is a thread on G scale Central that Rik and GregH seem to subscribe to. https://www.gscalecentral.net/threads/t ... 8742/print
Phil

Sporadic Garden Railer who's inconsistencies know no bounds

My Line - https://gardenrails.org/forum/viewtopic ... 41&t=11077

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 12:24 pm

Thanks Phil. Yes I know of all of those - I've spent ages reading and re-reading all of Greg's stuff ( and Rik's) and I've been on the Picaxe forum, but haven't found anything specifically like my problem. Trouble is I don't know if it is a programming or hardware problem and I don't have enough knowledge or experience to be able to second guess it.
Philip

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

Re: Picaxe servo control

Post by tom_tom_go » Fri May 25, 2018 1:01 pm

Can you dump your code here using the codes tags so we can have a look.

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 1:13 pm

tom_tom_go wrote: Fri May 25, 2018 1:01 pm Can you dump your code here using the codes tags so we can have a look.
Happily, but don't know what you mean by "codes tags"?
Philip

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

Re: Picaxe servo control

Post by tom_tom_go » Fri May 25, 2018 1:21 pm

Use [#code][/code#] but without the hash tags.

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 1:23 pm

Cheers Tom

Code: Select all

'Servo Control for TurnOuts using Picaxe 18M2.  P.Y 25-05-2018

'Initial setup:
'Pin identification:
	'Inputs:
  		'pinC.0 input1
   		'pinC.1 input2
    		'pinC.2 input3
     	 	'pinC.5 input4
'Note: pins C.3 and C.4 not usable as inputs

	'Outputs:
    		'pinB.0 output1
    		'pinB.1 output2
    		'pinB.2 output3
    		'pinB.3 output4

'Variables - the previous state of the input pins
	symbol previnput1=b6
	symbol previnput2=b7
	symbol previnput3=b8
	symbol previnput4=b9
	
	symbol loop1=b12
	symbol loop2=b13
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#rem  initialize:							'to be finished, but not req for testing
   'servo 2								'Note - servo 2 (&3) will be the first to be installed
     	if pinC.1 = 1 and previnput1 = 0 then    			'compares the Rx input with it's previous state
	pulsout B.1,100							'if different, sends 1ms pulse to servo 1 TO TURN LEFT
      
	      	 
	endif
 #endrem									'Will require a repeat for each servo eventually.
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

main:
 
	
	if pinC.1 = 1 and previnput1 = 0 then    			'compares the Rx input with it's previous state               	      			
	pulsout B.1,100							'if different, sends 1ms pulse to servo 1 TO TURN LEFT
								
	loop1 = loop1+1 							'counts number of time a pulse is sent
	pause 50
	
	if loop1 = 100 then						'loops pulse to servo enough times to effect the change
	previnput1  = 1							'records the new state		
									
	'pause 2500								'temporary wait to debug the program
	endif									'Input is now LEFT and previnput is now LEFT ( pinC.1 = 1 and previnput1 = 1)
	
	if pinC.1 = 1 and previnput1 = 1 then 
	loop1=0								'reset counter
	
	endif
	endif
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	if pinC.1 = 0 and previnput1 = 1 then  		 	'compares the Rx input with it's previous state
	pulsout B.1,200						 	'if different, sends 2ms pulse to servo 1  TO TURN RIGHT
	
	loop2 = loop2+1							'counts number of time a pulse is sent
	pause 50
	
	if loop2 = 100 then						'loops pulse to servo enough times to effect the change
	previnput1  = 0							'records the new state	
								
	'pause 2500								'temporary wait to debug the program
	endif									'input is now RIGHT and previnput is now RIGHT ( pinC.1 = 0 and previnput1 = 0)
	
	if pinC.1 = 0 and previnput1 = 0 then 
	loop2=0								'reset counter
	
	endif
	endif

	goto main

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Philip

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 1:55 pm

Sorry, I forgot to change lines 46 and 64 to something like loop1 & 2 =5 not 100! At 100 it takes forever to go round the loop!
Philip

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

Re: Picaxe servo control

Post by tom_tom_go » Fri May 25, 2018 2:08 pm

I downloaded the PICAXE Editor as interested in playing with this and loaded up your code simulating chip 18M2 (assumed that's correct from
looking at the code you posted).

When I step through the code you are doing a test here:

Code: Select all

if pinC.1 = 1 and previnput1 = 0 then    			'compares the Rx input with it's previous state               	      			
	pulsout B.1,100							'if different, sends 1ms pulse to servo 1 TO TURN LEFT
Does this happen?

After the test you then move on to running loop1 which is defined as a variable here:

Code: Select all

symbol loop1=b12
But there is no input B12 on that chip so how would this work?

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 2:51 pm

tom_tom_go wrote: Fri May 25, 2018 2:08 pm I downloaded the PICAXE Editor as interested in playing with this and loaded up your code simulating chip 18M2 (assumed that's correct from
looking at the code you posted).
Yes thats correct
tom_tom_go wrote: Fri May 25, 2018 2:08 pm When I step through the code you are doing a test here:

Code: Select all

if pinC.1 = 1 and previnput1 = 0 then    			'compares the Rx input with it's previous state               	      			
	pulsout B.1,100							'if different, sends 1ms pulse to servo 1 TO TURN LEFT
Does this happen?
It appears to happen on the simulator, but nothing at all happens in real life, so I don't know.
tom_tom_go wrote: Fri May 25, 2018 2:08 pm After the test you then move on to running loop1 which is defined as a variable here:

Code: Select all

symbol loop1=b12
But there is no input B12 on that chip so how would this work?
Picaxe inputs, pins, etc, are extremely confusing. B.12 is not the same b12. B.12 doesn't exist, as you say, but b12 is a variable address and can exist:
" Variables
On the M2 parts there are now up to 512 general purpose variables. 28 of these, known as b0 to b27, can be used
directly in any command (as with other PICAXE parts)."

Hope that helps.
Philip

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

Re: Picaxe servo control

Post by tom_tom_go » Fri May 25, 2018 3:20 pm

Ok, but what is b12 - what's it suppose to do?

I don't understand what any of these variables do:

Code: Select all

'Variables - the previous state of the input pins
	symbol previnput1=b6
	symbol previnput2=b7
	symbol previnput3=b8
	symbol previnput4=b9
	
	symbol loop1=b12
	symbol loop2=b13
Did you write this or copy and paste it from somewhere? I am not being critcal but when I first started using Arduino I cut and pasted bits from the Internet without understanding what I was doing and I got into all sorts of problems so I started from scratch and learnt line by line what I was actually typing.

Greg/Rik, you the picaxe boys help!

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 3:36 pm

tom_tom_go wrote: Fri May 25, 2018 3:20 pm Ok, but what is b12 - what's it suppose to do?

I don't understand what any of these variables do:

Code: Select all

'Variables - the previous state of the input pins
	symbol previnput1=b6
	symbol previnput2=b7
	symbol previnput3=b8
	symbol previnput4=b9
	
	symbol loop1=b12
	symbol loop2=b13
Did you write this or copy and paste it from somewhere? I am not being critcal but when I first started using Arduino I cut and pasted bits from the Internet without understanding what I was doing and I got into all sorts of problems so I started from scratch and learnt line by line what I was actually typing.

Greg/Rik, you the picaxe boys help!
Tom,
Some of them don't do anything atm. Eventually the plan is have 7 servo's controlled by 6 outputs so I started defining them all then realised it was pointless until I got one working!

However, the 'previnput's simply record a 1 or 0 ( the previous position) for comparison with a change on the input from high to low or low to high, on receipt of a signal from the Rx.
The two 'loop's are counters and this may be where I'm going wrong. 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!!

Yes I did start copying and pasting from Gregg/Rik, but also realised it was creating more confusion than assistance, so I went DIY, but pinching some of their ideas.
Philip

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

Re: Picaxe servo control

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

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

Re: Picaxe servo control

Post by Southern188 » Fri May 25, 2018 3:54 pm

Philip

Sorry, I can't be much help as I haven't used a Picaxe before but can't you use the servo function? Looks much simpler.

http://www.picaxe.com/BASIC-Commands/Di ... put/servo/

Michael

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: Picaxe servo control

Post by philipy » Fri May 25, 2018 3:59 pm

Southern188 wrote: Fri May 25, 2018 3:54 pm Philip

Sorry, I can't be much help as I haven't used a Picaxe before but can't you use the servo function? Looks much simpler.

http://www.picaxe.com/BASIC-Commands/Di ... put/servo/

Michael
Michael,
Thanks for the thought.
Yes, I could in theory, but it will only support the servo function on one output and I need 7. So it wouldn't actually solve my problem overall.
Philip

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

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.
Philip

metalmuncher
Cleaner
Cleaner
Posts: 95
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: 40
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
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

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.
Philip

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

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.
Philip

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests