Solved

MPLAB simulation time differs from actual execution time.

Posted on 2011-02-15
5
573 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now