Shop OBEX P1 Docs P2 Docs Learn Events
How can I Make this code FASTER — Parallax Forums

How can I Make this code FASTER

krazyideaskrazyideas Posts: 119
edited 2012-06-23 20:02 in Propeller 1
Beginer Me again.

Well after seeing the abiltiy of assembly, namely so mind boggaling freakin fast. I have a couple questions


First Question is Does anyone know where I can find some tutorals of how to program in assembly?? hopefully a tutoral that would teach me enough so that I could write the below code in assembly.

Second Question is How could I speed this code up. It works great but being the beginner that I am, I betting there is a better way, even remaining in Spin. Here it is.
PUB Coil1 | extra1

dira [7]~~           

extra1 := 0

ONadj1  := 0
Offadj1 := 0 

Final11 := 24       'Final1 represents the 2nd pulses ON DEGREE     
Final12 := 264      'Final2 represents the 2nd pulses OFF DEGREE

Repeat

  if count => 1464 + ONadj1 and count =< 1680 + Offadj1         
    outa[7]~                                                                           
               
                 {The numbers repesent the starting point then I modifi ONadj1 and Offadj1 to change                                                                       ' 
                  the triggering points}

 
  elseif Final12 + OFFadj1 => Final11 + ONadj1 and Final11 + ONadj1 =< 2880
     if count => Final11 + ONadj1 and count =< Final12 + OFFadj1
       outa[7]~
     else
       outa[7]~~

  elseif Final11 + ONadj1 =< 2880    
     if count => Final11 + ONadj1     
       outa[7]~
      

     elseif count =< Final12 + OFFadj1    
       outa[7]~
     else
       outa[7]~~
     
        
  elseif Final11 + ONadj1 > 2880
       extra1 := (Final11 + ONadj1) - 2880
     if count => extra1 and count =< Final12 + OFFadj1
       outa[7]~
     else
       outa[7]~~
                                          
  else
    outa[7]~~   
                       

The first conditon is easy and simple because no matter how much a move it around with my control program it will never cross the reset of the count.
The second condition is like 5 times more complacated because as I change it's values I need it to stay in time.
EXAMPLE. Say Final12 + OFFadj1 added to 2900. Well the count only goes to 2880 before it is reset. So I have to take the difference of 2900 - 2880 and make that the trigger point. The trigger point should be 20 not 2900.


Third Question is how fast is the above Spin program I wrote?
I want the motor to turn at 7,000 RPM, the encoder has 2880 pluses per rotation. So would the code be capable of doing that accuratly??

Foruth Question I run the above code in 1 cog to 1 coil ratio. Would the code be able to handle say 2, 3, 4, 5 ,6 ect... coils???

