Solved

MPLAB simulation time differs from actual execution time.

Posted on 2011-02-15
5
584 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Gain an elementary understanding of Blockchain technology.
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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