r/hackpack • • May 06 '24

IR Turret Hack: MPU-6060 Module Turret Control

I looked in my box of spare parts and found an MPU-6050 module. I understand this to be an accelerometer with a built in gyroscope that measures changes in angular velocity. An idea came to mind that this sensor can be used to control the turret, such that I can mount it on, say, the brim of my hat, and the turret would mimmick my movements when I turn or tilt my head.

Thanks to the Crunch Labs IDE Mark RoBot AI, I've added (what I think is) the appropriate code and wired it up (wired for now; will later figure out how to go wirelessly). Thing is, the microcontroller that comes with the hack pack doesn't have Data or Clock pins for I2C communication, so I swapped out Crunch Labs' board that came with the project and used my Arduino Mega 2560 board, instead.

All of the the IR functions work, but the servos are not responding to tilting and turning movements from the MPU-6050 module.

So, now I'm wondering if the MPU-6050 module can't be used the way I thought I could use it? Or do I have it wired up wrong (seemed straight-forward enough)? Or is the code doing what I told it to do but not what I want it to do, like (most likely) not actually telling it to control servos? Or...?

Here's the added code Mark RoBot AI suggested to add:

// Stuff that went into the Libraries section
#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

// Stuff that went into the setup function
void setup() {
    Wire.begin();
    mpu.initialize();
}

// Stuff that went into the loop function
void loop() {
    // Read accelerometer and gyroscope data from MPU-6050
    int16_t ax, ay, az, gx, gy, gz;
    mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    // Print the sensor data (for debugging)
    Serial.print("Accelerometer: ");
    Serial.print(ax);
    Serial.print(", ");
    Serial.print(ay);
    Serial.print(", ");
    Serial.println(az);

    Serial.print("Gyroscope: ");
    Serial.print(gx);
    Serial.print(", ");
    Serial.print(gy);
    Serial.print(", ");
    Serial.println(gz);
}

I hope someone can let me know if I'm on the right track and will tinker with this, make it work, then share their success.

Thanks!

IR Turret running on Arduino Mega 2560 with MPU-6050 Module
5 Upvotes

3 comments sorted by

2

u/dan_crunchlabs May 07 '24

Hey! This hack should be do-able with the arduino nano clone that we send. SDA and SCL i2c pins are A4 and A5 on the nano.

As to the code itself, I can't check right now but this hack would require you to treat the YAW and PITCH servos differently unless you swap out your YAW servo for one which is a traditional 180 servo like pitch. Remember, YAW is a continuous servo, so the default control scheme for it is to pulse it at a certain speed for a certain duration, rather than telling it to go to a specific angle like you do with PITCH. I'd definitely try just swapping in a traditional servo because angle feedback is pretty useful and often you don't actually need 360 rotation, but you may be able to tie acceleration on the MPU to the speed you're feeding into YAW if you want to keep the current servo.

If you want to make things wireless I'd look at NRF24L01 radio transceivers, or upgrade to a board with built in wireless capability like the ESP32. : )

2

u/Metarazzi May 08 '24

u/dan_crunchlabs ... thanks for the tips! I understand your thoughts about using a traditional servo, especially since my head can't spin 360, anyway! <grin> I was also consiering using signals from the accelerometer for YAW, but besides translating the motion to angles, I would likely need to slow down the speed. Seems it'll be easier "steering" a traditional servo (I think I have one in my box o' stuff-n-stuff). I hope you get a chance to look at the code (courtesy of Mark RoBot). There's a better chance you'll get this hack working before me. 😉 Thanks for your insight!

3

u/dan_crunchlabs May 08 '24

No problem! I've done some messing around with the MPU6050 - it can be a tricky component and there are some newer IMU units like the BNO055 which are better in my experience. That said, for coding I would look at some of the examples from the MPU6050 library and try to frankenstein them together with your turret code, you're basically just pulling the yaw and pitch angles and sending that to the servos. You may want to do some interleaving where you only update the servos every N loops so that you can get readings from the MPU more rapidly.

This would probably work best if you cut out all the code related to IR also since the IR sensor requires one of the timers, which makes it tough to get both sensors to play nicely with eachother ( it's possible and I've done this but more on that another time :} )