5.3. Setting up frequency feedback
This section explains how to connect a frequency feedback signal to the Jrk G2 and configure the Jrk G2 to do PID speed control. Please note that this is different from setting up RC control, which is documented in Section 6.5.
Also, please note that the Jrk cannot detect the direction your motor is moving in this mode. It will measure the frequency on FBT using one of the methods described below and set the “Feedback” variable to 2048 plus or minus the frequency measurement (restricted to be between 0 and 4095). It will use “plus” if the “Target” variable (which specifies the desired speed) is 2048 or more, and it will use “minus” otherwise.
Connecting frequency feedback
If you have not done so already, you should follow the instructions in Section 4.3 to configure and test your motor. Next, with the system unpowered, connect your frequency feedback signal to the Jrk as described below.
You should connect the frequency feedback signal to the Jrk’s FBT pin. The Jrk will measure the signal on this line as a digital input. The voltage on FBT should be between 0 V and 5 V with respect to GND; signals outside of this range could damage the Jrk. The FBT pin is pulled up to 5 V by an on-board 100kΩ resistor.
Connecting tachometer feedback to the Jrk G2.
The sensor’s ground pin should be connected to a GND pin on the Jrk, and you can also use the nearby 5V pin to the power the sensor if needed.
If you are using one channel of a quadrature encoder to do speed control on the Jrk G2, here are some things to keep in mind:
- The Jrk G2 does not support position control with a quadrature encoder; this section is only about setting up speed control.
- The Jrk can only use one of the two signals from the quadrature encoder. That signal should be connected to FBT, while the other one is left disconnected.
Configuring frequency feedback
Now connect your Jrk to your computer via USB. In the Jrk G2 configuration utility, go to the “Feedback tab” and set the “Feedback mode” to “Frequency (speed control)”. Also, if you changed any of the settings in the “Scaling” box, you should make sure to uncheck the “Invert feedback direction” box and click the “Reset to full range” button to effectively turn off feedback scaling.
You will also need to pick your frequency feedback measurement method and configure the other frequency feedback settings appropriately. The Jrk G2 supports two measurement methods: “Pulse counting” and “Pulse timing”, and they are documented below.
Determining the tachometer frequency range
It is important to know what range of frequencies you expect to get out of your motor/tachometer setup before proceeding. The frequency that matters is the number of pulses on the FBT pin per second. For example, if you expect 1500 pulses on the FBT pin per second, that would be a frequency of 1500 Hz, which can also be written as 1.5 kHz.
You should refer to the documentation of your motor/tachometer setup, while also considering your power supply and desired speed range, in order to calculate the frequency range. Keep in mind that if the encoder documentation indicates that the encoder is, say, 12 CPR (12 counts per revolution), that actually means 3 pulses per revolution for the purpose of Jrk frequency feedback. This factor of 4 difference (between 12 and 3) is because the Jrk G2 is only measuring one of the two signal lines, and there are two encoder counts per pulse. You will also need to know whether the encoder is connected to the output shaft of the motor or a high-speed shaft on the rear of the motor, since that affects whether you need to account for the gear ratio (by dividing or multiplying by it) in your calculation.
Pulse counting mode (faster tachometers)
In this mode, the Jrk G2 will count the number of rising edges that happen on the FBT pin each PID period (which is 10 ms by default).
With the default settings, the Jrk will set the “Feedback” variable to 2048 plus or minus the number of rising edges that happened on the FBT pin in the last PID period, which is 10 ms.
These default pulse counting settings are probably not appropriate if you want to do speed control at frequencies below 5 kHz. At frequencies below 5 kHz, there would be fewer than 50 pulses during each 10 ms PID period. It might be difficult to achieve good PID speed control results when working with such small numbers. Therefore, if you need to measure frequencies slower than 5 kHz, you should probably use pulse timing mode, which is described later in this section.
In pulse counting mode, the “Pulse samples” setting is the number of consecutive pulse counts to add together. The default pulse samples value is 1, and it can be set to any whole number between 1 and 32. For example, with a value of 8, the Jrk will set the “Feedback” variable to 2048 plus or minus the number of pulses in the last 8 PID periods. This can help with measuring lower frequencies, but would also lower the response time of the Jrk since the “Feedback” variable would be considering the speed over a longer period of time. Another way to get more counts in pulse counting mode is to increase the PID period.
The frequency measurement is divided by the value of the “Frequency divider” setting before it is added to or subtracted from 2048 in the calculation of the “Feedback” variable. If your tachometer is fast enough that you expect to get more than 2047 counts per PID period (which would saturate the “Feedback” variable, since it is limited to be between 0 and 4095), you can increase the frequency divider, allowing you to measure a more frequencies on the high end, but also reducing your ability to measure lower frequencies.
Pulse timing mode (slower tachometers)
In this mode, which is appropriate for most of our gearmotor/encoder products, the Jrk G2 will measure the width (duration) of pulses and calculate the corresponding frequency by taking the reciprocal of the pulse width (scaled by various constants). The Jrk will set the “Feedback” variable to 2048 plus or minus this calculated frequency.
The “Pulse samples” setting, which can be set to any whole number between 1 and 32, determines how many pulse widths the Jrk will average together when calculating the frequency. It is generally a good idea to learn how your encoder works and set this setting appropriately to cancel out physical variations in the encoder, which could cause the pulse widths to vary. For example, consider a 12 CPR (counts per revolution) encoder that is based on a rotating disk with three slots. Since the three slots will have slightly different widths, you might want to set “Pulse samples” to 3 in order to average the last 3 pulses, which would imply you are measuring one pulse from each slot, and thus any variation you see in the averaged pulse width is due to the speed changing, instead of which slot the Jrk happens to see last before doing PID calculations. If your tachometer frequency is close to or higher than your PWM frequency (20 kHz or 5 kHz), then the Jrk might miss some pulses, and that could defeat the point of this averaging because the average would no longer come from consecutive pulses.
The “Pulse timing polarity” setting determines whether to measure low pulses or high pulses. The default is “Active high”, which means the Jrk is measuring high pulses. If the low pulses produced by your tachometer have less variation than the high pulses (in terms of percentage change), it would make sense to change the polarity to “Active low” so the Jrk can measure the low pulses and therefore have less variation in its readings.
The “Pulse timing timeout” setting lets you specify a timeout in milliseconds. When the Jrk has not recorded any pulses in the specified amount of time, it records a maximum-width pulse (65535 in units of pulse timing clock ticks). This mechanism is what allows the Jrk’s “Feedback” variable to reset when the motor has stopped. You can generally leave this setting alone. If you decrease it below its default value of 100 ms, it might affect the lower range of the frequencies you can measure.
The “Pulse timing clock” is the frequency of the 16-bit timer that is used to measure pulses. This clock speed matters because the Jrk cannot measure any pulses that are longer than 65535 divided by the pulse timing clock. For example, at 1.5 MHz the slowest pulse it can measure is 65535 divided by 1.5 MHz (43 ms), which corresponds to a tachometer frequency of 1.5 MHz divided by 65535 divided by 2, or 11.4 Hz. The factor of 2 in the calculation comes from the fact that the Jrk only measures one of the two pulses (either the high one or the low one) during each period of the tachometer signal, and we are assuming that the high and low pulses are roughly equal, so each pulse has a 50% duty cycle. (The Jrk’s pulse timing frequency feedback can work with signals where the high and low pulses are not equal, but many of the calculated frequency numbers in this section would be inaccurate in that case.)
The Jrk will measure a pulse width (or average together multiple pulse widths) in units of the pulse timing clock ticks, and it will be a number between 0 and 65535 (0xFFFF). To convert this pulse width to a frequency, it will take 2 raised to the power of 26 (0x4000000, or 67,108,864) and then divide it by the pulse width. It will then divide this reading by the “Frequency divider” setting, which is a power of two between 1 and 32768. Finally, it adds or subtracts the reading from 2048 in order to set the “Feedback” variable, which is constrained to be within 0 to 4095.
Therefore, there are two settings that determine what range of frequencies you can measure in pulse timing mode: the “Pulse timing clock” setting and the “Frequency divider” setting. The Jrk G2 Configuration Utility takes these two settings into account when it calculates the frequency measurement range, which you can see in the “Feedback” tab, after you have set the feedback mode to “Frequency”. This frequency measurement range is not absolute: it makes some assumptions about how much resolution you would want to have in the pulse width and frequency measurements. However, it should give you a good estimate of what frequencies you can expect to measure with your selected pulse timing settings.
To choose your “Pulse timing clock” and “Frequency divider” settings, we recommend following this procedure, while looking at the displayed frequency measurement range:
- Set the “Frequency divider” to 32, while leaving the “Pulse timing clock” at its default value of 1.5 MHz. This should give you a frequency measurement range of 17.9 Hz to 715 Hz.
- If you do not need to measure frequencies as high 715 Hz, try decreasing the frequency divider one step at a time (which basically just lowers the upper end of the range), while making sure the frequency measurement range still contains all the frequencies you care about measuring. This will give you more resolution when converting the pulse width reading to a feedback value, allowing the PID algorithm to work with more counts.
- If you need to measure frequencies higher than 715 Hz, start increasing the “Pulse timing clock” until the frequency measurement range contains all the frequencies you care about measuring. Once you achieve this, you should not increase the clock frequency any further, since that will reduce your ability to measure low frequencies. The highest frequency you can measure with this method is 22.9 kHz.
- If you need to measure frequencies higher than 22.9 kHz, leave the pulse timing clock at its maximum value of 48 MHz and start increasing the frequency divider.
The table below shows the settings you can arrive at by following this procedure, and some of the resulting properties of the Jrk’s frequency measurement system:
|Frequency measurement range
(assuming 50% duty cycle)
|1.5 MHz||1||58982 to 33554||3185 to 4048||12 Hz to 22 Hz|
|1.5 MHz||2||58982 to 16777||2616 to 4048||12 Hz to 44 Hz|
|1.5 MHz||4||58982 to 8388||2332 to 4048||12 Hz to 89 Hz|
|1.5 MHz||8||58982 to 4194||2190 to 4048||12 Hz to 178 Hz|
|1.5 MHz||16||58982 to 2097||2119 to 4048||12 Hz to 357 Hz|
|1.5 MHz||32||41943 to 1048||2098 to 4048||17 Hz to 715 Hz|
|3 MHz||32||41943 to 1048||2098 to 4048||35 Hz to 1.43 kHz|
|6 MHz||32||41943 to 1048||2098 to 4048||71 Hz to 2.86 kHz|
|12 MHz||32||41943 to 1048||2098 to 4048||143 Hz to 5.72 kHz|
|24 MHz||32||41943 to 1048||2098 to 4048||286 Hz to 11.4 kHz|
|48 MHz||32||41943 to 1048||2098 to 4048||572 Hz to 22.9 kHz|
|48 MHz||64||20971 to 524||2098 to 4048||1.14 kHz to 45.8 kHz|
|48 MHz||128||10485 to 262||2098 to 4048||2.29 kHz to 91.6 kHz|
|48 MHz||256||5242 to 131||2098 to 4048||4.58 kHz to 183 kHz|
|48 MHz||512||2621 to 65||2098 to 4048||9.16 kHz to 369 kHz|
|48 MHz||1024||1310 to 50||2098 to 3358||18.3 kHz to 480 kHz|
The frequency measurement range shown in the table above (which is the same as the range shown in the Jrk G2 Configuration Utility) and the other numbers reported in the right three columns of this table are not absolute limits. These numbers were made using some assumptions about how much resolution you want to have in the pulse width and frequency measurements. However, these numbers should give you a good estimate of what frequencies you can expect to measure with different pulse timing settings. The numbers in the table were calculated with the assumption that the pulse timing timeout is set to its default value of 100 ms or higher.
Setting initial PID coefficients
You will need to set PID coefficients to make the Jrk respond to the frequency feedback. In frequency feedback mode, the integral coefficient serves the same role that the proportional coefficient does in PID position feedback: it accumulates errors in the measured speed over time, so it is actually a measurement of a position error. So the first step to setting up PID coefficients is to set the integral coefficient, while leaving the proportional and derivative coefficients set to zero. In some of our tests, we found that a good starting point is to set the “Integral limit” setting to something high like 32000 (or its maximum value of 32768), and then set a relatively low integral coefficient, like 0.03125 (1/32).
Testing frequency feedback
After you set up your frequency measurement settings and initial PID coefficients, you should test the frequency feedback by dragging the slider at the bottom of the window and looking at the variables in the “Status” tab. You should open the graph window in the Jrk G2 Configuration Utility and plot the “Target”, “Scaled Feedback”, “Integral”, and “Duty cycle” variables. It is important to look at the “Scaled feedback” to make sure that it reflects the speed of your system and that its value gets close to the “Target” value, which is set by the slider. It is also important to watch the “Integral”, and see how it accumulates speed errors over time. In some cases, the motor might be stationary while the integral builds for several seconds, until the integral term is finally large enough to move the motor. You can use the “Integral divider” setting if you want to decrease how fast the integral builds up.
Tuning PID coefficients
If your initial tests are successful, you might try to further tune the PID coefficients. We are working on adding more information about tuning PID coefficients to this section. In the meantime, you might find the “Tuning the PID constants” subsection of the Setting Up Your System section in the user’s guide for the original Jrk controllers to be helpful, even though it was written for a different product. However, keep in mind that that section was written for systems doing position feedback. The Jrk’s integral coefficient in a speed feedback system will act like a proportional coefficient in a position feedback system, and the proportional coefficient in a speed feedback system will act like a derivative coefficient in a position feedback system.