Adapting code from 08M to 18M2

elanman99

Senior Member
A couple of years ago I used an 08M (actually 2 of them) to control servos on a pan and tilt on a underwater ROV. I now need to write another vaguely similar programme and thought I would convert what I already had to familiarise myself with the 18M2. I have no skills as a programmer but apart from getting help from this wonderful forum I use trial and error until I succeed.

The first major difference I found was now having to define in some way the inputs so the picaxe knows where the switches and servos are connected.

I have tried searching the archives but might have used the wrong search terms as so far have not found any easy explanation.

I'm sorry if I have missed the obvious but any pointers would be appreciated.

I have attached my 08M code in case it is of any interest to anyone.

Ian

Code:
'For Picaxe 08M

'Modification Dates
'28-4-2010 Programmed to chip

'29-4-2010 Default values changed & delay set to give about a 10 second camera transit time

'10-9-2010 Servo now driven by pulsout command which appears to solve glitch problem. Values 'have 'now been adjusted and servo pulse width (measured using BS2 with serial LCD) is in the 'range 500 'to 1000 mS although this does not give full mechanical travel of servo.

'This controller is for one axis of a (for 'Driving' not filming) small underwater video camera pan and tilt head, (so two of these are required). Servo position adjusted by using momentary up/down buttons that move the servo at a slowish speed. Servo stops moving when button released. At switch-on the servo is set to nominal mid position. The end positions of the servo can be set by the user so that when installed in its housing the servo travel only covers the range that the camera lens can see through the housing window.
'Setting end stop positions is done by a 'Set' switch which reads the current servo position 'and 'saves it as a max, min or mid value. Depending on the where the current position is the 'programme decides where to put the new value. Another switch 'Reset' clears all the stored 'values and allows the programme to cater for brand new blank chip.
'In use the supply to both ICs is interupted in order reset the pan/tilt head to mid position (not enough pins on an 08M really)
 
'Hardware description
'Servo on output 0			(IC pin 7)
'Increment sw on Input 1		(IC pin 6)
'Decrement sw on input 2		(IC pin 5)
'Position SET sw on input 3		(IC pin 4)
'Reset memory sw on input 4   	(IC pin 3)

Symbol  POSN   = b6		'Current servo position
Symbol  CW     = b4		'Variable to decrement output pulse width
Symbol  ACW    = b5		'Variable to increment output pulse width
Symbol  CWEOT  = b0		'Clockwise end of user set travel
Symbol  ACWEOT = b1		'Anticlockwise end of user set travel
 
Output 0				'Confirms i/o 0 as an output pin
Input 1				'Confirms i/o 1 as an input pin
Input 2				'Confirms i/o 2 as an input pin

Read 0,b0				'Read saved values from NV memory into the variables
Read 1,b1				'If its a brand new chip the values will all be blank
Read 2,b6				'so reset button inserts some sensible numbers


'Programme starts here

Main:

If Pin3 = 0 Then Gosub CHOOSER		'SET switch has been pressed
If Pin4 = 0 Then Gosub RST			'Reset Switch pressed

Button 1, 1, 1, 1, CW, 0, NOTPRESSED1	'Anticlockwise travel button
POSN=POSN +1 Max CWEOT
NOTPRESSED1:

Button 2, 1, 1, 1, ACW, 0, NOTPRESSED2	'Clockwise travel button
POSN=POSN -1 Min ACWEOT
NOTPRESSED2:

pause 20						'Slows travel speed (5=3, 20=6, 40=8 seconds)					

Pulsout 0,POSN					'Output the servo pulse on 0

Goto Main

'Subroutines
     
CHOOSER:							'Decides where to put values
If POSN <135 then Gosub SETACWEOT			'Set Clockwise end of travel
If POSN >165 then Gosub SETCWEOT			'Set Anticlockwise end of travel
If POSN >=135 and POSN <=165 Then Gosub SETMID	'Set Midpoint of travel (Needed at power up)
Return

SETCWEOT:
Write 0,POSN 					'Puts current servo position into NV memory
Return

SETACWEOT:
Write 1,POSN					'Puts current servo position into NV memory
Return

SETMID:
Write 2,POSN					'Defines mid position for startup
Return

