Help with Servo and Servopos Commands

edmunds

Senior Member
Dear all,

I used to have servo issues and it is quite possible I will have them again and now I will have a software solution.

However, the way I dealt with the problems up until now was design and order a PCB. Good ground plane, decoupling where needed and careful routing of traces eliminated the servo twitching problem that I almost always have on the breadboard. Also, as noted above, particular servo does matter. I have not tried any expensive or let alone digital servos, but there are cheap servos and cheap servos. Since manufacturers and brands from certain big country come and go and it is unlikely you can buy the same product next year and even if you manage, it is likely made of different components, it does not pay to name or remember which works and which does not, but keeping a few different servos around normally works.

Good luck to all,

Edmunds
 

hippy

Ex-Staff (retired)
Here's an untested version which moves from A to B, waits 5000ms, moves B to A, waits 5000ms, then repeats forever -

Code:
Symbol SERVO_PIN  = C.1

Symbol fromPos    = w0
Symbol toPos      = w1
Symbol pulseTime  = w2
Symbol pauseTime  = w3
Symbol timeout    = w4

Symbol POS_A      = 199
Symbol POS_B      = 100

Symbol SPEED      = 100

#Macro MoveAndWait( fromNum, toNum, msTime )
  fromPos = fromNum * 100
  toPos   = toNum   * 100
  timeout = msTime
  Low SERVO_PIN
  Do
    pulseTime = fromPos / 100
    pauseTime = 2000 - pulseTime
    PulsOut SERVO_PIN, pulseTime
    PauseUs pauseTime
    Select case fromPos
      Case < toPos : fromPos = fromPos + SPEED Max toPos
      Case > toPos : fromPos = fromPos - SPEED Min toPos
    End Select
    timeout = timeout Min 20 - 20
  Loop Until timeout = 0
#EndMacro

MainLoop:
  Do
    ;            .-------------------- From
    ;            |      .------------- To
    ;            |      |      .------ Wait time (ms)
    ;            |____  |____  |____
    MoveAndWait( POS_A, POS_B, 5000 )
    MoveAndWait( POS_B, POS_A, 5000 )
  Loop
Now playing a tune at the same time; that would be tricky. The main problem would be that the TUNE command will block and therefore disrupt the servo loop timing.

The best bet would be a two PICAXE system, one moving the servo, the other playing the tune.
 

hippy

Ex-Staff (retired)
I like your code and may use it in a similar situation to Tricky Dicky, though I confess not to understand all of it!! I mean, why multiply by 100 on one line then divide it again on the next - must be a reason that escapes me :confused: :D!
The reason for that is so the speed can be slower than one step every 20ms.

If we just moved from 105 to 115 with a slowest SPEED of 1, it would move 105, 106 ... 114, 115 and be done, 10 steps and all over with in 200ms.

By multiplying by 100 we are actually moving the variable from 10500 to 11500 so it will actually take 1000 loops, 20000ms (20s) to make the move with the slowest SPEED of 1. The pulse we have to send out will still need to be 105 to 115 so we have to divide our 10500 to 11500 value by 100 to get the pulse width.
 
Many thanks Hippy, can't believe how quick you came up with an answer! Anyway I'll give it a go over the next few days and report back. I'm on Christmas deccy duty tomorrow!
 
I can confirm that the code in post #42 works fine, thanks Hippy. However, as you have confirmed that a second processor would be needed to play the sound, that is exactly what I now propose to do. It will be a simple enough job to piggy back a second 08M2 project pcb and this will be now be used to play the sound and provide a trigger input to the first cpu now using the original code based on a switch input. And anything else I can make it do ;) Sorted! Thanks very much again. Now to the Christmas decs :(
 
Top