Log in

View Full Version : speed control



Kees Reedijk
- 30th March 2005, 20:06
I want to do simple speed control of a small dc motor with gearbox and optical encoder. Proportional control will be accurate enough, only one direction. Since the motorspeed will be quite low I thought of using the pulsin command to measure pulse length for the feedback.
Has anyone done this before?
thanks

mister_e
- 30th March 2005, 20:13
you can use pulsin for sure... if you read few times and do an average, it can provide great results if your pulses are short enough to avoid an overflow of pulsin variable.

BUT i'll prefer to convert pulses to voltage and read from a/d converter.

have a look to this thread can give you some hints...
Click Here (http://www.picbasic.co.uk/forum/showthread.php?t=1297&highlight=speed+control)

Kees Reedijk
- 31st March 2005, 20:16
Thanks Steve
Today I managed to get it working using the pulsin command. I use a surplus maxon motor with a 32 step optical encoder, pulse length is not a problem. It works really well. I'm using a pic16F628A.
Here's part of the code I used:
p.s.for some reason all my indentation disappears when I posted this message, don't know why
regards, Kees

motor CON 3
sensor VAR PORTB.0
temp VAR WORD
setspeed VAR BYTE 'speed set by user
power VAR WORD 'control variable (CV) , PWM dutycycle
PV VAR WORD 'process value, or actual speed
error VAR BYTE 'difference between set speed and actual speed
Kprop VAR BYTE 'constant for proportional feedback control
Kprop = 4

run:
Pot 5,25,setspeed
PulsIn sensor,0,temp ' measure speed
IF temp2 > 0 Then ' check if motor is running
PV = 10000 / temp ' depends on encoder and motor
IF PV >255 Then PV = 255
IF setspeed > PV Then
error = setspeed - PV
power = power + (error / Kprop) 'new PWM value
IF power > 255 Then power = 255 'otherwise overflow
Else
error = PV - setspeed
power = power - (error / Kprop) 'new PWM value
IF power > 255 Then power = 0 'otherwise overflow EndIF
Else
power = setspeed ' so a motor without optical sensor
EndIF ' can also be used
HPwm motor,power,10000 ' Send a PWM signal at 10kHz, drive motor
LCDOut $FE,1,"spd ",DEC3 setspeed 'show on screen
LCDOut $FE,$C0,"pv ", DEC3 PV 'for debugging
GoTo run

mister_e
- 31st March 2005, 23:07
hi kees, great to know that's working for you now.



p.s.for some reason all my indentation disappears when I posted this message, don't know why


must use VB code click here to know how (http://www.picbasic.co.uk/forum/misc.php?do=bbcode)

after that your code can look something like that


motor CON 3
sensor VAR PORTB.0
temp VAR WORD
setspeed VAR BYTE 'speed set by user
power VAR WORD 'control variable (CV) , PWM dutycycle
PV VAR WORD 'process value, or actual speed
error VAR BYTE 'difference between set speed and actual speed
Kprop VAR BYTE 'constant for proportional feedback control
Kprop = 4

run:
Pot 5,25,setspeed
PulsIn sensor,0,temp ' measure speed
IF temp2 > 0 Then ' check if motor is running
PV = 10000 / temp ' depends on encoder and motor
IF PV >255 Then PV = 255
IF setspeed > PV Then
error = setspeed - PV
power = power + (error / Kprop) 'new PWM value
IF power > 255 Then power = 255 'otherwise overflow
Else
error = PV - setspeed
power = power - (error / Kprop) 'new PWM value
IF power > 255 Then power = 0 'otherwise overflow EndIF
Else
power = setspeed ' so a motor without optical sensor
EndIF ' can also be used

HPwm motor,power,10000 ' Send a PWM signal at 10kHz, drive motor
LCDOut $FE,1,"spd ",DEC3 setspeed 'show on screen
LCDOut $FE,$C0,"pv ", DEC3 PV 'for debugging
GoTo run


and once it's done, it appear that there's one ENDIF missing ;)