Closed Loop Motor Control.

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Closed Loop Motor Control.

Post by medelec35 »

I would like to control two separate motors using PWM, but I would like set the speed via two separate pots, using taco output form each motor to keep speed constant, and change an output port from low to high if either motor has been enabled and has stalled and cannot restart. e.g. jammed motor. I would also like to have each motor with facility to be enabled or disabled
I can only get so far.
Here's where I have achieved:
I can speed up and slow down two motors. If either motor stalls then alarm led lights and 1 added to duty cycle until motor starts again. This is not ideal, since if one motor is disabled then other motor temporary stalls, which adds one to stalled motor pwm, so when it starts back up, it is running too fast.
These motors give 1 pulse per rev. I am running PWM's at 2KHz, with an internal clock frequency of 4MHz. When motor is enabled I am only using a duty cycle between 5% and 40%
Any help will be greatly appreciated.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Closed Loop Motor Control.

Post by medelec35 »

Since no reply yet, am I asking too much?
I realise different motors run at different speeds with same duty, so I don't expect it to run without first tweaking values.
All I am after is basic feedback principle to get me started, and I will do the rest.
As an example:
Enable using 16f737 @ int osc frequency 4MHz and pwm frequency of 2KHz.
Motor 1enabled by setting RA6 high. Duty of 6% achieved by turning pot on ANA0 to 0.6V. Motor should be running, taco feedback into RC0. Taco should be 12.9 Hz, is monitored.
If taco is less then 12.9Hz (this value can be stored in a look-up table e.g. 12Hz - 20 Hz in steps of 0.1) then pwm duty is increase by 0.2 % until frequency is matched to value 12.9 Hz (I can only increase by 0.2 due to resolution of 10 bits), and if taco frequency is higher then 12.9 Hz, then duty should be decrease etc. If taco is not detected after a given time of say 5 seconds and RA6 is at logic 1 , then RC6 should go from logic 0 to logic 1 indicating a fault, and RC6 should go to logic 0 if Taco is detected and RA6 is still at logic 1.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Closed Loop Motor Control.

Post by medelec35 »

It has been one week and a day since first asked a question regarding closed loop motor control. I would like some help or pointer in the right direction, someone asking me for more information (if required), someone replying ' working on it, will post a solution by.....(date) or even a: 'sorry your asking the impossible even for us at matrix!' :lol:
Please reply before I go bold, tearing hair out does actually hurt :lol:
Martin

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Closed Loop Motor Control.

Post by Steve »

Sorry you've not received a reply. I've seen your questions, but I'm unsure how to answer it - motors are not my strong point and I am not sure where exactly you need help.

Are you able to read back the motor speeds? If you can, then I think the rest is easy.

A basic feedback control loop is fairly trivial and you have basically described what you need to do. If you need to do anything more complex (e.g. PID control), then I think there are a few resources on our site that may help.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Closed Loop Motor Control.

Post by medelec35 »

That is one of my problems, I am not sure how to do flowchart for reading back speed.

What I would like to do is determine the motor speed, and calculate the difference between actual speed and what the speed should be, which is determined by ADC value then adjust duty up or down until motor is running at required speed.

This is how it could be achieved:
First I need to determine the speed from the taco o/p.
There should be a delay to allow settling time of the motor, and then motor speed (RPM) is determined by 1/time taken from the start of the first pulse to the start of the next pulse *60. If RPM too slow when compared with reference RPM (determined by ADC pot setting and look up table) then PWM duty is increased. If RPM is too fast then PWM duty is decreased.

If taco is always high or always low, then RC6 should go from logic 0 to logic1.

Referring back to lookup table.


I need two lookup tables. E.g. table A for normal speeds and Table B for boost speeds
Normal speed if RB0 = 0, Boost speed if RB0= 5V (digital i/p)

Table A

ADC V, RPM
0, 600
0.2, 620
0.4, 640
0.6, 700
0.8, 800
1, 820
1.2, 830
..
..
5, 1000

Table B

ADC V, RPM
0, 900
0.2, 950
0.4, 1020
0.6, 1110
0.8, 1215
1, 1318
1.2, 1410
..

5, 1700


Can a flowchart be produced using both lookup tables?

I am not sure how to produce a flow chat which follows the above method to determine RPM of motor, and changes duty until motor RPM speed is = lookup table motor speed for a given ADC V.
Martin

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Closed Loop Motor Control.

Post by Steve »

There's an article about lookup tables in our "Articles" section of the forum - this may help. But also, be aware that the "voltage" reading will probably be a byte value (0-255) representing the ADC value. Also, the RPM value you use may not necessarily be the actual RPM value, but could well be some kind of "normalized" value which is mathematically easier to work with (i.e. it will probably be easier to use the raw data value from the taco input data, instead of converting it to an actual RPM value).

Your other problem is reading in a bitstream and calculating the frequency. There is an example of a frequency counter on our Flowcode examples page that should help you.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Closed Loop Motor Control.

Post by medelec35 »

Thanks Steve that's a great idea!
The frequency counter is the way I’m going. I will be using 16F737 with a internal clock set at 4MHz.
I am assuming TMR0 has no involvement with ccp1 and ccp2, since I am using 2 PWM channels.
What would be the config required to ensure it all works?
Currently I have config set at 0x2007,0x3f98 and 0x2008,0x3fbd, I assume that still is correct?
So does frequency counter work by enabling TMR0, waiting i/p to go low, then wait till i/p to go high, then keep repeating and each time is repeated add 1 to frequency until TMR0 overflows (I am assume with prescaler it takes exactly 1 second to overflow)?
Also is the watchdog affected, since it shares TMR0?

Just looked again at the datasheet, and I was wondering how the compiler knows what the correct prescaler (PSA and PS) bits should be set to since this is done in the option reg and not C code on the flowchart
Or in config settings?
Update1: I have changed values in the Freq_Counter example to the following: Clock Speed=4000000, From TRM0 properties: Prescale=1:64. Since interupt freq is given as = 61.0352, changed If count <75 to <61 which is 61 interupts per second.
Update2: I have managed to sucessfully run and control speed of two motors using PWM method, and measure plus store both RPMs. I used int variables so ADC = 0 to 1023 and RPM = 0 to 32767
I am now after some help with look up tables. None of LUT on examples page has helped (unlike reading frequency example which was a great help)I run all 3 examples on Flowcode simulator, and stepped whilse observing variables. Its probably not working since LUT in examples are in C and simulator won't simulate C code. I will still try and work out correct LUT in the mean time,
can some one please post a simple LUT example which does the following:

Have ADCV array of 0,10,20,30,40 an array for RPM . RPM = 700,720,730, 780,800. So if a varaible called MOTORV =28, then nearest ADCV=30 so this would make A varaible called MOTORRPM=780. This is becasue nearest ADCV to MOTORV = 30, which is 4th array along. So 4th array along in RPM = 780
If I can then follow that LUT example running, I should be able to make up my own LUT.
Thanks.
Martin

Post Reply