servo and pulsein

rnoguera

New Member
Hi,

I have a small board using a 08M.

I read the signal coming from a RC radio receiver in one pin, using pulsein.
I control a RC servo from another pin.

If I use pulseout to control the servo, it runs fine. If I use servo + servopos, then the pulsein readings get a lot of noise. They change every time, even when the RC emitter is sending the same signal.

It doesnt seem to be a problem of electrical noise, since it happens wether the RC servo is connected or not.

It looks like servo is affecting the acuracy of pulsein a lot...

Any idea?
 

hippy

Technical Support
Staff member
And vice-versa probably, the PULSIN affecting the servo.

The PICAXE can only do one thing at a time. While sending a servo pulse ( or perhaps just setting it or clearing it ) the PICAXE cannot also be measuring the PULSIN time accurately. While measureing PULSIN time accurately it cannot be updating the servo pulses. Because servo control 'free-wheels' in the background you have no control over when it needs to create its output pulse and there's a ( 10% ? ) chance it will occur at the same time PULSIN does.

Using PULSIN and PULSOUT works because there is no chance that two things can happen at the same time.
 

rnoguera

New Member
Almost all processors can only do one thing at once, but they do it so fast, you believe they can do many.

Running servo and pulsein at the same time, should not be a problem.

If it was a problem, it should be documented in the manual.

I think it is a bug.
 

Technical

Technical Support
Staff member
As Hippy states, the PICAXE can't do a pulsin accurately whilst servo pulses are running in the background. This is because the servo processing will disrupt the accuracy of pulsin - the PICAXE cannot look for a change in the pulsin pin status and generate the pulses at exactly the same time.

Servo description in part 2 of the manual does already state it will have timing overhead effects on other commands.
 

rnoguera

New Member
The manual says:

"Servo cannot be used at the same time as timer or pwmout/hpwm as they share a common internal timer resource."

It does not say it is incompatible with pulsein.

"Note that the overhead processing time required for processing the servo commands every 20ms causes the other commands to be slightly extended i.e. a pause command will take slightly longer than expected."

I would not call pseudo-randomly disturbing pulsein readings by 20% a "slight extension"...

The software or the manual need a fix. It would be much better if it is the software.

Don't misunderstand me: I really like the product!!
 

hippy

Technical Support
Staff member
A 20% disturbance to a 1.75mS pulse will be about 300uS; that seems to be in the right ballpark for how long background processing could take, although that's a guesstimate.

The command does only take slightly longer, but the result of that is more drastic. I don't think it's going to be possible to fix it in software so it can only be documented. There is also the software work round using PULSIN and PULSOUT.

One thing you could try is -

- PulsIn IN_PIN, ?, var
- Pause 10
- Servo OUT_PIN, var
- Do
--- PulsIn IN_PIN, ?, var
--- ServoPos OUT_PIN, var
- Loop

That may offset the SERVO update timing away from the incoming pulse stream timing, enough to remove or minimise the problem, although it could creep back in over time.

It is also possible to re-synch the loop above every so often, and/or average the incoming pulse so the occasional error doesn't get through ...

- Do
--- PuslsIn IN_PIN, ?, var
--- If counter = 0 Then
----- Pause 10
----- Servo OUT_PIN, var
--- Else
----- ServoPos OUT_PIN, var
--- End If
--- counter = counter + 1
- Loop
 

rnoguera

New Member
I am using this:


main:

pulsin 3,1,PW_in

PW_out = PW_in

pulsout 2,PW_out
goto main:

And it works really fine. I still do not figure why (In fact my program is much longer, and I do some calculations to get PW_out from PW_in, but is not relevant here).

Surprisingly it is not influenced by the lenght or the program or its complexity. The servo turns smoothly and without any vibration.

This is the complete program:

'Radio control servo turn range enhacement using PICAXE-08M
'Gets almost 180º turn from a standard RC emmiter-receiver that would turn only 90º
'Can detect lose of radio signal and stop servo when it happens


'PICAXE-08M connections are:
' Leg 1 +5V
' Leg 2 (Serial in) Tie to Ground through 10K resistor
' Leg 3 (In4/Out4) Wiper of 10K potentiometer connected to +5V and ground
' Leg 4 (In3) Pulse input from radio receiver
' Leg 5 (In2/Out2) Pulse output to servo
' Leg 6 (In1/Out1) Mode indicator output
'ON - Starting
'Steady off - Running
'Flicker - Radio Signal is lost
' Leg 7 (Out0/Serial Out) NC
' Leg 8 Ground


