Show Posts
Pages: [1] 2
1  Community / Exhibition / Gallery / 2 Wheel Self Balancer Using Wheel Chair Motors on: December 07, 2012, 08:34:47 pm
Here is my second arduino project.

A 2 wheel self balancing Segway using wheel chair motors.

http://glennselectric.blogspot.com/2012/12/2-wheel-self-balancer-using-wheel-chair.html

2  Community / Exhibition / Gallery / $400 self balancing unicycle with working code on: December 01, 2012, 12:34:37 pm
I've seen quite a few self balance projects, but they never seemed to have good ( simple ) code. 

This works well and can be built quickly and its semi cheap.

http://glennselectric.blogspot.com/

3  Using Arduino / Programming Questions / Trying to add steering to my self balancing segway on: November 28, 2012, 01:03:59 pm
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:
//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;
    
4  Using Arduino / Programming Questions / Re: I create an int called "speed" and it turns orange like a command on: November 20, 2012, 02:28:23 pm
Ah, got it.  It worked so i left it alone.  I don't mind these little anomalies when we have a help resource so smart, so fast and so free.

thanks a lot!!

glenn
5  Using Arduino / Programming Questions / I create an int called "speed" and it turns orange like a command on: November 19, 2012, 03:50:03 pm
I create an integer called "speed" and it turns orange like a command.  I have looked all over, but cannot find such a command.

Code:
void MoveWheels(WheelDirection (dir), int speed) {

    if (MotorOff) return;

    if (speed > 255) speed = 255;

    else if (speed < 0) speed = 0;

    if (dir == DIR_STOP) {

        digitalWrite(ahi, HIGH);      //set ahi and bhi osmc
        digitalWrite(bhi, HIGH);
        digitalWrite(dis, LOW);
        digitalWrite(ali, LOW);
        digitalWrite(bli, LOW);

    } else if (dir == DIR_FORWARD) {

        digitalWrite(ahi, HIGH);      //set ahi and bhi osmc
        digitalWrite(bhi, HIGH);
        digitalWrite(dis, LOW);
        digitalWrite(ali, LOW);
        analogWrite(bli, speed);
6  Using Arduino / Programming Questions / Re: results are 0.00 when adding 2 floats together on: June 15, 2012, 08:37:51 pm
good suggestion.  i did check that several ways as i juggled the formula around, but ill check that one more time since you mentioned it...
7  Using Arduino / Programming Questions / Re: results are 0.00 when adding 2 floats together on: June 15, 2012, 02:33:52 pm
i have attached the complete code.  please excuse the mess.  i am a novice.  i copied one piece of code to read the accelerometers, and one for the balancing formulas.  i recently did my own code to read the accelerometers and gyro.  all this is functional except for the blending formula.  thanks for the help!!!
8  Using Arduino / Programming Questions / Re: results are 0.00 when adding 2 floats together on: June 15, 2012, 02:19:19 pm
ok, i changed the formula.

gyroangle = 0.95 * (prevAngle + degX) comes out with a result of 0.00

if i change the line to:

currAngle=0.95 * (prevAngle + degX) i get the correct number

acclangle= 0.05 * angle; i get the correct number

when i add gyroangle and acclangle i get 0.00



Code:

 degX = (float) ggyrox2 * (float) elapsedTimeSec;
       
      acclangle= 0.05 * angle;
      gyroangle = 0.95 * (prevAngle + degX);
 
      currAngle = acclangle + gyroangle;

}
   
        prevAngle = currAngle;
       
        Serial.print(currAngle);
   Serial.print (",");
   Serial.print(prevAngle);
   Serial.print (",");
   Serial.print(gyroangle);
   Serial.print (",");
   Serial.print(acclangle);
   Serial.println ("");
       
   
    return currAngle;
   
     }   

9  Using Arduino / Programming Questions / results are 0.00 when adding 2 floats together on: June 15, 2012, 01:27:15 pm
im adapting balancing robot code i got from http://www.kerrywong.com. ( i think ) i am down to one final issue. when i blend the accelerometer and gyro data i get a 0.00 result.

 i can get both parts of the equation if i do them individually, but as soon as i add them my result goes to 0.00.
i can serial print all components of the formula and they are all floats...

