Go Down

Topic: Trying to add steering to my self balancing segway (Read 444 times) previous topic - next topic

This balances great, but when I try to add the steering based off a potentiometer it stops balancing and clunks forward or back.
The whacked out lines is the code that works...

Where do I add/subtract my turn command so as not to affect the balancing?
Code: [Select]
//Steering
  
  steeringValue = analogRead (steering);
  turnRate = (steeringValue - 509) * .05;          //turnRate = ( pot - pot offset ) * turnRateGain
  


    //PID CALC
    Cn = (curErr + integralTerm + derivativeTerm) * float(outputValue1);

    //MOTOR DRIVE
   // Serial.println(Cn);

    WheelDirection dir;

 //   if (Cn > 0) dir = DIR_BACKWARD;
    if (Cn - turnRate > 0) dir = DIR_BACKWARD;

    else if (Cn - turnRate < -0) dir = DIR_FORWARD;
  //  else if (Cn < -0) dir = DIR_FORWARD;

    else dir = DIR_STOP;

    throttle = abs(Cn) - turnRate;
   //   throttle = abs(Cn);

    if (abs(ang) > 0.7)  MoveWheels(DIR_STOP, 0); //if angle too large to correct, stop motor       

    else MoveWheels(dir, throttle);
    
    //LEFT MOTOR DRIVE
    
    LeftWheelDirection leftdir;

    if (Cn + turnRate > 0) leftdir = LeftDIR_BACKWARD;
  //  if (Cn > 0) leftdir = LeftDIR_BACKWARD;

    else if (Cn + turnRate < -0) leftdir = LeftDIR_FORWARD;
  //   else if (Cn < -0) leftdir = LeftDIR_FORWARD;

    else leftdir = LeftDIR_STOP;

      leftthrottle = abs(Cn) + turnRate;
  //    leftthrottle = abs(Cn);
    
//Serial.print("\t throttle = ");      
//Serial.print(turnRate);   
//Serial.print(""); 

    if (abs(ang) > 0.7)  LeftMoveWheels(LeftDIR_STOP, 0); //if angle too large to correct, stop motor       

    else LeftMoveWheels(leftdir, leftthrottle);
    
    prevErr = curErr;
    


Go Up