Skip to content

Commit 9204c34

Browse files
committed
switch over to EasyBNO055
1 parent 8ca7af0 commit 9204c34

1 file changed

Lines changed: 70 additions & 68 deletions

File tree

ArduinoClassRobot.ino

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,95 @@
11
/*
2-
Blink
2+
Blink
33
4-
Turns an LED on for one second, then off for one second, repeatedly.
4+
Turns an LED on for one second, then off for one second, repeatedly.
55
6-
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
7-
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
8-
the correct LED pin independent of which board is used.
9-
If you want to know what pin the on-board LED is connected to on your Arduino
10-
model, check the Technical Specs of your board at:
11-
https://www.arduino.cc/en/Main/Products
6+
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
7+
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
8+
the correct LED pin independent of which board is used.
9+
If you want to know what pin the on-board LED is connected to on your Arduino
10+
model, check the Technical Specs of your board at:
11+
https://www.arduino.cc/en/Main/Products
1212
13-
modified 8 May 2014
14-
by Scott Fitzgerald
15-
modified 2 Sep 2016
16-
by Arturo Guadalupi
17-
modified 8 Sep 2016
18-
by Colby Newman
13+
modified 8 May 2014
14+
by Scott Fitzgerald
15+
modified 2 Sep 2016
16+
by Arturo Guadalupi
17+
modified 8 Sep 2016
18+
by Colby Newman
1919
20-
This example code is in the public domain.
20+
This example code is in the public domain.
2121
22-
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
23-
*/
22+
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
23+
*/
2424
#include <ESP32Servo.h>
2525
#include <WiiChuck.h>
2626
#include <Wire.h>
27-
#include <Adafruit_Sensor.h>
28-
#include <Adafruit_BNO055.h>
29-
#include <utility/imumaths.h>
27+
#include <EasyBNO055_ESP.h>
3028

3129
Servo left;
3230
Servo right;
3331
Accessory nunchuck;
34-
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28, &Wire);
32+
EasyBNO055_ESP bno;
33+
void otherI2CUpdate() {
34+
nunchuck.readData(); // Read inputs and update maps
35+
}
3536

36-
bool bnoStarted = false;
37-
float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
38-
const float run = in_max - in_min;
39-
if(run == 0){
40-
log_e("map(): Invalid input range, min == max");
41-
return -1; // AVR returns -1, SAM returns 0
42-
}
43-
const float rise = out_max - out_min;
44-
const float delta = x - in_min;
45-
return (delta * rise) / run + out_min;
37+
float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
38+
const float run = in_max - in_min;
39+
if (run == 0) {
40+
log_e("map(): Invalid input range, min == max");
41+
return -1; // AVR returns -1, SAM returns 0
42+
}
43+
const float rise = out_max - out_min;
44+
const float delta = x - in_min;
45+
return (delta * rise) / run + out_min;
4646
}
47+
int lCenter = 86;
48+
int rCenter = 87;
4749
// the setup function runs once when you press reset or power the board
4850
void setup() {
49-
left.attach(33,1000,2000);
50-
right.attach(32,1000,2000);
51-
left.write(90);
52-
right.write(90);
53-
Serial.begin(115200);
54-
nunchuck.begin();
55-
bnoStarted=bno.begin();
56-
delay(100);
51+
52+
Serial.begin(115200);
53+
Serial.println("Starting ESP32");
54+
left.attach(33, 1000, 2000);
55+
right.attach(32, 1000, 2000);
56+
left.write(lCenter);
57+
right.write(rCenter);
58+
nunchuck.begin();
59+
bno.start(&otherI2CUpdate);
60+
5761
}
5862

5963
// the loop function runs over and over again forever
6064
void loop() {
61-
if(bnoStarted){
62-
sensors_event_t orientationData;
63-
bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER);
64-
Serial.print("Orient:");
65-
double x = orientationData.orientation.x;
66-
double y = orientationData.orientation.y;
67-
double z = orientationData.orientation.z;
68-
if(abs(x)<0.001 && abs(y) < 0.001 && abs(z)<0.0001){
69-
Serial.println("IMU Died, reset");
70-
bnoStarted=false;
71-
}else{
72-
Serial.print("\tx= ");
73-
Serial.print(x);
74-
Serial.print(" |\ty= ");
75-
Serial.print(y);
76-
Serial.print(" |\tz= ");
77-
Serial.println(z);
78-
}
79-
}else{
80-
bnoStarted=bno.begin();
81-
}
82-
nunchuck.readData(); // Read inputs and update maps
8365

84-
float x= -fmap(nunchuck.values[1],0,255,-1.0,1.0);
85-
float y= -fmap(nunchuck.values[0],0,255,-1.0,1.0);
86-
int lval = 90*x -90*y +90;
87-
int rval = -90*x -90*y +92;
66+
float x = -fmap(nunchuck.values[1], 0, 255, -1.0, 1.0);
67+
float y = -fmap(nunchuck.values[0], 0, 255, -1.0, 1.0);
68+
int lval = 90 * x - 90 * y + lCenter;
69+
int rval = -90 * x - 90 * y + rCenter;
70+
if (lval < 0)
71+
lval = 0;
72+
if (rval < 0)
73+
rval = 0;
74+
if (lval > 180)
75+
lval = 180;
76+
if (rval > 180)
77+
rval = 180;
78+
79+
delay(10);
80+
81+
//left.write(lval);
82+
//right.write(rval);
8883

89-
left.write(lval);
90-
right.write(rval);
91-
delay(10);
84+
Serial.print("\n\tx= ");
85+
Serial.print(bno.orientationX);
86+
Serial.print(" |\ty= ");
87+
Serial.print(bno.orientationY);
88+
Serial.print(" |\tz= ");
89+
Serial.print(bno.orientationZ);
90+
Serial.print(" |\tL= ");
91+
Serial.print(lval);
92+
Serial.print(" |\tR= ");
93+
Serial.print(rval);
9294

9395
}

0 commit comments

Comments
 (0)