Shop OBEX P1 Docs P2 Docs Learn Events
Help with PWM object, and Spin - Solved — Parallax Forums

Help with PWM object, and Spin - Solved

JasonDorieJasonDorie Posts: 1,930
edited 2009-02-03 07:42 in Propeller 1
I have·a simple PWM motor driver that I downloaded from the Obex which I'm using to drive the enable pin on a dual H-bridge.· Initially I included the PWM object in my top-level spin file, set the output ports, ran the PWM object, and all was well.

I've moved the PWM object into another object which will handle both direction and speed in a single call, but it doesn't work.· I'm guessing it's something very trivial, and I'd love another pair of eyes on it.

In the attached 'MotorControl.spin' file, the SetSpeed() function is currently hardcoded to call PWM.SetDuty() with a constant value.· This works as expected.· If I change that line to use the absolute value of the 'Speed' variable instead, it no longer works.· I have verified with SimpleDebug that the 'Speed' value being passed is correct.· Can anyone spot anything wrong with this?

Thanks in advance,
Jason


Post Edited (JasonDorie) : 2/3/2009 5:55:36 AM GMT

Comments

  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-02-03 01:22
    6 downloads and no replies? Hmmm... Perhaps it's not as trivial as I thought. I spent another hour on this last night and still can't see anything wrong with it. More debugging tonight.

    If anyone spots anything I'll be grateful.

    Thanks,
    Jason
  • mojorizingmojorizing Posts: 249
    edited 2009-02-03 03:39
    I usually hang out in the SX forum, but I just got my prop proto board and I made an Led blink, so I guess I'm an expert now.........

    anywhoo, if I add a couple of lines to the motorcontrol object, does it work as you expect?· I've added the variable "Kevin", and the Setspeed argument now makes it thru to the Setduty method.

    {{
      MotorControl
    }}
    CON
      _xinfreq = 5_000_000                      
      _clkmode = xtal1 | pll16x
    OBJ
      PWM   : "PWMAsm.spin"
    VAR
      long DirPinA, DirPinB, Kevin
      
    PUB Start( _DrivePin, _DirPinA, _DirPinB ) | ok
      DirPinA := _DirPinA
      DirPinB := _DirPinB
      DirA[noparse][[/noparse] _DrivePin ] := 1
      DirA[noparse][[/noparse] DirPinA ] := 1
      DirA[noparse][[/noparse] DirPinB ] := 1
      ok := PWM.Start( _DrivePin )
      PWM.SetPeriod( constant(80_000_000 / 20_000) )
      SetSpeed( 0 )
    
    PUB SetSpeed( Speed )
       Kevin := || Speed
       
      if( Speed >= 0 )
        OutA[noparse][[/noparse]DirPinA] := 1         
        OutA[noparse][[/noparse]DirPinB] := 0
      else
        OutA[noparse][[/noparse]DirPinA] := 0
        OutA[noparse][[/noparse]DirPinB] := 1
      'PWM.SetDuty(40 )
      PWM.SetDuty( Kevin)
                      
    


    ·This is from a total prop newbie, maybe one of the forum heavies will put this straight....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Bad spellers of the world untie!
  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-02-03 04:16
    I went a little farther.· This original code doesn't work:· (the PWM.SetDuty function is called, but seems to get a zero argument)
    PUB SetSpeed( Speed )
      if( Speed >= 0 )
        OutA[noparse][[/noparse]DirPinA] := 1
        OutA[noparse][[/noparse]DirPinB] := 0
      else
        OutA[noparse][[/noparse]DirPinA] := 0
        OutA[noparse][[/noparse]DirPinB] := 1
     
      PWM.SetDuty( ||Speed )
    
    

    ·...so I changed it to this, which also doesn't work:
    PUB SetSpeed( Speed ) | AbsSpeed
      if( Speed >= 0 )
        OutA[noparse][[/noparse]DirPinA] := 1
        OutA[noparse][[/noparse]DirPinB] := 0
      else
        OutA[noparse][[/noparse]DirPinA] := 0
        OutA[noparse][[/noparse]DirPinB] := 1
     
      AbsSpeed := ||Speed
      PWM.SetDuty( AbsSpeed )
    
    

    ...however if I change it to this, now it works:
    PUB SetSpeed( Speed ) | AbsSpeed
      AbsSpeed := ||Speed
      if( Speed >= 0 )
        OutA[noparse][[/noparse]DirPinA] := 1
        OutA[noparse][[/noparse]DirPinB] := 0
      else
        OutA[noparse][[/noparse]DirPinA] := 0
        OutA[noparse][[/noparse]DirPinB] := 1
    
      PWM.SetDuty( AbsSpeed )
    

    All I did was move the AbsSpeed assignment above the if, instead of below, and it does what it's supposed to.· Am I misunderstanding something, or is this indeed a PropTool compiler bug?· This is with both V1.1 and V1.2.5

    Jason
    ·
  • mparkmpark Posts: 1,305
    edited 2009-02-03 05:21
    >= might not mean what you think it does.

    "a >= b" means "a := a > b"

    I think you want to use => ("greater than or equal")
  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-02-03 05:50
    That's the first time I've hit that one.

    While this is probably going to sound like sour grapes, making operators that are visually equivalent to another language (C/C++) that have dramatically different meaning seems like a bad idea. At the very least, a compiler warning that I was doing an assignment in a conditional statement might've offered a clue.

    I also recently discovered that >>= is shift right, while ~>= is arithmetic shift, as opposed to doing them the other way around and following the C/C++ convention for the arithmetic shift. I understand that it's too late to change this (existing code written to spec would break) but again, a compiler warning could be useful here too.

    Complaints aside, thanks for telling me that - It explains a lot, and hopefully I'll remember for next time. [noparse]:)[/noparse]

    Jason
  • mparkmpark Posts: 1,305
    edited 2009-02-03 06:53
    If you've ever studied a foreign language, just think of these as "false friends."

    Besides, Spin is just taking C's op= notation to its logical conclusion.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2009-02-03 07:27
    I'll probably get some naysayers coming out by me saying this, but C is lke the english of languages, a hodge podge of things and standards are frequently broken. Spin is a little more structured, sorta like early latin where there is concerted effort to establish standrds and to consistantly follow them.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
  • JasonDorieJasonDorie Posts: 1,930
    edited 2009-02-03 07:42
    Paul / mpark - I generally agree.· C isn't without its own quirks, however in another parallel to english, C is one of the more 'universal' programming languages around.· If you're going to write a language with as much similarity in the operators as there is between Spin and C, breaking that similarity for two (possibly more - I don't know for sure) seems like a potential stumbling block for a lot of people.· I think the Propeller is a really cool chip, but I find some of the oddities of the language and tools frustrating, and that's made worse by having no debugger.

    I'm not really complaining - I expect a learning curve, but I've been bitten by a few things like this that seem like obvious no-no's to me.· I'm also biased because I know C/C++/C# quite well, and all of them have very similar operators.· I'm not familiar with other languages that might have influenced the design of Spin, so maybe there are solid reasons I'm not aware of.

    Jason
    ·
Sign In or Register to comment.