Components and supplies
1
Jumper wires (generic)
1
Arduino Nano R3
1
6 DOF Sensor - MPU6050
1
Resistor 100 ohm
1
General Purpose Transistor NPN
1
Relay (generic)
Tools and machines
1
Soldering iron (generic)
Project description
Code
Fire_punch
c_cpp
1 2 3#include "I2Cdev.h" 4 5#include "MPU6050_6Axis_MotionApps20.h" 6 7#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 8 #include "Wire.h" 9#endif 10 11 12MPU6050 mpu; 13 14 15 16 17 18 19#define OUTPUT_READABLE_REALACCEL 20 21 22 23 24 25 26#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards 27#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6) 28bool blinkState = false; 29 30// MPU control/status vars 31bool dmpReady = false; // set true if DMP init was successful 32uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU 33uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) 34uint16_t packetSize; // expected DMP packet size (default is 42 bytes) 35uint16_t fifoCount; // count of all bytes currently in FIFO 36uint8_t fifoBuffer[64]; // FIFO storage buffer 37 38// orientation/motion vars 39Quaternion q; // [w, x, y, z] quaternion container 40VectorInt16 aa; // [x, y, z] accel sensor measurements 41VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements 42VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements 43VectorFloat gravity; // [x, y, z] gravity vector 44float euler[3]; // [psi, theta, phi] Euler angle container 45float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector 46 47// packet structure for InvenSense teapot demo 48uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\ ', '\ 49' }; 50 51 52 53 54 55// ================================================================ 56// === INITIAL SETUP === 57// ================================================================ 58 59void setup() { 60 pinMode(2, OUTPUT); 61 62 // join I2C bus (I2Cdev library doesn't do this automatically) 63 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 64 Wire.begin(); 65// Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties 66 #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE 67 Fastwire::setup(400, true); 68 #endif 69 70 // initialize serial communication 71 // (115200 chosen because it is required for Teapot Demo output, but it's 72 // really up to you depending on your project) 73 Serial.begin(9600); 74 75 76 // initialize device 77 Serial.println(F("Initializing I2C devices...")); 78 mpu.initialize(); 79 pinMode(INTERRUPT_PIN, INPUT); 80 81 // verify connection 82 Serial.println(F("Testing device connections...")); 83 Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); 84 85 86 87 // load and configure the DMP 88 Serial.println(F("Initializing DMP...")); 89 devStatus = mpu.dmpInitialize(); 90 91 // supply your own gyro offsets here, scaled for min sensitivity 92 mpu.setXGyroOffset(220); 93 mpu.setYGyroOffset(76); 94 mpu.setZGyroOffset(-85); 95 mpu.setZAccelOffset(1788); // 1688 factory default for my test chip 96 97 // make sure it worked (returns 0 if so) 98 if (devStatus == 0) { 99 // Calibration Time: generate offsets and calibrate our MPU6050 100 mpu.CalibrateAccel(6); 101 mpu.CalibrateGyro(6); 102 mpu.PrintActiveOffsets(); 103 // turn on the DMP, now that it's ready 104 Serial.println(F("Enabling DMP...")); 105 mpu.setDMPEnabled(true); 106 107 108 109 110 Serial.println(F("DMP ready! Waiting for first interrupt...")); 111 dmpReady = true; 112 113 // get expected DMP packet size for later comparison 114 packetSize = mpu.dmpGetFIFOPacketSize(); 115 } else { 116 // ERROR! 117 // 1 = initial memory load failed 118 // 2 = DMP configuration updates failed 119 // (if it's going to break, usually the code will be 1) 120 Serial.print(F("DMP Initialization failed (code ")); 121 Serial.print(devStatus); 122 Serial.println(F(")")); 123 } 124 125} 126 127 128 129// ================================================================ 130// === MAIN PROGRAM LOOP === 131// ================================================================ 132 133void loop() { 134 135 // if programming failed, don't try to do anything 136 if (!dmpReady) return; 137 // read a packet from FIFO 138 if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) { // Get the Latest packet 139 140 141 142 143 #ifdef OUTPUT_READABLE_REALACCEL 144 // display real acceleration, adjusted to remove gravity 145 mpu.dmpGetQuaternion(&q, fifoBuffer); 146 mpu.dmpGetAccel(&aa, fifoBuffer); 147 mpu.dmpGetGravity(&gravity, &q); 148 mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); 149 Serial.print("areal\ "); 150 Serial.print(aaReal.x); 151 Serial.print("\ "); 152 Serial.print(aaReal.y); 153 Serial.print("\ "); 154 Serial.println(aaReal.z); 155 #endif 156 157 158 159 if (aaReal.y <= -9500 ){ 160 digitalWrite(2, HIGH); 161 delay(175); 162 digitalWrite(2, LOW); 163 delay(175); 164 } 165 166 } 167 168} 169
Fire_punch
c_cpp
1 2 3#include "I2Cdev.h" 4 5#include "MPU6050_6Axis_MotionApps20.h" 6 7#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 8 #include "Wire.h" 9#endif 10 11 12MPU6050 mpu; 13 14 15 16 17 18 19#define OUTPUT_READABLE_REALACCEL 20 21 22 23 24 25 26#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards 27#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6) 28bool blinkState = false; 29 30// MPU control/status vars 31bool dmpReady = false; // set true if DMP init was successful 32uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU 33uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) 34uint16_t packetSize; // expected DMP packet size (default is 42 bytes) 35uint16_t fifoCount; // count of all bytes currently in FIFO 36uint8_t fifoBuffer[64]; // FIFO storage buffer 37 38// orientation/motion vars 39Quaternion q; // [w, x, y, z] quaternion container 40VectorInt16 aa; // [x, y, z] accel sensor measurements 41VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements 42VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements 43VectorFloat gravity; // [x, y, z] gravity vector 44float euler[3]; // [psi, theta, phi] Euler angle container 45float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector 46 47// packet structure for InvenSense teapot demo 48uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' }; 49 50 51 52 53 54// ================================================================ 55// === INITIAL SETUP === 56// ================================================================ 57 58void setup() { 59 pinMode(2, OUTPUT); 60 61 // join I2C bus (I2Cdev library doesn't do this automatically) 62 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 63 Wire.begin(); 64// Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties 65 #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE 66 Fastwire::setup(400, true); 67 #endif 68 69 // initialize serial communication 70 // (115200 chosen because it is required for Teapot Demo output, but it's 71 // really up to you depending on your project) 72 Serial.begin(9600); 73 74 75 // initialize device 76 Serial.println(F("Initializing I2C devices...")); 77 mpu.initialize(); 78 pinMode(INTERRUPT_PIN, INPUT); 79 80 // verify connection 81 Serial.println(F("Testing device connections...")); 82 Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); 83 84 85 86 // load and configure the DMP 87 Serial.println(F("Initializing DMP...")); 88 devStatus = mpu.dmpInitialize(); 89 90 // supply your own gyro offsets here, scaled for min sensitivity 91 mpu.setXGyroOffset(220); 92 mpu.setYGyroOffset(76); 93 mpu.setZGyroOffset(-85); 94 mpu.setZAccelOffset(1788); // 1688 factory default for my test chip 95 96 // make sure it worked (returns 0 if so) 97 if (devStatus == 0) { 98 // Calibration Time: generate offsets and calibrate our MPU6050 99 mpu.CalibrateAccel(6); 100 mpu.CalibrateGyro(6); 101 mpu.PrintActiveOffsets(); 102 // turn on the DMP, now that it's ready 103 Serial.println(F("Enabling DMP...")); 104 mpu.setDMPEnabled(true); 105 106 107 108 109 Serial.println(F("DMP ready! Waiting for first interrupt...")); 110 dmpReady = true; 111 112 // get expected DMP packet size for later comparison 113 packetSize = mpu.dmpGetFIFOPacketSize(); 114 } else { 115 // ERROR! 116 // 1 = initial memory load failed 117 // 2 = DMP configuration updates failed 118 // (if it's going to break, usually the code will be 1) 119 Serial.print(F("DMP Initialization failed (code ")); 120 Serial.print(devStatus); 121 Serial.println(F(")")); 122 } 123 124} 125 126 127 128// ================================================================ 129// === MAIN PROGRAM LOOP === 130// ================================================================ 131 132void loop() { 133 134 // if programming failed, don't try to do anything 135 if (!dmpReady) return; 136 // read a packet from FIFO 137 if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) { // Get the Latest packet 138 139 140 141 142 #ifdef OUTPUT_READABLE_REALACCEL 143 // display real acceleration, adjusted to remove gravity 144 mpu.dmpGetQuaternion(&q, fifoBuffer); 145 mpu.dmpGetAccel(&aa, fifoBuffer); 146 mpu.dmpGetGravity(&gravity, &q); 147 mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); 148 Serial.print("areal\ "); 149 Serial.print(aaReal.x); 150 Serial.print("\ "); 151 Serial.print(aaReal.y); 152 Serial.print("\ "); 153 Serial.println(aaReal.z); 154 #endif 155 156 157 158 if (aaReal.y <= -9500 ){ 159 digitalWrite(2, HIGH); 160 delay(175); 161 digitalWrite(2, LOW); 162 delay(175); 163 } 164 165 } 166 167} 168
Downloadable files
arduino, mpu6050, realy
arduino, mpu6050, realy
Comments
Only logged in users can leave comments