Shop OBEX P1 Docs P2 Docs Learn Events
using multiple frequencies to control a stepper motor. - Page 2 — Parallax Forums

using multiple frequencies to control a stepper motor.



  • Is there a way to set up the two counters with different frequencies and duty cycles.
  • The PWM object you are using has one frequency setting that is used by both channels. We could modify it so that you can set the frequency for each channel. We would have to add a method that can dynamically change the frequency as well. The duty cycle can be set to 50% for the channel that uses the stepper motor. Which channel uses the stepper motor, RA or DEC?
  • RA is a stepper
  • In my telescope test2b program I was able to set the registers in the two counters independently and in telescope mountDH12 their doesn't seem to be a way to set the counters independently , is this true, or am I missing something?

  • Dave HeinDave Hein Posts: 6,347
    edited 2020-12-07 21:02
    @bbrien, try the attached code to see if it works. I created a simple stepper object that works with a controller that accepts clock and direction inputs. Based on your initial post I think that's what you need.
  • bbrienbbrien Posts: 561
    edited 2020-12-08 00:46
    Great I'll give it a try tomorrow.


  • @bbrien, I found a couple of bugs in stepper.spin. You should use this version of stepper.spin instead of the one that I posted earlier. The default value of SCALE is now 40 instead of 100, and I pre-scaled clkfreq by 20 instead of 50. The value of 50 was causing an overflow.

    You can adjust the value of SCALE to get the desired speed that you want. If you set SCALE to 1 you can hook an LED up to the stepper clock pin and see the toggle rate change depending on which buttons you press. I tested it by typing "4" and "8" on the console. I also tried typing "d" and "h" to get the highest speed.
  • Okay Dave I checked and I have a Freq. of 27 then153Hz with duty cycle approx. 50% and then 30 % duty cycle and freq. of 30% at 100 Hz.
  • The duty cycle should be 50%, except for one cycle when changing from one frequency to another. The frequencies should be either 75, 225, 375, 525 or 675. I'll run some more tests to see if it's working correctly.
  • I added some debug code to measure the duty cycle and frequency, and everything looks OK. I measured 50% for the duty cycle, and I get the correct frequencies.

    How are you measuring the frequency and duty cycle? Did you run the code I posted, or did you modify it?
  • code is unmodified as you sent. I am looking at the output pin directly. Did you read SMD1,it requires a freq. between20 and 80Hz but I have had it run as slow as 8Hz.
  • The 20 to 80Hz range refers to the test oscillator in the SMD1. You can run a stepper motor from DC to some upper limit that depends on the motor and the load. I've never used a stepper motor, but from what I can tell the maximum speed is around 1000 RPM. If a motor requires 120 step per revolution that works out to a maximum step frequency of 2000 Hz.

    I don't know the specs for your stepper motor, or how it is geared. I assume you want the sidereal rate to be 1 revolution in 24 hours. From the specs you should be able to calculate what frequency you'll need to achieve that. You can tweak the speed a bit by adjusting the value of SCALE in stepper.spin. It is currently set at a value of 40, which sets the frequency to the value passed to the set_speed method. The top object currently uses a value of 75 for the sidereal speed, so the sidereal frequency is 75 Hz. If you change SCALE to 80 it will double the frequency, and the sidereal frequency would then be 150 Hz. If you change SCALE to 20 it will reduce the sidereal frequency to 37.5 Hz. To get an exact frequency you will probably need to change the values in the top object.

    You never really answered the question about how you measured the frequency and duty cycle. Did you use an oscilloscope, or some other device to measure it?
  • I used a Velleman LCD hand scope which I've had for about 18 years. Let me check the output frequency of the unit I have ready for Thailand. It uses a arduino using counter 2 to generate the pulse ,I think it's around 8-10 Hz.
  • Here is a simple frequency generator to test and drive your clock pin, Pinx is your output pin, you don't need the LCD, has serial term.
  • Dave HeinDave Hein Posts: 6,347
    edited 2020-12-11 00:20
    @bbrien, I added some more debug code to record every time the RA output pin toggles. I imported the timestamps into an Excel spreadsheet and then graphed the frequencies based on the data. The attached graph shows the resulting frequencies when typing in the characters 4, 8, D, H, T, X, d and h. There's a little bit of jitter in the frequencies due to the speed of Spin code, but overall the frequencies are correct. I computed the frequency from the duration of each half cycle, so if the duty cycle deviated from 50% this would show up in the resulting frequency.
    1135 x 801 - 84K
  • I did a test run using the Telescope mount DH12 which has only dual motor ez mod OBJ and ran the MC3479 with associated circuits and using frequencies ranging between 20 and 80 Hz, and have good performance, but when I try to go below 20 Hz the prop PWM flatlines. What happened?
  • Dave HeinDave Hein Posts: 6,347
    edited 2020-12-11 02:06
    The set_speed method in dual_mode_ez_MOD.spin computes the number of cycles in a pulse using "speed := speed * pwmticks / 100_0". At 20 Hz, pwmticks has a value of 4_000_000. If speed is greater than 536 this will cause an overflow, and will result in a negative value.

    Did you try DH14? If the frequencies are too high reduce the value of SCALE in stepper.spin. If you want frequencies less than 80 Hz you could try setting SCALE to 4. If that's too slow try setting SCALE to a larger number between 4 and 40.
  • bbrien,

    Does your stepper motor list the manufacturer, model, or any of the specs on it?
  • Genetix: no it doesn't, it's sold by by Orion telescope as part of the dual axis motor drives for the EQ-2 mount.
    Dave H; I have the MC3479 circuit running at 9.4Hz Which is 5 on the "scale". I will probably continue in this direction but I will try DB's freq. generator.
    Thanks loads.
  • The frequency synth. works but can you show the incorporation into 2b-7 which is included.
  • @bbrien, why don't you include the frequency synth. in dh14 instead? You would replace stepper.spin with synth_vision.spin, and call Synth instead of set_speed. You'll need to scale the value that you pass to Synth to get the appropriate frequency.

    To be honest, I don't think you'll see much performance difference between using the counters versus bit-banging it in Spin.
  • The external step drivers make it easy. All you need to do is tell a pin to go high or low for direction and enable when you push a button, then have preset frequencies for speed. I would include an ADC and have that read a pot for speed control.
  • no pots in my circuits.
  • In stepper.spin we set the freq. in the object but how do I scale the freq. in synth, I need maybe to add a line to the program or do I scale in the object?
  • In my Hz. program Avg2 set the frequency, In your program have a preset frequency when you hit a certain button. I can't do any programming for you, I have my own projects to work on.
  • Hi Dave ; Using Telescope mount DH14 as a template I replaced Stepper.spin with SJynth.spin in the OBJ and added two longs(AVG , AVG2 ) to VAR. Added a PUB OutputFrequency
    Avg2 := 9
    Freq.Synth("B",Pin, Avg2) before the main PUB and got an error(expected an instruction or a variable) obviously in the wrong order, where should I have put it?
  • @bbrien, please attach your spin files. It's difficult to determine what changes you made based on your description.
  • This is where I am at now.
  • You don't have an object named Freq declared. You do have an object step1 that uses the file Synth.spin. So you should reference the Synth method using step1.Synth instead of Freq.Synth.

    You don't have a variable named Pin declared. I assume this is for the RA motor, so you should use RA_SPEED instead.

    Since you are not using stepper.spin you should remove the call to step1.start.
  • Made the suggested changes but still have error on motor.start(-1, -1, -1, -1, DEC_SPEED, DEC_DIR, PWM_FREQ). PWM_FREQ is highlighted and error is "expected expression term.
Sign In or Register to comment.