PDA

View Full Version : Model Train controller - revisited



malc-c
- 22nd April 2007, 15:19
Guys, a while ago Darrel and Steve (and a few others) helped me out with the development of a PWM pic based train controller. However although the prototype worked fine on the breadboard, when I re-designed the board to include the powersupplies (5v and 12v) I got some strange behaviour from the resulting PCB. Sometimes the PWM would stop and the PIC needed resetting, but I put this down to the noise these models cause (RF etc) and I also noted that whilst the slow speed was exceptional, the range of control was very fine, so I went back to the breadboard.

I've breadboarded the schematic attached, but with the 20 Mhz xtal. The code in the PIC is as follows



'************************************************* ***************
'* Name : Twin Track Controller.BAS *
'* Author : M. Crabbe *
'* Notice : Copyright (c) 2007 M. Crabbe *
'* : All Rights Reserved *
'* Date : 17/02/2007 *
'* Version : 1.0 *
'* Notes : 16F767 using 20 Mhz xtal *
'* : *
'************************************************* ***************



@ __CONFIG _HS_OSC & _WDT_ON & _PWRTE_ON & _MCLRE_OFF

DEFINE OSC 20

ANSEL = %00010111 ' Set which ports are digital and which are analogue
CMCON=7 ' Turm Comparitors off
TRISA=%11111111 ' set PORTA as all input
TRISC=%00000001 ' set PORTC as all output apart from RC0

DEFINE ADC_BITS 8 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 2 ' ADC clock source (Fosc/32)
DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)

INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "SPWM_INT.bas" ' Software PWM module

DEFINE SPWM_FREQ 150 ' SPWM Frequency (Hz)
DEFINE SPWM_RES 254 ' SPWM Resolution (0 - 255)

DutyVar VAR byte[4] ' Array used to store ADC [channels]

ASM
SPWM_LIST macro ; Define Pin's to use for SPWM
SPWM_PIN PORTC, 2, _DutyVar ; and the associated DutyCycle variables
SPWM_PIN PORTC, 3, _DutyVar+1 ; (Additional channels DutyVar+2, +3 etc)
endm
SPWM_INIT SPWM_LIST ; Initialize the Pins
ENDASM

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, SPWMhandler, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

Main:
ADCIN 0, DUTYVAR[0] ; if having more channels add ADCIN 2, DUTYVAR[2] etc
ADCIN 1, DUTYVAR[1]
GOTO Main ; program simply loops round reading ADC and sending corresponding PWM out


The result of this code can be seen in the avi (4MB DIVX) http://www.micro-heli.co.uk/scope2.avi which you will see and hear is a good variable square wave. This drives the 2N3904 and darlington pair well and the resulting in fantastic slow speed crawl as can be seen in the avi http://www.micro-heli.co.uk/controller.avi

However, the amount of rotation of the pot is quite small before the loco is up to a medium speed. I've tried changing the pot for 100K, 22K and even 2k, and I've played about with different values for R1, R2 and R3 and it still makes no difference.

So my question... Is there a way to "scale" or range the ADC so that the PWM performs is less proportional, and more log like in action. Ie instead of the pulse being 50% when the pot is 50% travel (and presumably the value is 127 on the ADC) its say 25%, but when the pot is at 75% travel the value is 75% if you follow my drift ? This way it will provide better slow motion control and still give excellenf high speed performance as well.

Also, what would be the best way to protect the PIC from interfearence, placing 0.1uf caps between inputs and gnd or simply take all unsused pins to gnd ?

skimask
- 22nd April 2007, 17:39
However, the amount of rotation of the pot is quite small before the loco is up to a medium speed. I've tried changing the pot for 100K, 22K and even 2k, and I've played about with different values for R1, R2 and R3 and it still makes no difference.

Are you using a linear pot or a 'log' ('audio') pot? I've been tripped up by those before, thought I had a linear type, ended up being a log type.


Is there a way to "scale" or range the ADC so that the PWM performs is less proportional, and more log like in action

Maybe use PBP's built in SIN function. It won't be LOG-like, but if you use the first 90 degrees...upside down (if that makes any sense) and a bit of programming, at least it won't be a linear function and should be a bit smoother at the low end.


