Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pid ?

Status
Not open for further replies.
Hi, im trying to do a PD for a robot to let it move in straight line , im just experimenting with just one motor connected to an encoder , well , when i get some samples from the enocders and when its not pointed to the motor encoder's strips , the motor change its speed as a function of the samples from the encoder is that correct ? also when i point the encoder sensor to the motor , the motor oscillates , dunno why ? and the desired_speed constant doesnt affect the speed of the motor ....

im using that code


Code:
#define left_velocity 10
#define K_PRO 3 /* proportional constant */
#define K_DRV 3 /* derivative constant */

int Lvel, Rvel; /* requested input velocities, ticks per 1/20 sec */
int leftacc, rightacc; /* accumulators for motor output values */
int leftlast, rightlast; /* history values for calculating derivative */

void speedcontrol() /* PID controller*/
{
int lf, rt;

lf = (Lvel - left_velocity)*256;
leftacc += ((lf/K_PRO) + ((lf - leftlast)/K_DRV));
leftlast = lf;

pwmL(leftacc/256);
 
yea im using it from the same link
im posting the complete code , i removed the D Term , still not working prolly
could ya check it ^^ ?
Code:
--------------------------------------------------------------------------------

Code:
#define K_PRO 3		/* proportional constant */
int Lvel = 10;	/* requested input velocities, */
int leftacc;	/* accumulators for motor output values */
int leftlast; /* history values for calculating derivative */

unsigned char left_velocity = 0;
void external_interrupt () using interrupt 1 
{
left_velocity++;
}

void pwm(int T_period,int Ton_time)
{ 
 int I;
for( I =0; I < T_period; I++) //loop for one cycle,
{ if ( I < Ton_time){
             Motor1_2 = 1; // P1^6 // Motor on 
             Motor1_3 = 0; // P1^7
}
else{ 
             Motor1_2 = 0; // P1^6 // Motor off
             Motor1_3 = 0; // P1^7
}
}
}

void speedcontrol()	
{
	int lf, rt;

	lf = (Lvel - left_velocity)*256; 
	leftacc +=  ((lf/K_PRO);
	leftlast = lf; 
	
pwm(60, leftacc/256); 
}
 
the main function is just
Code:
void main (void )

{ 

while ( true ) 
{

speedcontrol();
}

i think the PD is wrong , that's why i removed the D term ...
 
Do you need to enable the external interrupt?

I don't even know what MCU, compiler, speed, etc. that you're using.
 
left_velocity is never decremented, so on every interrupt it just grows.

Code:
leftacc +=  ((lf/K_PRO);
is missing a parenthesis.

This may sound picky, but could you rename the variable 'lf'? 'llf' would be fine, at least it wouldn't seem like 'If".

Check the compiler optimization level and/or make sure that P1 is declared as static.
 
Velocity is the number of interrupts per unit of time. Your program just counts interrupts forever without regard for time.
 
Status
Not open for further replies.

Latest threads

Back
Top