Solved

MPLAB simulation time differs from actual execution time.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Direct mail marketing is the act of mailing materials straight to prospective customers. This wide form of marketing is one of the oldest methods of communicating with a geographic based demographic. So is it useful in 2017 and beyond?
Adults who share images on social media aren’t the only ones who need to worry about their privacy. Our culture’s tendency to share every move and celebration affects the privacy of our children, too.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 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