PDA

View Full Version : (Picbasic Pro Compiler) Distinct management of two servos per timer(s) of 12F675?



zorgloub
- 26th May 2023, 16:39
Hello to the community

I used to control my servos with a PIC12F675 (internal 4MHz clock) with the PULSOUT command, renewed every 20 msec.

However, I could see, with a logical analyzer, that the impulses were not always perfectly identical, thus causing a "dusy" of the servos.

I could then hear that it was possible to control these servos using the Timer(s) of the PIC ... (?)
Unfortunately, I know nothing about the use of these timer(s) and their configurations to use in PBP.

Could a good soul provide me with a small explanatory program incorporating the elementary commands of configurations of the timer(s) to distinctly pilot two servos?

I will assimilate it and then use it by completing it with my needs.

Thank you in advance for your help.

(User: PicBasicPro Compiler V:2.40)

Acetronics2
- 27th May 2023, 09:11
Salut Zorglub, salue Champignac pour moi ... ce pseudo ne m'est pas totalement inconnu :D

The question is ...
what happens is quite predictable ... BUT, having a look to your code and knowing your application goal could lead to an adequate answer ... or better, closer solution.

Alain

Sherbrook
- 28th May 2023, 18:58
I would do it like this. Change PULSE_PIN for the second channel
Code not tested but should give some idea how to use timer 1




PIE.0 = 1 'Enable timer 1 overflow flag
T1CON = 0 'Timer 1 stopped, internal clock
PULSE_OUT VAR WORD

START:
PULSE_OUT = 1500 'Servo center value
PULSE_OUT = $FFFF - PULSE_OUT 'Timer 1 counts upwards
TMR1H = PULSE_OUT.HIGHBYTE
TMR1L = PULSE_OUT.LOWBYTE 'Set timer 1
PULSE_PIN_1 = 1 'Start pulse
T1CON.0 = 1 'Start timer 1
WHILE PIE1.0 = 0 : WEND 'Wait for Timer 1 overflow
PULSE_PIN_1 = 0 'End pulse
T1CON.0 = 0 'Stop timer 1
PIE1.0 = 0 'Reset overflow flag
'wait 20ms .............................
GOTO START

zorgloub
- 31st May 2023, 10:08
Bien le bonjour Alain et... salutations de Champignac ;)


Hello Alain.
My application is to read two servo control signals from a classic radio -controlled receiver.
These signals are "processed" and redirected to two outings.

We find, on these outputs, the inverted signals, or not, depending on various possible combinations.

A configuration jumper allows three possible modes (recorded in EEPROM).

I note that for a condition giving a defined pulsout of 1 or 2 msec the servo is stable.
But for pulsout "copying" or reversing the Pulsin values to outputs, they are no longer stable!
It is therefore not the inversion calculation (3msec - Pulsin) which induces the problem. (Look at the PRG0 which does not include a reversal calculation but just a "copy" of the pulsin value.)

Finally, note that if I connect the servos directly to the receivers' outputs, they remain stable. We cannot therefore incriminate an instability of the signals from the receiver!

Thank you for your ideas.

CODE: (PBP v2.4)



'************************************************* *********************
' Programme: Test1.PBP (c)RGL 05-2023
' ---> PIC 12F675
'************************************************* **********************
' PicBasicPro Compiler V:2.40 448 Words compiled

'*** PIC Pinouts/Specifications
'------------------------------------
' PIC 12F675 (Internal 4Mhz Osc) DEFINE OSC =Default.Value =4
' __ __
' Vcc +| U |- Gnd
' (nc)GP5 x| |< GP0 _Config Jumper
' _Horizontal GP4 >| |> GP1 _Servo1
' _Vertical GP3 >| |> GP2 _Servo2
' -----
@ Device Pic12F675, intrc_osc, wdt_off, pwrt_on, mclr_off, protect_off

DEFINE OSC 4 '10us Pulsout Unit: 100 = 1ms @4Mhz
' 1ms Pause Unit : 1 = 1ms @4Mhz

CMCON = 7 'Comparator Off
ANSEL = 0 'Set Port to Digital i/o
ADCON0 = 0 'All ADC Off

TRISIO = %011001 ; 1=Input
GPIO = %00 ; Make all pins 0 except input pins

OSCCAL = $38 ' $38=56d ' Also to be manually placed at 3FF Address (3438)!

'*** In/Out pins ..................................................
'GND pin8 - Gnd
Input 0 : Symbol _Config = GPIO.0 'Jumper pin7 - In0
Output 1 : Symbol _Servo1 = GPIO.1 'Servo1 pin6 - Out1
Output 2 : Symbol _Servo2 = GPIO.2 'Servo2 pin5 - Out2
Input 3 : Symbol _Vertical = GPIO.3 'Vertical Position pin4 - In3
Input 4 : Symbol _Horizontal = GPIO.4 'Horizontal Position pin3 - In4
;Input 5 : Symbol _xxx = GPIO.5 pin2 - nc
' 'Vcc pin1 - Vcc
'*** Variables

Menu VAR byte 'Program Mode index (0 to 3)
Vertical VAR word 'Vertical Position
Horizontal VAR word 'Horizontal Position
RVertical VAR word 'Reverse Vertical Position Value (3msec - Vertical)
RHorizontal VAR word 'Reverse Horizontal Value (3msec - Horizontal)

'=== MAIN PROGRAM ================================================== ===
Init:
READ 0, Menu 'Read Eeprom when starting (Check Mode Prg)

MainLoop:

IF _Config=0 Then Gosub MODE ' Enabled Mode Configuration Jumper

Pulsin _Vertical,1,Vertical 'Read Vertical Position
gosub Tri
Pulsin _Horizontal,1,Horizontal 'Read Horizontal Position
gosub Tri

