Thread: Clean Code

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Clean Code

    Just finished some code and trying to tidy it up, is there a cleaner of doing the following?

    Code:
    Valve_Demand			= P_Term + Integral_Value + D_Term;
    				if(Valve_Demand <0)
    					Valve_Demand = 0;
    				if(Valve_Demand >100)
    					Valve_Demand = 100;
    Basically if the result is over 100 it returns 100 or below 0 it returns 0.

    Thanks

    James

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I can't really tell if that's in a function or not, and it's unclear what types the variables are but you could try something like this...
    Code:
    int GetValveLoad(int P_Term, int Integral_Value, int D_Term)
      { int tmp = P_Term + Integral_Value + D_Term;
         if (tmp < 0)
           return 0;
         else if (tmp > 100)
           return 100;
         
         return tmp; }
    Except for the else if structure, your code is already pretty efficient ...
    The function example may look more complex but in use it should (?) be more efficient.

    Hint: Sometimes it pays to think in smaller blobs.
    Last edited by CommonTater; 11-07-2011 at 08:01 AM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    if it makes any difference its simply in my main loops, I have like 9 equations all with constraints like the one above and i do have to be fast

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In that case I would consider making a somewhat generic function to deal with the constraints...

    Code:
    int Constrain(int Value, int Max, int Min)
      {  if (value > Max)
            return Max;
          else if (Value < Min)
            return Min;
          
          return Value; }
    
    
    // can be called as...
    Pressure = Constrain(Press1 + Press2 / Flow, 100, 10);
    (Adjust the variable types as needed)

    In C, anything you're doing in more than one place is often a good candidate for a function and as the example shows, unless the calculations are hyper complex you could even to them right in the function call's parameters.

    It might help if you posted your code... or at least enough to give us some idea what your doing...
    Last edited by CommonTater; 11-07-2011 at 08:25 AM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    61
    Okay, what I’m coding is a control algorithm for a engine on a robot (in practice this code is going four times and reason it has to be fast is because they all simultaneous).

    The code is in two parts, the control algorithm which is what will be used in the end, and a motor part which only coded to replicate the engine to make sure the controller works (if that makes sense). For this reason I’ve simply put it in a function.

    I know there are problems with the actual maths (im working on them atm) I’m inexperienced in code and really after pointers about better ways of doing things cleaner ways etc if tht makes sense,
    The graph this gives is a straight line which I know is wrong but when combined with other parts it works (if that’s makes sense)
    Although the maths is incorrect the correct maths will be in the same structure as below.

    Thanks In Advance

    James

    Code:
    #include <stdio.h>
    #include < math.h>
    #include <stdlib.h>
     
    /*Function Calls*/
    void Preliminary(void);
    void Engine(void);
    void Speed(void);
     
    int Time;
     
    /*Controller Value*/
    float Speed_Demand;
    float Starting_Speed;
    float Correction_Time;
    float P_Coefficient;
    float I_Coefficient;
    float D_Coefficient;
    float P_Term;
    float D_Term;
    float I_Term;
    float Integral_Value;
    float Previous_Integral_Value;
     
    /*Speed Controller*/
    float Speed_Error;
    float Previous_Speed_Error;
    float Required_Acceleration;
    float Torque_Error;
    float Required_Torque;
     
    /*Engine Value*/
    float Inertia;
    /*Engine Simulation*/
    float Throttle_Position;
    float Engine_Time_Constant;
    float Throttle_Factor;
    float Torque_Without_Varience;
    float Previous_Torque_Without_Torque_Varience;
    float Engine_Speed;
    float Torque_Varience;
    float Engine_Torque;
    float Amount_Affected_by_Torque;
     
    /*Speed Value*/
    float Actual_Speed;
    /*Speed Simulation*/
    float Previous_Speed;
    float Acceleration;
    float Tolerance;
    float Tolerance_Input;
     
     
    float Valve_Demand;
     
    main()
    {     
          FILE *fp;
          fp=fopen("c:\\test.csv", "w");
         
          /*Constants Toughout Simulation*/
          P_Coefficient = -0.2;
          I_Coefficient = 0.02;
          D_Coefficient = 10;
          Inertia = 5;
          Throttle_Position = 10;
          Engine_Time_Constant = 40;
          Tolerance_Input = 0;
          Amount_Affected_by_Torque = 0;
          Speed_Demand = 1500;
          Starting_Speed = 1400;
     
          /*Initial Set Up Values*/
          Time=0;
          Previous_Speed = Starting_Speed;
          Previous_Torque_Without_Torque_Varience=0;
          Correction_Time = 0.1;
     
          /*Used only for simulation*/
          Preliminary();
         
          /*Prints Initial Heading In File*/
          fprintf(fp, "Time, Actual_Speed\n");
         
          while(Time<10000)
                      {
                            /*Used only for simulation*/
                            Engine();
                            Speed();
                           
     
                      /* Control System Code */ 
                      Speed_Error                   = Speed_Demand - Actual_Speed;
                      Required_Acceleration   = Speed_Error / Correction_Time;
                      Required_Torque               = Required_Acceleration * Inertia;
                      Torque_Error                  = Required_Torque - Engine_Torque;
                      P_Term                              = Torque_Error * P_Coefficient;
                            if(P_Term <-100)
                                  P_Term = -100;
                            if(P_Term >100)
                                  P_Term = 100;
                      I_Term                              = P_Term * I_Coefficient;
                      Integral_Value                = I_Term + Previous_Integral_Value;
                      D_Term                              = D_Coefficient * (Previous_Speed_Error - Speed_Error);
                      Valve_Demand                  = P_Term + Integral_Value + D_Term;
                            if(Valve_Demand <0)
                                  Valve_Demand = 0;
                            if(Valve_Demand >100)
                                  Valve_Demand = 100;
                     
                      /*Outputs the values to File*/
                      fprintf(fp, "%d, %f\n ",Time, Actual_Speed);
                     
                      /*Sets Values for Next Pass*/
                      Previous_Speed_Error = Speed_Error;
                      Previous_Integral_Value = Integral_Value;
                      Time = Time+1;
                      }
          /*Closes the File*/
          fclose(fp);
     
    }
     
    /*Used Solely for Simulation*/
     
    void Preliminary(void)
    {
    /*Engine Simulation*/
    Throttle_Factor = 5/Engine_Time_Constant;
    }
     
    void Engine(void)
    {
    Torque_Without_Varience=Valve_Demand*Throttle_Factor + (1 -Throttle_Factor)*Previous_Torque_Without_Torque_Varience;
    Engine_Speed = (Torque_Without_Varience-Engine_Torque)/Inertia;
    Torque_Varience = (Torque_Without_Varience/100)*Amount_Affected_by_Torque*(sin(Time/(1000/(Engine_Speed/60))));
    Engine_Torque=Throttle_Position*Throttle_Factor + (1 -Throttle_Factor)*Previous_Torque_Without_Torque_Varience+Torque_Varience;
     
     
    Previous_Torque_Without_Torque_Varience = Engine_Torque;
    }
     
    void Speed(void)
    {
    Acceleration = Engine_Torque/Inertia;
    Actual_Speed = Acceleration +Previous_Speed;
          if (Actual_Speed <0) 
                Actual_Speed=0;
     
    Tolerance=(((Actual_Speed/100)*Tolerance_Input)*1000);
     
    Tolerance = (rand(Actual_Speed))/1000;
    Actual_Speed = Acceleration +Previous_Speed+Tolerance;
          if (Actual_Speed <0) 
                Actual_Speed=0;
     
    Previous_Speed=Actual_Speed;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 10:03 AM
  2. Help me clean this up please.
    By Cstudent2121 in forum C Programming
    Replies: 19
    Last Post: 12-01-2005, 11:18 PM
  3. Clean Code...
    By Devil Panther in forum C Programming
    Replies: 24
    Last Post: 07-16-2005, 01:50 AM
  4. clean up?
    By nerore in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2003, 07:30 PM