Help Please: PICAXE and Robot Boat

hippy

Technical Support
Staff member
All what MartinM57 said, plus, when switching 0V one has to consider what happens to signals which are between the switched device and PICAXE when it's off. For example if there were a serial out from the PICAXE to iPAQ it could be holding the iPAQ to 0V via its serial in. Similar problems can occur when switching +V if signals into it were high.

In this case, switching iPAQ 0V could cause pin3 to float up to +V and the PICAXE jam up expecting serial, and while doing that never timeout and turn iPAQ power back on. Putting a pull-down on pin3 to stop that could allow some current to still flow through the iPAQ.

It's quite complicated sometimes to switch a component off in the middle of an otherwise powered system, most microprocessors with internal ESD diode clamps are prone to suffering from 'phantom power' effects where they can still keep running even with +V disconnected.

One thing from the above, is that it's probably best to disable interrupts when power is off and re-enable them when turned back on.
 

Robin Lovelock

Senior Member
Many Thanks Hippy and Martin. Useful things for me to consider when making that switch - but more importantly, test during the 24/7 testing - which usually has gone on for several months before a launch. Hence my purchase of several iPAQs - even if cheap ones :) If anyone has made such a switch, I'll obviously be interested. Meanwhile I'm progressing with GPSSppc - I see it takes about 1 minute from cold start - mostly the Windows Mobile startup. That's about the time it will take the GPS to start tracking, so my switch will probably be a simple one - switching on and off both iPAQ computer and GPS.
Robin
www.gpss.co.uk/autop.htm
 

papaof2

Senior Member
For low power operation and water immunity, a sealed latching relay would work. I expect a new one would be $$$, but a much better price if you can them in surplus.

A latching relay requires a pulse to turn it on and an opposite pulse to turn it off. For an example, search the Forum for
latching relay

John
 

Robin Lovelock

Senior Member
Thanks John. I'm always eager to learn more. Can you give me the URL of an example product ? I'd like to see if it has any advantage - particularly reliability.

As it happens, I think I've found the right solution: a reed relay from Maplins:
http://www.maplin.co.uk/search.aspx?menuno=12457

My earlier guess was wrong, when I thought a reed relay might not be reliable enough: it seems they claim 1 million switch operations: that's ten times the reliability I hoped for - should be good for ten years :)

It helps also that both my local Maplins stores have them in stock, and they are inexpensive - other than the petrol burnt to reach them. They should wire directly into the output pin of my PICAXE controller. Over the years I've always been an advocate of KISS in engineering ;-)

So please tell me more about these latching relay, with a URL if possible.
 

Robin Lovelock

Senior Member
Thanks Eclectic. This latching relay info may come in useful at a later date.

However, I now have several of those 500 ohm reed relays from Maplins and am ready to connect one to my PICAXE 08M based board. I'm not too concerned by the 10mA consumed, since this will only be when the switch is ON, and at that time, it's small compared with the iPAQ and GPS. When the system is OFF, it shouldn't consume anything.

Now some more advice please: one end of the reed relay coil must go to Pin0, but I have a choice: the other end to 0v or to +5v ? I can obviously swap around the HIGH 0 and LOW 0 statements in the program as needed.

I took a quick look in the PDF files, for the circuit inside the 08M, and/or output electronic tips. Perhaps I was looking in the wrong place.

It would be nice if I can simply connect it, without other components, but it's obviously not difficult to add other things like diodes to protect against back EMF. I'm guessing such things may already be inside the 08M chip. I get the impression it is already able to drive such compenents directly.

Robin
www.gpss.co.uk/autop.htm
 

Robin Lovelock

Senior Member
Thanks Hippy. I'm sure that 500 ohm relay in the thread was the same as mine :) Also, I should have seen the excellent notes early on the site under "Section 3 - Interfacing circuits ". Will be giving it a try in the next few days.
Robin
www.gpss.co.uk/autop.htm
 

hippy

Technical Support
Staff member
Actually ... the 08M and others do have clamping diodes on I/O pins which point in the same direction as a catch diode would, but are not intended as such and may not be up to that job. Not all I/O pins have these diodes but most do.
 

Brietech

Senior Member
Robin - Sorry to de-rail the conversation a bit, but I'm interested in possibly trying a similar project (except I was intending to use electric motors, as a sail seemed quite fragile to me). What are you intending to do for sea-based communication? I've been unable to find a reasonably-priced/reliable communication method that would work more than a few miles off shore. Your input would be greatly appreciated!
 

Robin Lovelock

Senior Member
Hi Folks. No need for hurried replies because after explaining progress and my latest questions, I'm going to relax in front of the (rubbish) TV with a shot of whiskey. Also, don't forget that it's a bank holiday in UK tomorrow, so don't work too hard :)

But first that question above: Most of us are planning to use SPOT Messengers - suitably modified. Search on ebay and you'll find them at their standard price of 150 GBP. But don't forget that the air time will probably cost another 150 GBP per year. That's for deep ocean tracking, and for just one, a a few, position reports per day. When within 5 miles of the coast, a GPS/GSM tracker may work well enough and allow a faster update. Something like a Globalsat TR-101. Check out the AVL page on my www.gpss.co.uk - but the more relevant page is www.gpss.co.uk/autop.htm - make sure to watch the "Snoopy Sails!" video :) Oh yes, checkout that bottle tracking project also: for that we just used GPS/GSM trackers - but not as neat as the modern ones, that consume far less power.

Now me: I'm now testing the new setup, intended to consume less power, by the PICAXE controller being used to switch off power to iPAQ and GPS for 1 or more minutes. I've wired in the reed relay with protection diode, and now the iPAQ GPSSppc autopilot ends by sending 127,9,1 to say switch off for 1 minute. The latest version of the PICAXE program is below. The startup test now also toggles the power output - not just waggling servos.

BUT it looks as if the new lines HIGH 0 and LOW 0 are interrering with
that old test pattern of servo movement at the start.

I've done a few tests of different things in isolation, but I guess I need to do quite a few other tests. I THINK my soldering is OK - and the simple tests seem to show the switch acting as it should.

However, for future use, I've ordered off ebay some of that old-fashioned lead-tin multicore solder, because I just can;t stand this new lead-free stuff. I find it MUCH more difficult to get nice shiny joints that are not dry. Googling today convinced me when I discovered that lead-free solder is not used in the defence or avionics industry. That doesn't surprise me.

I'm probably doing something dumb in the software, and so I thought I would at least tell you where I am. Many Thanks in advance guys.

Robin
www.gpss.co.uk/autop.htm

Code:
'SC1.BAS servo controller for robot boat
'v1e 27 Aug 2009 (c) Robin Lovelock www.gpss.co.uk/autop.htm 
' - with a great deal of help from HIPPY on Picaxeforum.co.uk :-)
'input is serial RS232 input group of 3 bytes: 
'127=sync, 0,1 or 2=servo number, 0 to 126 servo position
'the input servo position from RS232 is 0 to 126, 64=centre
'position sent to servo system is 100 to 191, 146=centre