Thanks is advance.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-06-21 12:42
    You use various combinations of Final11 and Final12 with ONadj1 and OFFadj1. You might precompute these once, then use the precomputed value. Any way you can reduce the number of operations done on each loop will help.

    There are several links to tutorials on assembly language in the "sticky" threads at the top of the forum thread list. deSilva's Machine Language Tutorial is good, but look at the others.

    To really estimate the speed of the Spin program, you'd have to compile it with something like BST which can give you a listing of the interpretive code produced. Unfortunately, there's not a list of each of the interpretive operations and its timing. It's more complicated than that ... you have to look at addressing modes, etc.

    You're talking about roughly 250K pulses per second or 4us per pulse. Very simple Spin might do that ... not what you've got. You'll need to use assembly language or do something completely different using the cog counters. I couldn't tell you what because you've not supplied enough information about what you're trying to do, but the cog counters are fast.

    Assembly is a lot faster than Spin. You should be able to handle multiple coils in assembly.
  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-06-21 13:37
    Third Question is how fast is the above Spin program I wrote?

    One of the great things about the Propeller is that it has the tools to time itself. Just this morning I wanted to find out how long it took to read 512 bytes (128) longs from a microSD card and then transfer the low byte of each to a DMX driver (for a stand-alone DMX cue broadcaster). Here's the general trick in Spin:
    t := -cnt
    
      ' your test code goes here
    
      t += cnt - 544
    


    The value in t is the number of system ticks your code consumed. I tend to send the result to a terminal. Note, too, that when you know the system frequency you can easily convert to the raw counts to a useful value.
  • HShankoHShanko Posts: 402
    edited 2012-06-21 15:36
    JonnyMac,

    Why the -544 value? Is that explained anywhere?

    And why "t := -cnt" instead of just 'cnt'?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-21 15:40
    I have some links to tutorials in post #3 of my index.

    JonnyMac's Spin Zone articles really helped when I was learning to program in PASM.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-06-21 15:41
    Duane Degn wrote: »
    I have some links to tutorials in post #3 of my index.

    JonnyMac's Spin Zone articles really helped when I was learning to program in PASM.

    I've read enough of Jon's stuff to know this.

    The "-544" compenstates for the overhead of setting up the timer.

    By using "t := -cnt" and then adding the later value of cnt, you can have a timer with just one variable.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2012-06-21 15:50
    As others have said, the 544 is overhead time. If you were to run the program without the -544 and without any test code, the number returned would be 544. So to compensate and 'zero out the reference' you subtract 544 from your result. That way any code that you do place within the your test code goes here is properly represented.
    CON
    
      _CLKMODE = XTAL1 + PLL16X
      _XINFREQ = 5_000_000
    
    
    OBJ
    
    PST           : "Parallax Serial Terminal"
    
    
    PUB TimingTest|t
        PST.Start(19200{<- Baud})
    
        repeat
          t := -cnt
    
          ' your test code goes here
          
          t += cnt - 544
    
          PST.dec(t)
          PST.Char(13)
    
  • cavelambcavelamb Posts: 720
    edited 2012-06-21 16:42
    Duane Degn wrote: »
    I have some links to tutorials in post #3 of my index.

    JonnyMac's Spin Zone articles really helped when I was learning to program in PASM.


    That's another big +1.

    I can dig and experiment and eventually find something that will work.
    But working examples are priceless.
  • krazyideaskrazyideas Posts: 119
    edited 2012-06-21 17:02
    Thanks for all the info guys this is great.
    I'll let you know when I find some more questions

    Take care
  • krazyideaskrazyideas Posts: 119
    edited 2012-06-22 19:32
    So just one more question.
    My cousin was telling me about C++ and how awesome it is, and how you write in a high level language like Spin. But when it is complied it is complied into assembly and therefore no slower then assembly.

    So I was wondering if that was true (or if there are some BUT's to be considered) and also if the Prop can understand C++ ??
  • pik33pik33 Posts: 2,397
    edited 2012-06-23 00:43
    There is C (not C++) for the Propeller. At least two different compilers are available: gcc and Catalina. C++ is a new version of C with all these class/object stuff
  • KyeKye Posts: 2,200
    edited 2012-06-23 09:44
    As long as you do not use dynamic objects you can write C++ code for the propeller. GCC compiles C++ code.

    The limitation is that you cannot use all of the C++ features. Dynamic objects are a no-no for example. (Dynamic objects are where you create new objects during run time and they are not statically allocated).
  • jazzedjazzed Posts: 11,803
    edited 2012-06-23 10:32
    Kye wrote: »
    As long as you do not use dynamic object you can write C++ code for the propeller. GCC compiles C++ code.

    The limitation is that you cannot use all of the C++ features. Dynamic objects are a no-no for example. (Dynamic objects are where you create new objects during run time and they are not statically allocated).


    As Kye points out Propeller GCC C++ can be used with Propeller when used within reason.
    Using std:: namespace for things like std::cout requires a big solution like C3 or others 1MB flash + SRAM.

    Kye, Ted (denominator) added support for small dynamic objects in his tiny library for LMM mode.
    I haven't tested this aspect yet, but his work is quite good, so I expect it should work.
    See this wiki for more info: http://code.google.com/p/propgcc/wiki/PropGccTightMemory
  • KyeKye Posts: 2,200
    edited 2012-06-23 10:55
    As long as you do not use dynamic object... I hate when I post something with improper grammar.

    I haven't tried any prop GCC stuff yet. I've been busy with CMUcam4. SparkFun is going to be selling them soon. I have to prepare for a big release.
  • jazzedjazzed Posts: 11,803
    edited 2012-06-23 11:07
    Kye wrote: »
    I hate when I post something with improper grammar.

    Me too, but I've gotten over it mostly. My wife of 20+ years is Thai, and we influence each other's language habits.

    Kye wrote: »
    I haven't tried any prop GCC stuff yet. I've been busy with CMUcam4. SparkFun is going to be selling them soon. I have to prepare for a big release.

    No pressure :) Best of luck with your release!
  • krazyideaskrazyideas Posts: 119
    edited 2012-06-23 18:48
    So Still in the air is "Is C just as fast as assembly?"
    What I am wanting to do is covert a Spin program I have into assembly but I am haveing a heck of time doing it and am looking for another way to get the speed of assembly, because my brain is rejecting the learning of assembly.

    Thanks
  • krazyideaskrazyideas Posts: 119
    edited 2012-06-23 18:48
    So Still in the air is "Is C just as fast as assembly?"
    What I am wanting to do is covert a Spin program I have into assembly but I am haveing a heck of time doing it and am looking for another way to get the speed of assembly, because my brain is rejecting the learning of assembly.

    Thanks
  • Cluso99Cluso99 Posts: 18,069
    edited 2012-06-23 19:33
    C has a big overhead, and for all but the smallest programs runs some form of LMM/XMM mode (where the code is held in hub or external memory). This introduces slighlty more than 4 times slower operation (because each instruction is fetched from hub/external memory into the cog before it is executed.

    I am not sure, but I would expect that both Catalina and GCC can output a listing. If so, then why not write your code in C and look at the output. Then you can use this as the basis for your PASM code. Of course, if you can get away with the size and slower overheads of C, then just use it.
  • jazzedjazzed Posts: 11,803
    edited 2012-06-23 20:02
    krazyideas wrote: »
    So Still in the air is "Is C just as fast as assembly?"
    What I am wanting to do is covert a Spin program I have into assembly but I am haveing a heck of time doing it and am looking for another way to get the speed of assembly, because my brain is rejecting the learning of assembly.

    Thanks

    Propeller GCC supports different modes COG, LMM, and the XMM variants.
    COG C programs run in a COG, not LMM.

    Therefore COG C programs will run as fast as PASM in a COG.

    That is not to say that it will be as good as hand-crafted PASM programs.
    For one thing, COG C programs use some HUB RAM as stack space if necessary.
    We have some drivers written in COG C and some example programs.

    It is possible write a driver with COG C that can be used with Spin.
Sign In or Register to comment.