greetings i am new to the forum first i apologize if the topic does not go in this section i would appreciate your help
i'm trying to develop an angular position control for an axis i'm using a MPU6050 and a brushless motor A2212 i need to acquire some samples for later graphics in (jupyter notebook)
#include <TimerOne.h>
#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>
MPU6050 sensor;
// Valores RAW (sin procesar) del acelerometro
int ax, ay, az;
Servo ESC;
int pwm_T;
void setup() {
Serial.begin(115200); // Iniciando Monitor Serie
Wire.begin(); // Iniciando I2C
sensor.initialize(); // Iniciando el sensor
// Vamos a configuración del sensor
configurar_sensor();
//configuracion de la interrupcion
Timer1.initialize(1000);
Timer1.attachInterrupt(readData);
//armado del ESC
ESC.attach(8); //pin donde va conectado
ESC.writeMicroseconds(1000);
delay(5000);
pwm_T = 1350;
ESC.writeMicroseconds(pwm_T);
}
void configurar_sensor()
{
//Activa el MPU-6050
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission();
// Configura el acelerometro con (+/-8g)
Wire.beginTransmission(0x68);
Wire.write(0x1C);
Wire.write(0x10); // 8g. Sensibilidad = 4096
Wire.endTransmission();
}
void loop() {
}
void readData()
{
// Leer las aceleraciones angulares
sensor.getAcceleration(&ax, &ay, &az);
float ax_m_s2 = ax * (9.81/4096.0);
float ay_m_s2 = ay * (9.81/4096.0);
float az_m_s2 = az * (9.81/4096.0);
//calcular angulo de pitch y roll
//float accel_ang_x = atan(ax / sqrt(pow(ay, 2) + pow(az, 2)))*(180.0 / 3.141592);
float accel_ang_y = atan(ay / sqrt(pow(ax, 2) + pow(az, 2)))*(180.0 / 3.141592);
Serial.print("$");
Serial.print(accel_ang_y);
Serial.print("$");
Serial.println(pwm_T);
}
I don't get an error in the code but when I run it the sensor doesn't show me the data I would appreciate it if you could help me
Thanks for using code tags on your first post!
when I run it the sensor doesn't show me the data
Please describe the error. Tell us what you expected, and what happened instead.
Hint: you are not using the correct formula for tilt angles. Here are the correct ones
Greetings, thank you very much for responding and I apologize, I don't think I was very clear.
the error or what I expected is to print the data in this case angles on the serial monitor but I get blank which does not happen if I put the code within the void loop, ie without using the timer
Thank you for the suggestion that I am using the MPU6050 and a question if you allow me to conflict with the timer library1 with the wire.h library
The loop() function is empty, so you never read or print data.
Try this:
void loop() {
readData();
delay(500); //not required
}
If you want to measure tilt angles, it is NOT a good idea to set the accelerometer range to 8g. Take out the code associated with this comment.
// Configura el acelerometro con (+/-8g)
Realmente aprecio tu ayuda para eliminar la parte de una sensibilidad de (+/- 8g) funciona perfectamente
pero me gustaría tomar muestras de esos datos (ángulos) cada período de tiempo, encuentro que con la biblioteca TimerOne puedo hacerlo pero no sé cómo
#include <TimerOne.h>
#include <Wire.h>
//Direccion i2C de la IMU
#define MPU 0x68
//ratios de conversion
#define A_R 16384.0
#define G_R 131.0
//conversion de radianes a grados 180/pi
#define RAD_ADEG = 57.295779
//valores sin refinar
int Acx, Acy, Acz;
void setup() {
// put your setup code here, to run once:
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(115200);
Timer1.initialize(5000); //inicializar cada 5milisegundos
Timer1.attachInterrupt(IMU_0); //activa la interrupcion
}
void loop() {
// put your main code here, to run repeatedly:
}
void IMU_0(void)
{
//leer los valores del acelerometro
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,6,true);
Acx = Wire.read()<<8|Wire.read();
Acy = Wire.read()<<8|Wire.read();
Acz = Wire.read()<<8|Wire.read();
Serial.print(Acx);
}
Timer1.initialize (5000)
Timer1.attachInterrupt (IMU_0);
como entiendo cada período de tiempo, ejecutaré lo que tengo en la función IMU_0 pero no lo está haciendo
Aprecio tu ayuda
The Timer1 library won't be any more accurate than millis() for sampling the data, and it is simpler to use millis().
Try this code with your first program.
unsigned long start=0, period = 5000UL;
void loop() {
if (millis()-start >= period) { //read and print data every 5 seconds
start = millis();
readData();
}
}
thank you very much , you are absolutely right I added that part to the code and it worked and I can see the data
thank you very much for all the help provided 