'switching between 4MHz and 8MHz was done to program
'around the restriction in the PICAXE system that
'SERVO/SERVOPOS only works at 4MHz whereas
'SERIN at 4800 baud only works at 8MHz
'The use of IF PIN3 THEN allows the switch
'to 8MHz to be brief, allowing the servos to be held
'in position rather than allowed to go limp.
'However use of IF PIN3 causes occasional twitching
'of servos for an unkown reason.- maybe in the PICAXE system.
'We hope to program around this in a future version
'since unwanted twitching of servos may decrease their life.

'Version 1d did not need to switch speed or use IF PIN3 
'because it uses PULSOUT instead of SERVO/SERVOPOS
'However, SERVO/SERVOPOS are used at the start for the test pattern.

'Version 1e uses Pin0 as a timed switch
'to permit the computer to switch off power for a time
'for this the instruction is sync,9,time
'e.g. 127,9,15 = switch off for 15 minutes
'code below is unfinished. i.e. need timing in loop ?

 #Picaxe 08M
 SETFREQ M4 'standard 4MHz
  
 SERVO 1,146: SERVO 2,146: SERVO 4,146 'middle positions
 'test pattern: fully clockwise, middle, anti-clockwise.
 'also toggles power switch.
 for b0 = 0 to 126 step 63 
   HIGH 0
   b13 = b0 * 91 / 126 + 100
   b1=b13: b2=b13: b3=b13
   SERVOPOS 1, b1 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b2 'move servo 1
   PAUSE 1000
   LOW 0
   SERVOPOS 4, b3 'move servo 2
   PAUSE 2000
 next b0

 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SetFreq M8
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
w3 = 320 ' 160 @ 4MHz
w4 = -1  '1e power switch timer. -1 = inactive
HIGH 0 '1e power switch starts in ON state.

SetInt %01000, %01000

w0 = 4000 - w1 - w2 - w3 / 100
Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    High 0 'switch ON
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If
Loop

Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
  'w0 = b13 * 91 / 126 + 100 'scale position 100 to 191

  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             Low 0 'switch OFF and start timer to switch ON.
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
 

Brietech

Senior Member
I had considered a SPOT, but I wasn't sure what their coverage would be like at see. It's good to know that that seems to be the common solution. At 150 GBP to buy one though, you might be getting ripped off! They are on Ebay in the US for like $80-150 USD =)
 

Robin Lovelock

Senior Member
Good Morning Gentlemen. I've done some more tests, and my best guess is that there is some kind of (undocumented?) interference between use of PIN0 and the other three pins for servo control. My latest tidied up and simplified program is below. Is it possible that use of HIGH 0 or LOW 0 would have any side effect on the control of the servos ? I do seem to get some funny servo movement when the 127,9,1 is received, to switch power off. Also, the switching of power on again does not seem to work now.

For Brietech: best that you contact me directly, since this is not the best place to discuss general robot boat topics. There are far better Forums such as that for the MicroTransat project. Please contact me direct on gpss@compuserve.com. If you are in UK, you could call me on 01344 620775. You will find all my contact details, including home address in Sunninghill Berkshire UK on www.gpss.co.uk You may want to browse our family pages on www.gpss.co.uk/family.htm if you have any nervousness about disclosing your address and 'phone number to me. e.g. if you are very young. Then, perhaps after a chat on the 'phone, I can put you in contact with the right people, and/or we can chat about robot boats directly.

Meanwhile, I look for a lead on what to try next from our PICAXE experts.
Robin
www.gpss.co.uk/autop.htm

Code:
'SC1.BAS servo controller for robot boat
'v1e 31 Aug 2009 (c) Robin Lovelock www.gpss.co.uk/autop.htm 
' - with a great deal of help from HIPPY on Picaxeforum.co.uk :-)
'input is serial RS232 input group of 3 bytes: 
'127=sync, 0,1 or 2=servo number, 0 to 126 servo position
'the input servo position from RS232 is 0 to 126, 64=centre
'position sent to servo system is 100 to 191, 146=centre

'switching between 4MHz and 8MHz was done to program
'around the restriction in the PICAXE system that
'SERVO/SERVOPOS only works at 4MHz whereas
'SERIN at 4800 baud only works at 8MHz
'The use of IF PIN3 THEN allows the switch
'to 8MHz to be brief, allowing the servos to be held
'in position rather than allowed to go limp.
'However use of IF PIN3 causes occasional twitching
'of servos for an unkown reason.- maybe in the PICAXE system.
'We hope to program around this in a future version
'since unwanted twitching of servos may decrease their life.

'Version 1d did not need to switch speed or use IF PIN3 
'because it uses PULSOUT instead of SERVO/SERVOPOS
'However, SERVO/SERVOPOS are used at the start for the test pattern.

'Version 1e uses Pin0 as a timed switch
'to permit the computer to switch off power for a time
'for this the instruction is sync,9,time
'e.g. 127,9,15 = switch off for 15 minutes
'code below is unfinished. i.e. need timing in loop ?

 #Picaxe 08M
 SETFREQ M4 'standard 4MHz
  
 SERVO 1,146: SERVO 2,146: SERVO 4,146 'middle positions
 'test pattern: fully clockwise, middle, anti-clockwise.
 for b0 = 0 to 126 step 63 
   b13 = b0 * 91 / 126 + 100
   b1=b13: b2=b13: b3=b13
   SERVOPOS 1, b1 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b2 'move servo 1
   PAUSE 1000
   SERVOPOS 4, b3 'move servo 2
   PAUSE 2000
 next b0
 

 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SetFreq M8
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
w3 = 320 ' 160 @ 4MHz
w4 = 0  '1e power switch timer. Start ON.

SetInt %01000, %01000

w0 = 4000 - w1 - w2 - w3 / 100
Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If
Loop

Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
  'w0 = b13 * 91 / 126 + 100 'scale position 100 to 191

  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
 

hippy

Technical Support
Staff member
I've done some more tests, and my best guess is that there is some kind of (undocumented?) interference between use of PIN0 and the other three pins for servo control. Is it possible that use of HIGH 0 or LOW 0 would have any side effect on the control of the servos ?
There shouldn't be any interaction between pin0 and servo control.

I do seem to get some funny servo movement when the 127,9,1 is received, to switch power off. Also, the switching of power on again does not seem to work now.
Are you saying pin0 control doesn't work as expected or additional hardware for controlling power does not work ?

Go back to keeping the iPAQ permanently powered-up and checking by use of a LED that pin0 is controlled as expected. Replace the LED with relay and check it works as before with the relay switching the LED.

As noted earlier, it can take effort to integrate something which can power-off to a PICAXE. If it works without powering-off actually happening but doesn't when it does power-off, you have to investigate the cause and then resolve it, or work round it.

If power-up isn't occurring this suggests the HIGH 0 is not being activated, which suggests the servo loop is not being activated ( which may also explain servo twitching ). For that to occur the likely candidate for the program jamming up IMO is entering the interrupt when no serial is forthcoming. You may need to disable checking for serial from the iPAQ when powered-off and may need to delay accepting serial for a while when it is powered-up again.

Untested, but this should disable serial from the iPAQ when powered-down, and during the minute after it is powered back up.

Code:
w0 = 4000 - w1 - w2 - w3 / 100
Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
    If w5 > 0 Then
      w5 = w5 - 1
      If w5 = 0 Then
        SetInt %01000, %01000
      End If
    End If
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If  
Loop
Code:
  Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
             w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
             Return ' Return with interrupt disabled
 

