@@ -152,32 +152,59 @@ Self-balancing robots with gyro sensor
...
@@ -152,32 +152,59 @@ Self-balancing robots with gyro sensor
### Setup
### Setup
The code for this exercise is inspired by the code described and linked in [5]. The full implementation can be found at [6], with the corresponding PC program located at [7].
#### Software Setup
The code for this exercise is inspired by the code described and linked in [5]. The full implementation can be found at [6], with the corresponding PC program located at [7]. The important parts of the logic are shown in the this section. The following listing how calculating angular velocity (`gyroSpeed`) and `gyroAngle` from the raw gyro output is performed:
```java
voidcalcGyroValues(floatinterval)
{
intgyroRaw=gyro.readValue();
// STIKORD:::
// adjust the offset dynamically to correct for the drift of the sensor
offset=EMAOFFSET*gyroRaw+(1-EMAOFFSET)*offset;
/// Indsæt billede
//calculate speed and angle. Interval is the actual time for the last loop iteration.
gyroSpeed=gyroRaw-offset;
gyroAngle+=gyroSpeed*interval;
}
```
Sensoren har siddet to steder, først i toppen og dernæst længere nede -> Mindre rystelser, men også mindre udsving i vinklen.
The motor terms `motorSpeed` and `motorPosition` is calculated as shown in the following listing:
```java
privatevoidcalcMotorValues(floatinterval)
{
//get encoder values
intleft=leftMotor.getTachoCount();
intright=rightMotor.getTachoCount();
//position is the sum of the two encoder values
intsum=left+right;
intdelta=sum-oldMotorSum;
motorPosition+=delta;
//calculate motorspeed as the average motor speed of the last 4 iterations.