Also, what would be the best way to protect the PIC from interfearence, placing 0.1uf caps between inputs and gnd or simply take all unsused pins to gnd ?

Pulling all unused inputs to either Vdd or Vdd is never a bad idea.
Putting .1uf across all the inputs might 'slow down' the inputs, you might miss a pulse for something here and there. Maybe you need more than .1uf across Vdd & Vss on your PIC, 'cause the .1uf tends to only decouple one frequency range, generally the higher ones. Maybe you need to add a big 'ol 10uf or something in parallel with that .1uf, to help get some of the lower frequencies. And maybe make sure you've got good quality cap's doing that job, low ESR types.

Acetronics2
- 22nd April 2007, 18:04
Hi, Malc

Try this:

TEMP var WORD

Main:

ADCIN 0, DUTYVAR[0] ; if having more channels add ADCIN 2, DUTYVAR[2] etc

TEMP = DUTYVAR[0] * DUTYVAR[0] / 256 '********************

IF DUTYVAR[0] < 10 THEN ' 10 = value for a neat motor start , if needed !

DUTYVAR[0] = 0
GOTO Jump0

ENDIF

DUTYVAR[0] = TEMP MAX 10

Jump0:


ADCIN 1, DUTYVAR[1]

TEMP = DUTYVAR[1] * DUTYVAR[1] / 256 '********************

IF DUTYVAR[1] < 10 THEN ' 10 = value for a neat motor start ,if needed !


DUTYVAR[1] = 0
GOTO Jump1

ENDIF

DUTYVAR[1] = TEMP MAX 10

Jump1:

GOTO Main ; program simply loops round reading ADC and sending corresponding PWM out

That what I use for my boat speed controllers ... works really fine !!!

Regards
Alain

PS: here is the original

'************************************************* *****************************
'Mise à l'echelle 0 - 255 de la sortie PWM pour 0 - 96 % de la course
'************************************************* *****************************

' 265 = 255 / 0.96 ... CQFD

'************************************************* *****************************
'Moteur Gauche
'************************************************* *****************************

EcartGazG = ( ABS ( PulsGazG - MidGaz)) ' for fwd/rev on the same stick

IF EcartGazG < 55 THEN 'Center Deadband
PowerG = 0
GOTO MotD
ENDIF

dummy = EcartGazG * 265
PowerG = DIV32 CourseGaz

PowerG = (PowerG + 1 ) MIN 255 ' 255 value as MAXI for PWM

'************************************************* *****************************
'Variation exponentielle
'************************************************* *****************************

dummy = PowerG * PowerG
PowerG = DIV32 255

'************************************************* *****************************
'Puissance mini appliquee au moteur - démarrage franc ( 20/255 )
'************************************************* *****************************

PowerG = PowerG MAX 20

malc-c
- 22nd April 2007, 18:27
Guys, thanks for the replies.

To answer some of the questions, yes the pots are lin, or at least they are marked as lin and purchased as such, and using a DVM it seem that they are liniar. I'll look at taking all the unused pins to +ve, possibly via 10k resistors.

Alain,

I think that the original code above basically works on the same principle of taking the 0 - 254 from the adc and sending that out as the pulse. However it all got a bit complicated when Darrel's routines were used as all I know is that i need to do is change then two values


DEFINE SPWM_FREQ 150 ' SPWM Frequency (Hz)
DEFINE SPWM_RES 254 ' SPWM Resolution (0 - 255)


I'll try adding your code and will report back

mister_e
- 22nd April 2007, 18:27
The LOG pot is a nice idea, Alain's routine too. You can also build a huge LOOKUP table with excel and read from it. If you have some VCA (like ssm2164) you can use a Linear pot and get a LOG curve.

Many different ways.

At least, there's some progress :D

PS: you can trick the curve a little bit by adding a resistor in parrallel with the wiper... far to be LOG, but could be interesting to trace the curve...

THE PWM freq shouldn't affect the results... shouldn't..

malc-c
- 22nd April 2007, 19:20
Well it compiles Ok and it does indeed work, providing a better range of travel for the lower end of the POT. It produces an interesting wave form :)

