Shop OBEX P1 Docs P2 Docs Learn Events
need programming help pwmpal — Parallax Forums

need programming help pwmpal

sport270sport270 Posts: 82
edited 2013-10-13 07:52 in BASIC Stamp
I have a commercial automation system i am bringing online. I am in a EXTREME TIME CRUNCH.
I just need some help finding programming code to test and see if a bs2 or bs2 / pwmpal will handle the hydraulic control circuits i am needing to setup.
I am currently using in house built proportional hydraulic controls. I need to update this control for a new application.
I need three PWM signals one example is : 210hz 56.9% duty cycle. 4.62v dc
the preferred hz is 100hz. and need the duty cycle to be adjustable with pot or programming.


Also need to read 3 incoming signals that are from the shafts that the pwm signals are controlling. and display these rpms on lcd serial display.
would also like another mode possibly, that would read signal & change PWM duty cycle to keep shaft rpm at set speed.


If anyone in your organization can help with this problem please contact me asap Thanks,
Jeff Kirk
Farm Enhancement Systems
Lubbock, Texas 79416
806-767-0715
sport270@yahoo.com


Ps... I noticed you have other chips and options available, I am not set on just the bs2 or pwmpal, just am familiar with programming them in past projects.
And i have no problem with paying for programming & or hardware help, but need to atleast have a test unit in next 24-48 hrs.... :)
«1