Robin Lovelock

Senior Member
Thanks Hippy. That's a lot for me to get on with - but maybe I've found my mistake. It's related to that jumper that is normally from pin0 to another pin when the device is being programmed. After programming, I put the jumper in the other position.

And that's where I think I've done things wrong: I take the output from this other pin (now connected to pin0) and take this to the reed relay coil. But I neglected to see the significance of what else is connected to that third pin. Maybe that's the source of my problems. Maybe the solution is to remove the jumper completely and wire direct to pin 0 ? But would that effect use of pin 0 when the jumper is put back on for programming ?

Before seeing this I had added another 1k + LED, to between pin0 and ground. I already have 1K+LED from my reed relay +5v output and ground. That's when I started to see things differently from what I would expect.

Now going out for a long walk with the wife. Thank god it's not to the shops :)

Robin
www.gpss.co.uk/autop.htm
 

hippy

Technical Support
Staff member
When programming Pin 0 has to be routed out ( as Serial Out ) for the programming PC, when running it can be used for comms back to the PC ( via SERTXD ) or for hardware control. In some cases Pin 0 can go to both the PC and the hardware with no adverse or serious effects; LED's may flicker and piezo sounders might squeal while downloading but what the heck, but not so good if it's driving a relay, servo or controlling something important.

The PICAXE-08/M proto board has a jumper for routing to PC or hardware and could be shorted to drive both pins together if desired. When developing it would normally have Pin 0 linked to PC and change to hardware for running, in a final standalone product it may be permanently wired to hardware only.

The real problem here is that you've run out of I/O lines and have a clash in usage of the dual purpose Pin 0. You could move the relay to one of the servo pins and put that servo on Pin 0 and switch the jumper link when downloading and running, or live with it jittering whilst downloading having Pin 0 connected to both ( but that doesn't feel like a good idea to me ).

This is a natural consequence of using the smaller / cheaper PICAXE's. Space and cost savings bring with them constraints and limitations. Some constraints may not be too much of a problem ( that LED or Pizeo on Pin 0 ), or if Pin 0 isn't used other than for downloading, but in other cases it can be awkward.

It would be worth considering moving up to a larger PICAXE which has enough I/O to not have to fiddle with jumpers and that could also bring other benefits; no clamping diode for serial input needed if you use the right pin, and multiple pins can be used to switch the power relay which gives more protection against in-rush currents, plus you can use SERTXD and DEBUG without affecting any other hardware, and you can have other I/O dedicated to diagnostic LED's and the like during development. You could port the finished PICAXE program down to an 08M for the final product if needs be but for a one off or small scale project it's not really worth the effort.

I'm a strong advocate of using a PICAXE which is overkill for the intended task during development, especially when you don't know where that development is ultimately going to end up. That minimises the risk of hitting a wall through running out of I/O, variables or program space. There's nothing worse IMO than being up against the limits and not being able to add debugging commands needed to solve a problem or having to fight to squeeze in a line of necessary code. With 'not enough' you can end up taking things out you'd like to keep and compromising what you want.

I see it like any other venture, best to be over prepared, climb the Andes wearing shoes and carrying litres of water before trying it with flip-flops and an ice-lolly. If you do find it can't be done with anything less you've still succeeded, which is far better than getting half way and having to find a Plan B.

Ultimately it's a decision between sticking with what you have with the issues that brings, or moving to something which may be more comfortable to work with. Only you can make that decision, but my personal opinion would be to move up to a 14M for this project, maybe further; 08M, 14M, 20M, 28X1, 20X2 and 28X2 best suit projects which need to run down to low voltages.
 

Robin Lovelock

Senior Member
Many Thanks Hippy. I'm very happy to work within the restrictions of the 08M. If I wanted overkill I wouldn't be using any of the picaxe stuff :)

I see my problem as simply understanding how best to connect into pin0. I will not be changing that picaxe program often, and I certainly don't mind changing jumpers and unplugging servos when I do change it.

I expect if I look back through your documentation I will see the answer to my question - but you probably know it off the top of your head.

I will obviously put the jumper in the position required when changing the program. My question relates to what is the purpose of the other jumper position. I've not checked docs yet, but the 3rd pin - to which I was connecting the coil of my reed relay, seems to go back into the circuit. I'm guessing that's what's causing my problems.

I could cut the track after this 3rd pin, so, when the jumper is moved across, it ONLY connects pin0 to my reed relay. Or I could do something different. like wire pin0 to the reed relay permanently - but I don't want to screw up the program loading process.

Sorry if I've not explained clearly.
Will probably all be obvious to me in the morning :)
Robin
www.gpss.co.uk/autop.htm

P.S. Brietech has been put in touch with the robot boat community.
 

Robin Lovelock

Senior Member
Hi Folks. I trawled through documentation and could not find relevant info about that other jumper position, when not set for programming.

BUT, I have found a problem, and I'm sure you will point me to the source. The relevant code is below. I've removed the reed relay and am now simply using the LED and resistor to monitor the output of Pin0. i.e. the LED turns ON when I do HIGH 0 and goes OFF after LOW 0.

It seems the Do Loop is not being reached. Execution reaches the "SetInt %01000, %01000" but no further. Sorry that I should have done this simpler test earlier.

Any ideas ?

After I've got the LED on Pin0 doing what it should, I can then think about adding that reed relay.

Robin
www.gpss.co.uk/autop.htm

Code:
 'test PIN 0 power switch
 HIGH 0
 PAUSE 4000
 LOW 0
 PAUSE 1000
 
 HIGH 0
 PAUSE 4000
 LOW 0
 PAUSE 1000
 

 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SetFreq M8
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
w3 = 320 ' 160 @ 4MHz
w4 = 0  '1e power switch timer. Start ON.

SetInt %01000, %01000
HIGH 0 'to see if got here - it doesn't

w0 = 4000 - w1 - w2 - w3 / 100
Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If
Loop

Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
  'w0 = b13 * 91 / 126 + 100 'scale position 100 to 191

  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
 

hippy

Technical Support
Staff member
If it's executing the SETINT but not the next HIGH 0 that suggests being stuck in the interrupt routine, executing SERIN but no data being received.

Remove all HIGH 0 and LOW 0, put a HIGH 0 at start of interrupt LOW 0 at the end ( or use SERTXD, or SOUND and a piezo sounder ) to determine if that is what the code is doing.

This is the, sometimes long and hard, slog that is debugging. Determining why something that previously worked now doesn't, tracking down what changed and why, determining if it's a software or hardware issue.
 

Robin Lovelock

Senior Member
Many Thanks Hippy - your guess was right, and I did not even need to do the intermediate tests - I just put the HIGH 0 before the statement that enabled interrupts. The logic then worked, with the iPAQ GPSSppc autopilot doing what it should, including changing the servo position and switching off for 1 minute - shown by the LED.

So tomorrow I can put the reed relay back in, and try testing with the picaxe actually switching power to the GPS and iPAQ. If you have any answers to my question about that 3rd pin and second jumper position, that'll be good. If not, I will probably try it as I have now, with the 3rd pin track being cut. But it would be nice to know what the purpose was.