The only problem is that oscilatons produce noise in the loco's armature. I tried the old trick of using a capacitor across the output which worked to some degree, but as it effectivly smoothed the pulses it defeats the object of using PWM (or PPM)

Steve, I'll try your suggestion

malc-c
- 22nd April 2007, 19:33
Oh and one other thing I've noticed,which may be causingn the PIC to bomb is when the when the wiper is at the top and thus connected to the 5.21v supply the pulses stop and the voltage out drops, and on some occasions this has caused the driver to sort of lock as moving the pot after the effect has no effect as the PMW generation has stopped. Is there a way of setting the voltage ref or ADC value so that a PMW signal is always produced ?

Edit:
maybe this video might help explain (2.2 MB Divx format) http://www.micro-heli.co.uk/scope4.avi

mister_e
- 22nd April 2007, 20:24
The weird results you have is probably caused by the delay to do the calc... well i think.

Don't try the Resistor... i was 100% wrong.. it's not going to be LOG... just alog... as per i got from EXCEL... so just worst than before

<img SRC="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1557&stc=1&d=1177269772">

oops, it's sunday :D

...mmm something to do anyway with that... scratch scratch...

mister_e
- 22nd April 2007, 20:26
What happen to your PWM lock-up thingy if you replace the Motor by a resistor? I don't see any back EMF diode here...

EDIT: check the maximum range of your scalling maths ;)

EDIT2: well it's really sunday !!

However, the amount of rotation of the pot is quite small before the loco is up to a medium speed.
so if i'm right... the curve above should work?

do i need to sleep or get another brain? I've attach my noobie-style excel sheet.

Just play with the Value in D1 cell and enjoy.

malc-c
- 22nd April 2007, 21:31
Hi again Steve,

The EMF feedback was removed following a suggestion from Alain in the original thread ( http://www.picbasic.co.uk/forum/showpost.php?p=32700&postcount=46 )

Could you elaborate on the scaling thing...

I'll have a play with the speadsheet and add a few resistors in parallel to see what happens. It will probably won't be tonight as I don't have access to the loco anymore (It shot off the end of the test track and now boxed up as it is in need of repair, and my son has gone to bed now so I can't get one of the other locos !)

Sorry guys for going over this again, but I still cant get my head round some of these aspects of programming.

malc-c
- 22nd April 2007, 21:43
Well sticking a 1K in parallel with the wiper and gnd does show a great improvement on the scope trace... will just have to wait to see how it performs with a loco

mister_e
- 22nd April 2007, 21:52
o.k. i see, from your previous schematic the Back emf diode where installed in a weird way, maybe why Alain suggested that. The diode should be in parallel with your motor. Like when you drive a Relay. It's not a bad thing to install a capacitor as well.

Do the calc of Alain's routine with some ADC value. The results have to be bellow 255 or at very least bellow SPWM_RES value... i didn't did it here, but just for safety sake you should check that.

mister_e
- 22nd April 2007, 21:54
Well sticking a 1K in parallel with the wiper and gnd does show a great improvement on the scope trace... will just have to wait to see how it performs with a loco

Woohoo! Sweet! now depending what you need, 2-3K curve seems nice to me... but i'm not the guy who drive this loco :D

Acetronics2
- 25th April 2007, 15:16
Hi, Malc

I do not know wich pic you really use ... but if it is the same as in your Listing ( 16F767)

you should have a look to Post #8899 from Bruce ... ( Jul 2003 )

Alain

malc-c
- 25th April 2007, 17:36
Hi, Malc

I do not know wich pic you really use ... but if it is the same as in your Listing ( 16F767)

you should have a look to Post #8899 from Bruce ... ( Jul 2003 )

Alain

Well I've searched for posts older than a year from Bruce and the stupid search engine still brings up posts dated 2007 !

Any chance of a direct link as I amd using that PIC

Malc

mister_e
- 25th April 2007, 17:39
i think it's that one
http://www.picbasic.co.uk/forum/showthread.php?t=1770&highlight=16f767

but it's may 2005 :(

anyways, by searching with post#... you'll redirected there
http://www.picbasic.co.uk/forum/showpost.php?p=8899

which is the same ;)

