Skip to content

Commit e437879

Browse files
committed
Adding a chassis class to manage orentation
1 parent 9204c34 commit e437879

1 file changed

Lines changed: 61 additions & 23 deletions

File tree

ArduinoClassRobot.ino

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,59 @@
2626
#include <Wire.h>
2727
#include <EasyBNO055_ESP.h>
2828

29-
Servo left;
30-
Servo right;
29+
30+
class Chassis{
31+
public:
32+
int lCenter = 86;
33+
int rCenter = 87;
34+
Servo left;
35+
Servo right;
36+
double fwdTarget = 0;
37+
double rotZTarget = 0;
38+
double currentRotationZ=0;
39+
double rotZIncrement=0.6;
40+
double kp=0.01;
41+
int lval ;
42+
int rval ;
43+
Chassis(){}
44+
void setTargets(double fwd, double rotz,double currentRotZ){
45+
if(abs(rotz)<0.01){
46+
rotz=0;
47+
}
48+
fwdTarget=fwd;
49+
rotZTarget+=(rotz*rotZIncrement);
50+
currentRotationZ=currentRotZ;
51+
52+
write();
53+
}
54+
void begin(){
55+
left.attach(33, 1000, 2000);
56+
right.attach(32, 1000, 2000);
57+
left.write(lCenter);
58+
right.write(rCenter);
59+
}
60+
void write(){
61+
double rotZErr = -kp*(rotZTarget-currentRotationZ);
62+
lval = 90 * fwdTarget - 90 * rotZErr + lCenter;
63+
rval = -90 * fwdTarget - 90 * rotZErr + rCenter;
64+
if (lval < 0)
65+
lval = 0;
66+
if (rval < 0)
67+
rval = 0;
68+
if (lval > 180)
69+
lval = 180;
70+
if (rval > 180)
71+
rval = 180;
72+
left.write(lval);
73+
right.write(rval);
74+
}
75+
};
76+
3177
Accessory nunchuck;
3278
EasyBNO055_ESP bno;
79+
80+
81+
3382
void otherI2CUpdate() {
3483
nunchuck.readData(); // Read inputs and update maps
3584
}
@@ -44,17 +93,15 @@ float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
4493
const float delta = x - in_min;
4594
return (delta * rise) / run + out_min;
4695
}
47-
int lCenter = 86;
48-
int rCenter = 87;
96+
97+
98+
Chassis puppy;
4999
// the setup function runs once when you press reset or power the board
50100
void setup() {
51101

52102
Serial.begin(115200);
53103
Serial.println("Starting ESP32");
54-
left.attach(33, 1000, 2000);
55-
right.attach(32, 1000, 2000);
56-
left.write(lCenter);
57-
right.write(rCenter);
104+
puppy.begin();
58105
nunchuck.begin();
59106
bno.start(&otherI2CUpdate);
60107

@@ -65,31 +112,22 @@ void loop() {
65112

66113
float x = -fmap(nunchuck.values[1], 0, 255, -1.0, 1.0);
67114
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;
115+
puppy.setTargets(x, y,bno.orientationZ);
116+
if(nunchuck.values[11]>0){
117+
puppy.rotZTarget = 0;
118+
}
78119

79120
delay(10);
80121

81-
//left.write(lval);
82-
//right.write(rval);
83-
84122
Serial.print("\n\tx= ");
85123
Serial.print(bno.orientationX);
86124
Serial.print(" |\ty= ");
87125
Serial.print(bno.orientationY);
88126
Serial.print(" |\tz= ");
89127
Serial.print(bno.orientationZ);
90128
Serial.print(" |\tL= ");
91-
Serial.print(lval);
129+
Serial.print(puppy.lval);
92130
Serial.print(" |\tR= ");
93-
Serial.print(rval);
131+
Serial.print(puppy.rval);
94132

95133
}

0 commit comments

Comments
 (0)