Skip to content

Commit 5c8a44a

Browse files
BalanceBot example
Refactoring of balance bot
1 parent 176cc23 commit 5c8a44a

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

RoboHeart.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ float RoboHeart::getRotationZ(){
227227
}
228228

229229

230+
void RoboHeart::resetGyro(){
231+
this->_rotationX = 0;
232+
this->_rotationY = 0;
233+
this->_rotationZ = 0;
234+
}
235+
230236
float RoboHeart::getTemperatureC(){
231237
return (imu.readRawTemp() / 256.) + 25;
232238
}

RoboHeart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class RoboHeart {
5050
float getRotationX();
5151
float getRotationY();
5252
float getRotationZ();
53+
void resetGyro();
5354

5455
RoboHeartStepperMotor stepper;
5556
RoboHeartDRV8836 motorA;

examples/RoboHeartBalanceBot/RoboHeartBalanceBot.ino

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
RoboHeart heart = RoboHeart(Serial);
2222

2323
// PID controller parameters
24-
#define Kp 20
25-
#define Kd 0.001
26-
#define Ki 40
24+
#define Kp 6.5
25+
#define Kd 0
26+
#define Ki 0
2727

2828
#define CONTROL_TICK_PERIOD_US 100.0
2929

@@ -53,8 +53,8 @@ unsigned long prevTimeIntervalMS = 0;
5353
// handle -180 after crossing 180 Degrees
5454
float processAngle(float angle) {
5555
// handle -180 after crossing 180
56-
if (angle < -90) {
57-
return angle = 360 + angle;
56+
if (angle > 180) {
57+
return angle - 360;
5858
}
5959
return angle;
6060
}
@@ -71,8 +71,9 @@ void tick() {
7171
// and later used to indicate stable vertical position
7272
// of the Balancing Bot.
7373
void processPinInterrupt() {
74-
offsetAngleDeg = heart.getRotationX();
74+
offsetAngleDeg = processAngle(heart.getRotationX());
7575
targetAngleDeg = offsetAngleDeg;
76+
errorSum = 0;
7677
}
7778

7879
// Periodic timer executes the control ticks

0 commit comments

Comments
 (0)