Reading Data from LSM9DS0 gyroscope from HC-06 Bluetooth Module and Arduino MEGA 2560) to MATLAB wirelessly

2 visualizaciones (últimos 30 días)
Hi, I'm trying to read data wiressely from an LSM9DS0 3-axis gyroscope using Arduino MEGA 2560 to MATLAB using the HC-06 bluetooth module.
This is the error I'm getting: Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
I'm using one computer that is connected to the Arduino MEGA 2560 with the Bluetooth HC-06 and LSM9DS0 wired to it.Then the other computer has MATLAB running code to read the gyroscope data wirelessly.
This is my MATLAB code:
%%Interfacting w/ HC-06 Bluetooth Module
% Pair with your bluetooth device before continuing
% Lists available Bluetooth devices instrhwinfo('Bluetooth');
% Create a bluetooth variable and open it at channel 1 device = Bluetooth('HC-06', 1);
% Pair with device fopen(device)
%% Write command to HC-06/Arduino fwrite(device, 'c'); % Prompts lists to do more commands read = fscanf(device, '%d') fwrite(device, 'h'); % Makes LED low read = fscanf(device, '%d') fwrite(device, 'a'); % Read from accl
%% Reading and Analyzing Values from HC-06 data = [];
for count = 1:10 read = fscanf(device, '%d') data = [data; read] end
final = str2double(data);
% Matrix manipulation here
%% Clear Bluetooth Object when Finished fclose(device); clear('device')
I'm only trying to read one angle value from the gyroscope, which in my Arduino I print out as: Serial.println( int(anglez)*(-1) % (360) );
This is my Arduino Code:
// ULTRA SONIC VARS
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_LSM9DS0.h> // Needed for accel/mag/gyro
#include <Adafruit_Sensor.h> // Needed to convert values to usable units i.e. m/s^2, Gs, deg/s
//double anglex = 0; //double angley = 0; double anglez = 0; double dt = 0; double prev_time = 0; int i = 0; double biasz = 0;
//double gyro.gyro.x = 0; //double gyro.gyro.y = 0; //double biasx = 0; //double biasy = 0;
// Define pins for ultrasonic sensor #define trigPin1 13 #define echoPin1 12 #define trigPin2 9 #define echoPin2 8
#define led 13 #define led2 10
/* * Connections for HC-06 (Bluetooth Module) * HC-06 Arduino * Vcc -> 5v * Gnd -> Gnd * Rx -> Tx1 * Tx -> Rx1 * * If you use Tx2/Rx2 on the Mega, ALL the Serial1 needs to be changed to Serial2. Same for Tx3/Rx3. * They are separate communications so it requires a different serial monitor instead of the default * Serial. * * After uploading sketch, connect bluetooth to device (password is either 0000 or 1234). * NOTE: If you've connected to the bluetooth module on your device before, REMOVE it from device COMPLETELY, * i.e. delete/forget HC-06, and connect again. Don't ask why, just do it. * You need to remove HC-06 from device EVERY TIME the bluetooth module is turned back on. * Using Putty or MobaXTerm or TeraTerm, click the 'Serial' option and type in the COM name for the * bluetooth connection, check device manager for the COM port to make sure. */
// Create Adafruit_LSM9DS0 object Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(1000); // Use I2C, ID #1000 int acceReadCounter = 1;
/* FOR SPI MODE #define LSM9DS0_XM_CS 10 #define LSM9DS0_GYRO_CS 9 //Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0(LSM9DS0_XM_CS, LSM9DS0_GYRO_CS, 1000);
// Or, use Software SPI: // G_SDO + XM_SDO -> tied together to the MISO pin! // then select any pins for the SPI lines, and the two CS pins above
#define LSM9DS0_SCLK 13 #define LSM9DS0_MISO 12 #define LSM9DS0_MOSI 11 */
// Byte (one character) read in from Virtual Serial1 COMM (Bluetooth) byte inByte = 0;
// Acknowledgment variable (while-loop sentinel) byte ack = 0; /* void displaySensorDetails(void) { sensor_t accel, mag, gyro, temp;
lsm.getSensor(&accel, &mag, &gyro, &temp);
delay(500);
}
*/
void configureSensor(void)
{
// 1.) Set the accelerometer range
lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_4G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_6G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_8G);
//lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_16G);
// 2.) Set the magnetometer sensitivity
lsm.setupMag(lsm.LSM9DS0_MAGGAIN_2GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_4GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_8GAUSS);
//lsm.setupMag(lsm.LSM9DS0_MAGGAIN_12GAUSS);
// 3.) Setup the gyroscope
lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_245DPS);
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_500DPS);
//lsm.setupGyro(lsm.LSM9DS0_GYROSCALE_2000DPS);
}
// Setup void setup() { // Open up Bluetooth Virtual Serial1 COMM port Serial1.begin(9600);
// SETUP ULTRA SONIC
#ifndef ESP8266 while (!Serial1); // will pause Zero, Leonardo, etc until Serial1 console opens #endif
// Serial1.println(F("LSM9DS0 9DOF Sensor Test")); Serial1.println(""); //Serial1.println("Starting lsm sensor..."); /* Initialise the sensor / if(!lsm.begin()) { / There was a problem detecting the LSM9DS0 ... check your connections */ // Serial1.print(F("Ooops, no LSM9DS0 detected ... Check your wiring or I2C ADDR!")); while(1); } //Serial1.println(F("Found LSM9DS0 9DOF"));
/* Display some basic information on this sensor */
// displaySensorDetails();
/* Setup the sensor gain and integration time */
configureSensor();
/* We're ready to go! */
// Serial1.println("");
// Configure digital for OUTPUT
pinMode(led,OUTPUT);
}
// Main void loop() { /*
while (!ack) {
ack = getAck();
delay(3000);
}
byte Serial1Cmd = getSerial1Cmd();
doSerial1Cmd(Serial1Cmd);
doSerial1Cmd(getSerial1Cmd());
*/
while(1) {
acceRead();
}
// Performs Serial1 command based on user input
} /*
void doSerial1Cmd( byte cmd ) { switch( cmd ) { // Turn LED HIGH case ('h'): ledON(); printNewCmdLn(); break;
// Turn LED LOW
case ('l'):
ledOFF();
printNewCmdLn();
break;
// Get Ultra Sonic Reading
case ('g'):
acceReadCounter++;
Serial1.print("AcceRead counter: "); Serial1.println(acceReadCounter);
printNewCmdLn();
break;
// Display CMD List
case ('c'):
printCmdList();
printNewCmdLn();
break;
// Display accel readings
case ('a'):
acceRead();
printNewCmdLn();
break;
}
}
*/
/*
// Prompts User for input Serial1 command // Returns Serial1 command
byte getSerial1Cmd() { byte inByte; if (Serial1.available()) { inByte = Serial1.read(); Serial1.write(inByte); printNewLn(); return inByte; } }
*
*/
// Turns LED ON and writes to Serial1 void ledON() { digitalWrite(led, HIGH); Serial1.write(" The LED is ON!"); }
// Turns LED OFF and writes to Serial1 void ledOFF() { digitalWrite(led, LOW); Serial1.write(" The LED is OFF!"); }
void acceRead() { // if (acceReadCounter%2) // { while(1) { /* Get a new sensor event */ sensors_event_t accel, mag, gyro, temp;
lsm.getEvent(&accel, &mag, &gyro, &temp);
//Serial.println(millis());
//Serial.print("\t\t\t\t\t\t\t\t");
// print out accelleration data
//Serial.print(""); Serial.print(accel.acceleration.x); Serial.print(" ");
//Serial.print(""); Serial.print(accel.acceleration.y); Serial.print(" ");
//Serial.print(" \tZ: "); Serial.print(accel.acceleration.z); Serial.println(" \tm/s^2");
// print out magnetometer data
//Serial.print("Magn. X: "); Serial.print(mag.magnetic.x); Serial.print(" ");
//Serial.print(" \tY: "); Serial.print(mag.magnetic.y); Serial.print(" ");
//Serial.print(" \tZ: "); Serial.print(mag.magnetic.z); Serial.println(" \tgauss");
// print out gyroscopic data
//Serial.print(""); Serial.print(gyro.gyro.x); Serial.print("\t\t");
//Serial.print(""); Serial.print(gyro.gyro.y); Serial.print("\t\t\t");
//Serial.print(" \tZ: ");
//Serial.print(gyro.gyro.z);
//Serial.println(" \tdps");
//Serial.println("");
dt = (millis() - prev_time)/(1000.0); anglez = (anglez) + (gyro.gyro.z-biasz)*(dt); //anglex = (anglex) + (gyro.gyro.x-biasx)*(dt); //angley = (angley) + (gyro.gyro.y-biasy)*(dt); prev_time = millis();
//Serial.println("Angle Z"); Serial.println( int(anglez)*(-1) % (360) ); //Serial.print(anglex); //Serial.print("\t\t\t"); //Serial.println(angley);
// print out temperature data
//Serial.print("Temp: "); Serial.print(temp.temperature); Serial.println(" *C");
//Serial.println("**********************\n");
}
}
/*
// Prints the command list void printCmdList() { Serial1.write(" Commands:\r\n"); Serial1.write(" h Turn ON LED\r\n"); Serial1.write(" l Turn OFF LED\r\n"); Serial1.write(" g Ultra Sonic Reading\r\n"); Serial1.write(" c Command List\r\n"); Serial1.write(" a Read from accelerometer"); }
*/
/*
// Prints a new command line cursor void printNewCmdLn() { printNewLn(); Serial1.write("> "); }
// Prints a modified new line void printNewLn() { Serial1.write("\r\n"); }
*/
/*
byte getAck() { Serial1.write("\r\nPress c for command list\r\n"); Serial1.write(">"); // having this if statement messes up the next loop. fails the conditional statement //if (Serial1.available()){ byte inByte = Serial1.read(); //Serial1.println("readbyte"); // }
if ( inByte == 'c' ) {
Serial1.println(" inbyte == C");
Serial1.write(inByte);
ack = 1;
printNewLn();
printCmdList();
printNewCmdLn();
return 1;
}
return 0;
}
*/
We are using a LED to make sure it is working. The LED works fine while we are running our code. Our Bluetooth module is also perfectly synced.
At first, the issue was the different data types (command list is of string data type while the angle output is in double). So I commented everything that has a string data type and only left out the angle I want to read in MATLAB from the Arduino code.
I know it's not a latency issue because when I had the command list it was working pretty fast. I was able to collect data. however, getting rid of the command list, which I don't need, causes the warning.
My goal is to only read that angle output from the Arduino code. Nothing actually shows up in the serial monitor on the first computer when I run the Arduino code. I'm assuming that's because the Arduino never exits the setup mode.

Respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by