Acetronics2
- 25th April 2007, 17:41
i think it's that one
http://www.picbasic.co.uk/forum/showthread.php?t=1770&highlight=16f767

but it's 2005 :(

Hi, Steve

Ok, You're right !!! 2003 is my join year ...

don't you think 3 nice working channels are better than 2 " not so good " ???


But I still do not understand why Malc wants to read its pots @ 150 Hz ...
may be timing issues he gets come from here !!!

Alain

Alain

mister_e
- 25th April 2007, 17:48
From what i remind 150Hz is less noisy for those motors. And it's also a kind of limitation of the SSPWM as well.

Sure, the noise you hear from your motor is the PWM frequency. Depending the accuracy you need and the OSC you're the maximum PWM frequency should be in the 'yes i can hear that frequency' range ;)

PicMultiCalc came handy... @8MHZ 30KHz PWM give 9 bit accuracy (well 0-267). Question is... how the motor will react to this... i worry a little bit about the output transistor efficiency already in use for that frequency range. Maybe good enough. Don't remind the whole specs.

Acetronics2
- 25th April 2007, 17:57
Heuuuuu

Something really, really, really, stupid ...

is it really necessary to have such a high oscillator freq ??? PWM freq would follow ( with HPWM use ... of course !!! )

@ 4Mhz, it gives 245 Hz as a mini ... that's bearable !!!

as there are no time dependant issues ... could use a different XTAL than declared !!! ( 1, 2 or 2.5 Mhz ... from stock ! )

Alain

mister_e
- 25th April 2007, 18:14
Who knows... what i wanted to point-out is around the noise you can hear from the motor. 30KHz... who can hear it here ;) but for sure, the whole thing may not work at all... or not as expected as such high frequency... a little bit why i already suggested something around a linear voltage regulation instead of PWM in the original thread 'Recommendations - Model train controller'
http://www.picbasic.co.uk/forum/showpost.php?p=32881&postcount=94

in 250 Hz range PWM, it should work...BTW, let's see what will happen.

malc-c
- 25th April 2007, 18:58
Glug.. help ! glug glug... I'm drowning...... this is way over my head :)

As far as I recall Darrels routines are software PWM and not Hardware ? However having said that I have not tried all channels at the same time to control two independant tracks.....

On the subject of frequencies... when using a 4 Mhz xtal the rate at which the pulses are sent is quite noticeable at slow speed as each pulse is sent causes the model to "jump" at each step. Using a 20Mhz Xtal this is less noticable. When we were trying different settings, higher frequencies (estimated at around a few Khz ) were produced and the armature sang as the model made its way down the track.

Steve hit on the subject of a frequency higher than the ear can hear, but I'm wondering if these pulses were sent at say 30Khz, the loco would start to see them as almost a constant voltage level. Its a bit irrelevent as under the settings in Darrels code, I can't get a frequency higher than 260 hz with two channels when running the 16f767 with a 20 mhz xtal.

mister_e
- 25th April 2007, 19:04
As i said, i worry about such high frequency for this type of motor which are usually drive in 'pure-dc'. Maybe something to try... but for long term... i really can't say what could happen. One more reason why i posted a possible solution schematic... kind of linear regulation, sort of.

But first, have all your loco available and try with <300Hz. HPWM and/or SSPWM.

malc-c
- 25th April 2007, 19:28
The only thing I couldn't work out with your op amp schematic, is where the power for the motor comes from as it shows the supply connected across the motor

Smoothing capacitors across the output helps with the harshness of the PWM, and I'll try using a diode in parallel with the motor, but won't that effectively short the output to gnd ?

mister_e
- 25th April 2007, 20:03
Oh yeah, my mistake.. anyways, the power is suppose to be attach to the Op-amp and transistor Collector. This work, but it may (will) heat much.

Acetronics2
- 26th April 2007, 07:53
A look to µChip AN 1074 ... ??

Alain

Acetronics2
- 1st May 2007, 18:21
Hi, Malc

I tried to compile your project and get a strange error at compiling : PIR2 not found ...

which is the processor you use ? 767 or 676 ???
and is the posted "twin track controller" your source ???

the SPWM might run properly ...

Alain

