Go Down

Topic: $ Balancing Tray Project Troubleshooting $ (Read 954 times) previous topic - next topic

cedarlakeinstruments

#15
Sep 23, 2019, 01:52 pm Last Edit: Sep 23, 2019, 01:53 pm by cedarlakeinstruments
I'm not using a (RC) servo. I'm using a DC gear motor and in essence, that and the MPU6050 and code form my own servomechanism.

The difference between desired angle and current angle is interpreted as an error. If the error is inside a certain deadband (5 degrees in your case), then the PWM generated to control the motor is zero, so it won't move. Otherwise, depending on the sign of the error, the motor direction is set clockwise or counterclockwise and the PWM value is sent to the motor.


Code: [Select]

    double deadband = 2.0;
    double error = -1.0 * ((ypr[2] * 180/M_PI) - _setpoint);
    int absError = abs(error);
    int pwm = (absError < 45.0 && absError > deadband) ? (int)(absError * 7.0) & 255 : 0;
    bool dir = error > 0 ? true : false;
    digitalWrite(A1, dir);
    digitalWrite(A2, !dir);           
    analogWrite(SPD_PIN, pwm);
Electronics and firmware/software design and assistance. No project too small

CzarOR

I'm not using a (RC) servo. I'm using a DC gear motor and in essence, that and the MPU6050 and code form my own servomechanism.

The difference between desired angle and current angle is interpreted as an error. If the error is inside a certain deadband (5 degrees in your case), then the PWM generated to control the motor is zero, so it won't move. Otherwise, depending on the sign of the error, the motor direction is set clockwise or counterclockwise and the PWM value is sent to the motor.


Code: [Select]

    double deadband = 2.0;
    double error = -1.0 * ((ypr[2] * 180/M_PI) - _setpoint);
    int absError = abs(error);
    int pwm = (absError < 45.0 && absError > deadband) ? (int)(absError * 7.0) & 255 : 0;
    bool dir = error > 0 ? true : false;
    digitalWrite(A1, dir);
    digitalWrite(A2, !dir);           
    analogWrite(SPD_PIN, pwm);


Oh ok Thank You!

Technically this is the same concept I am trying to achieve, I check the MPU6050 reading and then depending on if it's greater than 5 or -5 make the servo motor move accordingly, however I believe I have it wrong in the sense that I am hard coding the numbers in instead of making the servo motor rotate clockwise/ counter clockwise until they reach the condition of between -5 to 5. Will try to change that up with some commands I have found online for this.

I was also wondering, I noticed that many if not all of these balancing projects use MPU offsets. I used the following tutorial to find mine so called offsets to input into my program.

https://42bots.com/tutorials/arduino-script-for-mpu-6050-auto-calibration/

Do you happen to know what's the purpose of these offsets? They seem important but I can't quite figure out what exactly they change in my program.

cedarlakeinstruments

I haven't looked into the specifics of calibration and my program is still using the default values. However, based on my previous experience with accelerometers & gyros, it's probably compensating for the difference in ideal placement of the sensor, versus the at-rest position in your mechanism.

One thing I have found is that my system really wants to have the z-axis of the '6050 vertical at power-on. This could be because I haven't calibrated it yet.
Electronics and firmware/software design and assistance. No project too small

Go Up