Model Train controller - revisited


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1
    malc-c's Avatar
    malc-c Guest

    Default Model Train controller - revisited

    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

    Code:
    '****************************************************************
    '*  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 ?
    Attached Images Attached Images  
    Last edited by malc-c; - 22nd April 2007 at 15:23.

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    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.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink simple way ...

    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
    Last edited by Acetronics2; - 22nd April 2007 at 18:27.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    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
    Code:
    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

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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

    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..
    Last edited by mister_e; - 22nd April 2007 at 18:30.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default getting there

    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
    Attached Images Attached Images  

  7. #7
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by malc-c; - 22nd April 2007 at 20:00.

Similar Threads

  1. Microstepper controller
    By aratti in forum Schematics
    Replies: 14
    Last Post: - 3rd January 2015, 16:52
  2. problem with the GSM controller
    By Dariolo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th May 2009, 20:33
  3. Graphics controller for the Sitronix ST7549
    By ttease in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 26th May 2007, 21:45
  4. Recommendations - Model Train Controller
    By malc-c in forum mel PIC BASIC Pro
    Replies: 101
    Last Post: - 8th March 2007, 08:17
  5. Dedicated LCD Controller question
    By chuckles in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th February 2006, 14:44

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts