Duty cycle - Another Math dilemma


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Duty cycle - Another Math dilemma

    Okay so I found another 18F1320 and it does not communicate either!
    Did I miss something in the code?
    It may be time for new glasses!

    XTILT.txt

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Duty cycle - Another Math dilemma

    Hi Ed,
    Is that all the code? I don't see a Goto Read_Force anywhere so it would run once and then weird things might occur, like stop communicating with the software ICD perhaps.

    /Henrik.

  3. #3
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Duty cycle - Another Math dilemma

    Okay so I have to be doing something fundamentally stupid! Just not sure what? Any ideas?


    DEFINE OSC 20 'Define crystal frequency

    Include "Modedefs.bas"
    include "hpwm10L.pbp"
    Enable Debug

    #CONFIG
    __CONFIG _CONFIG1H, _HS_OSC_1H & _FSCM_OFF_1H
    __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_8K_2H
    __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _BORV_27_2L
    __CONFIG _CONFIG3H, _MCLRE_OFF_3H
    __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_ON_4L & _STVR_ON_4L
    __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L
    __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L
    __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L
    __CONFIG _CONFIG7H, _EBTRB_OFF_7H
    #ENDCONFIG


    '********************* Declaired Pins **************************

    INPUT PortB.2 ' X Data is clocked on rising edge of this pin


    ' ** Variables **

    PxRaw VAR Long ' Low part of Pulse from Memsic 2125
    WxRaw var Long ' High part of Pluse from Memsic 2125
    WxTRaw var long ' Total of High and low Pluse from Memsic 2125
    Dcycle var Long ' Duty cycle
    xGForce VAR Word ' x axis g force (1000ths)
    xTilt VAR Word ' x axis tilt (100ths)
    xidx VAR byte ' Table index
    xmult VAR Word ' Multiplier - whole part
    xfrac VAR Word ' Multiplier - fractional part
    xPosNeg var byte ' ositive angle or Negative angle flag

    ADCON1 = 255 ' Make everything digital (no analog)

    TRISB.2 = 1 ' Make B.2 an input

    Read_Force:

    PULSIN PORTB.2, 1, WxRaw
    PULSIN PORTB.2, 0, PxRaw

    WxRaw = WxRaw * 20 ' The high part of the pulse
    PxRaw = PxRaw * 20 ' The low part of the pluse

    WxTRaw = wxraw + pxraw ' The total time of the pluses

    Dcycle = (Pxraw * 10000) / WxTRaw

    xGForce = (((WxRaw * 10000)/ WxTRaw) - Dcycle) * 8 'xGForce = ((t1 / t2) - duty) / 12.5%


    Read_Tiltx: 'tilt = g x k Select tilt conversion factor based on static G force. Table data derived from Memsic specs.

    LOOKDOWN2 ABS xGForce, <=[174, 344, 508, 661, 2000], xidx ' Table is in thousandths g results for 10,20,30,40 and max value degrees
    LOOKUP xidx, [57, 58, 59, 60, 62], xmult 'integer lookup
    LOOKUP2 xidx, [32768, 10486, 2621, 30802, 22938], xfrac 'gets fractional result
    xTilt = xmult * (ABS xGForce / 10) + (xfrac ** (ABS xGForce / 10))

    Check_SignX: 'Check the sign of X angle
    IF (xGForce.Bit15 = 0) THEN XT_Exit ' if positive, skip correct for g force sign
    xPosNeg = 0 ' If xPosNeg = 0 then the angle is negative (-)

    XT_Exit:
    xPosneg = 1 ' If xPosNeg = 1 then the angle is positive (+)


    gosub Read_Force

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Duty cycle - Another Math dilemma

    Okay so I have to be doing something fundamentally stupid! Just not sure what? Any ideas?
    Yes, you're using GOSUB instead of GOTO.
    GOSUB is used to jump to a subroutine which "ends" with a RETURN - that's not what you have.

    /Henrik.

  5. #5
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Duty cycle - Another Math dilemma

    Thanks, easy to fix. I still can not get the math to come out right!

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Duty cycle - Another Math dilemma

    Hi Ed,

    (I'm answering your PM here since it related to this thread.)

    Lets see if we can get the numbers straight on paper as a first step, then we'll move on to PBP.

    According to the datasheet for the MXD2125 accelerometer (is that the one you're using). The formula is g=(T1/T2-0.5)/12.5%

    The dutycycle of the output signal is 50% at 0g, that's where the subtraction of 0.5 comes from.
    12.5% is the sensitivity of the device, it varies the dutycycle of the output signal 12.5% per g.

    At +1g the dutycycle is 50+12.5=62.5%, the numbers becomes (0.625-0.5)/12.5% = 1g
    At -1g the dutycycle is 50-12.5=37.5%, the numbers becomes (0.375-0.5)/12.5% = -1g

    12.5% is the same as dividing by 12.5/100 which is the same as dividing by 0.125 which is the same as multiplying by 8.

    According to the datasheet the acceleration at 30° is 0.5g which corresponds to a dutycycle 56.25% (50% is zero g, the output swings 12.5% per g so 50+6.25), lets run those numbers on paper:
    (0.5625-0.5) * 8 = 0.5g

    It all seems to work out on paper - wohoo!

    What to do in PBP (before continuing I must point out that I haven't actaully tested this, hopefully I haven't messed it up too badly....):
    I see from your posts that you're now using LONGS which makes it a bit easier. Let's try with your 0° numbers first.
    Obviously we cant just do 4927/9867 (the numbers you gave for 0°) because that will result in a number less than 0 which is not of any use to us. What we can do though is to multiply the T1 time by say 10000.
    Code:
    T1 VAR WORD
    T2 VAR WORD
    Duty VAR LONG
    g VAR LONG
    
    ' Number for 0°
    T1 = 4927
    T2 = 9867
    
    Duty = (T1 * 10000) / T2   ' Duty will now be 4993
    g = (Duty - 5000) * 8    ' g will now be -56 which is equal to -0.0056
    
    HSEROUT["Acceleration is: ", SDEC g/10, "mg",13]
    Now, in your PM you said that at 30° your values are 4535/9578 but that only works out to a dutycycle change of about 2.65% when you really should be getting around 6.25%. However you also say that the result of 4535/9578 is 0.4491 which isn't the case so something seems to be wrong with your numbers there.

    But lets for the cause of the exercise use some the theoretical numbers corresponding to 30°, 0.5g, 56.25% dutycycle
    Code:
    T1 = 5625
    T2 = 10000
    
    Duty = (T1 * 10000) / T2   'Duty will now be 5625 (56.25%)
    g = (Duty - 5000) * 8   ' G will now be 5000 which is equal to 0.5G
    
    HSEROUT["Acceleration is: ", SDEC g/10, "mg",13]  ' should print 500mg
    That's the acceleration part, when that works I suspect you want to convert the acceleration value into an actual angle....

    /Henrik.

  7. #7
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Duty cycle - Another Math dilemma

    Hi Henrik!
    Thank you and I finally got that the duty cycle is a "fixed" number assumed to be at 50%. According to the information about the device, each device can have a zero duty cycle of from 48.7% to 51.3%. My experience has been that at a room temperature of about 72 degrees F, my device has a measured duty cycle of 49.93%. However as the room temperature increase or decrease so does the zero point duty cycle! So if the submarine is sitting on a table at the park with an air temperature of 72 degrees F then 4993 works. Put the sub in water where the water temperature is 65 degrees F and 4993 is no longer valid. Originally the thinking was to measure and calculate the duty cycle however this method does not work as you first must be at an known zero degrees of tilt.

    Best Wishes, Ed

    Name:  28017-P2.jpg
Views: 1745
Size:  810.6 KB

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