Yes, I am aware of the classic problems of diagnosing if it is hardware or software. Back in the 1960s my undergraduate project was interfacing a teletype to a Ferranti Pegasus computer. Went into production in 1949 based on the Enigma code breaking machine. Had 55 words of 39 bit RAM :)

What's nice these days is you can afford to have several copies of the hardware which makes it easier to eliminate some hardware faults like dry joints. That old but nice erskin 40/60 lead-tin solder is already on it's way to me :)

Robin
www.gpss.co.uk/autop.htm
 

hippy

Technical Support
Staff member
If you have any answers to my question about that 3rd pin and second jumper position, that'll be good.
For the AXE021, 08 Proto Board, as I understand it, the centre leg of the 3-way Molex goes to the PICAXE physical pin 0 ( leg 7 ), the "PROG" position leg goes out to the Serial Out of the 3.5mm Jack to PC, the "OUT" position leg goes to the prototyping area marked "0".
 

Robin Lovelock

Senior Member
Good Morning Gentlemen and Many Thanks Hippy.

Thanks for your last posting: silly me - as soon as I looked at one of my spare prototype boards, I could see that 3rd pin simply went to a bit of track just waiting to be used: as I have.

More importantly, I've made some progress, but we are not quite there yet.

The reed relay is now back in, with LEDs both on pin0 and the reed relay power out which is going to the GPS and iPAQ running GPSSppc-Autopilot.

Symptoms indicated that it was probably doing as you suspected in an earlier posting: powering down of the iPAQ (which happened when it should, after the iPAQ sent the 127,9,1 after adjusting servos) caused repeated interrupts and blocked return to the earlier loop that switches power back on.

This is where I've used your suggested code in an earlier thread, intended to disable interrups while the power is off and the picaxe is timing down to switch power back on. I've probably not included the code as I should.

Symptoms now are that all works as it should, but instead of powering back up after 1 minute, it is after several minutes. In fact, this happened after I decided it was not going to switch back on after one minute, and I started to type this posting. I then heard the auopilot talking - meaning it was back on ! :) However, the system was now in the state of the RS232 interface not appearing to work, because the servo was not moving and power not being switched off again. OR it could be that interrups were still disabled, so SERIN was not getting done.

I'm hoping you will see some minor mistake in the latest version below.

Robin
www.gpss.co.uk/autop.htm

Code:
'SC1.BAS servo controller for robot boat
'v1e 2 Sep 2009 (c) Robin Lovelock www.gpss.co.uk/autop.htm 
' - with a great deal of help from HIPPY on Picaxeforum.co.uk :-)
'input is serial RS232 input group of 3 bytes: 
'127=sync, 0,1 or 2=servo number, 0 to 126 servo position
'the input servo position from RS232 is 0 to 126, 64=centre
'position sent to servo system is 100 to 191, 146=centre

'switching between 4MHz and 8MHz was done to program
'around the restriction in the PICAXE system that
'SERVO/SERVOPOS only works at 4MHz whereas
'SERIN at 4800 baud only works at 8MHz
'The use of IF PIN3 THEN allows the switch
'to 8MHz to be brief, allowing the servos to be held
'in position rather than allowed to go limp.
'However use of IF PIN3 causes occasional twitching
'of servos for an unkown reason.- maybe in the PICAXE system.
'We hope to program around this in a future version
'since unwanted twitching of servos may decrease their life.

'Version 1d did not need to switch speed or use IF PIN3 
'because it uses PULSOUT instead of SERVO/SERVOPOS
'However, SERVO/SERVOPOS are used at the start for the test pattern.

'Version 1e uses Pin0 as a timed switch
'to permit the computer to switch off power for a time
'for this the instruction is sync,9,time in minutes
'e.g. 127,9,15 = switch off for 15 minutes
'code below is unfinished. i.e. need timing in loop ?

 #Picaxe 08M
 SETFREQ M4 'standard 4MHz
  
 SERVO 1,146: SERVO 2,146: SERVO 4,146 'middle positions
 'test pattern: fully clockwise, middle, anti-clockwise.
 for b0 = 0 to 126 step 63 
   b13 = b0 * 91 / 126 + 100
   b1=b13: b2=b13: b3=b13
   SERVOPOS 1, b1 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b2 'move servo 1
   PAUSE 1000
   SERVOPOS 4, b3 'move servo 2
   PAUSE 2000
 next b0
 
 'test PIN 0 power switch ON for 4 secs
 HIGH 0
 PAUSE 4000
 LOW 0
 PAUSE 1000
 
 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SetFreq M8
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
w3 = 320 ' 160 @ 4MHz
w4 = 0  '1e power switch timer. Start ON.

HIGH 0 'start with power ON
w0 = 4000 - w1 - w2 - w3 / 100

SetInt %01000, %01000

'loop below not entered until some RS232 input
Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
    If w5 > 0 Then
      w5 = w5 - 1
      If w5 = 0 Then
        SetInt %01000, %01000
      End If
    End If
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If
Loop

Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
  'w0 = b13 * 91 / 126 + 100 'scale position 100 to 191

  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
             w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
             Return ' Return with interrupt disabled  
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
 

Robin Lovelock

Senior Member
Just a quick follow up which may be of help: it looks as if the problem relates to values of the new timing counter for disabling interrupts.

The switch ON is after about 5 minutes, instead of 1 minute. Then it stays on without the servo or switch off working. BUT, I noticed that after 15 minutes (it may have been sooner) it was doing as it should again: adjusting the servo and switching off. But this is not happening every time.

Program below unchanged from earlier posting.

Robin
www.gpss.co.uk/autop.htm

Code:
'SC1.BAS servo controller for robot boat
'v1e 2 Sep 2009 (c) Robin Lovelock www.gpss.co.uk/autop.htm 
' - with a great deal of help from HIPPY on Picaxeforum.co.uk :-)
'input is serial RS232 input group of 3 bytes: 
'127=sync, 0,1 or 2=servo number, 0 to 126 servo position
'the input servo position from RS232 is 0 to 126, 64=centre
'position sent to servo system is 100 to 191, 146=centre

'switching between 4MHz and 8MHz was done to program
'around the restriction in the PICAXE system that
'SERVO/SERVOPOS only works at 4MHz whereas
'SERIN at 4800 baud only works at 8MHz
'The use of IF PIN3 THEN allows the switch
'to 8MHz to be brief, allowing the servos to be held
'in position rather than allowed to go limp.
'However use of IF PIN3 causes occasional twitching
'of servos for an unkown reason.- maybe in the PICAXE system.
'We hope to program around this in a future version
'since unwanted twitching of servos may decrease their life.

'Version 1d did not need to switch speed or use IF PIN3 
'because it uses PULSOUT instead of SERVO/SERVOPOS
'However, SERVO/SERVOPOS are used at the start for the test pattern.