Comments

  • PublisonPublison Posts: 12,366
    edited 2013-09-03 08:50
    Do you have access to PMWPAL's? They have been discontinued for a couple of years. They may be available at:

    http://www.parallaxdirect.com/cart/catalog/Parallax-28020-PWMPAL-202.html


  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-03 09:36
    It sounds like the PWMPAL could generate the PWM signals you need and could count the shaft revolution pulses. I'd use one of the faster Stamps like the BS2sx or BS2px just on general principles.

    You'd need a timebase for counting the shaft RPMs. A 32KHz crystal and CD4060 clock oscillator / divider (see datasheet) would give you a 1/2 second clock (or other submultiple) that would be fed into the PWMPAL's 4th counter channel. That way, you could read the 4 counters all at the same time and it would give you the shaft counts and the time. You could subtract the previous counts then divide the revs by the elapsed time then multiply by a fixed scale factor to get RPMs.

    Note that the PWMPAL is now made and sold by ParallaxDirect.
  • sport270sport270 Posts: 82
    edited 2013-09-03 10:44
    Yes, i have a pwmpal
  • sport270sport270 Posts: 82
    edited 2013-09-03 10:46
    the main 2 shaft rpms are on 60 tooth gears, so simple hz = rpm
  • sport270sport270 Posts: 82
    edited 2013-09-03 10:52
    any sample programs that you could share, that would simulate 210hz @ 56.9% duty cycle = 4.62 vdc
    on either the bs2 or pwmpal..... just to test
  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-03 11:30
    What's the range of RPMs?

    The reason for suggesting a separate timebase for measuring RPM is that the Stamp has to stop doing other things to measure time. You can use the PWMPAL to count all 3 pulse sources by zeroing the counters, PAUSEing for a specific amount of time, then reading the counters from the PWMPAL. The PAUSE time has to be adjusted for the amount of time it takes to send the "!PWMC0" command to read the counters like this for a BS2sx or BS2px
    serout 0, T19K2, ["!PWMX0"]   ' clear the counters
    pause 250   ' wait 250 ms
    serout 0, T19K2, ["!PWMC0"]   ' read the counts
    serin 0, T19K2, [str ctrs\8]   ' read all 4 counts
    
    ctrs would be defined as an array of 4 words and T19K2 would be the Baud constant for 19200 Baud.
    The above would tie up the Stamp for 250ms and read 1/4 the RPMs so you'd have to multiply them by 4 for display.

    You could read the pot values using an RCTIME statement, but you'd have to choose the R and C values so that the RC time (and RCTIME execution time) wouldn't be too long. You'd want to keep the time well under 250ms, particularly because you've got 3 pots to read. You probably want to keep your control loop (read RPMS, read 3 pots, update RPM display, adjust PWMs) under 1/2 second so it seems responsive enough. You could speed up the control loop by adding a simple 4-channel ADC like the MCP3204 to read the pot positions as analog voltages rather than using the RCTIME statement. There's example code on that webpage. You could also use an ADC0834.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-03 11:35
    Page 3 of the PWMPAL documentation shows how to calculate the on-time and off-time values for the PWMPAL for any frequency and duty cycle.
  • sport270sport270 Posts: 82
    edited 2013-09-03 11:43
    k, would freq in not be faster than time based?
    For the immediate need of testing and alleviating my customer concerns, could you help me with a simple program to duplicate this output. 210hz 56.9% duty cycle 4.62 vdc ... and possibly have a ramp up over 2-3 seconds. And is it possible to have a serial lcd 4x20 display also connected to the stamp that is also connected to the pwmpal?
  • sport270sport270 Posts: 82
    edited 2013-09-03 11:45
    is there a sample program that sets all the var bits and i/o
  • sport270sport270 Posts: 82
    edited 2013-09-03 11:55
    let me give you a bit more knowledge of the task. I am controlling the hydraulic output of two 0-50 gpm variable displacement pumps. These two seperate pumps in turn provide hydraulic flow to a couple of hydraulic motors that are turning equipment shafts. The reason i wanted to change from the hardware i was using, was because there seemed to be some issues when they turned all there lights on at night, my signal generators would change enough to increase the shaft speed by 30-50 rpm. and sometimes the load would cause the shaft speed to droop by as much as 70 rpm. so i figured i would just replace with something that could change output if need be to keep rpm constant. and the only thing that would change might be the duty cycle to make up the difference from the load.... and the change in duty cycle could therefore possibly signal how much load was on the shaft, and also the rpm readout on lcd was needed....
  • sport270sport270 Posts: 82
    edited 2013-09-03 11:58
    pumps are suer danfos H1 series tandem variable displacement pumps.
    preferred 100hz signal but can have 70hz to 300hz i think
    1800 ma max draw at 12vdc
    nominal coil 3.66-4.52 ohm
    inductance of 33mh

    shaft speed is 630 hz / rpm 60 tooth gear
  • stamptrolstamptrol Posts: 1,731
    edited 2013-09-03 12:56
    Using feedback, as you are planning, is definitely the way to go. If the present signal generator is changing with high electrical load, its probably related to system voltage fluctuating. Mechanical load will eventually make the shaft speed droop if it gets too high. The feedback, of course, will allow that to be compensated for.

    As has been pointed out, using an external PWM controller and maybe pulse counter is the only practical way to get the Stamp to be fast enough to both measure the speed, adjust the PWM and display the output of three channels. It can be done with the parts you've mentioned or you could consider a controller with some of those features built-in.
  • sport270sport270 Posts: 82
    edited 2013-09-03 13:23
    not to sound needy or nothing, but what would be my easiest route to get a working test model together?
    i need to somehow duplicate one pwm signal for test purpose, it needs to duplicate what my proportional controller was outputing as a start point. 4.62 vdc, 210 hz @56.9% duty cycle on a Irf540
    i just need sample code for bs2 or pwmpal for 1 output channel for test.
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2013-09-03 13:28
    I would suggest starting by looking over the documentation and example source code for the PWMPAL ...

    http://www.parallax.com/search?search_api_views_fulltext=PWMPAL
  • sport270sport270 Posts: 82
    edited 2013-09-03 13:29
    i dont have a problem with compensation for design, effort, ect... i just need to get one together like in the next few hours.
    i already have a pwmpal, and bs2 related accessories. i also already have a inhouse built pc board with bs2 that was used in the past for load control. it already has all necessary buffers on input for rpm of shafts, and bs2 to ULN2003 TO Irf540 for output.
    can i not rewrite some of the code and just let bs2 output simple pwm just for this test. and is it possible to redirect the rpm shaft speeds that i have displaying on debug screen to goto lcd screen aswell
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2013-09-03 13:41
    Without understanding your exact circuit configuration and what the sensors are providing it is difficult to blindly write code and expect it to work right off the bat. In your circumstance it can even be dangerous. I personally have been writing code for 36 years and I seldom get it exactly right the very first time. Sometimes I do, but more often I do not. What I would suggest is to use a scope and monitor and observe the wave patterns that you currently have with your existing setup. Create a mock circuit that you can test with and compare the signals.... you can probably even piggyback the input signals, just to make sure that your "new" circuit follows the same output characteristics as the current circuit.
  • sport270sport270 Posts: 82
    edited 2013-09-03 13:53
    one more question, can both a pwmpal & 4x20 serial lcd screen, be connected to the same bs2 that sits atop the pwmpal?
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2013-09-03 14:02
    As long as the connection rules are followed on page 2 of the PWMPAL manual then yes, you can talk to an LCD during times that you are not talking to the PWMPAL.

    http://www.parallax.com/downloads/pwmpal-documentation

    Using the PWM directly from the BS2 is not recommended, since this PWM output is not designed to directly drive a motor. The reason is because the frequencies are too high for the MOSFET and while it may appear to work, the MOSFET spends too much time operating in the linear region causing excessive heat from the MOSFET that can be destructive.
  • Hal AlbachHal Albach Posts: 747
    edited 2013-09-03 19:15
    For what it's worth, have you considered stabilizing the power for the current configuration? Unless I misunderstand your earlier posts It sounds as though the system is running on 12 volts with two motors drawing around 1.8 Amps each. How is the system voltage derived and why would it be subject to power sags when all the lights are turned on?
  • sport270sport270 Posts: 82
    edited 2013-09-05 09:51
    ok, i finally got a pwmpal setup and outputing basically 4.6vdc straight out of chip, but when connected to irf540 it was 13+vdc when connected to the 12vdc power source . i was trying to adjust the duty cycle and somehow messed it up and now im stuck. the sample program i was using is..... PWMPAL_Simple_motor.bs2
    this sample program is part of the pwmpal 28020 documentation. I need help in where or how to change the duty cycle with a pot
  • sport270sport270 Posts: 82
    edited 2013-09-05 09:55
    [h=2]icon1.png Re: Urgent pwm help[/h]
    ok, i finally got a pwmpal setup and outputing basically 4.6vdc straight out of chip, but when connected to irf540 it was 13+vdc when connected to the 12vdc power source . i was trying to adjust the duty cycle and somehow messed it up and now im stuck. the sample program i was using is..... PWMPAL_Simple_motor.bs2
    this sample program is part of the pwmpal 28020 documentation. I need help in where or how to change the duty cycle with a pot


  • sport270sport270 Posts: 82
    edited 2013-09-05 09:56
    here is the sample program
    Example 2 – PWM Output with Hardware Control


    This example uses an active-high pushbutton circuit to control a DC motor connected to the PWM

    channel specified by the MotorNum constant. When the switch is pressed, the motor speed will ramp

    from 25% (minimum speed to get test motor moving) to 100% (full speed). When the switch is released

    the motor will stop – without having to send a new speed value to the PWM channel. This program

    shows how the BASIC Stamp microcontroller can monitor and use the control inputs.
    ' =========================================================================
    
    '
    
    '   File....... PWMPAL_Simple_Motor.BS2
    
    '   Purpose.... Simple DC motor control – with hardware input control
    
    '   Author..... Parallax
    
    '   E-mail..... [EMAIL="support@parallax.com"]support@parallax.com[/EMAIL]
    
    '   Started....
    
    '   Updated.... 09 FEB 2005
    
    '
    
    '   {$STAMP BS2}
    
    '   {$PBASIC 2.5}
    
    '
    
    ' =========================================================================
    
    ' -----[ Program Description ]---------------------------------------------
    
    ' Demonstrates simple DC motor control with the PWMPAL.
    
    ' NOTE: Do NOT connect a DC motor directly to the PWMPAL / micro.  You
    
    ' must use a buffer (transistor, MOSFET, etc.) to switch the current
    
    ' required by the motor.
    
    
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    PpPin           PIN     0                       ' PWMPAL Serial I/O
    
    SpdCtrl         PIN     8                       ' speed button
    
    '© Parallax, Inc.  •  PWMPAL (#28020)  •  02/2005 Page 10
    
    
    ' -----[ Constants ]-------------------------------------------------------
    
    MotorNum        CON     1                       ' PWMPAL motor output
    
    #SELECT $STAMP
    
      #CASE BS2, BS2E, BS2PE
    
        T9600       CON     84
    
        T19K2       CON     32
    
        T38K4       CON     6
    
      #CASE BS2SX, BS2P
    
        T9600       CON     240
    
        T19K2       CON     110
    
        T38K4       CON     45
    
    #ENDSELECT
    
    
    PpBaud          CON     T38K
    
    Yes             CON     1
    
    MinSpeed        CON     25                      ' minimum DC to spin motor
    
    
    ' -----[ Variables ]-------------------------------------------------------
    
    speed           VAR     Byte                    ' speed, 0% to 100%
    
    status          VAR     Byte                    ' motor control status
    
    onTime          VAR     Word                    ' PWM timing
    
    offTime         VAR     Word
    
    
    ' -----[ Program Code ]----------------------------------------------------
    
    Main:
    
      DO
    
        IF (SpdCtrl = Yes) THEN                     ' button pressed
    
          IF (speed < 100) THEN                     ' can we speed up?
    
            speed = speed + 1 MIN MinSpeed          ' increase speed
    
            GOSUB Set_Speed                         ' update PWMPAL
    
          ENDIF
    
        ELSE
    
          speed = 0
    
        ENDIF
    
        DEBUG HOME, "Speed = ", DEC speed, CLREOL   ' show current speed
    
        PAUSE 100                                   ' speed ramp delay
    
      LOOP
    
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    Set_Speed:
    
      IF (speed = 100) THEN
    
        onTime = $FFFF                              ' full on for 100%
    
        offTime = $0001
    
      ELSE
    
        onTime = speed                              ' set duty cycle
    
        offTime = 100 - speed
    
      ENDIF
    
    
    '© Parallax, Inc.  •  PWMPAL (#28020)  •  02/2005 Page 11
    
    
      SEROUT PpPin, PpBaud, ["!PWMM", (48 + MotorNum),
    
                              onTime.BYTE0, onTime.BYTE1,
    
                              offTime.BYTE0, offTime.BYTE1]
    
    
      status.HIGHNIB = %0001 << (MotorNum - 1)    ' set enable bit
    
      status.LOWNIB = %0001 << (MotorNum - 1)     ' set control bit
    
      SEROUT PpPin, PpBaud, ["!PWMSS", status]
    
      RETURN
    
  • PublisonPublison Posts: 12,366
    edited 2013-09-05 10:15
    It is against forums rules to make duplicate posts. You are basically continuing a conversation on your old PWM post. It will be confusing to readers which post the should reply to if they have an answer to your question(s).

    When including code, use the CODE tag in the advanced edit. This will preserve indentations and such. You could also attach the code with the attachment icon, and it will be save as a zip file, (preferred).
  • davejamesdavejames Posts: 4,047
    edited 2013-09-05 10:19
    Sport - FYI...the Forum guidelines may be found here:
    http://forums.parallax.com/showthread.php/134682-Forum-Guidelines

    Also, I went ahead and added the "code tags".
  • PublisonPublison Posts: 12,366
    edited 2013-09-05 10:22
    davejames wrote: »
    Sport - FYI...the Forum guidelines may be found here:
    http://forums.parallax.com/showthread.php/134682-Forum-Guidelines

    Also, I went ahead and added the "code tags".

    Thanks Dave, I was just getting around to include the coded tags. You faster than me. :)
  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-05 10:34
    Here's an example of how you'd set the PWMPAL given a duty cycle at 210Hz ... derived from the PWMPAL documentation on page 3
    ' Here's a fragment of a program to set the duty cycle of the PWM signal on pin 12.
    ' Typically you'd read a pot value using RCTIME as shown in the Stamp Manual and
    ' normalize it so it's in the range of 0-198.  The reason for the extra factor of 2
    ' is to get the maximum precision when doing the 16-bit unsigned arithmetic.
    freq   con   210   ' 210 Hz
    duty   var   word   ' duty cycle in half %
    ' period = 1 / 210 = 0.0047619
    pwmpal   con   190   ' period / 0.000025 (25us)
    onTime   var   word
    offTime   var   word
    
    duty = 56 * 2   ' for demonstration purposes ... could be from RCTIME
    onTime = pwmpal * duty / 200   ' adjust for half %
    offTime = pwmpal - onTime
    serout 0, T19K2, ["!PWMM1",onTime.byte0, onTime.byte1, offTime.byte0, offTime.byte1]
    
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-09-06 07:12
    ok, i finally got a pwmpal setup and outputing basically 4.6vdc straight out of chip, but when connected to irf540 it was 13+vdc when connected to the 12vdc power source .

    How exactly are you doing that?
  • sport270sport270 Posts: 82
    edited 2013-09-09 09:08
    dont suppose any of you could answer my question as to where in the above program i would change to adjust the duty cycle? and possibly how to read pot and have that adjust duty cycle?
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2013-09-09 10:11
    dont suppose any of you could answer my question as to where in the above program i would change to adjust the duty cycle?

    This part --
      IF (SpdCtrl = Yes) THEN                     ' button pressed
        IF (speed < 100) THEN                     ' can we speed up?
          speed = speed + 1 MIN MinSpeed          ' increase speed
          GOSUB Set_Speed                         ' update PWMPAL
        ENDIF
    

    speed is the PWM'ing variable.
    and possibly how to read pot and have that adjust duty cycle?

    An application of RCTIME, the result of which would be used to "map" to a range of 1-100

    DO..LOOP'ing an RCTIME every iteration would slow things down.
    Do a button check and then GOSUB to an RCTIME subroutine.
  • Mike GreenMike Green Posts: 23,101
    edited 2013-09-09 10:14
    There's a detailed explanation of the RCTIME statement and it's use in measuring the resistance of a pot starting on page 363 of the Stamp Manual. This discusses how to choose the R and C values. The result will be a number and you should be able to choose R and C values and to scale the resulting number to get a value either between 0 and 100 (%) or between 0 and 200 (1/2%). The Stamp Editor's help files have a similar discussion under the RCTIME statement.

    Above post #27 shows how to take the number from the RCTIME statement and use it to adjust the duty cycle.
Sign In or Register to comment.