Code:
degX = (float) ggyrox2 * (float) elapsedTimeSec;

        currAngle = 0.95 * (prevAngle + degX) + 0.05 * angle;
       
   //    currAngle = 0.05 * angle;
   //    currAngle = 0.95 * (prevAngle + degX);
    }
   
        prevAngle = currAngle;
   
        return currAngle;
   
     }   

10  Using Arduino / Programming Questions / Re: current error and prev error are the same in PID calculation on: May 15, 2012, 01:32:26 pm
My resolution was too low.  I multiplied the result by 10k and it showed up.

Thanks!
11  Using Arduino / Programming Questions / Re: current error and prev error are the same in PID calculation on: May 14, 2012, 12:34:06 pm
ok, i attached the code.  i combined 2 programs, they both had time functions and i have been switching between the 2 time calculations to get good data.

the attached code gets a current error, but should assign the current error from the last loop calc to the prev error for the next loop calc.

thanks for the help!
12  Using Arduino / Programming Questions / current error and prev error are the same in PID calculation on: May 14, 2012, 11:44:47 am
This code should take the current error and make it the previous error for the next loop.  they are always the exact same number. Any suggestions to fix this??

Code:
void PIDcalc() {

// float a = EstimateAngle() - INIT_ANGLE;
   float a = RwEst[1] - INIT_ANGLE; 
   angleBuffer[angleBufferIndex] = a;
    angleBufferIndex = (angleBufferIndex + 1) % ANGLE_BUFFER_LENGTH;
   
    float ang = GetAvgAngle();

    curErr = ang - INIT_ANGLE; //error   

    // Proprtional Term
  //  curErr = RwEst[1] - INIT_ANGLE; //error   
   
    // Integral Term
    SumErr += curErr;
    if (SumErr > SumErrMax) SumErr = SumErrMax;
    else if (SumErr < SumErrMin) SumErr = SumErrMin;
 //   integralTerm = SumErr * Ki / (Kp * elapsedTimeSec * 10.0);  // Ki*SumE/(Kp*Fs*X)
    integralTerm = SumErr * interval / 1000000 * Ki / Kp * 10.0;  // Ki*SumE/(Kp*Ts*X)   


    // Derivative Term
    derivativeTerm = curErr - prevErr;
     if(derivativeTerm > 0.1) derivativeTerm = 0.1;
    else if (derivativeTerm < -0.1) derivativeTerm = -0.1;
    derivativeTerm = derivativeTerm * Kd * interval / 1000000 / Kp;  // Kd(curErr-prevErr)*Ts/(Kp*X)
    if(elapsedTimeSec > 120) derivativeTerm = 120;
    else if (derivativeTerm < -120) derivativeTerm = -120;

    // PID Calc
    Cn = (curErr + integralTerm + derivativeTerm) * Kp / 10.0;
   // Serial.println (Cn);
    // Motor drive calc
    WheelDirection dir;
    if (Cn > 0) dir = DIR_FORWARD;
    else if (Cn < -0) dir = DIR_BACKWARD;
    else dir = DIR_STOP;
    throttle = abs(Cn);
    if (abs(ang) > 0.7)  MoveWheels(DIR_STOP, 0); //if angle too large to correct, stop motor       
    else MoveWheels(dir, throttle);

  prevErr = curErr;
13  Using Arduino / Programming Questions / Re: What is 1e6f in this code? (float) elapsedTime / 1e6f; on: May 12, 2012, 02:43:59 pm
makes sense.  thanks for the quick reply!
14  Using Arduino / Programming Questions / What is 1e6f in this code? (float) elapsedTime / 1e6f; on: May 12, 2012, 02:40:19 pm
Im not getting any time readings from this piece of code and i dont know what 1e6f is referring to...


float EstimateAngle() {

    currTimeStamp = micros();

    elapsedTime = currTimeStamp - prevTimeStamp;

    prevTimeStamp = currTimeStamp;

 

    elapsedTimeSec = (float) elapsedTime / 1e6f;

 
thanks!!
15  Using Arduino / Programming Questions / Re: Self balancing unicycle code. need help with data from PID on: May 12, 2012, 11:05:14 am
aaaahhh.  i see.  i fixed a couple other issues and im starting to get some output that makes sense.

thanks for the help.

glenn
Pages: [1] 2