Menu

odd behaviour from pwm after program or power up then all works fine!

Help
2015-04-24
2015-04-30
  • tony golding

    tony golding - 2015-04-24

    i have spent the last couple of days/nights stripping away pieces of code and trying to find the cause of my issue but have to admit defeat and ask if a different pair of eyes can help me find out the problem.

    my current project is measuring back emf from a motor and if needed have it set to run on cruise control so to speak.

    now the code in general does what it needs to and adjusts the speed output depending on an increasing or decreasing load to maintain the desired speed.

    the problem im having currently thats just bugging me happens when i run the code after a fresh program or power up and no matter what i just cant narrow it down

    when i select auto mode for either direction by bringing pin RA.5 high the pwm output briefly goes to what i assume is zero and the motor completely stops for a few hundred ms then picks right back up and maintains the desired speed,

    this only happens once for either direction when i set the cruise control pin if i have just programmed the chip or just powered it up, after this anytime i go back and forth between manual speed mode or auto then its fine and i dont get the odd behaviour

     #chip 16F1825, 16
     #config PLLEN = on, MCLRE = off
    
     #define dir_pin PORTA.2   'forward/reverse direction control for h bridge
     #define led     PORTA.0   'power up/ brown out indicator
     #define cruise  PORTA.5   'cruise control on/off
    
     #define n1_fet PORTC.3 ' forward low side fet hpwm2
     #define p2_fet PORTC.4 ' forward high side fet
     #define n2_fet PORTC.5 ' reverse low side fet hpwm1
     #define p1_fet PORTA.4 ' reverse high side fet
    
     dir cruise in
     dir dir_pin in
     dir led out
    
     dir n2_fet out
     dir p1_fet out
     dir n1_fet out
     dir p2_fet out
    
     set n1_fet off
     set n2_fet off
     set p1_fet off
     set p2_fet off
    
     set led off
    
     dim m_speed as integer
     dim new_speed as byte
    
     dim current_speed as word
     dim cruise_speed as word
     dim speed_diff as word
    
      m_speed = 0
      new_speed = 0
      cruise_speed = 0
      current_speed = 0
    
      wait 1 s
      set led on
      wait 2 s
      set led off
    
    main:
    
     do
    
        do while dir_pin = 0              'forward rotation
    
           set p2_fet On
    
           m_speed = readad (AN4)         'read 10k pot for manually set speed
           HPWM 2, 5, m_speed             'update pwm with new pot reading
    
           read_speed_fwd                 'read back emf from motor
           cruise_speed = current_speed   'set desired speed to current speed to maintain if auto mode enabled
    
           if cruise = 1 then fwd_auto    'if cruise pin = high then maintain current speed in fwd_auto
    
         loop
    
         dead_band_delay                  'delay to ensure bridge outputs off for direction change
    
        do while dir_pin = 1              'reverse rotation
    
           set p1_fet On
    
           m_speed = readad (AN4)         'read 10k pot for manually set speed
           HPWM 1, 5, m_speed             'update pwm with new pot reading
    
           read_speed_bck                 'read back emf from motor
           cruise_speed = current_speed   'set desired speed to current speed to maintain if auto mode enabled
    
           if cruise = 1 then bck_auto    'if cruise pin = high then maintain current speed in bck_auto
    
         loop
    
        dead_band_delay
    
     loop
    
    sub dead_band_delay
    
    set p1_fet off
    set p2_fet off
    hpwm 1, 5, 0
    hpwm 2, 5, 0
    
    wait 250 ms
    m_speed = 0
    
    end sub
    
    sub fwd_auto
    
       do while cruise = 1
    
           read_speed_fwd
    
           select case current_speed
    
           case = cruise_speed
           wait 1 ms
    
           case > cruise_speed
           speed_dec
    
           case < cruise_speed
           speed_inc
    
           end select
    
           HPWM 2, 5, m_speed
    
           wait 100 ms
    
         Loop
    
    end sub
    
    sub bck_auto
    
         do while cruise = 1
    
           read_speed_bck
    
           select case current_speed
    
           case = cruise_speed
           wait 1 ms
    
           case > cruise_speed
           speed_dec
    
           case < cruise_speed
           speed_inc
    
           end select
    
           HPWM 1, 5, m_speed
    
           wait 100 ms
    
        Loop
    
    end sub
    
    sub speed_inc
    
          speed_diff = cruise_speed - current_speed
    
          select case speed_diff
    
          case 0 to 20
          new_speed = m_speed + 2
    
          case 21 to 30
          new_speed = m_speed + 3
    
          case > 31
          new_speed = m_speed + 15
    
         end select
    
         if new_speed =>255 then
            new_speed = 254
         end if
    
          m_speed = new_speed
    
    end sub
    
    sub speed_dec
    
          speed_diff = current_speed - cruise_speed
    
          select case speed_diff
    
          case 0 to 20
          new_speed = m_speed - 2
    
          case  21 to 30
          new_speed = m_speed - 3
    
          case > 31
          new_speed = m_speed - 15
    
         end select
    
         if new_speed < 10 Then
            new_speed = 10
         end if
    
         m_speed = new_speed
    
    end sub
    
     sub read_speed_fwd
    
        wait 100 ms
    
          HPWM 2, 5, 0
          set p2_fet off
    
          wait 2 ms
    
          current_speed = readad10 (AN5)
    
          set p2_fet on
          HPWM 2, 5, m_speed
    
    end sub
    
     sub read_speed_bck
    
        wait 100 ms
    
          HPWM 1, 5, 0
          set p1_fet off
    
          wait 2 ms
    
          current_speed = readad10 (AN6)
    
          set p1_fet on
          HPWM 1, 5, m_speed
    
    end sub
    

    i have attemted to clean the code back up after stripping it away in chunks and this is one of the variations that works except for the momentary pwm loss issue when jumping from the main loop to either sub fwd_auto or bck_auto from a fresh programming or power up.

    i have also tried variations of if...then statements for my speed selection to see if that helps but so far im not winning, clearly i have to assume that ive spent so long looking at this and altering things that im probably missing the obvious.

    i know its not the greatest of faux closed loop dc motor control but it actually works ok apart from the issue but i cant continue further until i can find out whats causing it.

    tony

     

    Last edit: tony golding 2015-04-24
  • tony golding

    tony golding - 2015-04-24

    also just to mention i am using the standard pwm.h file not the modded one i had for the 4ccp modules and this behaviour is also still present on a 16f1829 when tested, so its either something small i have missed or a bug.

    tony

     
  • William Roth

    William Roth - 2015-04-24

    Hi Tony

    You stated:

    " ... When i select auto mode for either direction by
    bringing pin RA.5 high the pwm output briefly goes to
    what i assume is zero and the motor completely stops
    for a few hundred ms then picks right back up and maintains
    the desired speed ..."

    We can't assume anything here. Can you look at the PWM signal with a scope or logic analyzer and confirm that the PWM actually turns off ?

    What else could cause the motor to stop completely? (Rhetorical)

    Suggest you take some measurements and add some debugging code at strategic locations in the program to display ADC values, HPWM settings,etc.

    What kind of test equipment do you have? Do you have an LCD display? Or a cheapo Saleae Logic Analyzer clone? What programmer are you using?

    As always, a schematic might be useful.

     
  • tony golding

    tony golding - 2015-04-24

    well i just hooked it up to the ikalogic and captured pwm waveform on pin change and it seems ok,

    the assumption that it appears that the pwm signal dissapears is more down to the visual and audible signs when i bring the switch high, the motor comes to an almost stop for a very short period before the pwm ramps it back up to the desired speed and then holds it fine.

    after that one hiccup then its fine no matter how many times i switch back and forth.

    the pic has a 100nf decoupling cap across the power pins, power is 5v to the whole circuit from a good stable dc regulator with 2 100uf caps in the breadboard power bars.

    switch is pulled low with 10k pulldown and 1k between input and jumper wire.

    the reason it puzzles me is that once i make the appropriate pin high then once it goes to the sub procedure and measures again it still has the original preset speed value to use when it turns the fets back on to keep it running until it decides what that value should be but for some reason it just momentarily stops just one time when switching operating modes and then behaves after.

    ive spent so long staring at it ive probably gone brain dead and missing the obvious

    schematic.... that wont be glamourous, my ms paint skills are barely existent lol

     

    Last edit: tony golding 2015-04-24
  • William Roth

    William Roth - 2015-04-24

    Something you may be interested in. LTSPICE IV.

    http://www.linear.com/designtools/software/#LTspice

    It is a very powerful SPICE simulator as has a nice schematic capture as well. And the good new is that it is totally free with good support on Yahoo Groups. Add a few libraries for digital devices and it will do just about everything including transient analysis, DC Transfer, DC Op Point, etal.

    You can do up a nice looking schematic in no time, capture it to a bitmap, and then doctor it up in paint if you need to. It won't simulate a PIC but it will simulate most TTL and CMOS logic chips as well as hundred of Mosfets, Diodes, Transistors, etc and thousands Linear Tech IC's.

    The bad news is that it takes a few days to get the hang it, like most advanced software.

    Here is a screenshot of a "Delay on Power Up" circuit that I cobbled together in a few minutes. And it works! First in the simulator and then on the breadboard. After I adjust the resistor/cap values, I will use this circuit to delay a PIC from powering up until 100ms after a finicky display powers up. Using the SPICE saved me quite a bit of faffing about on the breadboard.

     

    Last edit: William Roth 2015-04-24
  • tony golding

    tony golding - 2015-04-25

    well i have attempted to create a schematic, probably not great by many standards.

    RC0 is a potentiometer but i couldnt find one and i have not got any component library updates downloaded for it yet.

    im sure their will be many questions.

    tony

     
  • William Roth

    William Roth - 2015-04-30

    Tony,

    Has this been solved or do you need more help ?

     
  • tony golding

    tony golding - 2015-04-30

    i did make some changes to the code layout and strip a few things of to try and get it running in a better loop.

    it seems to have done the trick as i now have my modified servo winch fighting back as soon as it sees the bemf drop from the desired preset or reduce the pwm in an overspeed situation although i still have to add braking into the mix as well just in case the pwm dropping to 0 still needs additional braking to keep the speed where it needs to be, very handy when using a scale winch with a 9lb+ RC truck on the end of the winch that wants to go with gravity as soon as the fet braking is removed.

    i think it could still do with more work to improve overall but have had to put it on hold the last several days as i had lots of RC vehicles to service and repair for a friend.

    hopefully i can get back at it very soon and at least get the code to a satisfactory point that i can move it up from 5V testing to the intended voltages of 2s to 3s lipo, i just want to be happy with its performance before i start modifying the circuit with voltage dividers for the bemf adc inputs

    the only other thing i had noticed is that regardless of which side i measure for the motor feedback for forward or reverse and with either adc channel i get large fluctuations of the measured value unless i use a pulldown on the adc channel input i need to measure then its pretty stable with small variations in the returned value.

    i did spend quite some time searching online at how other people do this with a H bridge and see that some people tap off either side of the motor whereas looking at the microchip brushed motor control tips pdf only shows the need to measure off one side of the bridge regardless of direction so their is definately more investigating to be done as to how to get the best overall results, but thats half the fun of doing this i guess.

    tony

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.