'Version 1e uses Pin0 as a timed switch
'to permit the computer to switch off power for a time
'for this the instruction is sync,9,time in minutes
'e.g. 127,9,15 = switch off for 15 minutes
'code below is unfinished. i.e. need timing in loop ?

 #Picaxe 08M
 SETFREQ M4 'standard 4MHz
  
 SERVO 1,146: SERVO 2,146: SERVO 4,146 'middle positions
 'test pattern: fully clockwise, middle, anti-clockwise.
 for b0 = 0 to 126 step 63 
   b13 = b0 * 91 / 126 + 100
   b1=b13: b2=b13: b3=b13
   SERVOPOS 1, b1 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b2 'move servo 1
   PAUSE 1000
   SERVOPOS 4, b3 'move servo 2
   PAUSE 2000
 next b0
 
 'test PIN 0 power switch ON for 4 secs
 HIGH 0
 PAUSE 4000
 LOW 0
 PAUSE 1000
 
 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SetFreq M8
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
w3 = 320 ' 160 @ 4MHz
w4 = 0  '1e power switch timer. Start ON.

HIGH 0 'start with power ON
w0 = 4000 - w1 - w2 - w3 / 100

SetInt %01000, %01000

'loop below not entered until some RS232 input
Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
    If w5 > 0 Then
      w5 = w5 - 1
      If w5 = 0 Then
        SetInt %01000, %01000
      End If
    End If
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If
Loop

Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
  'w0 = b13 * 91 / 126 + 100 'scale position 100 to 191

  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
             w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
             Return ' Return with interrupt disabled  
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
[/QUOTE]
 

hippy

Technical Support
Staff member
The power-on and enable interrupts look okay to me and should I believe work as intended so it's really now an exercise in debugging to find out what is happening in real time and with the real hardware.

The time to handle w5 does extend the loop time a little but nowhere near double it. You can run the program through the simulator to see how it behaves. Replacing the SERIN with "b12=9:b13=1" simulates it receiving a power off for a minute command which will make simulating and testing easier.
 

Robin Lovelock

Senior Member
Thanks Hippy. I'm afraid I've not made much progress here. The system works just fine when the iPAQ has it's internal battery to keep it powered up. i.e. the GPS and iPAQ charging gets switched off when expected, then switched on a minute later.

Problems happen when the internal battery is removed, so that powering off the GPS and the iPAQ screws things up - perhaps related to the RS232 line going low, and something like repeated interrupts - as you suspect.

Sadly, despite trying many variations on your logic, I did not get a good result. I gave the simulator a try but was not sure how to simulate interrupts. I tried a lot of things including SETINT OFF and the HIGH 0 only being done once. Most changes had some logic behind them, but not a lot of knowledge :)

I'm wondering if some lateral thinking is needed. e.g. something like a pull up resistor on the RS232 input, so that the powering off does not give the unwanted side effects.

Robin
www.gpss.co.uk/autop.htm
 

hippy

Technical Support
Staff member
I'm afraid I've not made much progress here.
I disagree, I think you have ...

The system works just fine when the iPAQ has it's internal battery to keep it powered up ... Problems happen when the internal battery is removed
At least that narrows it down to a systems / interfacing / hardware issue, not faulty PICAXE software per se. So that narrows it down to what's causing the PICAXE to behave differently.

With the internal battery removed, and the with the iPAQ permanently powered ( bypass the relay power-off ), does it all work okay ?

It could be a simple pull-down needed. I cannot recall exctly how the iPAQ serial interfaces, maybe just a 22K, in which case a 10K on the iPAQ side of the 22K to 0V should do it.

That said, the program should be inhibiting interrupts and from getting to the SERIN until well after powered up ( 1 minute ).
 

Robin Lovelock

Senior Member
Thanks Hippy. I reloaded the program, and have listed it below, just in case all those temporary changes today ended up with a mistake.

I seem to be getting similar behaviour for three situations:
1) with the iPAQ battery included (so just GPS being switched on/off)
2) with +5v power to GPS and iPAQ (with no battery) bypassing relay
3) with relay switching +5v to GPS and iPAQ (without battery) as intended.

The AutoPilot runs OK to point where it has finished adjusting servo and sends 127,9,1 to switch off. The LED goes out indicating Pin0 LOW and relay off. BUT it does not switch on again after 1 minute. After maybe 4 or 5 minutes, the LED comes back on, but it seems the serial interface is not working (or SERIN/Interrupts still disabled) because the servo does not move and the LED is not switched off again.

Time to switch the PC off and relax I think :)

Robin
www.gpss.co.uk/autop.htm

Code:
'SC1.BAS servo controller for robot boat
'v1e 2 Sep 2009 (c) Robin Lovelock www.gpss.co.uk/autop.htm 
' - with a great deal of help from HIPPY on Picaxeforum.co.uk :-)
'input is serial RS232 input group of 3 bytes: 
'127=sync, 0,1 or 2=servo number, 0 to 126 servo position
'the input servo position from RS232 is 0 to 126, 64=centre
'position sent to servo system is 100 to 191, 146=centre

'switching between 4MHz and 8MHz was done to program
'around the restriction in the PICAXE system that
'SERVO/SERVOPOS only works at 4MHz whereas
'SERIN at 4800 baud only works at 8MHz
'The use of IF PIN3 THEN allows the switch
'to 8MHz to be brief, allowing the servos to be held
'in position rather than allowed to go limp.
'However use of IF PIN3 causes occasional twitching
'of servos for an unkown reason.- maybe in the PICAXE system.
'We hope to program around this in a future version
'since unwanted twitching of servos may decrease their life.

'Version 1d did not need to switch speed or use IF PIN3 
'because it uses PULSOUT instead of SERVO/SERVOPOS
'However, SERVO/SERVOPOS are used at the start for the test pattern.

'Version 1e uses Pin0 as a timed switch
'to permit the computer to switch off power for a time
'for this the instruction is sync,9,time in minutes
'e.g. 127,9,15 = switch off for 15 minutes
'code below is unfinished. i.e. need timing in loop ?

 #Picaxe 08M
 SETFREQ M4 'standard 4MHz
  
 SERVO 1,146: SERVO 2,146: SERVO 4,146 'middle positions
 'test pattern: fully clockwise, middle, anti-clockwise.
 for b0 = 0 to 126 step 63 
   b13 = b0 * 91 / 126 + 100
   b1=b13: b2=b13: b3=b13
   SERVOPOS 1, b1 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b2 'move servo 1
   PAUSE 1000
   SERVOPOS 4, b3 'move servo 2
   PAUSE 2000
 next b0
 
 'test PIN 0 power switch ON for 4 secs
 HIGH 0
 PAUSE 4000
 LOW 0
 PAUSE 1000
 
 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SETFREQ M8 '8 MHz so SERIN will work
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
w3 = 320 ' 160 @ 4MHz
w4 = 0  '1e power switch timer. Start ON.

HIGH 0 'start with power ON
w0 = 4000 - w1 - w2 - w3 / 100

SetInt %01000, %01000

Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
    If w5 > 0 Then
      w5 = w5 - 1
      If w5 = 0 Then
        SetInt %01000, %01000
      End If
    End If
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If
Loop

Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
  'w0 = b13 * 91 / 126 + 100 'scale servo position 100 to 191
  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
             w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
             Return ' Return with interrupt disabled               
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
 

Robin Lovelock

Senior Member
Hippy: your absence is greatly missed :)

I guess the implication from my recent postings, and your last advice, is that I may be experiencing bugs in the low level picaxe system ? e.g. side effects on interrupts / SERIN of things like LOW 0 ?

