Created
June 19, 2023 04:14
-
-
Save dj1711572002/38541b4be5b73175e0e25fcd82a47570 to your computer and use it in GitHub Desktop.
BNO055 ESp32Devmodule BlueTooth SPP send
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BNO055.h> | |
#include <utility/imumaths.h> | |
#include "BluetoothSerial.h" | |
/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor), | |
which provides a common 'type' for sensor data and some helper functions. | |
To use this driver you will also need to download the Adafruit_Sensor | |
library and include it in your libraries folder. | |
You should also assign a unique ID to this sensor for use with | |
the Adafruit Sensor API so that you can identify this particular | |
sensor in any data logs, etc. To assign a unique ID, simply | |
provide an appropriate value in the constructor below (12345 | |
is used by default in this example). | |
Connections | |
=========== | |
Connect SCL to analog 5 | |
Connect SDA to analog 4 | |
Connect VDD to 3.3-5V DC | |
Connect GROUND to common ground | |
History | |
======= | |
2015/MAR/03 - First release (KTOWN) | |
*/ | |
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) | |
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it | |
#endif | |
#if !defined(CONFIG_BT_SPP_ENABLED) | |
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip. | |
#endif | |
BluetoothSerial SerialBT; | |
uint64_t chipid; | |
char chipname[256]; | |
/* Set the delay between fresh samples */ | |
uint16_t BNO055_SAMPLERATE_DELAY_MS = 50; | |
// Check I2C device address and correct line below (by default address is 0x29 or 0x28) | |
// id, address | |
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28, &Wire); | |
void setup(void) | |
{ | |
Serial.begin(115200); | |
pinMode(21, INPUT_PULLUP); //SDA 21番ピンのプルアップ(念のため) | |
pinMode(22, INPUT_PULLUP); //SDA 22番ピンのプルアップ(念のため) | |
while (!Serial) delay(10); // wait for serial port to open! | |
Serial.println("Orientation Sensor Test"); Serial.println(""); | |
/* Initialise the sensor */ | |
if (!bno.begin()) | |
{ | |
/* There was a problem detecting the BNO055 ... check your connections */ | |
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!"); | |
while (1); | |
} | |
delay(1000); | |
chipid = ESP.getEfuseMac(); | |
sprintf( chipname, "ESP32D_%04X", (uint16_t)(chipid >> 32)); | |
SerialBT.begin(chipname); | |
Serial.print("BT chipname:"); | |
Serial.println(chipname); | |
} | |
void loop(void) | |
{ | |
/* | |
//could add VECTOR_ACCELEROMETER, VECTOR_MAGNETOMETER,VECTOR_GRAVITY... | |
sensors_event_t orientationData , angVelocityData , linearAccelData, magnetometerData, accelerometerData, gravityData; | |
bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER); | |
bno.getEvent(&angVelocityData, Adafruit_BNO055::VECTOR_GYROSCOPE); | |
bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL); | |
bno.getEvent(&magnetometerData, Adafruit_BNO055::VECTOR_MAGNETOMETER); | |
bno.getEvent(&accelerometerData, Adafruit_BNO055::VECTOR_ACCELEROMETER); | |
bno.getEvent(&gravityData, Adafruit_BNO055::VECTOR_GRAVITY); | |
printEvent(&orientationData); | |
printEvent(&angVelocityData); | |
printEvent(&linearAccelData); | |
printEvent(&magnetometerData); | |
printEvent(&accelerometerData); | |
printEvent(&gravityData); | |
int8_t boardTemp = bno.getTemp(); | |
Serial.println(); | |
Serial.print(F("temperature: ")); | |
Serial.println(boardTemp); | |
uint8_t system, gyro, accel, mag = 0; | |
bno.getCalibration(&system, &gyro, &accel, &mag); | |
Serial.println(); | |
Serial.print("Calibration: Sys="); | |
Serial.print(system); | |
Serial.print(" Gyro="); | |
Serial.print(gyro); | |
Serial.print(" Accel="); | |
Serial.print(accel); | |
Serial.print(" Mag="); | |
Serial.println(mag); | |
Serial.println("--"); | |
*/ | |
bnoread(0); | |
delay(BNO055_SAMPLERATE_DELAY_MS); | |
} | |
void printEvent(sensors_event_t* event) { | |
double x = -1000000, y = -1000000 , z = -1000000; //dumb values, easy to spot problem | |
if (event->type == SENSOR_TYPE_ACCELEROMETER) { | |
Serial.print("Accl:"); | |
x = event->acceleration.x; | |
y = event->acceleration.y; | |
z = event->acceleration.z; | |
} | |
else if (event->type == SENSOR_TYPE_ORIENTATION) { | |
Serial.print("Orient:"); | |
x = event->orientation.x; | |
y = event->orientation.y; | |
z = event->orientation.z; | |
} | |
else if (event->type == SENSOR_TYPE_MAGNETIC_FIELD) { | |
Serial.print("Mag:"); | |
x = event->magnetic.x; | |
y = event->magnetic.y; | |
z = event->magnetic.z; | |
} | |
else if (event->type == SENSOR_TYPE_GYROSCOPE) { | |
Serial.print("Gyro:"); | |
x = event->gyro.x; | |
y = event->gyro.y; | |
z = event->gyro.z; | |
} | |
else if (event->type == SENSOR_TYPE_ROTATION_VECTOR) { | |
Serial.print("Rot:"); | |
x = event->gyro.x; | |
y = event->gyro.y; | |
z = event->gyro.z; | |
} | |
else if (event->type == SENSOR_TYPE_LINEAR_ACCELERATION) { | |
Serial.print("Linear:"); | |
x = event->acceleration.x; | |
y = event->acceleration.y; | |
z = event->acceleration.z; | |
} | |
else if (event->type == SENSOR_TYPE_GRAVITY) { | |
Serial.print("Gravity:"); | |
x = event->acceleration.x; | |
y = event->acceleration.y; | |
z = event->acceleration.z; | |
} | |
else { | |
Serial.print("Unk:"); | |
} | |
Serial.print("\tx= "); | |
Serial.print(x); | |
Serial.print(" |\ty= "); | |
Serial.print(y); | |
Serial.print(" |\tz= "); | |
Serial.println(z); | |
} | |
void bnoread(int no){ | |
//delay(20);//15+5=20msec半周期ずらす | |
sensors_event_t event; | |
sensors_event_t orientationData , angVelocityData , linearAccelData, magnetometerData, accelerometerData, gravityData; | |
bno.getEvent(&event); | |
/* | |
//Euler angle | |
eX[no]=360-(float)event.orientation.x; | |
eY[no]=(float)event.orientation.y; | |
eZ[no]=(float)event.orientation.z; | |
*/ | |
//Quartenion Copy from https://nobita-rx7.hatenablog.com/entry/27642606 | |
imu::Quaternion quat = bno.getQuat(); | |
float qW=(float)quat.w(); | |
float qX=(float)quat.x(); | |
float qY=(float)quat.y(); | |
float qZ=(float)quat.z(); | |
//time pulse millis | |
//Calc Euler from Quaternion | |
double w=qW; | |
double x=qX; | |
double y=qY; | |
double z=qZ; | |
double ysqr = y * y; | |
// roll (x-axis rotation) | |
double t0 = +2.0 * (w * x + y * z); | |
double t1 = +1.0 - 2.0 * (x * x + ysqr); | |
double roll = atan2(t0, t1); | |
// pitch (y-axis rotation) | |
double t2 = +2.0 * (w * y - z * x); | |
t2 = t2 > 1.0 ? 1.0 : t2; | |
t2 = t2 < -1.0 ? -1.0 : t2; | |
double pitch = asin(t2); | |
// yaw (z-axis rotation) | |
double t3 = +2.0 * (w * z + x * y); | |
double t4 = +1.0 - 2.0 * (ysqr + z * z); | |
//rad->deg | |
double yaw = atan2(t3, t4); | |
roll *= 57.2957795131; | |
pitch *= 57.2957795131; | |
yaw *= 57.2957795131; | |
if(yaw<0){//yaw 0-> -180deg= | |
yaw=360+yaw;; | |
} | |
yaw=360-yaw; | |
//------------------------------ | |
//btime[no]=millis(); | |
double eX=yaw; | |
double eY=pitch; | |
double eZ=roll; | |
//--------------get Linear Acc---------------------- | |
// bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL); | |
// printEvent(&linearAccelData,no); | |
//------------get Gravity------------------------ | |
// bno.getEvent(&gravityData, Adafruit_BNO055::VECTOR_GRAVITY); | |
// printEvent(&gravityData,no); | |
//Serial2.printf("bnoread:%d:%4.3f,%4.3f,%4.3f,%4.3f,%4.3f,%4.3f,%4.3f,%d,%d,%4.3f,%4.3f,%4.3f\n\r",no,eX[no],eY[no],eZ[no],qW[no],qX[no],qY[no],qZ[no],millis()-tp0,tst[no],yaw,pitch,roll); | |
SerialBT.printf("eX=,%4.2f,eY=%4.2f,eZ=%4.2f,t=,%d\n\r",eX,eY,eZ,millis()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment