Anmelden

Archiv verlassen und diese Seite im Standarddesign anzeigen : MPU6050 und bluetoothverbindung über HC05



inka
23.11.2017, 12:04
hallo allerseits,

ich mache jetzt für diesen roboter
33064
die ersten notwendigen schritte mit der MPU6050. Verwende dabei diesen

// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
// 2013-05-08 - added multiple output formats
// - added seamless Fastwire support
// 2011-10-07 - initial release


/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include <Arduino.h>




// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"


// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif


// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high


int16_t ax, ay, az;
int16_t gx, gy, gz;






// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
// not so easy to parse, and slow(er) over UART.
#define OUTPUT_READABLE_ACCELGYRO


// uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit
// binary, one right after the other. This is very fast (as fast as possible
// without compression or data loss), and easy to parse, but impossible to read
// for a human.
//#define OUTPUT_BINARY_ACCELGYRO




#define LED_PIN 13
bool blinkState = false;


void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif


// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(115200);
Serial1.begin(115200);


// initialize device
Serial.println("Initializing I2C devices...");
//Serial1.println("Initializing I2C devices...");
accelgyro.initialize();


// verify connection
Serial.println("Testing device connections...");
//Serial1.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");


// use the code below to change accel/gyro offset values
/*
Serial.println("Updating internal sensor offsets...");
// -76 -2359 1688 0 0 0
Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
Serial.print("\n");
accelgyro.setXGyroOffset(220);
accelgyro.setYGyroOffset(76);
accelgyro.setZGyroOffset(-85);
Serial.print(accelgyro.getXAccelOffset()); Serial.print("\t"); // -76
Serial.print(accelgyro.getYAccelOffset()); Serial.print("\t"); // -2359
Serial.print(accelgyro.getZAccelOffset()); Serial.print("\t"); // 1688
Serial.print(accelgyro.getXGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getYGyroOffset()); Serial.print("\t"); // 0
Serial.print(accelgyro.getZGyroOffset()); Serial.print("\t"); // 0
Serial.print("\n");
*/


// configure Arduino LED pin for output
pinMode(LED_PIN, OUTPUT);
}


void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);


// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);


#ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y/z values
Serial.print("a/g:\t");
Serial.print(ax); Serial.print("\t");
Serial.print(ay); Serial.print("\t");
Serial.print(az); Serial.print("\t");
Serial.print(gx); Serial.print("\t");
Serial.print(gy); Serial.print("\t");
Serial.println(gz);
/*
Serial1.print("a/g:\t");
Serial1.print(ax); Serial1.print("\t");
Serial1.print(ay); Serial1.print("\t");
Serial1.print(az); Serial1.print("\t");
Serial1.print(gx); Serial1.print("\t");
Serial1.print(gy); Serial1.print("\t");
Serial1.println(gz);
*/
#endif


#ifdef OUTPUT_BINARY_ACCELGYRO
Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
/*
Serial1.write((uint8_t)(ax >> 8)); Serial1.write((uint8_t)(ax & 0xFF));
Serial1.write((uint8_t)(ay >> 8)); Serial1.write((uint8_t)(ay & 0xFF));
Serial1.write((uint8_t)(az >> 8)); Serial1.write((uint8_t)(az & 0xFF));
Serial1.write((uint8_t)(gx >> 8)); Serial1.write((uint8_t)(gx & 0xFF));
Serial1.write((uint8_t)(gy >> 8)); Serial1.write((uint8_t)(gy & 0xFF));
Serial1.write((uint8_t)(gz >> 8)); Serial1.write((uint8_t)(gz & 0xFF));
*/
#endif


// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}

der auch in der dargestellten form funktioniert und die RAW-werte der MPU über die Serialprint funktion auf dem monitor ausgibt....

das hier, also der start der Serial und Serial1
-------------------------------------
Serial.begin(115200);
Serial1.begin(115200);
------------------------
macht noch keine probleme. Wenn ich aber in diesen zeilen:
-----------------------------------------
// initialize device
Serial.println("Initializing I2C devices...");
//Serial1.println("Initializing I2C devices...");
accelgyro.initialize();
------------------------------------------
die zwei slashes vor Serial1.Println entferne (der erster versuch über serial1 zu drucken), geht plötzlich nichts mehr....

Ich habe keinen blassen schimmer warum das so ist. Hat jemand von Euch evtl. eine erklärung dafür?

HaWe
23.11.2017, 12:27
warum übergibst du Textmeldungen zusätzlich an Serial1? Welches Device oder welche Konsole soll denn an Serial1 deinen String entgegen nehmen? Wozu soll Serial1 überhaupt benutzt werden?

inka
23.11.2017, 13:07
ich übergebe die textmeldungen an Serial und Serial1. testweise. Wenn der roboter unterwegs ist, habe ich ja kein lade/datenkabel am arduino, aber bluetooth der an RX1/TX1 hängt, am PC auch ein BT modul welches die textmeldungen am seriellem monitor ausgibt...

HaWe
23.11.2017, 13:32
aha!

Zur Fehler-Eingrenzung:
lass doch mal den ganzen mpu Teil weg und teste, ob nur der Serial + Serial1.print Teil funktioniert, am besten in einer Zählschleife, in denen ein Loop-Count hochgezählt und ausgegeben wird.