symbol PW_in=w0 'Signal from radio / Senyal procedent de la radio
symbol PW_out=b2 'Signal to servo / Senyal enviada al servo
symbol PW_prev=b3 'Signal to servo (previous scan)/ Senyal enviada al servo durant el cicle anterior
symbol PW_clc=b4 'Signal to servo (calculated) Senyal calculada
symbol PW_in_prev=b5 'Signal from radio (previous scan) / Senyal rebuda de la radio en el cicle anterior
symbol PW_in_inc=b6 'Change in signal from radio since / Canvi en la senyal de radio entre cicles succesius
symbol PW_ok = b7 'Number of correct signals reciv. from radio / Polsos aparentment correctes


'This section initializes the system...
initialize:

output 1 'Configure I/O / Configurar entrades sortides

output 2

input 3

high 1 'Turn LED ON / Encendre LED per indicar arranc

Pause 3000 'Wait 3 second initially

low 1 'Turn LED OFF / Apagar LED



'This section deals with normal operation
main:

pulsin 3,1,PW_in 'Read signal from radio / Mesurar la senyal procedent de la radio

PW_in_inc = PW_in_prev - PW_in 'Evaluate change in radio signal since previous scan / Canvi de la senyal de radio entre cicles successius

retorn:

'If change is too high, we assume radio signal is lost..

if PW_in_inc > 25 and PW_in_inc < 231 then goto perdua_senyal:


'If value is unreasonable, we assume radio signal is lost..
if PW_in < 90 and PW_in > 214 then goto perdua_senyal:


PW_in_prev = PW_in 'Store signal from radio / Memoritzar senyal de radio en el cicle

PW_clc = Pw_in *216/100-184 'Calculate output to servo / Calcular la senyal de sortida al servo

PW_out = 3* PW_prev + PW_clc /4 'Average output signal to filter noise / Promitjar la senyal calculada amb les senyals anterior

PW_out = PW_out MIN 60 'Limit output signal according to servo limits / Limitar senyal de sortida

PW_out = PW_out MAX 235

pulsout 2,PW_out 'Send output signal to servo / Enviar la nova senyal

PW_prev=PW_out 'Store output signal / Guardar el valor enviat

goto main: 'Restart loop / Reiniciar el bucle



'This section deals with lost signals
perdua_senyal:

high 1 'Turn LED ON / Encendre LED

Pause 20 'Wait 20 ms

low 1 'Turn LED OFF / Apagar LED

pause 20 'Wait 20 ms

pulsin 3,1,PW_in 'Read signal from radio / Mesurar la senyal procedent de la radio

PW_in_inc = PW_in_prev - PW_in ''Evaluate change in radio signal since previous scan / Canvi de la senyal de radio entre cicles successius


PW_in_prev = PW_in 'Store signal from radio / Memoritzar senyal de radio en el cicle


'If change in signal from radio is reasonable (low) we increase the counter off correct signals...
if PW_in_inc < 10 or PW_in_inc > 246 and PW_in<>0 then

PW_ok = PW_ok +1

else

PW_ok =0

end if



'When 30 correct signals have been received, normal operation is restored...
if PW_ok = 30 then

goto retorn:

endif



goto perdua_senyal:
 

rnoguera

New Member
For me it's unclear why I don't have to care about the time between consecutive pulses.

In my program you will see not pause or other command to achieve a certain delay between consecutive pulseout commands, and however the program works well.

I suspect it works because of the RC receiver. It is sending the signals at the correct pace (let's say every 20ms). The 08M waits for the signal (pulsein) then sends its own signal (pulseout) at the same pace...
 

BeanieBots

Moderator
You suspect correctly.
Pulsin will WAIT until the pulse has been received. Your code then has 20mS to do everything it needs to do (including the duration of the pulsout) before it must initialise another pulsin command to get the next pulse.
Hence, your pulsouts are spaced at the same rate as the incomming pulses.

Also, most servos are not particularly fussy about the 20mS frame rate.
Most will work quite happily anywhere between about 5mS and 30mS.
Try it. The outout torque of a Hitec servo will be much greater with a frame rate of 5mS. Alternatively, it will still work but be weak and sloppy with a frame rate of 30mS.
 
Top