Hi all,
At the moment I am using the HC-05 bluetooth shield to wirelessly transmit IMU data from the arduino to my computer.
The idea is to get the current serial string that is being sent from matlab (ex. 255,255,255,255) and split it into 4 integers that are sent to 4 different motors.
Basically, the communications are as follows
- IMU -> Ard -> BT Serial Transmit
- MATLAB -> BT Serial -> Ard
Currently, the issue that I am facing is that arduino will not transmit any data on its own WITHOUT opening the com port first. Once the COM port is open, I can see that data is being transmitted and the sketch works as intended. After the COM is closed the data transfer and motor control continues to work as intended.
The problem is that I have to have a direct wire serial connection between the Ard and my laptop in order to open the COM port. This isn't very desirable since I need to be using bluetooth connection.
I have been working with arduino for a while and have never seen anything like this. My code is below, please help ![]()
// Define libraries
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <OneWire.h>
#include "MPU9255.h"
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 6
#define TxD 7
// SDA = A4, SCL = A5
// IMU Acceleration: X(+front/-back) & Z(+left/-right)
// IMU Gyroscope: Y(+CW/-CCW Yaw)
SoftwareSerial blueToothSerial(RxD, TxD);
// Define 16bit integers
int16_t accelCount[3];
int16_t gyroCount[3];
int16_t magCount[3];
// Define floats;
float f_accelCount[3]; // Array of size 3 for X Y Z
float f_gyroCount[3];
float f_magCount[3];
// Define scales and accuracy
float AccelScale, GyroScale, MagScale;
int gAccuracy = 250; // degrees per second
int aAccuracy = 2; // +- 2g
int mAccuracy = 6; // 0.6mGauss
// Define Calibration Offsets
const float aOffSet_x = -0.02;
const float aOffSet_y = 1.0;
const float aOffSet_z = -0.1;
const int gOffSet;
// Define packet count
int packet = 0;
// Define time;
unsigned long timer = millis();
// Define Motor Pins
int enA = 5;
int enB = 9;
int enC = 10;
int enD = 11;
// Initialize Incoming Serial Commands
int FL, FR, RL, RR;
String path;
MPU9255 mpu(12, gAccuracy, aAccuracy, mAccuracy); // Create MPU9255 Sensor object
void setup() {
// Minor environment initialization
Wire.begin();//initialization of the I2C protocol
TWBR = 24; // Two Wire Bit Rate Register
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(enC, OUTPUT);
pinMode(enD, OUTPUT);
// Setup BT Connection
setupBlueToothConnection();
mpu.initMPU9250();// // Initalization of MPU Sensor
float magCalibration[3];
mpu.initAK8963(magCalibration);// Initialize magnometer
GyroScale = 131.0;
AccelScale = 16384.0;
MagScale = 0.6;
}
void loop() {
int blueToothLength;
int incomingSerialDataIndex = 0; // Stores the next 'empty space' in the array
char incomingSerialData[15];
String blueToothData;
while(blueToothSerial.available()) {
incomingSerialData[incomingSerialDataIndex] = blueToothSerial.read();
incomingSerialDataIndex++; // Ensure the next byte is added in the next position
incomingSerialData[incomingSerialDataIndex] = '\0';
}
if (Serial.available()){ //check if there's any data sent from the local serial terminal, you can add the other applications here
}
blueToothData = String(incomingSerialData); // Cast Char to Str
//Serial.println(blueToothData);
blueToothLength = blueToothData.length(); // Remove Null
if (blueToothLength == 1) {
if (incomingSerialData[0] == 'S') {
blueToothData = '0';
//Serial.println("Stopped");
stopMotors();
}
}
getData();
transmitData();
FL = splitSerial(blueToothData, ',', 0).toInt();
FR = splitSerial(blueToothData, ',', 1).toInt();
RL = splitSerial(blueToothData, ',', 2).toInt();
RR = splitSerial(blueToothData, ',', 3).toInt();
// Enable the according pins
MotorControl(FL, FR, RL, RR);
delay(100);
}