2626#include < Wire.h>
2727#include < EasyBNO055_ESP.h>
2828
29-
30- class Chassis {
29+ class Chassis {
3130public:
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=1.2 ;
40- double kp=0.01 ;
41- double fwdConstant=15 ;
42- double resetTarget=0 ;
43- int lval ;
44- int rval ;
45- Chassis (){}
46- void setTargets (double fwd, double rotz,double currentRotZ){
47- if (abs (rotz)<0.01 ){
48- rotz=0 ;
49- }
50- fwdTarget=fwd;
51- rotZTarget+=(rotz*rotZIncrement);
52- currentRotationZ=currentRotZ;
53-
54- write ();
55- }
56- void begin (){
57- left.attach (33 , 1000 , 2000 );
58- right.attach (32 , 1000 , 2000 );
59- left.write (lCenter);
60- right.write (rCenter);
61- }
62- void write (){
63- double rotZErr = -kp*(rotZTarget-currentRotationZ);
64- lval = fwdConstant * fwdTarget - 90 * rotZErr + lCenter;
65- rval = -fwdConstant * fwdTarget - 90 * rotZErr + rCenter;
66- if (lval < 0 )
67- lval = 0 ;
68- if (rval < 0 )
69- rval = 0 ;
70- if (lval > 180 )
71- lval = 180 ;
72- if (rval > 180 )
73- rval = 180 ;
74- left.write (lval);
75- right.write (rval);
76- }
31+ int lCenter = 86 ;
32+ int rCenter = 87 ;
33+ Servo left;
34+ Servo right;
35+ double fwdTarget = 0 ;
36+ double rotZTarget = 0 ;
37+ double currentRotationZ = 0 ;
38+ double rotZIncrement = 1.2 ;
39+ double kp = 0.013 ;
40+ double fwdConstant = 15 ;
41+ double resetTarget = 0 ;
42+ int lval;
43+ int rval;
44+ Chassis () {
45+ }
46+ void setTargets (double fwd, double rotz, double currentRotZ) {
47+ if (abs (rotz) < 0.01 ) {
48+ rotz = 0 ;
49+ }
50+ fwdTarget = fwd;
51+ rotZTarget += (rotz * rotZIncrement);
52+ currentRotationZ = currentRotZ;
53+
54+ write ();
55+ }
56+ void begin () {
57+ left.attach (33 , 1000 , 2000 );
58+ right.attach (32 , 1000 , 2000 );
59+ left.write (lCenter);
60+ right.write (rCenter);
61+ }
62+ void write () {
63+ double rotZErr = -kp * (rotZTarget - currentRotationZ);
64+ lval = fwdConstant * fwdTarget - 90 * rotZErr + lCenter;
65+ rval = -fwdConstant * fwdTarget - 90 * rotZErr + rCenter;
66+ if (lval < 0 )
67+ lval = 0 ;
68+ if (rval < 0 )
69+ rval = 0 ;
70+ if (lval > 180 )
71+ lval = 180 ;
72+ if (rval > 180 )
73+ rval = 180 ;
74+ left.write (lval);
75+ right.write (rval);
76+ }
7777};
7878
7979Accessory nunchuck;
8080EasyBNO055_ESP bno;
81-
82-
81+ Chassis puppy;
8382
8483void otherI2CUpdate () {
8584 nunchuck.readData (); // Read inputs and update maps
@@ -96,14 +95,12 @@ float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
9695 return (delta * rise) / run + out_min;
9796}
9897
99-
100- Chassis puppy;
10198// the setup function runs once when you press reset or power the board
10299void setup () {
103100
104101 Serial.begin (115200 );
105102 Serial.println (" Starting ESP32" );
106- puppy.begin ();
103+ puppy.begin ();
107104 nunchuck.begin ();
108105 bno.start (&otherI2CUpdate);
109106
@@ -114,14 +111,14 @@ void loop() {
114111
115112 float x = -fmap (nunchuck.values [1 ], 0 , 255 , -1.0 , 1.0 );
116113 float y = fmap (nunchuck.values [0 ], 0 , 255 , -1.0 , 1.0 );
117- puppy.setTargets (x, y,bno.orientationZ );
118- if (nunchuck.values [11 ]> 0 ) {
119- puppy.rotZTarget = puppy.resetTarget ;
120- }
121- if (nunchuck.values [10 ]> 0 ) {
122- puppy.resetTarget = puppy.currentRotationZ ;
123- puppy.rotZTarget = puppy.currentRotationZ ;
124- }
114+ puppy.setTargets (x, y, bno.orientationZ );
115+ if (nunchuck.values [11 ] > 0 ) {
116+ puppy.rotZTarget = puppy.resetTarget ;
117+ }
118+ if (nunchuck.values [10 ] > 0 ) {
119+ puppy.resetTarget = puppy.currentRotationZ ;
120+ puppy.rotZTarget = puppy.currentRotationZ ;
121+ }
125122 delay (10 );
126123
127124 Serial.print (" \n\t x= " );
0 commit comments