Solved

MPLAB simulation time differs from actual execution time.

Posted on 2011-02-15
5
580 Views
Last Modified: 2012-05-11
Hello!

I am using a PIC 18F4431 and MPLAB with C18 as my IDE.

My problem is that a certain while loop takes much longer (100-1000 times longer) to actually run on the micro-controller in circuit than it does in the simulation.

The loop is a PID algorithm that controls the position of a device that is powered by a DC motor. A quadrature encoder gives the position. The PIC that I am using has a quadrature encoder interface built in so I can't imagine that would be the source of delay. Also, there is a built in pulse width wave form generator. Attached is the code. Please let me know how I can clarify the problem. This is driving me nuts! Please help :)

Alex
while(bControl == 1){
		k++;
		j++;
		position = getQEIPos();   //get encoder position
		last_error = error;	
		error = setpoint - position;
		PIDintegral = PIDintegral + (error*Ti)+(output - control)/Tt;	//integral with track back to prevent wind up
		PIDderivative = (error - last_error)/Td;		}
		control = (Kp*error)+(Ki*PIDintegral)+(Kd*PIDderivative);
		if((error<10) && (error > -10)){   //this is to exit the loop if the error is small
			j++;
			if(j>157){		//this should take 0.2 seconds 
				bControl = 0;
			}
		}

		//******** ensure that output does not exceed possibilities of the actuator
		output = control;
		if(control > motor_limit){
			output = motor_limit;
		}
		if(control < (-100)){
			output = (-100);
		}
		setPDC0(output);    //set motor duty cycle
		
		Delay10TCYx(20); //delay 200 instruction cycles (40 uSeconds)
	}

Open in new window

0
Comment
Question by:0av067
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
HappyCactus earned 500 total points
ID: 34904479

I would check with a digital I/O pulse and an oscillator how many time takes to perform each block.
The most critical blocks in my opinion are: Line 4 (reading the encoder input - how is this done?), lines 7-8 (division!!!) line 25 (set motor cycle duty - how is this done?) and 27 (delay - how is this done?).
Also, check that there aren't any spurious interrupts, again, put a I/O pulse on output on interrupts to see if there are some interrupt within that cycle and how long it takes.

be warned that using "delay" to clock a controlling cycle is not a reliable way, you should use an interrupt source. PID controller requires an accurate sampling time, poles can be "moved" in an unstable zone if the sampling frequency changes.


0
 

Author Comment

by:0av067
ID: 35283880
This ended up being a weird one. I would imagine that the instruction cycles for that block of code in MPLAB SIM debug and PICKIT2 debug are the same. I noticed, however, that the status bar in MPLAB read 20 MHz in MPLAB SIM despite the OSCCON register being configured to 8 MHz. It was because my configuration bits were set to external oscillator.

So in the end, I was telling the SIM that I was using an external oscillator while in practice I was using the much slower internal oscillator. A quick edit of the configuration bits fixed the problem.

Regarding the PID, thank you so much for the advice. I didn't know that delays are unreliable. I thought they simply created a loop that waited a designated number of instruction cycles. I switched to using the timer to handle the delay as per your advice.
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 35291970
They work as you say, but interrupts can make the completion of the loop "delayed" with respect of the expected time. The only way to make it reliable, is to disable all interrupts - but be warned on DMA! - but sometime this is not good.
That's why you can't be assured that a loop is completed in a constant time.
0
 

Author Comment

by:0av067
ID: 35292278
I see. That was very clarifying... and don't worry, I'm not advanced enough to use DMA yet :) soon enough! Thank you very much.
0
 
LVL 12

Expert Comment

by:HappyCactus
ID: 35292391
You're welcome ;-)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question