malc-c
- 1st May 2007, 21:03
Hi,

I'm using the 16F676 - this is the code that I have (I think its the same as per the 1st post in this thread)



@ __CONFIG _HS_OSC & _WDT_ON & _PWRTE_ON & _MCLRE_OFF

DEFINE OSC 20

ANSEL = %00010111 ' Set which ports are digital and which are analogue
CMCON=7 ' Turm Comparitors off
TRISA=%11111111 ' set PORTA as all input
TRISC=%00000001 ' set PORTC as all output apart from RC0

DEFINE ADC_BITS 8 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 2 ' ADC clock source (Fosc/32)
DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)

INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "SPWM_INT.bas" ' Software PWM module

DEFINE SPWM_FREQ 150 ' SPWM Frequency (Hz)
DEFINE SPWM_RES 254 ' SPWM Resolution (0 - 255)

DutyVar VAR byte[4] ' Array used to store ADC [channels]

ASM
SPWM_LIST macro ; Define Pin's to use for SPWM
SPWM_PIN PORTC, 2, _DutyVar ; and the associated DutyCycle variables
SPWM_PIN PORTC, 3, _DutyVar+1 ; (Additional channels DutyVar+2, +3 etc)
endm
SPWM_INIT SPWM_LIST ; Initialize the Pins
ENDASM

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, SPWMhandler, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts

Main:
ADCIN 0, DUTYVAR[0] ; if having more channels add ADCIN 2, DUTYVAR[2] etc
ADCIN 1, DUTYVAR[1]
GOTO Main ; program simply loops round reading ADC and sending corresponding PWM out


Compiles fine here

malc-c
- 1st May 2007, 21:05
Opps.. just noticed in the 1st post I have the wrong pic stated in the header of the code - Sorry !

Acetronics2
- 4th May 2007, 09:18
Hi, Malc

Sorry to sink your dreams ...

http://www.electroniquepratique.com/couve.php?id=43

The very bottom item ...

Alain

PS: its This Month issue !!!

malc-c
- 5th May 2007, 10:38
Don't you juts love bablefish !





Regulator for railway model making
Ci to be downloaded: 32 MODFERCI.zip

If it is a field where electronics contributes an interesting share, it is well in model making. To our readers amateurs of railway circuits, we propose the realization of this regulator intended for the piloting of the machines of traction.



Extract of L’article page 32 of the Practical number 316’D Electronic:

We deliberately chose an operation close to reality. The rise to power as well as deceleration are extremely progressive by the placement of fifteen stages visualized by two-tone leds. The inversion of the direction of walk can be carried out only one time the stopped convoy. Lastly, this control panel also comprises a button of emergency braking...

Oh well.... just goes to show how good my concept was... :)

malc-c
- 7th May 2007, 22:54
Well to finally put this one to bed, I've found that in the real world the practical aspect of using the PIC in this environment, is not without issues. The main problem has been with the reliability of the PIC. Whilst the control at slow speed can't really be beaten even with commercial controllers, the PIC often falls over and stops generating the PWM signals and requires the power to be disconnected and re-connected. I'm not sure what causes this, but would suspect that its something to do with the noise that model railways generate, or the back emf or something.

The concept was great, I've gained a lot of information and found some nice new usefull routines and had fun playing with these circuits and code

how's that saying go... back to the drawing board ;)

malc-c
- 7th May 2007, 23:01
Hi, Malc

Sorry to sink your dreams ...

http://www.electroniquepratique.com/couve.php?id=43

The very bottom item ...

Alain

PS: its This Month issue !!!

Strange how the image is missing from that page, and the zip file gives a 404 error... maybe they have problems like I've experienced :) :D

Acetronics2
- 8th May 2007, 09:40
Hi, Malc

After this thread, I decided to make a single channel train controller ... for my sweet Daddy ( 85' y.o. in Sept. ... hé,hé !!! )

Drive will be in auto or semi-auto mode, with all whistles and bells ( w/ Brake, emergency, soft accel/decel ... etc, etc. ), so I need a 28 Pins chip for all indicators and pots !!!

a 16F87x @ 4 Mhz or less will perfectly do the job ...

I'll ring you when ready ...

Alain