Shop OBEX P1 Docs P2 Docs Learn Events
Using Objects (Synth.spin, SquareWave.spin CLOSED: END OF DISCUSSION — Parallax Forums

Using Objects (Synth.spin, SquareWave.spin CLOSED: END OF DISCUSSION

bbrienbbrien Posts: 561
edited 2022-02-12 19:08 in Propeller 1

I am trying to use the above objects to generate a 8 and 10 Hz signal on two counters of a single cog. My counters are declared in the con section of my program. the Pin numbers are declared separately. but I don't know where to put the needed frequency. I am including my Program.

«1

Comments

  • I think we need to move this to P1 thread to get a better response. I can do that if you wish.

  • If you think it will help.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2022-01-06 18:43

    I am trying to use the above objects to generate a 8 and 10 Hz signal on two counters of a single cog.

    No objects required -- just some simple PASM running in its own cog.

    con
    
      P10HZ    =  1  { O }
      P08HZ    =  0  { O }
    
    
    dat { 8Hz / 10Hz synth }
    
                    org       0
    
    synth810        andn      outa, mask8                           '  8Hz output low
                    or        dira, mask8
    
                    andn      outa, mask10                          ' 10Hz output low
                    or        dira, mask10
    
                    mov       timer, tix                            ' start timer
                    add       timer, cnt
    
    :loop           waitcnt   timer, tix                            ' hold
    
                    add       ctr8, #1                              ' bump count           
                    cmpsub    ctr8, #5                      wz      ' at 1/2 cycle?
        if_z        xor       outa, mask8                           ' yes, toggle pin
    
                    add       ctr10, #1
                    cmpsub    ctr10, #4                     wz
        if_z        xor       outa, mask10   
    
                    jmp       #:loop
    
    ' ---------------------------------------------------------------------------------------
    
    mask8           long      1 << P08HZ
    mask10          long      1 << P10HZ
    tix             long      (CLK_FREQ / 10) / 8 - 28              ' 1/8 period @ 10Hz
    
    ctr8            long      0
    ctr10           long      0
    
    timer           res       1
    
                    fit       496
    
    1920 x 1040 - 57K
  • The waveforms are just like the ones I generated in Experiment1a. I would like to incorporate into the DH15 program . I will remove all of the stepper method since I can't get to work. ( it's in first post of this thread)

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2022-01-07 22:04

    You say you want to use the two cog counters to generate frequencies of 8 Hz and 10 Hz, and that the counters are already defined in the CON section. Here is what I see in the CON section and I'm confused about all the numbers there, 50Hz, a couple of 8_0 and a 7.5% and a 9.5 Hz.

      PWM_FREQ  = 50                ' 50hz frequency : 50% dutycycle
      DEC_RATE  = 8_0               ' DEC base rate = 7.5% - Adjust as needed for desired slew rate
      DEC_AUTO  = DEC_RATE          ' DEC autoguider correction rate - currently set to base rate
    
      RA_RATE   = 8_0               ' RA base rate = 9.5 Hz - Adjust as needed to counteract Earth's rotation
      RA_AUTO   = RA_RATE           ' RA autoguider correction rate - currently set to base rate
    
    

    In any case, to set up a cog counters for 8 Hz and 10 Hz, you need

    ctra := %00100 << 26 + pinNumberA.   
    frqa := 430.   ' to produce 8 Hz (closer to 8.01 Hz)
    ctrb := %00100 << 26 + pinNumberB.   
    frqa := 537.   ' 10 Hz
    dira[pinNumberA] := 1
    dira[pinNumberB] := 1
    
    
  • Tracy , a lot of information is being disposed of in that program, a new partial program has been completed but untested, and incomplete. maybe you can take a look and help.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2022-01-07 16:41

    Thanks for the reminder, @"Tracy Allen" -- I was tired when I posted last night, and trying to pay a little attention to "The Book of Boba Fett" as a member of the Parallax community works on the show (I occasionally provide bits of P1 code for him). I completely forgot about NCO mode, for which I have a method in my jm_io.spin object.

    Edit: Fixed freq_out() method to eliminate loss of bit0

    pub freq_out(ctrx, px, fx)
    
    '' Sets ctrx to frequency fx on pin px (NCO/SE mode)
    '' -- fx in 0.1Hz units (100_0 = 100Hz)
    '' -- use fx of 0 to stop counter that is running
    
      if (fx > 0)
        fx := ($4000_0000 / ((clkfreq >> 2) / fx))                  ' convert freq for NCO mode (modified)
        fx := (fx + 5) / 10                                         ' round up if needed (added)
    
        case ctrx
          0, "a", "A":
            ctra := (%00100 << 26) | px                             ' configure ctra for NCO on pin
            frqa := fx                                              ' set frequency
            dira[px] := 1                                           ' make pin an output
    
          1, "b", "B":
            ctrb := (%00100 << 26) | px
            frqb := fx
            dira[px] := 1
    
      else
        case ctrx
          0, "a", "A":
            ctra := 0                                               ' disable counter
            outa[px] := 0                                           ' clear pin/driver
            dira[px] := 0
    
          1, "b", "B":
            ctrb := 0
            outa[px] := 0
            dira[px] := 0
    

    Tested with:

      io.freq_out("A", P08HZ,  8_0)
      io.freq_out("B", P10HZ, 10_0)  
    

    Output attached.

    1920 x 1040 - 57K
  • JonnyMacJonnyMac Posts: 9,102
    edited 2022-01-07 16:36

    The next morning -- after a cup of coffee -- I coded manually:

      ctra := (%00100 << 26) | P08HZ                                ' nco mode
      frqa := 429                                                   ' 8Hz = 2^32 / (clkfreq / 8)
      dira[P08HZ] := 1                                              ' enable output
    
      ctrb := (%00100 << 26) | P10HZ
      frqb := 537                                                   ' 10Hz = 2^32 / (clkfreq / 10)   
      dira[P10HZ] := 1 
    

    The output is just a smidge more accurate than my original freq_out() method which always dropped bit0 of the frqx setting (fixed now).

    1283 x 358 - 20K
  • concerning post #7
    I tried to correct an error by adding a pair of phrases to the CON section and got another error ( see Attached File), What does it mean and how to fix.****

    1280 x 720 - 150K
  • Jumpin' Jiminy on a cracker... you can't make method calls from a CON block (only PUB and PRI blocks).

  • bbrienbbrien Posts: 561
    edited 2022-01-08 23:06

    so those phrases have to be used with objects, guess i'll have to use the SquareWave objects. Next error,What is and why do I get da "expected a sub routine name" error ?

  • The difference between this and my dh14 code is little and it works but the dh15 code doesn't. 15 is on first post and here is 14.

  • Another question; is this program a spin or a propC.

  • bbrien,

    The changes I see between DH14 and DH15 are:
    RA_RATE was changed from 7_5 to 8_0
    DEC_SPEED and DEC_DIR were changed to -1 in Motor.Start, which isn't even used.
    Step2 was added
    Step2.Start was added for DEC
    Step2.Set-Speed was added for DEC
    Motor was changed to Step2 for DEC for (DEC_RATE * SLEW RATE), (-DEC_RATE * SLEW RATE), (DEC_AUTO), and (-DEC_AUTO).
    Motor.Set_Speed(DEC,0) was changed to Motor.Set_Speed(DEC_AUTO)

    Programs with a Spin extension (.spin) are Spin programs, and a C program would have a C extension (.c).

    All of the programs in this posting are Spin programs.
    Spin has some elements of C such as braces, { }.

  • If you compare dh15 and the originaldh17 the only difference other than pin numbers is the object "stepper". When there was only one instance of "stepper" and one instance of "dualmotorezmod" as in dh14, the stepper object worked fine but, when two objects of "stepper were used the the outputs were "dead", cannot figure out why. I would think the sub routine names could be the same.

  • bbrien,

    Which Stepper.spin are you using because he posted 2 in 'using multiple frequencies to control a stepper motor.'?

    You should be using the newer Stepper.spin, which has SCALE = 40, and uses ClkTicks := 20*CLKFREQ in Set_Speed.

  • bbrien,

    You keep adding code, removing it, and then adding it back, as well as changing adds to subtracts and then back to adds again.

    I see nothing in Stepper.spin that would be the same in more than one instance but I am not familiar with running Objects more then once.

    One thing I would suggest is using RAaxis and DECaxis instead of Step1 and Step2, because maybe you have one of the axes flipped later in the code.

    Also, can you explain in words, what should happen when each button is pressed, so I can better understand when values should be negative or positive and added or subtracted.

    Now if you comment out one of the Step Objects, does the other one work, and vice versa?

  • I am using the stepper which scales at 40. I will try commenting out one of the two stepper objects and see what happens. Let you know later
    North button drives Dec motor north and South drives Dec motor to south direction. West, RA motor turns west, East turns RA motor in east direction.

  • bbrien,

    I don't know what units your program uses, but according to Telescope.com RA is in Hours (though it can be converted to degrees) and DEC is in Degrees of Arc.
    It also says that Declination can only be from +90 degrees to -90 degrees, which would be horizon to horizon; + is to the North and - is to the South.

    The drive kit instructions also say that when the RA axis is moving at the Sidereal rate that objects will move Eastward and that when it's paused (Stopped) they will move Westward.

    I don't know what kinds of objects you are trying to see, but Telescope.com says that stars are in fixed positions while other objects such as the sun and planets move.

    I am just trying to understand how the Telescope system works since I am not familiar with it.

    Also, in the Dual Axis thread, you mention Astroview and when I do internet searches it brings up the EQ-3M drive kit.
    The EQ-2M and EQ-3M appear to use DC motors while the TrueTrack uses Steppers.

    And the Orion handbox has an LED between the 4 compass buttons that is Green when a button is not pressed but turns Red when a button is pressed.
    Did you plan on using a Bicolor LED to emulate this function on your handbox?

  • It is comprised of two motors which have 4 wires in each unit and they act like bipolar steppers when I connect them to a MC3479 stepper driver chip. As for the bicolor Led I'm not using at this time.

    1. I commented out the step1 instructions in dh17, but still no output onstep2( pin 19).
    2. I changed the step1 and step2 names toRAaxis and DECaxis and Still get a "expect a sub-routine name in dh15.
  • DH15 uses the Synth.spin Object which only has 1 Public Method (callable from an outside program) called Synth.

    Stepper.spin has the following public methods:
    Start
    Stop
    Set_Speed
    Driver

    Dave Hein might want to explain what his Driver method does but it appears to toggle the ClkPin back and forth after a time period of ClkTicks.

    Start with DH17 and save it as ClkTest, then add a Step2.Driver after the Step2.Set_Speed for a NORTH.
    Save it and try running it while pressing the NORTH button and hopefully you will see a clock pulse on a DEC pin.

  • I'll give it a try and let you know tomorrow have to wait till tomorrow, my wife don't work on this when she home.

  • I tried it but results are negative.

  • I am trying an experiment on dh15 changing Step1.set_speed to DEC_Freq.symph and I get a strange error. see what you make of it?

  • bbrien,

    You make it confusing when you keep using the same program name.
    At least add a different letter each time so it's easier to tell if the program is new or old.

    The Synth Object has 3 Parameters: Counter, Pin, and Frequency.
    You have only given 1 Parameter so that is why you are getting an error.

    What are you trying to do and are you absolutely positively sure that your wiring is correct?

  • bbrienbbrien Posts: 561
    edited 2022-01-31 20:54

    I am trying to send a 8Hz pulse to pin #17 and a 10Hz pulse To pin# 19. I will Indicate with Arrow, error.
    wiring is correct for the initial test. Four sw. on #4,5,6,7 and four leds on #16,17,18,19 which flash or turn on or off . Tested with Frequencysynth.spin . No sw. used with frequencysynth. Also I resend Files as Telescope BS1_A

  • JonnyMacJonnyMac Posts: 9,102
    edited 2022-01-31 22:40

    I am trying to send a 8Hz pulse to pin #17 and a 10Hz pulse To pin# 19.

    You can do that in just two lines of code with the method I shared earlier (post #8). You don't need the synth object.

      freq_out("A", 17,  8_0)
      freq_out("B", 19, 10_0)
    

    Each cog has two counters -- this uses the ctra for 8Hz, and ctrb for 10Hz.

  • bbrien,

    Telescope BS 1A is the same as the DH15 above it, except that you marked the line that had an error.

    Look at page 182 of the Propeller Manual, which is for PUB that declares a Public method or a subroutine that you can use in your programs.
    https://www.parallax.com/package/propeller-manual/

    PUB MethodName (ListOfParametersIntoTheMethod) :ReturnValue | ListOfLocalVariables
    

    Notice the List of Parameters in between the Parenthesis, ().
    Your program MUST give the same number of parameters that the method expects, otherwise you will get an error.
    Also, some methods will send back a Return Value, as shown above after the Colon, :.

    If you are unsure of what Parameters a Method needs or what values it returns then open up it's Mother Object and look at it in another tab.

    This is the Synth method from your Synth object.

    PUB Synth(CTR_AB, Pin, Freq) | s, d, ctr, frq
    
      Freq := Freq #> 0 <# 128_000_000     'limit frequency range
    
      if Freq < 500_000                    'if 0 to 499_999 Hz,
        ctr := constant(%00100 << 26)      '..set NCO mode
        s := 1                             '..shift = 1
      else                                 'if 500_000 to 128_000_000 Hz,
        ctr := constant(%00010 << 26)      '..set PLL mode
        d := >|((Freq - 1) / 1_000_000)    'determine PLLDIV
        s := 4 - d                         'determine shift
        ctr |= d << 23                     'set PLLDIV
    
      frq := fraction(Freq, CLKFREQ, s)    'Compute FRQA/FRQB value
      ctr |= Pin                           'set PINA to complete CTRA/CTRB value
    
      if CTR_AB == "A"
         CTRA := ctr                        'set CTRA
         FRQA := frq                        'set FRQA                   
         DIRA[Pin]~~                        'make pin output
    
      if CTR_AB == "B"
         CTRB := ctr                        'set CTRB
         FRQB := frq                        'set FRQB                   
         DIRA[Pin]~~                        'make pin output
    

    Notice that this method has 3 Parameters, so you MUST give it 3 values.
    The 1st is CTR_AB, and look down the code, you can see that it is looking for an A or a B.
    Next is Pin, which is just a pin number, and since the Propeller only has 32 I/O pins, its value must be 0 to 31.
    Last is a Frequency, and notice in the 1st line that it only wants from 0 to 128 MHz.
    For 8 Hz you would give it 8, and for 10 Hz you would give it 10.

    Just beware that some Objects are better written than others.
    Synth expects a frequency in Hz while JonnyMac's Freq_Out uses tenths of a Hertz, or 0.1 Hz.

    Sometimes Objects have Demo programs that show how to use them.

  • bbrienbbrien Posts: 561
    edited 2022-02-04 21:02

    I haven't been able to get Freq_out to Work yet but I made some changes to Telescope1_A and renamed to Telescope BS 2_B, and it Works to some extent. If you find your "QuickStart ,maybe, you can check it out for me.

Sign In or Register to comment.