PDA

View Full Version : Speed regulator for sewing machine



Kenwood
- 24th December 2013, 21:56
Hope to get some help or advice.
I am new to programing and trying to learn. This is also my first post.
I am trying to design a speed regulator for a sewing machine used for quilting. The sewing machine is on an X, Y table and I need to keep a constant number of stitches regardless whether sewing machine moves X or Y or diagonally. My encoders on the tacks give me 55 pulses per inch of travel and I feed these signals into the inputs of the Pic. I take the pulse width of each encoder convert the value back to hertz. I took these values of X and Y and do a hypotenuse calculation on them and take the value and feed a PWM output. The Pic works on the bench using signal generators etc but in practice it has problems. There is far too much time delay during acceleration and deceleration. I.e. it takes .65 second to sense a 0 pulse in. This circuit uses a 4 MHz crystal. I tried 20 mHz and it improved but causes other problem. Thanks



' PULSIN Command
' 16F88
' Serial Pulse Width Meter
Include "modedefs.bas" ' Include serial modes

DEFINE OSC 4



SO VAR PORTB.5 'Define serial out pin
FI VAR PORTB.4 'Define frequency input pin X1
FI2 VAR PORTB.2 'DEFINE FREQUENCY INPUT PIN Y1
W0 var WORD
W1 VAR WORD
W2 VAR word
W3 VAR WORD
W4 VAR WORD
TRISB = %00010100
#CONFIG __config _CONFIG1, _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF & _CCPMX_RB3 & _CCP1_RB3
#ENDCONFIG

displayloop:
Pulsin FI,1,W0 ' Measure pulse (in 10 uSec)
Pulsin FI2,1,W1 ' Measure pulse (in 10 uSec)

W2 = 50000 / W0
W3 = 50000 / W1
W4 = (W2 HYP W3) 'HYPOTENUSE CALCULATION