No problems if the bugs and restrictions are known, of course - since there are often work-arounds, or one can cut ones losses and limit how much time is spent trying to solve a problem. I feel as if I am back in the 1960s days of testing a new compiler :)

Any specific ideas on solving my problem above would be good. I tried the simulator, but it did not seem to extend to interrupt handling.

On a general question: is there documentation on the current known bugs and restrictions in the picaxe system ? e.g. those we've already uncovered on this thread (e.g. SERIN not supported at 4MHz and SERVO not supported at anything other than 4MHz.) Such a document, in your public PDF files, would be of great value - especially if it included work-arounds. The sales guys in your organisation would be uncomfortable with such information being public, and could always point at Microsoft on how such things are left unspoken. However, since your prime market is obviously small school projects, I'm sure that will not be a problem.

Since my last posting I've tried many things, but all with the same results.

Robin
www.gpss.co.uk/autop.htm
 
Last edited:

Robin Lovelock

Senior Member
Hi Folks - particularly Hippy. I found it and fixed it ! :)

It was staring us both in the face: that line after the SERIN: it now needed to be conditional to avoid doing it for the power switching command.

Code:
Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  if b12 < 9 then 'not for 127,9,1
    w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
    'w0 = b13 * 91 / 126 + 100 'scale servo position 100 to 191
  end if
  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
             w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
             Return ' Return with interrupt disabled               
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
After what seems a lifetime, it is now doing what it should, powering off and on the system. So it has now been left running, to check reliability.

Many Thanks for your excellent help on this Hippy.

I'm still interested in that information on known bugs and restrictions in the picaxe system.

Robin
www.gpss.co.uk/autop.htm
 

hippy

Technical Support
Staff member
I don't think you are experiencing any PICAXE bugs and there should not be any adverse side effects in the way described. No one can guarantee anything is a bug free product and we're pretty open about that as they are a fact of life. With a product like the 08M which has had considerable testing in-house and in the field, where end-users will try endless combinations of code, we are fairly certain that fundamental bugs do not exist.

The conflicts with combinations of commands are in Appendix 2 of PICAXE Manual 2 Basic Commands. It is correct to say SERVO only works at 4MHz ( on non-X2 parts ), but not correct to say that of SERIN.

What we have here is not so much a PICAXE bug but a program bug or an affect caused through hardware interaction. As you say in Post #105 the system works just fine when everything is powered and has batteries fitted and isn't actually being powered off, but when batteries are removed and things are powered off behaviour isn't as expected.

The code looks good to me, but I cannot test it for myself. I'm also quite willing to accept the program isn't behaving as would be expected in particular cases; the largest part of software engineering is in determining why that may be, debugging code and hardware. The way to progress that is to determine what should happen and compare with what actually does happen, then theorise on the cause, fix it or work round it, and repeat until everything works. Finding what does happen is a case of inserting 'debug statements' ( which may not be DEBUG but SERTXD, LED flashes or piezo tones ) so one can spot where behaviour differs from expectations.

In this case it all works until power is actually switched off and/or batteries are removed. That indicates it's those circumstances which introduce the problem. I cannot see why that is; the main servo loop should keep running, the w4 and w5 timers should therefore decrement at regular intervals and timing should be as expected. Something is disrupting that but I cannot see what. Servo pulses should remain regular once set regardless if the iPAQ is powered on or off so perhaps the first thing to do is to see what is happening on those pulses with a scope. They should be approx 20ms spaced; do they slow down, stop or become intermittent when the iPAQ is powered off ? That would explain the extending time. I'd suggest using a PULSOUT on a spare pin at the start of the loop to make that easier but there's no spare I/O to do that with - this is what I meant about problems of constraints, making testing and debugging so much harder; you cannot add debugging without having to take something else out and rewriting chunks of code which isn't an ideal way of proceeding.

At this stage I don't think there is any option but to get interacting with the code and see what does behave as expected and what does not. I don't believe it will be a previously unseen interaction between a LOW 0 and interrupts or SERIN directly, but the LOW 0 may well be the trigger for something with the circuit or configuration which then causes something else unexpected to happen.
 

hippy

Technical Support
Staff member
Looks like you found it while I was typing ( did it really take that long ! ).

A classic case of 'function side effect' where calling a subroutine / function / interrupt affects data used elsewhere in the program. In this case using 'w0' as a temporary when it is needed elsewhere in the code.

Also typical that it should be staring us in the face; most bugs are like that.

On the plus side, at least looking at the servo pulses would have shown the timing change as suggested, which leads to "how?" which would hopefully have indicated 'w0' was altering which leads to "why?", which should have led back to its use in the interrupt. Debugging is really a process of thinking what could go wrong, what could be the cause of that - timing stretches, is the timing variable right ?

Anyway, glad to hear you've got it sorted.
 

Robin Lovelock

Senior Member
Thanks Hippy. I'm sure your posting was done before you saw my last one: yes, my recent troubles were all down to that silly bug of ours - easy to do, but not easy to see - even when it stares you in the face. So now the system is wired up to solar panels, doing 24/7 tests.

You are preaching to the converted on "no software being bug free", and I feel sure you have as many years in the systems and software industry as myself. That is why it is of great value if known bugs and restrictions are openly declared. Sadly, in recent years, Microsoft have made a habit of keeping very quiet on this, and many smaller organisations follow where they lead.

So, to sum up, thanks for your excellent support in getting me to where I am now, on a PICAXE based solution for the robot boat servo and power control, which may well be what we need.

Robin
www.gpss.co.uk/autop.htm
 

Robin Lovelock

Senior Member
Hi Folks. I know it would be unreasonable to ask for programming help, even though Hippy has been very kind in actually providing me valuable code. However, maybe you can provide a tip or two on how I might approach my next problem - which seems to be having run out of 08M variables. Please don't suggest the obvious: "buy a bigger chip" :)

Last night's overnight test of the program below was useful: showing the picaxe doing what it should: switching ON the GPS and iPAQ, causing the iPAQ AutoPilot to boot up, adjust the servos, then switch itself OFF with the 127,9,1 command from iPAQ to the PICAXE.

