Announcement

Collapse
No announcement yet.

COS function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • COS function

    PBP manual states that COS interprets the input value as radians, but apparently interprets it as degrees.

    for i=0 to 10
    real[i]=cos i
    next i

    Returns

    127....123

    There's more to the story, so I guess I just don't understand it. Cos(60) returns 12, which is neither degrees nor radians input if scaling a (-1 to 1) output range to a (-127 to +127) specified output range.

    Can someone explain how to get correctly scaled (-127 to 127, equivalent to -1 to 1) COS output for a raw radian input from the PBP COS function?
    Last edited by Chuck Curtis; 03-26-2019, 06:45 PM.

  • #2
    It looks like I have to multiply the raw radian input by 256/(2*pi), or ~40.74, then feed it to the COS function. Man, not exactly what I had in mind.
    Last edited by Chuck Curtis; 03-26-2019, 09:40 PM. Reason: 256 was 255(incorrect), ~40.74 was ~40.58(incorrect)

    Comment


    • #3
      google brad for some more info in binary scaling

      try this

      Code:
      result var byte
      i var byte
      c var byte
      v var byte
      sb var byte
      pause 3000
      
      debug 13,10,9,"angle",9,"Brad",9,"Cos",9,"Dec",13,10
      
      for i=0 to 180 step 10
      c =       i*256/360
      result = cos  c
      v=100*abs(result)/128
      if result.7 then
      sb="-"
      else
      sb=" "
      endif
      
      debug 9,#i,9,#c,9,sdec result,9,sb,"0.",sdec v,13,10
      next
      stop

      Code:
          angle    Brad    Cos    Dec
          0    0    127     0.99
          10    7    125     0.97
          20    14    120     0.93
          30    21    111     0.86
          40    28    98     0.76
          50    35    83     0.64
          60    42    65     0.50
          70    49    46     0.35
          80    56    25     0.19
          90    64    0     0.0
          100    71    -22    -0.17
          110    78    -43    -0.33
          120    85    -63    -0.49
          130    92    -81    -0.63
          140    99    -96    -0.75
          150    106    -109    -0.85
          160    113    -118    -0.92
          170    120    -125    -0.97
          180    128    -127    -0.99

      Comment


      • #4
        shame forums totally root up white space

        Click image for larger version

Name:	cos.jpg
Views:	51
Size:	77.1 KB
ID:	7646
        Last edited by richard; 03-26-2019, 09:16 PM.

        Comment


        • #5
          Thank you very much, Richard! I learned something that makes using the COS function (which I now know is based on brads) better than I originally thought. I am building lookup tables with entries composed of multiples of pi (which I had to round to integers, naturally), but since the function works on brads, pi gets divided out and I can work with integers directly and no rounding. Well, ya don't suppose that's the reason the COS function works that way, do ya?

          Comment


          • #6
            What if you see this program and squema in order to make an inverter control signal.

            ' For PicBasic Pro compiler to be used by a 16F628A
            ' just the core of the program (not initial specifications)
            'This is a version for Sinus wave inverter with hardware PWM module
            ' in uController
            'It alternates pins, each one responsible of a branch on a H bridge
            'half positive cycle on 96 program cycles
            'Table of 1/4 cycle, 48 pulses then it decrement pointer to get 1/2 cycle
            'Then it repeats but for the other H branch
            'Pendant to shorten to compensate program time, taking some time 'to the off time of cycle Luis Jim
            Attached Files

            Comment


            • #7
              Program is attached in "prog_inversor_ingl" down the image of squema

              Comment


              • #8
                code as posted
                [QUOTE]
                'This is a version for Sinus wave inverter without PWM
                'It alternates pins, each one responsible of a branch oa an H bridge
                'half positive cycle on 96 program cycles
                'Table of 1/4 cycle, 48 pulses then it decrement pointer to get 1/2 cycle
                'Then it repeats but for the other H branch
                'Pendant to shorten to compensate program time, taking some time 'to the off time of cycle Luis Jim

                Comment


                • #9
                  code as posted
                  Code:
                  'This is a version for Sinus wave inverter without PWM
                  'It alternates pins, each one responsible of a branch oa an H bridge
                  'half positive cycle on 96 program cycles
                  'Table of 1/4 cycle, 48 pulses then it decrement pointer to get 1/2 cycle
                  'Then it repeats but for the other H branch
                  'Pendant to shorten to compensate program time, taking some time 'to the off time of cycle       Luis Jimnez
                  
                  'It can be done a timers and interrupt version  (not pulsout)
                  
                  ARRAYWRITE SineWave {0,2,5,8,10,13,16,19,21,24,27,29,32,34,37,39,42,44,46,49,51,53,55,57,59,61,63,65,67,68,70,71,73,74,75,77,78,79,80,81,82,82,83,84,84,84,85,85,85}   ' uS
                  
                  b0 VAR BYTE   ' array's pointer
                  Dir VAR BIT       ' pointer direction
                  MAXIT = [MAX SineWave Count]   '
                  dir =0   
                  b0=0  
                  Apagar PINs pendiente
                  
                  MAIN:
                    'BRANCH 1
                    PIN = RB0   '   DEFINE PORT (ver circuito)
                    DO HALF_CYCLE
                    pauseus 40   'histresis para OFF de IGBTs
                  
                    'BRANCH 2
                    PIN = RB1  ' DEFINE PORT
                    Do HALF_CYCLE
                    pauseus 40   'histresis
                  
                  GOTO MAIN
                  
                  HALF_CYCLE:         ' each cycle = 1/195 pulses
                    FIN=0
                    DO WHILE FIN=0
                      ON= SineWave[b0]
                      OFF = 85 - ON         'this makes a fixed pulse width   (uS)
                  
                      PULSOUT PIN, ON    ' send pulse by PIN, width as table elem.
                                                              'pulse width 1/11,700 + calc
                      Pauseus OFF  
                  
                      IF Dir = 0 THEN      ' increment or decrement pointer
                        b0 = b0 + 1
                      ELSE
                        b0 = b0 - 1
                      ENDIF
                  
                      IF (b0 = MAXIT)  THEN   'changes pointer direction or ends half second of one branch
                        Dir = 1
                      ENDIF
                      IF (b0 = 0) AND (Dir = 1) THEN
                        Dir = 0
                        FIN = 1  
                      ENDIF
                  
                    END WHILE
                  RETURN
                  
                  END SUB
                  Last edited by richard; 4 weeks ago.

                  Comment


                  • #10
                    the forum won't take unicode

                    main issue with proposed code apart from the syntax errors and missing definitions is that pin alias's can't be changed at runtime so you need to use the
                    integer 0-15 pin definitions, i don't see why it would not be a workable approach
                    Last edited by richard; 4 weeks ago.

                    Comment

                    Working...
                    X