Arduino code to control motor speed based on MPU-6050 sensor












1












$begingroup$


I have been trying to create arduino code where my MPU6050 reads its angle and based on that angle, change the motor speed. For example, 45° is the max motor speed, 22° is roughly half speed, and so on.



I have a 5065 200kv brushless motor going to a ESC with a bec which I can attach to the Arduino. I have modified some self-balancing drone code from a tutorial that used a proportional-integral-derivative controller. What I want to do is take out the PID and do a comparison: if the angle is between 5° and 45° then servo write (angle × 44), because if I multiply the maximum angle by 44 I will get around 2000, which is the original top speed of the code. Although mine can go faster, and I will change it later after testing. I am fairly new with Arduino and I have not used the MPU6050 before.



#include <Wire.h>
#include <Servo.h>

Servo right_prop;

int16_t Acc_rawX, Acc_rawY, Acc_rawZ,Gyr_rawX, Gyr_rawY, Gyr_rawZ;

float Acceleration_angle[2];
float Gyro_angle[2];
float Total_angle[2];

float elapsedTime, time, timePrev;
int i;
float rad_to_deg = 180/3.141592654;

double throttle=0; //initial value of throttle to the motors

void setup() {
Wire.begin(); //begin the wire comunication
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(250000);

right_prop.attach(3);

time = millis();
}
void loop() {

/////////////////////////////I M U/////////////////////////////////////
timePrev = time;
time = millis();
elapsedTime = (time - timePrev) / 1000;

Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(0x68,6,true);

Acc_rawX=Wire.read()<<8|Wire.read();
Acc_rawY=Wire.read()<<8|Wire.read();
Acc_rawZ=Wire.read()<<8|Wire.read();


Acceleration_angle[0] = atan((Acc_rawY/16384.0)/sqrt(pow((Acc_rawX/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;
/*---Y---*/
Acceleration_angle[1] = atan(-1*(Acc_rawX/16384.0)/sqrt(pow((Acc_rawY/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;

Wire.beginTransmission(0x68);
Wire.write(0x43); //Gyro data first adress
Wire.endTransmission(false);
Wire.requestFrom(0x68,4,true); //Just 4 registers

Gyr_rawX=Wire.read()<<8|Wire.read();
Gyr_rawY=Wire.read()<<8|Wire.read();

/*---X---*/
Gyro_angle[0] = Gyr_rawX/131.0;
/*---Y---*/
Gyro_angle[1] = Gyr_rawY/131.0;

/*---X axis angle---*/
Total_angle[0] = 0.98 *(Total_angle[0] + Gyro_angle[0]*elapsedTime) + 0.02*Acceleration_angle[0];
/*---Y axis angle---*/
Total_angle[1] = 0.98 *(Total_angle[1] + Gyro_angle[1]*elapsedTime) + 0.02*Acceleration_angle[1];

Serial.println(Total_angle[1]);

if( Total_angle[1]> 5 && Total_angle[1]< 45)
{
right_prop.write(Total_angle[1]*44);
}
else{
right_prop.write(0);
}
}

//end of loop void









share|improve this question









New contributor




miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    i have modified code using the code of a self balancing drone which used the mpu6050 and made a PID control system to balance the drone between two motors spinning at different speeds to counteract the other motor and make the drone level. My modified code is my attempt at removing the PID control and make it so the arduino reads what angle the sensor is at and tells my one motor to go a certain speed depending on the angle. If you would like i could post the original code or the video about it on youtube.
    $endgroup$
    – miles
    2 hours ago












  • $begingroup$
    Where did the original code come from?
    $endgroup$
    – 200_success
    2 hours ago










  • $begingroup$
    A youtube video youtu.be/AN3yxIBAxTA
    $endgroup$
    – miles
    2 hours ago










  • $begingroup$
    I have gone back and corrected all the errors from copying it. I appologize if i did this completely wrong. I believe i got rid of anything related to the PID and kept everything for reading the angle of the sensor to tell the motor what to do. Would it be simpler to make code that purely reads the angle of the motor and just add a “servo” to the code then do my if-then statement in the loop?
    $endgroup$
    – miles
    1 hour ago
















1












$begingroup$


I have been trying to create arduino code where my MPU6050 reads its angle and based on that angle, change the motor speed. For example, 45° is the max motor speed, 22° is roughly half speed, and so on.



I have a 5065 200kv brushless motor going to a ESC with a bec which I can attach to the Arduino. I have modified some self-balancing drone code from a tutorial that used a proportional-integral-derivative controller. What I want to do is take out the PID and do a comparison: if the angle is between 5° and 45° then servo write (angle × 44), because if I multiply the maximum angle by 44 I will get around 2000, which is the original top speed of the code. Although mine can go faster, and I will change it later after testing. I am fairly new with Arduino and I have not used the MPU6050 before.



#include <Wire.h>
#include <Servo.h>

Servo right_prop;

int16_t Acc_rawX, Acc_rawY, Acc_rawZ,Gyr_rawX, Gyr_rawY, Gyr_rawZ;

float Acceleration_angle[2];
float Gyro_angle[2];
float Total_angle[2];

float elapsedTime, time, timePrev;
int i;
float rad_to_deg = 180/3.141592654;

double throttle=0; //initial value of throttle to the motors

void setup() {
Wire.begin(); //begin the wire comunication
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(250000);

right_prop.attach(3);

time = millis();
}
void loop() {

/////////////////////////////I M U/////////////////////////////////////
timePrev = time;
time = millis();
elapsedTime = (time - timePrev) / 1000;

Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(0x68,6,true);

Acc_rawX=Wire.read()<<8|Wire.read();
Acc_rawY=Wire.read()<<8|Wire.read();
Acc_rawZ=Wire.read()<<8|Wire.read();


Acceleration_angle[0] = atan((Acc_rawY/16384.0)/sqrt(pow((Acc_rawX/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;
/*---Y---*/
Acceleration_angle[1] = atan(-1*(Acc_rawX/16384.0)/sqrt(pow((Acc_rawY/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;

Wire.beginTransmission(0x68);
Wire.write(0x43); //Gyro data first adress
Wire.endTransmission(false);
Wire.requestFrom(0x68,4,true); //Just 4 registers

Gyr_rawX=Wire.read()<<8|Wire.read();
Gyr_rawY=Wire.read()<<8|Wire.read();

/*---X---*/
Gyro_angle[0] = Gyr_rawX/131.0;
/*---Y---*/
Gyro_angle[1] = Gyr_rawY/131.0;

/*---X axis angle---*/
Total_angle[0] = 0.98 *(Total_angle[0] + Gyro_angle[0]*elapsedTime) + 0.02*Acceleration_angle[0];
/*---Y axis angle---*/
Total_angle[1] = 0.98 *(Total_angle[1] + Gyro_angle[1]*elapsedTime) + 0.02*Acceleration_angle[1];

Serial.println(Total_angle[1]);

if( Total_angle[1]> 5 && Total_angle[1]< 45)
{
right_prop.write(Total_angle[1]*44);
}
else{
right_prop.write(0);
}
}

//end of loop void









share|improve this question









New contributor




miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    i have modified code using the code of a self balancing drone which used the mpu6050 and made a PID control system to balance the drone between two motors spinning at different speeds to counteract the other motor and make the drone level. My modified code is my attempt at removing the PID control and make it so the arduino reads what angle the sensor is at and tells my one motor to go a certain speed depending on the angle. If you would like i could post the original code or the video about it on youtube.
    $endgroup$
    – miles
    2 hours ago












  • $begingroup$
    Where did the original code come from?
    $endgroup$
    – 200_success
    2 hours ago










  • $begingroup$
    A youtube video youtu.be/AN3yxIBAxTA
    $endgroup$
    – miles
    2 hours ago










  • $begingroup$
    I have gone back and corrected all the errors from copying it. I appologize if i did this completely wrong. I believe i got rid of anything related to the PID and kept everything for reading the angle of the sensor to tell the motor what to do. Would it be simpler to make code that purely reads the angle of the motor and just add a “servo” to the code then do my if-then statement in the loop?
    $endgroup$
    – miles
    1 hour ago














1












1








1





$begingroup$


I have been trying to create arduino code where my MPU6050 reads its angle and based on that angle, change the motor speed. For example, 45° is the max motor speed, 22° is roughly half speed, and so on.



I have a 5065 200kv brushless motor going to a ESC with a bec which I can attach to the Arduino. I have modified some self-balancing drone code from a tutorial that used a proportional-integral-derivative controller. What I want to do is take out the PID and do a comparison: if the angle is between 5° and 45° then servo write (angle × 44), because if I multiply the maximum angle by 44 I will get around 2000, which is the original top speed of the code. Although mine can go faster, and I will change it later after testing. I am fairly new with Arduino and I have not used the MPU6050 before.



#include <Wire.h>
#include <Servo.h>

Servo right_prop;

int16_t Acc_rawX, Acc_rawY, Acc_rawZ,Gyr_rawX, Gyr_rawY, Gyr_rawZ;

float Acceleration_angle[2];
float Gyro_angle[2];
float Total_angle[2];

float elapsedTime, time, timePrev;
int i;
float rad_to_deg = 180/3.141592654;

double throttle=0; //initial value of throttle to the motors

void setup() {
Wire.begin(); //begin the wire comunication
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(250000);

right_prop.attach(3);

time = millis();
}
void loop() {

/////////////////////////////I M U/////////////////////////////////////
timePrev = time;
time = millis();
elapsedTime = (time - timePrev) / 1000;

Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(0x68,6,true);

Acc_rawX=Wire.read()<<8|Wire.read();
Acc_rawY=Wire.read()<<8|Wire.read();
Acc_rawZ=Wire.read()<<8|Wire.read();


Acceleration_angle[0] = atan((Acc_rawY/16384.0)/sqrt(pow((Acc_rawX/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;
/*---Y---*/
Acceleration_angle[1] = atan(-1*(Acc_rawX/16384.0)/sqrt(pow((Acc_rawY/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;

Wire.beginTransmission(0x68);
Wire.write(0x43); //Gyro data first adress
Wire.endTransmission(false);
Wire.requestFrom(0x68,4,true); //Just 4 registers

Gyr_rawX=Wire.read()<<8|Wire.read();
Gyr_rawY=Wire.read()<<8|Wire.read();

/*---X---*/
Gyro_angle[0] = Gyr_rawX/131.0;
/*---Y---*/
Gyro_angle[1] = Gyr_rawY/131.0;

/*---X axis angle---*/
Total_angle[0] = 0.98 *(Total_angle[0] + Gyro_angle[0]*elapsedTime) + 0.02*Acceleration_angle[0];
/*---Y axis angle---*/
Total_angle[1] = 0.98 *(Total_angle[1] + Gyro_angle[1]*elapsedTime) + 0.02*Acceleration_angle[1];

Serial.println(Total_angle[1]);

if( Total_angle[1]> 5 && Total_angle[1]< 45)
{
right_prop.write(Total_angle[1]*44);
}
else{
right_prop.write(0);
}
}

//end of loop void









share|improve this question









New contributor




miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I have been trying to create arduino code where my MPU6050 reads its angle and based on that angle, change the motor speed. For example, 45° is the max motor speed, 22° is roughly half speed, and so on.



I have a 5065 200kv brushless motor going to a ESC with a bec which I can attach to the Arduino. I have modified some self-balancing drone code from a tutorial that used a proportional-integral-derivative controller. What I want to do is take out the PID and do a comparison: if the angle is between 5° and 45° then servo write (angle × 44), because if I multiply the maximum angle by 44 I will get around 2000, which is the original top speed of the code. Although mine can go faster, and I will change it later after testing. I am fairly new with Arduino and I have not used the MPU6050 before.



#include <Wire.h>
#include <Servo.h>

Servo right_prop;

int16_t Acc_rawX, Acc_rawY, Acc_rawZ,Gyr_rawX, Gyr_rawY, Gyr_rawZ;

float Acceleration_angle[2];
float Gyro_angle[2];
float Total_angle[2];

float elapsedTime, time, timePrev;
int i;
float rad_to_deg = 180/3.141592654;

double throttle=0; //initial value of throttle to the motors

void setup() {
Wire.begin(); //begin the wire comunication
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(250000);

right_prop.attach(3);

time = millis();
}
void loop() {

/////////////////////////////I M U/////////////////////////////////////
timePrev = time;
time = millis();
elapsedTime = (time - timePrev) / 1000;

Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(0x68,6,true);

Acc_rawX=Wire.read()<<8|Wire.read();
Acc_rawY=Wire.read()<<8|Wire.read();
Acc_rawZ=Wire.read()<<8|Wire.read();


Acceleration_angle[0] = atan((Acc_rawY/16384.0)/sqrt(pow((Acc_rawX/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;
/*---Y---*/
Acceleration_angle[1] = atan(-1*(Acc_rawX/16384.0)/sqrt(pow((Acc_rawY/16384.0),2) + pow((Acc_rawZ/16384.0),2)))*rad_to_deg;

Wire.beginTransmission(0x68);
Wire.write(0x43); //Gyro data first adress
Wire.endTransmission(false);
Wire.requestFrom(0x68,4,true); //Just 4 registers

Gyr_rawX=Wire.read()<<8|Wire.read();
Gyr_rawY=Wire.read()<<8|Wire.read();

/*---X---*/
Gyro_angle[0] = Gyr_rawX/131.0;
/*---Y---*/
Gyro_angle[1] = Gyr_rawY/131.0;

/*---X axis angle---*/
Total_angle[0] = 0.98 *(Total_angle[0] + Gyro_angle[0]*elapsedTime) + 0.02*Acceleration_angle[0];
/*---Y axis angle---*/
Total_angle[1] = 0.98 *(Total_angle[1] + Gyro_angle[1]*elapsedTime) + 0.02*Acceleration_angle[1];

Serial.println(Total_angle[1]);

if( Total_angle[1]> 5 && Total_angle[1]< 45)
{
right_prop.write(Total_angle[1]*44);
}
else{
right_prop.write(0);
}
}

//end of loop void






arduino device-driver






share|improve this question









New contributor




miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









200_success

130k16153417




130k16153417






New contributor




miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









milesmiles

62




62




New contributor




miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






miles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    i have modified code using the code of a self balancing drone which used the mpu6050 and made a PID control system to balance the drone between two motors spinning at different speeds to counteract the other motor and make the drone level. My modified code is my attempt at removing the PID control and make it so the arduino reads what angle the sensor is at and tells my one motor to go a certain speed depending on the angle. If you would like i could post the original code or the video about it on youtube.
    $endgroup$
    – miles
    2 hours ago












  • $begingroup$
    Where did the original code come from?
    $endgroup$
    – 200_success
    2 hours ago










  • $begingroup$
    A youtube video youtu.be/AN3yxIBAxTA
    $endgroup$
    – miles
    2 hours ago










  • $begingroup$
    I have gone back and corrected all the errors from copying it. I appologize if i did this completely wrong. I believe i got rid of anything related to the PID and kept everything for reading the angle of the sensor to tell the motor what to do. Would it be simpler to make code that purely reads the angle of the motor and just add a “servo” to the code then do my if-then statement in the loop?
    $endgroup$
    – miles
    1 hour ago


















  • $begingroup$
    i have modified code using the code of a self balancing drone which used the mpu6050 and made a PID control system to balance the drone between two motors spinning at different speeds to counteract the other motor and make the drone level. My modified code is my attempt at removing the PID control and make it so the arduino reads what angle the sensor is at and tells my one motor to go a certain speed depending on the angle. If you would like i could post the original code or the video about it on youtube.
    $endgroup$
    – miles
    2 hours ago












  • $begingroup$
    Where did the original code come from?
    $endgroup$
    – 200_success
    2 hours ago










  • $begingroup$
    A youtube video youtu.be/AN3yxIBAxTA
    $endgroup$
    – miles
    2 hours ago










  • $begingroup$
    I have gone back and corrected all the errors from copying it. I appologize if i did this completely wrong. I believe i got rid of anything related to the PID and kept everything for reading the angle of the sensor to tell the motor what to do. Would it be simpler to make code that purely reads the angle of the motor and just add a “servo” to the code then do my if-then statement in the loop?
    $endgroup$
    – miles
    1 hour ago
















$begingroup$
i have modified code using the code of a self balancing drone which used the mpu6050 and made a PID control system to balance the drone between two motors spinning at different speeds to counteract the other motor and make the drone level. My modified code is my attempt at removing the PID control and make it so the arduino reads what angle the sensor is at and tells my one motor to go a certain speed depending on the angle. If you would like i could post the original code or the video about it on youtube.
$endgroup$
– miles
2 hours ago






$begingroup$
i have modified code using the code of a self balancing drone which used the mpu6050 and made a PID control system to balance the drone between two motors spinning at different speeds to counteract the other motor and make the drone level. My modified code is my attempt at removing the PID control and make it so the arduino reads what angle the sensor is at and tells my one motor to go a certain speed depending on the angle. If you would like i could post the original code or the video about it on youtube.
$endgroup$
– miles
2 hours ago














$begingroup$
Where did the original code come from?
$endgroup$
– 200_success
2 hours ago




$begingroup$
Where did the original code come from?
$endgroup$
– 200_success
2 hours ago












$begingroup$
A youtube video youtu.be/AN3yxIBAxTA
$endgroup$
– miles
2 hours ago




$begingroup$
A youtube video youtu.be/AN3yxIBAxTA
$endgroup$
– miles
2 hours ago












$begingroup$
I have gone back and corrected all the errors from copying it. I appologize if i did this completely wrong. I believe i got rid of anything related to the PID and kept everything for reading the angle of the sensor to tell the motor what to do. Would it be simpler to make code that purely reads the angle of the motor and just add a “servo” to the code then do my if-then statement in the loop?
$endgroup$
– miles
1 hour ago




$begingroup$
I have gone back and corrected all the errors from copying it. I appologize if i did this completely wrong. I believe i got rid of anything related to the PID and kept everything for reading the angle of the sensor to tell the motor what to do. Would it be simpler to make code that purely reads the angle of the motor and just add a “servo” to the code then do my if-then statement in the loop?
$endgroup$
– miles
1 hour ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






miles is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214417%2farduino-code-to-control-motor-speed-based-on-mpu-6050-sensor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








miles is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















miles is a new contributor. Be nice, and check out our Code of Conduct.













miles is a new contributor. Be nice, and check out our Code of Conduct.












miles is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214417%2farduino-code-to-control-motor-speed-based-on-mpu-6050-sensor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

第一次世界大戦

Touch on Surface Book