If W4 = 0 Then disp ' If non-zero, display
Serout SO,N2400,[#W4]
disp: Serout SO,N2400,[" Hertz",13,10] ' Display trailer
HPWM 1,W4,1500
Goto DISPLAYLOOP ' Forever

Demon
- 24th December 2013, 23:59
What if you tweak PULSIN_MAX?

(See PBP manual for PULSIN)

If you can't speed up the oscillator, I'd decrease the maximum waiting time for a pulse.

Robert

Archangel
- 25th December 2013, 01:31
So you are using the encoders for speed calc instead of distance . . . have you tried using comparators to shape the pulses for you?

Kenwood
- 25th December 2013, 06:11
Hi Demon
Thanks for the reply. I have played with the PULSIN_MAX but as I decrease the value I loose slow speed response. As A beginner I realy don'y understand what is happening. I assume the PULSIN measures the first pulse width and puts that value into a var. I then divide it into 50000 and the do a hypotenuse calculation on the two values of X and Y. I'm guessing it should take 20 or so clock cycles for all this to happen. Does the PULSIN take the next pulse in or maybe the third pulse in and repeat the calculation? I have an LED across the PWM output and I can see a step in brightness as I increase the pulses in where it should be a smooth increase in brightness.
Thanks

Kenwood
- 25th December 2013, 06:31
Hi Arhangel. Thanks for the reply. Maybe you might be right. the encoders that I have put out very good defined square wave pulses at 55 pules an inch. I expect my maximum travel speed to be 2 inches per second. As I accelerate the PWM output should increase smoothly and it isn't. I am not sure how using comparators would help me? Other people I've talked to have suggested using linear encoders i.e 0 to 5 volts out with increase in speed and feeding the output into the A to D convertors. Just anothe Idea for me to try. Thanks

HenrikOlsson
- 25th December 2013, 08:47
Hi,
I Think it's just a matter of time, so to speak....
If your top speed is 2"/sec I'm going to guess that a typical slow speed is something like 0.05"/sec. At that speed, with 55 pulses per inch, the signals period time is 1/(55*0.05)=363ms, then you're doing that on two "channels" (no, they are not measuring the two pulses simultanously).

There's really not much the software can do about that, it can't update the output of it doesn't get any input and the input is only updated ~1.5 times per second at such slow speed.

Using tachometers is better since they are continous output devices but you may need some preamplifier to boost the output voltage at slow speed - it all depends. And you're going to need a precision rectifier or an external ADC that can measure both positve and negative voltages since the polarity of the output changes with direction of rotation. Changing to higher resolution encoder is another option.

/Henrik.

Kenwood
- 25th December 2013, 17:53
Hi Henrik
Thanks for the feedback. I started this project a number of months ago and just getting back into it. The reason I picked 55 pulses per inch was that the maximum number that the hypotenuse calculation on X and Y was about 127 before it would go into overflow. I am using a 16F88 and I purchased an 18F2550 to give me a larger number to work with. I only have the student version of PBP so that’s why the choices. I haven’t tried the 18F2550 yet as I’m a beginner it looks very complicated.

I don’t know why there is not a smooth rise or fall on the output of the PWM but maybe using the PULSIN is not the way to go.
Using the ADC convertors may be faster responding.

I have lots of options to try. The encoders can put out a maximum of 2048 pulses per revolution or about 1500 pulses per inch. I can also feed them into a DAC chip to give me a voltage out.
You can buy these speed controllers commercially so it can be done but they are a bit too much money for me. May someone out there has one that they can reverse enginner for me?
Thank Jan

HenrikOlsson
- 26th December 2013, 08:34
Hi,

> I don’t know why there is not a smooth rise or fall on the output of the PWM but maybe using the PULSIN is not the way to go.

It's because the PWM output is only updated after it has measured the pulsewidth of both X and Y, each taking several hundred ms since that is how wide the pulses are at slow speed. As the speed increases the update rate should too, to a point of course. Are you saying that the update rate remains slow even at higher speeds?


Lets look at it from another angle. What's the slowest PWM output update rate you can possibly live with? 5 times per second? 10? 20? 50?
Here's what I'm thinking: Increase the resolution of the encoders and feed them into two timer/counters on the PIC, one for X and one for Y. Now let the counters count pulses for as long as you can possibly wait between updating the output, read the count, differentiate from the previous count and you have a velocity value. Since the pulses are counted by the hardware timer/counters in the PIC they will be tracked simultanously as opposed to using PULSIN where they are measured sequentially.

You can do this with the 16F88 using TMR0 and TMR1 as counters and, for best results (but a bit more complicated), TMR2 as a timebase. You're going to have to select a resolution and timebase which doesn't overflow the 8 bit TMR0 register as the speed approaches max. For example, if the update rate is 10 times per second, there can be no more than 2550 pulses per second.

/Henrik.

Kenwood
- 26th December 2013, 18:50
Thanks Henrik.
I really appreciate your input. As a beginner it is sometimes hard to see past my own ideas and not realizing the amazing things these PIC’s can do. I am off to the workshop to try your ideas. I’ll let you know how things turn out.
Thanks again. Jan

Kenwood
- 21st January 2014, 00:56
Thanks again for your ideas. I increased the encoder resolution to about 2500 pluses per inch of travel and counted the pulses. I seems to work quite well with good low speed movement. My sewing machine drive is a servo so I need to feed it with a frequency. I feed the PWM output to a filter and then to an ADC ( AD654) chip to give me 0 to 15kHz out.


'************************************************* ***************
'* Name : Pulse Count.BAS *
'* Author : Jan *
'* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 27/12/2013 *
'* Version : 1.0 *
'* Notes : Count pulses in *
'* : 16F88 *
'************************************************* ***************

Include "modedefs.bas" ' Include serial modes
define OSC 20

#CONFIG __config _CONFIG1, _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF & _CCPMX_RB3 & _CCP1_RB3
#ENDCONFIG 'Configure CCP1 (RB3) to PWM out


SO VAR PORTB.5 'Define serial out pin
P1 var PORTA.0 'Define input pin X
P2 VAR PORTA.1 'Define input pin Y
W1 var WORD
W2 VAR WORD
W3 VAR WORD
led var PORTB.0
TRISA = %11111111
ANSEL = %00000000
output led
low led 'set led off

displayloop:

count P1,10,W1
COUNT P2,10,W2
W3 = (W1 HYP W2) 'HYPOTENUSE CALCULATION
if W3 > 120 THEN 'Turn on lrd if Hypot is over
HIGH LED 'Turn on led on RB0
ENDIF
'if W3 < 100 then
'low led
'endif

Serout SO,N2400,[#W3]

disp: Serout SO,N2400,[" Pulses",13,10] ' Display trailer
hpwm 1,W3,2000


Goto DISPLAYLOOP ' Forever
Now I am trying to improve the design and I was told to try changing the PWM to a 50% duty cycle and very the frequency (hpwm 1,127,W5). This idea gives me my 15K but the lowest I can get is about 1.5KHz and I really need close to 0.
I am trying to come up with a scheme that will use Timer 0 quickly sample the PWM output and replicate the output with a 1,500 hz offset. Any ideas would be appreciated.


'************************************************* ***************
'* Name : Pulse Count.BAS *
'* Author : Jan *
'* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 27/12/2013 *
'* Version : 1.0 *
'* Notes : Count pulses in *
'* : 16F88 *
'************************************************* ***************

Include "modedefs.bas" ' Include serial modes
define OSC 20

#CONFIG __config _CONFIG1, _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF & _CCPMX_RB3 & _CCP1_RB3
#ENDCONFIG 'Configure CCP1 (RB3) to PWM out


SO VAR PORTB.5 'Define serial out pin
P1 var PORTA.0 'Define input pin X
P2 VAR PORTA.1 'Define input pin Y

W1 var WORD
W2 VAR WORD
W3 VAR WORD
W4 var word
W5 var word

led var PORTB.0

TRISA = %11111111
ANSEL = %00000000
output led
low led 'set led off

displayloop:

count P1,10,W1
COUNT P2,10,W2

W3 = (W1 HYP W2) 'HYPOTENUSE CALCULATION
if W3 > 120 THEN 'Turn on lrd if Hypot is over
HIGH LED 'Turn on led on RB0
ENDIF
W4 = (W3 + 20) 'Add 20 to overcome min pwm frequence
W5 = (W4 * 100) 'Times 100 to give min of 2 kHz
Serout SO,N2400,[#W5]

disp: Serout SO,N2400,[" Pulses",13,10] ' Display trailer
hpwm 1,127,W5


Goto DISPLAYLOOP ' Forever

Thanks Jan

HenrikOlsson
- 21st January 2014, 06:17
Hi Jan,
I don't understand.....
The AD654 is a Voltage-to-Frequency converter, it takes an analog voltage in and outputs a frequency which is what you're feeding your servodrive, correct?

Using the HPWM command and a simple RC lowpass filter will give you an analog voltage from 0-Vcc for a duty cycle of 0-100% and as far as I can see the AD654 should be able to "get down" to 0kHz with 0V input signal. Are you saying that the output frequency of the AD654 is not reaching 0Hz when the input voltage is 0V?

Are you aiming to get rid of the AD654 feed the servo-drive directly with the PIC?

Can you upload a schematic? What servo drive are you using?

/Henrik.

Kenwood
- 21st January 2014, 17:45
7201
Hi Henrik
I guess I am rambling a bit. The circuit that uses the ADC (AD654) works quiet well. What I am trying to do is use the PWM output to give me a frequency output of 0 to 15 kHz. I can get to 15 k with no problem but I cannot get below 1.3K due to the limitations of the PWM circuit.

The idea was to use the PWM and vary the frequency (HPWM 1, 127,W5). This seems to work as I can set the variable W5 to give me 2000 at 0 input and ramp up from there. But I am stuck with the base frequency of 2K

What I am trying to come up with is circuit that will mask the first 2 kHz. I seem to remember doing something similar many many years ago using discreet logic. My guess now is to use the timers and interrupts to capture the pulses to isolate the first 2k.
Thanks for reading this any ideas would be helpfull.
Servo drives are Gecko G320X.
Jan

HenrikOlsson
- 21st January 2014, 19:09
Hi Jan,
OK, that makes sense. Yes, the PWM circuitry in the PIC can only go so low. Using a lower oscillator frequency will also change the lower limit of the PWM frequency, at 4MHz you'd be able to get down to 245Hz - even lower if you also change PIC to a 16F1xxx device - but it will never go to zero.

I don't know of any "frequency subtractor circuit" and if one exists you might as well use the V/F converter chip and I don't think generating a frequency with the CCP module, feed it back into the PIC and trip an interrupt on it is the way to go. You can equally well use the timer to internally generate an interrupt. However, generating interrupts at tens of kHz might be stretching it. Another issue is that you require 50% dutycycle, this means that for 10kHz output frequency you're going to have to generate interrupts at 20kHz. However, the G320X accepts "Active" pulses of as Little as 1us so I don't see the reason for the 50% dutycycle?

Finally, while the PIC is servicing the interrupt it won't count any pulses. The COUNT command sits in a software loop monitoring the the input and counts the transistions it sees. When the PIC is servicing the interrupt it can't count pulses. You'd be better off counting them with hardware counters but then you need yet another timer as the timebase interrupt....

In either case it's going to be tricky to get the dynamic range you need. You're going to have to use different methods depending on "where" on the speed curve you're at.

Is 15kHz the desired max frequency?
Does it have to go to all the way to 0Hz? Can you live with 10, 20, 50Hz?
The schematic shows the 16F84A, is that the one you're using? Can you move to another one?

Sorry, no solutions, only questions....

/Henrik.

Kenwood
- 21st January 2014, 22:53
Hi Henrik
Thanks for looking at this.

I am using a 16F88, I forgot to change the name.

Yes I really need 0 speed. When the X and Y movement stops the sewing machine needs to stop.

I am not sure if 15 kHz is required. I started off with a much lower frequency but I was having trouble with acceleration and deceleration time of the sewing machine motor. The faster I set the encoder the better the response time became.

Wel my first design does work so I will stick with it.

Thanks for your thoughts. I will let you know if I make any improvements.
Jan

HenrikOlsson
- 22nd January 2014, 06:30
Hi Jan,
You can always get 0Hz by simply not sending any pulses at all. What I actually meant was what the lowest "start frequency" needs to be.
Anyway, keep us posted!
/Henrik.