Hi. I need some help calculating Pitch and Roll from MPU6050 data.
my code
esphome:
name: solar_tracker
platform: ESP32
board: esp-wrover-kit
wifi:
ssid: "."
password: "."
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid:
password:
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
ota:
i2c:
sda: 21
scl: 22
scan: True
sensor:
- platform: mpu6050
address: 0x68
update_interval: 100ms
accel_x:
name: "MPU6050 Accel X"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
accel_y:
name: "MPU6050 Accel Y"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
accel_z:
name: "MPU6050 Accel z"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
gyro_x:
name: "MPU6050 Gyro X"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
gyro_y:
name: "MPU6050 Gyro Y"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
gyro_z:
name: "MPU6050 Gyro z"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
temperature:
name: "MPU6050 Temperature"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
#relay bank Manual Switching
switch:
- platform: gpio
pin: 23
id: relayUp
name: "Solar Up"
- platform: gpio
pin: 19
id: relayDown
name: "Solar Down"
- platform: gpio
pin: 5
id: relayEast
name: "Solar East"
- platform: gpio
pin: 17
id: relayWest
name: "Solar West"
formula
// Calculating Roll and Pitch from the accelerometer data
Roll = (atan(AccY / sqrt(pow(AccX, 2) + pow(AccZ, 2))) * 180 / PI) ;
Pitch = (atan(-1 * AccX / sqrt(pow(AccY, 2) + pow(AccZ, 2))) * 180 / PI);
I am unsure how to reference the raw data into the above formula.
any help would be very much appreciated.
tfmeier
(Thomas Meier)
August 2, 2021, 2:03am
3
@davidleswell Have you ever found the answer? I’m exploring using the MPU-6050 to monitor changes in concrete footings
Refer to @SpikeyGG post here for examples:
Even thought I really like seeing all the raw stats for the washer and dryer solution, I didn’t like how all the decision making was pushed to Home Assistant. I always felt like this was a cop-out when I should have written the ESPHome solution to determine the state of the washer and dryer… so I did!
The benefit of this is that it’s independent of the position of the MPU6050. Previously, I had painstakingly calibrated all my dimensions (accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z for bot…
This is simply a motion/vibration sensor for washer and dryer. But the example is valuable because it shows how to define a global
variable and then set the value of that global variable using a lambda
. It also shows how to use the sensor values from the MPU6050 in your own calculation.
sensor:
- platform: mpu6050
address: 0x68
accel_x:
id: accel_x
name: "MPU6050 Accel X"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
accel_y:
id: accel_y
name: "MPU6050 Accel Y"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
accel_z:
id: accel_z
name: "MPU6050 Accel z"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
gyro_x:
name: "MPU6050 Gyro X"
id: gyro_x
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
gyro_y:
name: "MPU6050 Gyro Y"
id: gyro_y
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
gyro_z:
name: "MPU6050 Gyro z"
id: gyro_z
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
temperature:
name: "MPU6050 Temperature"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 5
update_interval: 500ms
- platform: template
id: roll
name: roll
accuracy_decimals: 2
lambda: |-
return (atan( id(accel_y).state / sqrt( pow( id(accel_x).state , 2) + pow( id(accel_z).state , 2) ) ) * 180 / PI) ;
update_interval: 500ms
- platform: template
id: pitch
name: pitch
accuracy_decimals: 2
lambda: |-
return (atan(-1 * id(accel_x).state / sqrt(pow(id(accel_y).state, 2) + pow(id(accel_z).state, 2))) * 180 / PI);
update_interval: 500ms
4 Likes
Hint: If you place a single line of three back-ticks/back-quotes above and below your code, it will present properly for others to review.
Thanks. It is already corrected
Just what I needed.
Thank You!
felmatrix
(Felmatrix)
September 14, 2024, 6:45pm
9
omartinezgomez:
- platform: template
id: roll
name: roll
accuracy_decimals: 2
lambda: |-
return (atan( id(accel_y).state / sqrt( pow( id(accel_x).state , 2) + pow( id(accel_z).state , 2) ) ) * 180 / PI) ;
update_interval: 500ms
Hi, with this code I have a problem! At 0 ° and 180 ° it is quite precise but at 90 ° I give me 75 ° and to 45 I give me about 42. How can I solve? Thank you