Components and supplies
Android phone
HC-05 Bluetooth Module
6 DOF Sensor - MPU6050
Arduino Pro Mini 328 - 5V/16MHz
Jumper wires (generic)
Breadboard (generic)
Tools and machines
USB to serial FTDI adapter FT232RL
Arduino IDE
Android studio
Project description
Code
Code snippet #4
text
1private final int value_convert_out(int value) { 2 boolean negative = false; 3 if (value < 0) { 4 negative = f6D; 5 } 6 int value2 = value & 63; 7 if (negative) { 8 return value2 | 64; 9 } 10 return value2; 11}
Github
https://github.com/jrowberg/i2cdevlib
Code snippet #6
text
1/** * Libraries: 2 * https://github.com/jrowberg/i2cdevlib 3 4 * https://github.com/jrowberg/i2cdevlib 5 6 */ 7#include "I2Cdev.h" 8#include "MPU6050_6Axis_MotionApps20.h" 9#include "Wire.h" 10#include "SoftwareSerial.h"const int MAX_ANGLE = 45; 11const byte commandStering = 129; 12const byte commandSpeed = 130;bool initialization = false; // set true if DMP init was successful 13uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU 14uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) 15uint16_t packetSize; // expected DMP packet size (default is 42 bytes) 16uint16_t fifoCount; // count of all bytes currently in FIFO 17uint8_t fifoBuffer[64]; // FIFO storage buffer 18Quaternion q; // [w, x, y, z] quaternion container 19VectorFloat gravity; // [x, y, z] gravity vector 20float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector 21volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone highunsigned long lastPrintTime, lastMoveTime = 0;SoftwareSerial BTserial(10, 11); 22MPU6050 mpu;void setup() 23{ 24 Serial.begin(9600); 25 BTserial.begin(38400); 26 Serial.println("Program started"); 27 initialization = initializeGyroscope(); 28}void loop() { 29 if (!initialization) { 30 return; 31 } 32 mpuInterrupt = false; 33 mpuIntStatus = mpu.getIntStatus(); 34 fifoCount = mpu.getFIFOCount(); 35 if (hasFifoOverflown(mpuIntStatus, fifoCount)) { 36 mpu.resetFIFO(); 37 return; 38 } 39 if (mpuIntStatus & 0x02) { 40 while (fifoCount < packetSize) { 41 fifoCount = mpu.getFIFOCount(); 42 } 43 mpu.getFIFOBytes(fifoBuffer, packetSize); 44 fifoCount -= packetSize; 45 mpu.dmpGetQuaternion(&q, fifoBuffer); 46 mpu.dmpGetGravity(&gravity, &q); 47 mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); 48 steer(ypr[0] * 180/M_PI, ypr[1] * 180/M_PI, ypr[2] * 180/M_PI); 49 } 50}/* 51 * Receives angle from 0 to 180 where 0 is max left and 180 is max right 52 * Receives speed from -90 to 90 where -90 is max backwards and 90 is max forward 53 */ 54void moveZwheelsCar(byte angle, int speed) 55{ 56 if (millis() - lastMoveTime < 100) { 57 return; 58 } 59 byte resultAngle; 60 int resultSpeed; 61 if (angle >= 90) { 62 resultAngle = map(angle, 91, 180, 1, 60); 63 } else if (angle < 90) { 64 resultAngle = map(angle, 1, 90, 60, 120); 65 } 66 if (speed > 0) { 67 resultSpeed = map(speed, 0, 90, 0, 60); 68 } else if (speed < 0) { 69 resultSpeed = map(speed, 0, -90, 120, 60); 70 } 71 Serial.print("actualAngle=");Serial.print(angle);Serial.print("; "); 72 Serial.print("actualSpeed=");Serial.print(resultSpeed);Serial.println("; "); 73 BTserial.write(commandStering); 74 BTserial.write(resultAngle); 75 BTserial.write(commandSpeed); 76 BTserial.write((byte) resultSpeed); 77 lastMoveTime = millis(); 78}void steer(int x, int y, int z) 79{ 80 x = constrain(x, -1 * MAX_ANGLE, MAX_ANGLE); 81 y = constrain(y, -1 * MAX_ANGLE, MAX_ANGLE); 82 z = constrain(z, -MAX_ANGLE, MAX_ANGLE); 83 int angle = map(y, -MAX_ANGLE, MAX_ANGLE, 0, 180); 84 int speed = map(z, -MAX_ANGLE, MAX_ANGLE, 90, -90); 85 printDebug(x, y, z, angle, speed); 86 moveZwheelsCar(angle, speed); 87}void printDebug(int x, int y, int z, int angle, int speed) 88{ 89 if (millis() - lastPrintTime < 1000) { 90 return; 91 } 92 Serial.print("z=");Serial.print(x);Serial.print("; "); 93 Serial.print("y=");Serial.print(y);Serial.print("; "); 94 Serial.print("z=");Serial.print(z);Serial.print("; "); 95 Serial.print("angle=");Serial.print(angle);Serial.print("; "); 96 Serial.print("speed=");Serial.print(speed);Serial.println("; "); 97 lastPrintTime = millis(); 98}bool initializeGyroscope() 99{ 100 Wire.begin(); 101 mpu.initialize(); 102 Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed")); 103 devStatus = mpu.dmpInitialize(); 104 mpu.setXGyroOffset(220); 105 mpu.setYGyroOffset(76); 106 mpu.setZGyroOffset(-85); 107 mpu.setZAccelOffset(1788); 108 if (devStatus != 0) { 109 Serial.print(F("DMP Initialization failed (code "));Serial.println(devStatus); 110 return false; 111 } 112 mpu.setDMPEnabled(true); 113 Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)...")); 114 attachInterrupt(0, dmpDataReady, RISING); 115 mpuIntStatus = mpu.getIntStatus(); 116 Serial.println(F("DMP ready! Waiting for first interrupt...")); 117 packetSize = mpu.dmpGetFIFOPacketSize(); 118 119 return true; 120}void dmpDataReady() 121{ 122 mpuInterrupt = true; 123}boolean hasFifoOverflown(int mpuIntStatus, int fifoCount) 124{ 125 return mpuIntStatus & 0x10 || fifoCount == 1024; 126}
Github
https://github.com/pybluez/pybluez
Code snippet #6
text
1/** * Libraries: 2 * https://github.com/jrowberg/i2cdevlib 3 4 5 * https://github.com/jrowberg/i2cdevlib 6 7 */ 8#include "I2Cdev.h" 9#include 10 "MPU6050_6Axis_MotionApps20.h" 11#include "Wire.h" 12#include "SoftwareSerial.h"const 13 int MAX_ANGLE = 45; 14const byte commandStering = 129; 15const byte commandSpeed 16 = 130;bool initialization = false; // set true if DMP init was successful 17uint8_t 18 mpuIntStatus; // holds actual interrupt status byte from MPU 19uint8_t devStatus; 20 // return status after each device operation (0 = success, !0 = error) 21uint16_t 22 packetSize; // expected DMP packet size (default is 42 bytes) 23uint16_t fifoCount; 24 // count of all bytes currently in FIFO 25uint8_t fifoBuffer[64]; // FIFO 26 storage buffer 27Quaternion q; // [w, x, y, z] quaternion container 28VectorFloat 29 gravity; // [x, y, z] gravity vector 30float ypr[3]; // 31 [yaw, pitch, roll] yaw/pitch/roll container and gravity vector 32volatile bool 33 mpuInterrupt = false; // indicates whether MPU interrupt pin has gone highunsigned 34 long lastPrintTime, lastMoveTime = 0;SoftwareSerial BTserial(10, 11); 35MPU6050 36 mpu;void setup() 37{ 38 Serial.begin(9600); 39 BTserial.begin(38400); 40 41 Serial.println("Program started"); 42 initialization = initializeGyroscope(); 43}void 44 loop() { 45 if (!initialization) { 46 return; 47 } 48 mpuInterrupt 49 = false; 50 mpuIntStatus = mpu.getIntStatus(); 51 fifoCount = mpu.getFIFOCount(); 52 53 if (hasFifoOverflown(mpuIntStatus, fifoCount)) { 54 mpu.resetFIFO(); 55 56 return; 57 } 58 if (mpuIntStatus & 0x02) { 59 while (fifoCount 60 < packetSize) { 61 fifoCount = mpu.getFIFOCount(); 62 } 63 64 mpu.getFIFOBytes(fifoBuffer, packetSize); 65 fifoCount -= 66 packetSize; 67 mpu.dmpGetQuaternion(&q, fifoBuffer); 68 mpu.dmpGetGravity(&gravity, 69 &q); 70 mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); 71 steer(ypr[0] 72 * 180/M_PI, ypr[1] * 180/M_PI, ypr[2] * 180/M_PI); 73 } 74}/* 75 * Receives 76 angle from 0 to 180 where 0 is max left and 180 is max right 77 * Receives speed 78 from -90 to 90 where -90 is max backwards and 90 is max forward 79 */ 80void moveZwheelsCar(byte 81 angle, int speed) 82{ 83 if (millis() - lastMoveTime < 100) { 84 return; 85 86 } 87 byte resultAngle; 88 int resultSpeed; 89 if (angle >= 90) { 90 91 resultAngle = map(angle, 91, 180, 1, 60); 92 } else if (angle < 90) 93 { 94 resultAngle = map(angle, 1, 90, 60, 120); 95 } 96 if (speed > 97 0) { 98 resultSpeed = map(speed, 0, 90, 0, 60); 99 } else if (speed 100 < 0) { 101 resultSpeed = map(speed, 0, -90, 120, 60); 102 } 103 Serial.print("actualAngle=");Serial.print(angle);Serial.print("; 104 "); 105 Serial.print("actualSpeed=");Serial.print(resultSpeed);Serial.println("; 106 "); 107 BTserial.write(commandStering); 108 BTserial.write(resultAngle); 109 110 BTserial.write(commandSpeed); 111 BTserial.write((byte) resultSpeed); 112 113 lastMoveTime = millis(); 114}void steer(int x, int y, int z) 115{ 116 117 x = constrain(x, -1 * MAX_ANGLE, MAX_ANGLE); 118 y = constrain(y, -1 * MAX_ANGLE, 119 MAX_ANGLE); 120 z = constrain(z, -MAX_ANGLE, MAX_ANGLE); 121 int angle = map(y, 122 -MAX_ANGLE, MAX_ANGLE, 0, 180); 123 int speed = map(z, -MAX_ANGLE, MAX_ANGLE, 124 90, -90); 125 printDebug(x, y, z, angle, speed); 126 moveZwheelsCar(angle, 127 speed); 128}void printDebug(int x, int y, int z, int angle, int speed) 129{ 130 131 if (millis() - lastPrintTime < 1000) { 132 return; 133 } 134 Serial.print("z=");Serial.print(x);Serial.print("; 135 "); 136 Serial.print("y=");Serial.print(y);Serial.print("; "); 137 Serial.print("z=");Serial.print(z);Serial.print("; 138 "); 139 Serial.print("angle=");Serial.print(angle);Serial.print("; "); 140 141 Serial.print("speed=");Serial.print(speed);Serial.println("; "); 142 lastPrintTime 143 = millis(); 144}bool initializeGyroscope() 145{ 146 Wire.begin(); 147 mpu.initialize(); 148 149 Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : 150 F("MPU6050 connection failed")); 151 devStatus = mpu.dmpInitialize(); 152 mpu.setXGyroOffset(220); 153 154 mpu.setYGyroOffset(76); 155 mpu.setZGyroOffset(-85); 156 mpu.setZAccelOffset(1788); 157 158 if (devStatus != 0) { 159 Serial.print(F("DMP Initialization failed 160 (code "));Serial.println(devStatus); 161 return false; 162 } 163 mpu.setDMPEnabled(true); 164 165 Serial.println(F("Enabling interrupt detection (Arduino external interrupt 166 0)...")); 167 attachInterrupt(0, dmpDataReady, RISING); 168 mpuIntStatus 169 = mpu.getIntStatus(); 170 Serial.println(F("DMP ready! Waiting for first interrupt...")); 171 172 packetSize = mpu.dmpGetFIFOPacketSize(); 173 174 return true; 175}void 176 dmpDataReady() 177{ 178 mpuInterrupt = true; 179}boolean hasFifoOverflown(int 180 mpuIntStatus, int fifoCount) 181{ 182 return mpuIntStatus & 0x10 || fifoCount 183 == 1024; 184}
Github
https://github.com/danionescu0/arduino
Github
https://github.com/jrowberg/i2cdevlib
Github
https://github.com/pybluez/pybluez
Code snippet #4
text
1private final int value_convert_out(int value) { 2 boolean negative = false; 3 if (value < 0) { 4 negative = f6D; 5 } 6 int value2 = value & 63; 7 if (negative) { 8 return value2 | 64; 9 } 10 return value2; 11}
Downloadable files
Arduino repository
For this project navigate to: /projects/zen_wheels_remote
https://github.com/danionescu0/arduino
Fritznig skematic
Fritznig skematic
Arduino repository
For this project navigate to: /projects/zen_wheels_remote
https://github.com/danionescu0/arduino
Fritznig skematic
Fritznig skematic
Comments
Only logged in users can leave comments
danionescu
0 Followers
•0 Projects
Table of contents
Intro
9
0