ROBOTC.net forums
http://www.robotc.net/forums/

Problem with functions
http://www.robotc.net/forums/viewtopic.php?f=1&t=5987
Page 1 of 1

Author:  TheProphet [ Tue Jun 18, 2013 5:09 pm ]
Post subject:  Problem with functions

Hi everybody,
I have a little question about function's. First, I will explain quickly what I want to do :-) .
So Im doing a kind of Industrial robot with PF XL Motor's + Glidewheel from Mindsensors.com and the regulation is not like I want. So I have done my own PID function and desactivated the orignial PID (open loop). This function return the power of the motor in float.

Until here, everything works fine. The problem starts when I use this function inside another function, it returns this : -1.#Q0
I cannot find information about this error...

Thank you in advance for your help.

Mike

edit: this problem is the "MotPowerA" variable
Code:
#pragma config(Motor,  motorA,          axis1,         tmotorNXT, openLoop, encoder)
#pragma config(Motor,  motorB,          axis2,         tmotorNXT, openLoop, reversed, encoder)
#pragma config(Motor,  motorC,           ,             tmotorNXT, openLoop, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
float const GearRatio=pow((56),-1);
int const GearRatio2=(56);
float ValEncA;
float MotPowerA;
bool PosReached;

float PIDMotA(float ConsignA ,float ValCodA)
{
   float  Kp, Ki, Kd, Prop, Inte, Deri, OldErr, ErrDiff;
   float PowMotA;
   Kp=3;
   Ki=0;
   Kd=30;
  OldErr=ErrDiff;
  ErrDiff=ConsignA-ValCodA;
  Prop=ErrDiff;
  Inte=Inte+ErrDiff;
  Deri=ErrDiff-OldErr;
  PowMotA=(Kp*Prop)+(Ki*Inte)+(Kd*Deri);
  return PowMotA; //return motor power
}

void moveJ(float Aj, float Bj)
{
   while (!PosReached)
   {
      Aj*=GearRatio2;
      MotPowerA=PIDMotA(Aj,nMotorEncoder[axis1]);
      wait1Msec(10);
      motor[axis1]=MotPowerA;
      if (MotPowerA<10&MotPowerA>-10)
      {
         motor[axis1]=0; //Position reached
         PosReached=true;
      }
   }
   PosReached=false;
   return;
}

task main()
{
   moveJ(0,0);
   wait1Msec(2000);
   moveJ(45,0);
   wait1Msec(2000);
   moveJ(0,0);
   wait1Msec(2000);
}

Author:  mightor [ Tue Jun 18, 2013 5:20 pm ]
Post subject:  Re: Problem with functions

Perhaps if you supplied us with the code, we could take a look but right now the cause of this is anyone's guess. My vote is: planetary alignment or solar flares.

= Xander

Author:  TheProphet [ Tue Jun 18, 2013 5:33 pm ]
Post subject:  Re: Problem with functions

Sorry, I have edited now

Author:  mightor [ Wed Jun 19, 2013 1:13 am ]
Post subject:  Re: Problem with functions

First of all, your Inte variable is constantly reinitialised, instead of maintaining its value, which is kind of the point of the I part in a PID regulator. If you make it either global or a "static" type in your function, it will not lose its value each time the function is called.
Second, your ErrDiff value is assigned to OldDiff, but it wasn't assigned a value prior to that, so there's nothing to assign. Do the same thing with OldDiff as you should do with the Inte variable, make it a static variable.

= Xander

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/