(Mir fehlt zZt ein Erklärungsmodell, wie oder warum Serial1 und Wire für den mpu sich gegenseitig stören können)

inka
23.11.2017, 15:52
könnte das hier (https://github.com/MCUdude/MiniCore/issues/13) etwas damit zu tun haben?

HaWe
23.11.2017, 17:02
könnte das hier (https://github.com/MCUdude/MiniCore/issues/13) etwas damit zu tun haben?

weß ich nicht, ich kenne Serial1 auch nur von AVR Mega2560 oder ARM Due, daher dachte ich du hast einen von diesen beiden

inka
23.11.2017, 17:05
weß ich nicht, ich kenne Serial1 auch nur von AVR Mega2560 oder ARM Due, daher dachte ich du hast einen von diesen beiden
ich benutze den atmega 2560

HaWe
23.11.2017, 17:12
dein Link meint ja doch ATmega8 .
Ich hatte jedenfalls nie Probleme auf dem Arduino Mega mit i2c und vielen Serials gleichzeitig.
Was sagt der test mit Serial+Serial1 ohne mpu?

inka
23.11.2017, 17:32
also ohne mpu verwende ich die kombination sehr oft, hier z.b. werden an der gleichen hardware die radencoder ausgelesen und ausgegeben. Funktioniert mit serial, wie auch mit serial1. Gerade noch einmal getestet...

#include "Arduino.h"
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#define encoder_A 2
#define encoder_B 3

uint8_t i, speed, s_speed, m_speed, b_speed;

int wert_encoder_[] = {1, 2};
int mehrfach_enc_zaehler_[] = {1, 2};
int summe_encoder_[] = {1, 2};
int vorwaerts_[] = {1, 2};

unsigned long dauer_servo, dauer_fahrt, currentMillis, previousMillis;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *motor_A = AFMS.getMotor(3);//
Adafruit_DCMotor *motor_B = AFMS.getMotor(4);//
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
Serial.begin(115200);
Serial1.begin(115200);

lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.setBacklight(HIGH);
lcd.print("alle_mot_enc_1");
lcd.setCursor(0, 1);
lcd.print("ausl_li_re");
delay(2000);
lcd.clear();

pinMode(encoder_A, INPUT_PULLUP);
pinMode(encoder_B, INPUT_PULLUP);

s_speed = 100;
m_speed = 150;
b_speed = 200;
speed = 150;

for (i = 1; i < 2; i++)
{
vorwaerts_[i] = 1;
wert_encoder_[i] = 0;
mehrfach_enc_zaehler_[i] = 0;
}

AFMS.begin();
}

void loop()
{
if (vorwaerts_[1] == 1) rueckwaerts();
else vorwaerts();
}

void motor_stop(void)
{
motor_A->setSpeed(0);
motor_B->setSpeed(0);
motor_A->run(BRAKE);
motor_B->run(BRAKE);
motor_A->run(RELEASE);
motor_B->run(RELEASE);
delay(250);
summe_encoder_[1] = 0;
}


void rueckwaerts(void)
{
motor_A->setSpeed(speed);
motor_B->setSpeed(speed);
motor_A->run(BACKWARD);
motor_B->run(BACKWARD);
encoder_auslesen_A();

if (summe_encoder_[1] >= 100)

{
summe_encoder_[1] = 0;
vorwaerts_[1] = 0;
motor_stop();
}
}


void vorwaerts(void)
{
motor_A->setSpeed(speed);
motor_B->setSpeed(speed);
motor_A->run(FORWARD);
motor_B->run(FORWARD);
encoder_auslesen_A();

if (summe_encoder_[1] <= -100) //>= 100)
{
summe_encoder_[1] = 0;
vorwaerts_[1] = 1;
motor_stop();
}
}


void encoder_auslesen_A(void)
{
wert_encoder_[1] = digitalRead(encoder_A);
if (vorwaerts_[1] == 1)
{
if (wert_encoder_[1] == 0 && mehrfach_enc_zaehler_[1] == 0)
{
mehrfach_enc_zaehler_[1] = 1;
}
else if (wert_encoder_[1] == 1 && mehrfach_enc_zaehler_[1] == 1)
{
mehrfach_enc_zaehler_[1] = 0;
summe_encoder_[1] = (summe_encoder_[1] + wert_encoder_[1]);
Serial.print(wert_encoder_[1]);
Serial.print(" ");
Serial.println(summe_encoder_[1]);
Serial1.print(wert_encoder_[1]);
Serial1.print(" ");
Serial1.println(summe_encoder_[1]);
}
}
else
{
if (wert_encoder_[1] == 0 && mehrfach_enc_zaehler_[1] == 0)
{
mehrfach_enc_zaehler_[1] = 1;
}
else if (wert_encoder_[1] == 1 && mehrfach_enc_zaehler_[1] == 1)
{
mehrfach_enc_zaehler_[1] = 0;
summe_encoder_[1] = (summe_encoder_[1] - wert_encoder_[1]); //+
Serial.print(wert_encoder_[1]);
Serial.print(" ");
Serial.println(summe_encoder_[1]);
Serial1.print(wert_encoder_[1]);
Serial1.print(" ");
Serial1.println(summe_encoder_[1]);
}
}
}

HaWe
23.11.2017, 17:34
ok, dann muss ich passen, ich kenne auch deine mpu libs nicht.