RST:							'Resets memory to default values
write 0,200 					'These values suit Futaba servo
write 2,150
write 1,100

read  0,b0						'Copies value to variables so can be used
read  1,b1
read  2,b6
Return
 

srnet

Senior Member
There is a section in manual2 on the ways to address pins; Input / Output Pin Naming Conventions on page 27

This example is given;

In the case of if...then statements which check the status of the input pin variable, the naming convention of these input pin variables have changed in a similar
style from

if pin1 =1 then...

to

if pinC.1 = 1 then...
 

hippy

Ex-Staff (retired)
Perhaps the easiest way to port 08M code to an 18M2 is to relocate all 08M pins onto the 18M2 port B pins.

Servos operate only on Port B pins but in this case you are using PULSOUT for servo control so that does not matter. Rename pin number N to B.N and, for reading input pin states, PinN to PinB.N.

Once that is done you can consider moving B.N and PinB.N to alternative C.N and PinC.N pins, reallocating any remaining B.N pins, to suit the hardware you would like.
 

elanman99

Senior Member
Perhaps the easiest way to port 08M code to an 18M2 is to relocate all 08M pins onto the 18M2 port B pins.

Servos operate only on Port B pins but in this case you are using PULSOUT for servo control so that does not matter. Rename pin number N to B.N and, for reading input pin states, PinN to PinB.N.

Once that is done you can consider moving B.N and PinB.N to alternative C.N and PinC.N pins, reallocating any remaining B.N pins, to suit the hardware you would like.
Thanks Hippy
I have 'sort of' got the 18M2 running the edited programme but two of my buttons dont do anything and my Increment/Decrement buttons work but give strange results.

I changed 'If Pin3 = 0 Then Gosub Chooser' to 'If pinB.3 = 0 Then Gosub Chooser'
and did the same with 'If Pin4.... RST etc

Neither of those two buttons seem to do anything now

The servo output is on leg 2 (Pulsout C.3 POSN) seems to give a different range of pulse widths than the 08M did (I have another picaxe with LCD monitoring the O/P)