Goto MainLoop

'================================================= ======================
' --- SUBROUTINES ---
'================================================= ======================
TRI:

Select Case Menu
Case 0
Gosub PRG0
Case 1
Gosub PRG1
Case 2
Gosub PRG2
End Select
Return
'-------------------------------------------------
MODE: 'Change and memorize the 3 Menu Program index (0 to 2)

Menu = Menu + 1
IF Menu > 2 Then Menu = 0
WRITE 0,Menu 'Write to Eeprom @Addr.0 the menu index
PAuse 1000 '1 sec
Return
'-------------------------------------------------
PRG0:

IF Horizontal <120 then '1ms (<1,2ms)
Pulsout _Servo1,Horizontal
Pulsout _Servo2,Vertical
Else
IF Horizontal < 170 then '1,5ms (<1,7ms)
Pulsout _Servo1,100 '1ms
Pulsout _Servo2,100 '1ms
Else '2ms (>=1,7ms)
Pulsout _Servo1,Vertical
Pulsout _Servo2,Horizontal
Endif
Endif
Return
'-------------------------------------------------
PRG1:

RHorizontal = 300-Horizontal 'Reverse Horizontal Pösition (3msec-Horizontal)

IF Horizontal <120 then '1ms (<1,2ms)
Pulsout _Servo1,RHorizontal
Pulsout _Servo2,Vertical
Else
IF Horizontal < 170 then '1,5ms (<1,7ms)
Pulsout _Servo1,200 '2ms
Pulsout _Servo2,100 '1ms
Else '2ms (>=1,7ms)
Pulsout _Servo1,Vertical
Pulsout _Servo2,Horizontal
Endif
Endif
Return
'-------------------------------------------------
PRG2:

RHorizontal = 300-Horizontal 'Reverse Horizontal Pösition (3msec-Horizontal)
RVertical = 300-Vertical 'Reverse Vertical Position (3msec-Vertical)

IF Horizontal <120 then '1ms (<1,2ms)
Pulsout _Servo1,Horizontal
Pulsout _Servo2,Vertical
Else
IF Horizontal < 170 then '1,5ms (<1,7ms)
Pulsout _Servo1,100 '1ms
Pulsout _Servo2,200 '2ms
Else '2ms (>=1,7ms)
Pulsout _Servo1,RVertical
Pulsout _Servo2,RHorizontal
Endif
Endif
Return
'-------------------------------------------------

'==== END OF PRG ===============================================

Acetronics2
- 31st May 2023, 12:27
Hi,

I have a big problem with your program ...

What is it intended to do ??? ( Which inputs give which Outputs ??? ) or explain what you try to drive.

Understand by that there's a problem related to input signals use ...

try to write it down clearly on a paper ... there's some " overkill " in your program ... and it can be much, much, much simplified ...

Alain

zorgloub
- 15th June 2023, 12:01
Hi Alan.
Thanks for your interest in my question.

This is a system used in RC sailing to control the genoa of a radio-controlled sailboat.

The sail is controlled by two winches.

The RC Transmitter has :
1) a three-position switch which determines the side on which the sail will be set (port or starboard).
If the switch is in the middle position, the two winches each take up a specific end-of-travel position to make it easier to move the sail from one side of the boat to the other.
2) A potentiometer that adjusts the tension of the sail's sheet.


Finally, a small configuration jumper allows 4 different winch positioning configurations depending on how the sheeting circuits have been designed in the boat.

A more detailed explanation can be found in the appendix.

Note: In fact, the logic of my programme seems OK and the different servo winch positions obtained are correct. I just have a little instability in the positioning of the servos, which 'wiggle' a bit.

9452

Acetronics2
- 16th June 2023, 07:25
Hello,

just one thing missing : which radio model and which channels are you using ( to know if the two channels used follow each other or if there's a time lag between them ... )

zorgloub
- 16th June 2023, 08:57
Hi Alan,
The two channels follow each other:

Switch on channel 7 and potentiometer on channel 8.
The radio is a classic Robbe Promars.
Thanks.

Acetronics2
- 18th June 2023, 09:06
Hi Alan,
The two channels follow each other:

Switch on channel 7 and potentiometer on channel 8.
The radio is a classic Robbe Promars.
Thanks.

Ok, got it ...

PBP can't read two channels following each other without missing the second one ... because PULSIN needs some little " re-arming " time ... ( I make it short )

so, we must use a turnaround to achieve that ...



PULSIN _Vertical,1,Vertical
RCTIME _Horizontal,1,Horizontal


a look to the manual will give you the reason why ...

and may be 1 unit @ 4 Mhz to add to Horizontal to be straight true ... ;)

So, here, you have to re-write your sampling part without any command between the two channels signals sampling commands ...

Alain

zorgloub
- 19th June 2023, 21:56
Hi Alain,
Thanks
OK, I'll take it all back after the holidays ;)

zorgloub
- 13th July 2023, 14:12
Hi alan, here I am back from vacation :(
I read your explanation relating to the "rearming-time" of two successive Pulsin.
Note that here there is code between these two Pulsin (first the Gosub Tri, then the Gosub PRG n).
These are not directly successive pulsin commands and this notion of "rearming-time" does not seem to explain the problem.

tumbleweed
- 13th July 2023, 17:58
The two PULSIN commands in the main loop are going to add jitter to the pulse output timing since the time is going to vary depending on the input pulse times.

zorgloub
- 14th July 2023, 16:25
Apart from this problem, I did positioning stability tests with several servos.
If a servo is not stable, it is because the value of the pulses is not constant.
If the gap between these impulses (which is normally 20 msec) is not constant, this does not influence the servo at all!
I did a test by alternating 20msec differences and 40 sec while sending constant pulses: the servo remains stable.