Shop OBEX P1 Docs P2 Docs Learn Events
Need some formula help please: Time to PWM duty conversion — Parallax Forums

Need some formula help please: Time to PWM duty conversion

T ChapT Chap Posts: 4,223
edited 2008-10-03 17:43 in Propeller 1
Can one of you math wiz's please help me find a formula for this?

I am creating a profile of a speed curve generated in Spin that is made up of pulses. Waicnt sets the time/speed between pulses. I need to convert the time into duty to control a motor.

This is only a very simplified example below:


MaxSpeed := 400           'variable rate set by user for use in waitcnt
MinSpeed := 100_000     'variable rate set by user 
aRate := 200                   'rate to change the accel value, speeds up waitcnt

PUB RampUp
        Repeat 
           position =+ 1                   'increment the position of the profile pulses
           AccelRate := MinSpeed     'example 100_000
           waitcnt[noparse][[/noparse]accel + cnt]    'wait 100_000   , convert ACCEL to PWM, inverted
           ConversionVal :=      (spin formula)                         '<<<<<<<<<<<<
           PWMupdate(ConversionVal)  'duty value 0 - 1000
           accel := accel -  aRate        'adjust accel each loop making it faster





I don't yet know the Pulses Per Second maximum range to the motor for the profile curve. I am sure that 400 MinSpeed is way to fast for the application, it is just an example. The variables are MaxSpeed and MinSpeed. The PPS (pulses per sec) will eventually translate into IPS (inches per sec) in the real world.

The PWMupdate is my own method that uses the PWM obj. I set the parameter to be a value from 0 (0 duty) to 1000 (100% duty). I used 1000 for smoother resolution of change in PWM.

I need to covert Time ( MaxSpeed (ex 400) and MinSpeed (ex 100_000) which are delays in the repeat loop, which reflects speed in the profile, to PWM duty, which reflects speed on the motor. PWM duty to speed ratio is not yet know.

100_000 waitcnt is to 0 duty, as 400waitcnt is to 1000 duty(100%duty)

Any scalable formula that you could suggest would be really welcome.

EDIT: actually, the real value to send to PWM is Accel. It just needs to be inverted to to speak, as it gets lower while duty gets higher.

Thanks

Post Edited (Originator) : 10/2/2008 12:54:47 AM GMT

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,562
    edited 2008-10-02 04:02
    Originator,

    If this is a linear relationship, then something like this might work....


    ' ConversionVal := (spin formula)

    ConversionVal := ((100_000 - accel) * 10) / 996


    Edit:

    To make this a little more dynamic to user changes....

    divisor := (MinSpeed - MaxSpeed) / 100 '<--- Here 100 is duty divided by 10
    ConversionVal := ((MinSpeed - accel) * 10) / divisor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 10/2/2008 4:13:02 AM GMT
  • T ChapT Chap Posts: 4,223
    edited 2008-10-02 07:15
    Thanks Beau, I will try it out.
  • T ChapT Chap Posts: 4,223
    edited 2008-10-03 04:52
    Beau

    Made one change to get the Duty from 0-1000, works great! Many thanks for that formula. I lost plenty hours and got no where with it.

    Ranther than try to get too involved in PID which is foreign territory, what I have done is create a speed profile with pulses using waitcnt(accel + cnt), modifying the accel over the travel. Then use the formula to convert speed(accelval) to PWM to drive a motor. For feedback, read the encoder, compare the encoder counter with the profile generator counter, subtract and apply the error back into the formula's result. If the error is +20, subtract 20(X) from the PWM. Not sure what the X factor will be yet. With the gearbox there are 16000 pulses per rev, or approx 4000 pulses per inch. So I an determine pulses per inch easily, which gives me inches per second values to set the speed in IPS.

    If it were as simple as taking my error and running it through the PID object to get a better method of correction, that would be great.

    I think the trick will be tuning the PWM output to the pulses to get the precise actual motor speed (IPS) based on the actual time between pulses. That means there are some spin instructions to factor in. I am thinking that setting up a scope to a pin and running the loop to get the final time (including the instructions + waitcnt values) will be the best method to get the real time between pulses.

    Post Edited (Originator) : 10/3/2008 5:01:10 AM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,562
    edited 2008-10-03 06:20
    Originator,

    Cool!·· What was the code change that made it happen?·- just curious

    From reading your post, I thought that the duty was already from 0 to 1000 was it not?

    In your post: PWMupdate(ConversionVal)··'duty·value·0·-·1000



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • T ChapT Chap Posts: 4,223
    edited 2008-10-03 17:43
    My mistake Beau, I think I may have been referring to notes that were using the /996 that could have been producing the duty range 0-100, or maybe just an oversight. The edit you added is in fact what I am using exactly that produces 0 -1000, which amazes me that such a random batch of input can produce an exact range of output.
Sign In or Register to comment.