I have just read that I should really be using Servo or Servopos command, rather than Pulsout (ref Westaust's Tutorial)

The programme I want to create now will control a standard servo with a pot. I was going to use a 555 type 'Servo tester' circuit as an input to the 18M2 do some 'scaling' and then output the changed values to the servo. The servo is going to move the focus ring on several different lenses but I want the pot to use its full travel even though some of the lens rings only have a short travel.

Does 18M2 and the two Picaxe servo command seem the right way to go?

Ian
 

elanman99

Senior Member
Not deliberately bumping my post but I have noticed another little problem. When I made the pan and tilt controller I changed to the pulsout command as the when I used the servopos the servo would glitch randomly. I seem to remember it was well known issue caused by compiler/PIC/editor? error. Using Pulsout cured the problem.

Browsing through the archives I saw that earlier versions of 18M2 firmware have some shortcomings, the one I am using now reports as '2.A' Could that be causing any of my problems?

Ian
 

westaust55

Moderator
Timing matters between some commands such as serial Comms and servo functions are still present - a fact of life when PIC hardware resources are limited.

If you post your new 18M2 code folks can have a look at the problems (input switches etc) and may recommend a solution.
 

elanman99

Senior Member
I will not need serial comms, hopefully only pulsin so hopefully servopos will be OK. The listing below worked with an 08M (but using the pulsout command) but on an 18M2 behaves strangely. In the simulator I can see it going wrong but I cannot work out why.

Thanks for any help.

Ian in UK

Code:
'Originally written for 08M now changed to 18M2 For Test Purposes

'Modification Dates

'10-9-2010 Servo now driven by pulsout command which appears to solve glitch problem. Values 'have 'now been adjusted and servo pulse width (measured using Stamp BS2 with serial LCD) is in the 'range 500 'to 1000 mS although this does not give full mechanical travel of servo.

'This controller is for one axis of a small underwater video camera pan and tilt head (so two of these are required). Servo position adjusted by using momentary up/down buttons that move the servo at a slow speed. Servo stops moving when button released. At switch-on the servo is set to a user defined mid-position The end positions of the servo can also be set by the user so that when installed in its housing the servo travel only covers the range that the camera lens can see through the housing window.
'Setting end stop positions is done by a 'Set' switch which reads the current servo position 'and 'saves it as a max, min or mid value. Depending on the what the current position is the 'programme decides where to put the new value. Another switch 'Reset' clears all the stored values and allows the programme to cater for brand new blank chip.
'In use the supply to both ICs is interupted in order reset the pan/tilt head to mid position



'Pin assignments (IC numbers are physical pins)
'Servo on output B0			(IC pin 6)  
'Increment sw on input B1		(IC pin 7)  10K pull-up with mom switch to gnd
'Decrement sw on input B2		(IC pin 8)  10K pull-up with mom switch to gnd
'Position SET sw on input B3		(IC pin 9)  10K pull-up with mom switch to gnd
'Reset memory sw on input B4   	(IC pin 10) 10K pull-up with mom switch to gnd

#picaxe 18m2				'Non sure if this is needed 


Symbol POSN   = b6		'Current servo position
Symbol CW     = b4		'Variable to decrement output pulse width (Clockwise movement)
Symbol ACW    = b5		'Variable to increment output pulse width (AntiClockwise)
Symbol CWEOT  = b0		'Clockwise End Of Travel
Symbol ACWEOT = b1		'Anticlockwise End of Travel
 
 
'Output B.0				'Confirms B0 as an output (IC pin 6)
'Input  B.1				'Confirms B1 as an input  (IC pin 7)
'Input  B.2				'Confirms B2 as an input  (IC pin 8)
'Input  B.3				'Confirms B1 as an input  (IC pin 9)
'Input  B.4				'Confirms B2 as an input  (IC pin 10)


Init: Servo B.0, 150		'Seems to work without this

Read 0,b0				'Read saved values from NV memory into the variables
Read 1,b1				'If its a brand new chip the values will all be blank
Read 2,b6				'so reset button inserts some sensible numbers


'Programme starts here really

Main:

Button B.1, 1, 1, 1, CW, 0, NOTPRESSED1	'Anticlockwise travel button
POSN=POSN +1 Max CWEOT
NOTPRESSED1:

Button B.2, 1, 1, 1, ACW, 0, NOTPRESSED2	'Clockwise travel button
POSN=POSN -1 Min ACWEOT
NOTPRESSED2:

If PinB.3 = 0 Then Gosub CHOOSER		'SET switch pressed    (IC pin 9)
If PinB.2 = 0 Then Gosub RST			'Reset Switch pressed  (IC pin 10)

pause 20						'Slows travel speed (5=3, 20=6, 40=8 seconds)					

Servopos B.0,POSN					'Output the servo pulse on B0 (IC pin 6)

Goto Main

'Subroutines
     
CHOOSER:							'Decides where to put values
If POSN <135 then Gosub SETACWEOT			'Set Clockwise end of travel
If POSN >165 then Gosub SETCWEOT			'Set Anticlockwise end of travel
If POSN >=135 and POSN <=165 Then Gosub SETMID	'Set Midpoint of travel (Needed at power up)
Return

SETCWEOT:
Write 0,POSN 					'Puts current servo position into NV memory
Return

SETACWEOT:
Write 1,POSN					'Puts current servo position into NV memory
Return

SETMID:
Write 2,POSN					'Defines mid position for startup
Return

RST:							'Resets memory to default values
write 0,200 					'These values suit Futaba servo
write 2,150
write 1,100

read  0,b0						'Copies value to variables so can be used
read  1,b1
read  2,b6

Return
 

Rick100

Senior Member
I think the line: 'If PinB.2 = 0 Then Gosub RST ' should be 'If PinB.4 = 0 Then Gosub RST' .
Also examine your button commands closely in the simulator . They may be doing the opposite of what you think , but I'm not sure .

Good Luck
Rick
 

elanman99

Senior Member
Rick

Thanks for spotting my silly mistake on the input numbering. You are right too about the Button sense. I had been changing the sense just so when using the simulator I did not have to click on each port input.

Its getting nearer to working now and I am getting a better understanding of what is happening, I'm still confused about some of the naming conventions regarding the 'Write' command addresses that sometimes seem to be a single digit and sometimes have 'b' prefix.

Ian
 
Top