However, I found that I want a new feature in the PICAXE program: to limit the time that it stays switched ON, to perhaps 2 minutes. Last night this was shown up by battery power dropping below 4.8v, so that the iPAQ did not switch on (it's internal battery is removed), and so no "switch off" command was received, and the power to iPAQ and GPS remained on. Ideally, if power was switched off after 2 minutes, this would provide a "watchdog timer" on any failure of the iPAQ/GPS system. After switching off, it should do the same as if the iPAQ had switched it off. e.g. switch on again after 1 minute, to try again.

At first I thought it would be a doddle: just add an extra counter, using W6. After some time wondering why it would not work, I consulted the picaxe documentation and saw that W6 shares space with B12,B13 variables already used, and I seem to have used up what is available. Sorry but I don't want to replace all my spare 08M boards with 16 versions :)

Maybe there is an approach that can let me do a work-around ?
I guess I need some lateral thinking or expert knowledge here.

Many Thanks in advance.
Robin
www.gpss.co.uk/autop.htm

Code:
'SC1.BAS servo controller for robot boat
'v1e 2 Sep 2009 (c) Robin Lovelock www.gpss.co.uk/autop.htm 
' - with a great deal of help from HIPPY on Picaxeforum.co.uk :-)
'input is serial RS232 input group of 3 bytes: 
'127=sync, 0,1 or 2=servo number, 0 to 126 servo position
'the input servo position from RS232 is 0 to 126, 64=centre
'position sent to servo system is 100 to 191, 146=centre

'switching between 4MHz and 8MHz was done to program
'around the restriction in the PICAXE system that
'SERVO/SERVOPOS only works at 4MHz whereas
'SERIN at 4800 baud only works at 8MHz
'The use of IF PIN3 THEN allows the switch
'to 8MHz to be brief, allowing the servos to be held
'in position rather than allowed to go limp.
'However use of IF PIN3 causes occasional twitching
'of servos for an unkown reason.- maybe in the PICAXE system.
'We hope to program around this in a future version
'since unwanted twitching of servos may decrease their life.

'Version 1d did not need to switch speed or use IF PIN3 
'because it uses PULSOUT instead of SERVO/SERVOPOS
'However, SERVO/SERVOPOS are used at the start for the test pattern.

'Version 1e uses Pin0 as a timed switch
'to permit the computer to switch off power for a time
'for this the instruction is sync,9,time in minutes
'e.g. 127,9,15 = switch off for 15 minutes


 #Picaxe 08M
 SETFREQ M4 'standard 4MHz
  
 SERVO 1,146: SERVO 2,146: SERVO 4,146 'middle positions
 'test pattern: fully clockwise, middle, anti-clockwise.
 for b0 = 0 to 126 step 63 
   b13 = b0 * 91 / 126 + 100
   b1=b13: b2=b13: b3=b13
   SERVOPOS 1, b1 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b2 'move servo 1
   PAUSE 1000
   SERVOPOS 4, b3 'move servo 2
   PAUSE 2000
 next b0
 
 'test PIN 0 power switch ON for 4 secs
 HIGH 0
 PAUSE 4000
 LOW 0
 PAUSE 1000
 
 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SETFREQ M8 '8 MHz so SERIN will work
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
w3 = 320 ' 160 @ 4MHz
w4 = 0  '1e power switch timer. Start ON.

HIGH 0 'start with power ON
w0 = 4000 - w1 - w2 - w3 / 100

SetInt %01000, %01000

Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
    If w5 > 0 Then
      w5 = w5 - 1
      If w5 = 0 Then
        SetInt %01000, %01000
      End If
    End If
  Else
    w4 = w4 - 1 'count down before above switch ON
  End If
Loop

Interrupt:
  SERIN 3, N4800_8, (127), b12, b13
  if b12 < 9 then 'not for 127,9,1
    w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
    'w0 = b13 * 91 / 126 + 100 'scale servo position 100 to 191
  end if
  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
             w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
             Return ' Return with interrupt disabled               
  End Select
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  SetInt %01000, %01000
  Return
 

Robin Lovelock

Senior Member
Just to say that I've seen I can remove use of W5 for suppression of interrupts, and it seems to work. So will try use of W5 for my new timer.
Robin
 

hippy

Technical Support
Staff member
PEEK and POKE are your friends here. They allow internal RAM ( SFR in Microchip parlance ) to be read and written. In this case you can save b12 and b13 when you enter the interrupt routine, re-use those variables, then restore them after use. You have to make sure that all paths through the code do restore those variables, for example ...

Code:
Interrupt:
  [b]Poke $50, b12
  Poke $51, b13[/b]
  Serin 3, N4800_8, (127), b12, b13
  If b12 = 9 then ' for 127,9,1
    w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
    LOW 0 'switch OFF and start timer to switch back ON.
    w5 = 3000 ' w5 = 1 * 60 * 1000 / 20   Else
  Else
    w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
    Select case b12
      Case 0 : w1 = w0 'vane rudder
      Case 3 : w2 = w0 '1d Wing-Sail was Case 1
      Case 2 : w3 = w0 'or sail winch
    End Select
    SetInt %01000, %01000
  End If
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  [b]Peek $50, b12
  Peek $51, b13[/b]
  Return
That however adds extra time between interrupt and reading the SERIN which may cause a problem. If it does, you have to juggle the variable use in the program to get what you want. In the re-jigged code above, w0 is always re-calculated at the end, so b0 and b1 ( which form w0 ) can be overwritten within the interrupt, as long as the 'staging' w0 calculation is removed so ...

Code:
Interrupt:
  Serin 3, N4800_8, (127), [b]b0, b1[/b]
  If [b]b0[/b] = 9 then ' for 127,9,1
    w4 = [b]b1[/b] * 3000 ' w4 = b1 * 60 * 1000 / 20
    LOW 0 'switch OFF and start timer to switch back ON.
    w5 = 3000 ' w5 = 1 * 60 * 1000 / 20   Else
  Else
    Select case [b]b0[/b]
      Case 0 : w1 = [b]b1 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO[/b]
      Case 3 : w2 = [b]b1 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO[/b]
      Case 2 : w3 = [b]b1 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO[/b]
    End Select
    SetInt %01000, %01000
  End If
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  Return
That increases code size, so an alternative is to use an alternative intermediate, say w5, and store then recover w5 (b11:b10) ...

Code:
Interrupt:
   Serin 3, N4800_8, (127), b0, b1
  If b0 = 9 then ' for 127,9,1
    w4 = b1 * 3000 ' w4 = b13 * 60 * 1000 / 20
    LOW 0 'switch OFF and start timer to switch back ON.
    w5 = 3000 ' w5 = 1 * 60 * 1000 / 20   Else
  Else
   [b]Poke $50, b10
   Poke $51, b11[/b]
   w5 = b1 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
    Select case b0
      Case 0 : w1 = w5 'vane rudder
      Case 3 : w2 = w5 '1d Wing-Sail was Case 1
      Case 2 : w3 = w5 'or sail winch
    End Select
   [b]Peek $50, b10
   Peek $51, b11[/b]
   SetInt %01000, %01000
  End If
  w0 = 4000 - w1 - w2 - w3 / 100
  Pause w0
  Return
 

Robin Lovelock

Senior Member
Many Thanks yet again Hippy. I'm not sure if your posting about PEEK and POKE was before my very latest one, before I spoke about using W5, after avoiding it's use for inhibiting interrupts. If so, I wonder if it is better for me to do postings like this as an edited P.S. at the bottom of the earlier one ?

Anyway, your note was already useful, since I am clearly near the limits of the 08M, and may need to use PEEK and POKE. I had been working on the W5 solution - started before your PEEK/POKE notes. I thought this would be simpler. But maybe that use of W5 cannot be avoided, since I'm not getting it to work as needed.

So I will study your PEEK/POKE notes more carefully, then probably make use of them to solve my problem. I've already had to start shaving off the test code at the start, because I was getting "too many bytes" :)

PEEK and POKE : what a "blast from the past that was".
Are you old enough to remember the 60s ?
I'm thinking that nickname "Hippy" has some basis :)
Now off for a walk.

Many Thanks again
Robin
www.gpss.co.uk/autop.htm
 

hippy

Technical Support
Staff member
I wonder if it is better for me to do postings like this as an edited P.S. at the bottom of the earlier one ?
Posting while someone is replying will always happen ( it's remarkable how often two people will reply at exactly the same time ), so IMO the best thing to do is to make all posts following the latest.

Edits and PS's are likely to be missed by people who go to the end of a thread first and continuity can get quite confusing. The clearer things are the more likely people are to understand and offer help if they can.
 

Robin Lovelock

Senior Member
Good Morning Hippy. I think we are there: the latest v1f of the picaxe 08M program is below, and seems to do what was needed. I ended up dropping support for the third servo, so I could sqeeze in the extra counter logic for the watchdog timer. I left it running overnight, it is still running, and it is now into 24/7 tests with the solar panels.

As always, your advice has been crucial, and I know better what I can squeeze onto a picaxe 08M, and when to go for something bigger. Now I can do some more work on the iPAQ Pocket PC software, GPSSppc, so that the AutoPilot can fully exploit this new power switching function.

Many Thanks
Robin
www.gpss.co.uk/autop.htm

Code:
'SC1.BAS servo controller for robot boat
'v1f 6 Sep 2009 (c) Robin Lovelock www.gpss.co.uk/autop.htm 
' - with a great deal of help from HIPPY on Picaxeforum.co.uk :-)
'input is serial RS232 input group of 3 bytes: 
'127=sync, 0,1 or 2=servo number, 0 to 126 servo position
'the input servo position from RS232 is 0 to 126, 64=centre
'position sent to servo system is 100 to 191, 146=centre

'switching between 4MHz and 8MHz was done to program
'around the restriction in the PICAXE system that
'SERVO/SERVOPOS only works at 4MHz whereas
'SERIN at 4800 baud only works at 8MHz
'The use of IF PIN3 THEN allows the switch
'to 8MHz to be brief, allowing the servos to be held
'in position rather than allowed to go limp.
'However use of IF PIN3 causes occasional twitching
'of servos for an unkown reason.- maybe in the PICAXE system.
'We programmed around this in a later version
'since unwanted twitching of servos may decrease their life.

'Version 1d and later did not need to switch speed or use IF PIN3 
'because it uses PULSOUT instead of SERVO/SERVOPOS
'However, SERVO/SERVOPOS are used at the start for the test pattern.

'Version 1e and later uses Pin0 as a timed switch
'to permit the computer to switch off power for a time
'for this the instruction is sync,9,time in minutes
'e.g. 127,9,15 = switch off for 15 minutes
'v1f drops support for third servo so can use w3 for power timer
'so that power on is limited to power off + 2 minutes
'This means the picaxe card also acts as an iPAQ watchdog timer.


 #Picaxe 08M
 SETFREQ M4 'standard 4MHz
  
 SERVO 1,146: SERVO 2,146 '1f :  SERVO 4,146 'middle positions
 'test pattern: fully clockwise, middle, anti-clockwise.
 for b0 = 0 to 126 step 63 
   b13 = b0 * 91 / 126 + 100
   b1=b13: b2=b13: b3=b13
   SERVOPOS 1, b1 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b2 'move servo 1
   PAUSE 1000
   '1f SERVOPOS 4, b3 'move servo 2
   PAUSE 2000
 next b0
 
 'test PIN 0 power switch ON for 4 secs
 'HIGH 0
 'PAUSE 4000
 'LOW 0
 'PAUSE 1000
 
 'remember servo positions and start at centre positions.
 b1 = 146 : SERVOPOS 1, b1
 b2 = 146 : SERVOPOS 2, b2
 b3 = 146 : SERVOPOS 4, b3
 PAUSE 3000 'allow servos time to travel
 Low 1 : Low 2 : Low 4 'switch off SERVO background
 
 
'this is the code from Hippy - with recent changes
SETFREQ M8 '8 MHz so SERIN will work
Symbol MIN_INPUT = 0
Symbol MAX_INPUT = 126
Symbol GAP_INPUT = MAX_INPUT - MIN_INPUT

Symbol MIN_SERVO = 200
Symbol MAX_SERVO = 400
Symbol GAP_SERVO = MAX_SERVO - MIN_SERVO

w1 = 320 ' 160 @ 4MHz
w2 = 320 ' 160 @ 4MHz
'1f w3 = 320 ' 160 @ 4MHz
w4 = 0  '1e power switch timer. Start ON.
w3 = 9000 'power off 3 minutes after on

HIGH 0 'start with power ON
'w0 = 4000 - w1 - w2 - w3 / 100
w0 = 4000 - w1 - w2 / 100


SetInt %01000, %01000

Do
  Pause      w0
  Pulsout 1, w1 
  Pulsout 2, w2 
  'Pulsout 4, w3
  If w4 = 0 Then 'pin0 used as power switch
    HIGH 0 'switch ON
    If w5 > 0 Then
      w5 = w5 - 1
      If w5 = 0 Then
        SetInt %01000, %01000
      End If
    End If
  Else
    w4 = w4 - 1 'count down before above switch ON
    if w4 = 0 then
      w3 = 6000 'power off 2 minutes after on
    end if
  End If
  if w3 = 0 then
    w4 = 3000 'switch on to try again in 1 minute
    w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
    w3 = w4 + 6000 'power off 2 minutes after on
    LOW 0
  else
    w3 = w3 -1
  endif
Loop

Interrupt:
  if w5 > 0 then 'if interrupts still disabled
    Return
  end if
  SERIN 3, N4800_8, (127), b12, b13
  if b12 < 9 then 'not for 127,9,1
    w0 = b13 Min MIN_INPUT Max MAX_INPUT - MIN_INPUT * GAP_SERVO / GAP_INPUT + MIN_SERVO
    'w0 = b13 * 91 / 126 + 100 'scale servo position 100 to 191
  end if
  Select case b12
    Case 0 : w1 = w0 'vane rudder
    Case 3 : w2 = w0 '1d Wing-Sail was Case 1
    '1f Case 2 : w3 = w0 'or sail winch
    Case 9 : w4 = b13 * 3000 ' w4 = b13 * 60 * 1000 / 20
             LOW 0 'switch OFF and start timer to switch back ON.
             w5 = 3000 ' w5 = 1 * 60 * 1000 / 20 
             w3 = w4 + 6000 'power off 2 minutes after on
             Return ' Return with interrupt disabled               
  End Select
  'w0 = 4000 - w1 - w2 - w3 / 100
  w0 = 4000 - w1 - w2 / 100
  Pause w0
  SetInt %01000, %01000
  Return
 

hippy

Technical Support
Staff member
With a bit of code re-jigging it is possible to have all three servos plus all the auto-on and auto-off timers, but if you have works then no need to worry.
 

MartinM57

Moderator
...like your startup routine....this does almost exactly the same as your v1f (two servos) but saves 22 bytes for use elsewhere (three servos?)...
Code:
' SERVO 1,146: SERVO 2,146 '1f:  SERVO 4,146 'middle positions
'test pattern: fully clockwise, middle, anti-clockwise.
for b0 = 100 to 190 step 45 
   SERVOPOS 1, b0 'move servo 0
   PAUSE 1000
   SERVOPOS 2, b0 'move servo 1
   PAUSE 3000
'   SERVOPOS 4, b0 'move servo 2
'   PAUSE 2000
 next b0
...
 
Top