Hello,
I am currently working on a project involving an older RPi 1 Model B and the first BrickPi, as well as this C library. The project involves moving a pen on a piece of paper in order to draw various things. While extreme precision is not crucial, I am interested in finding the best method to accomplish this.
For the past few days, I’ve been trying to use a busy loop to simulate the motor running for some time. Example:
movePen(150);
waitMicrosec(500000);
I’ve done a lot of research and tried various timing mechanisms, and the final one I’ve arrived at is using the Cycle Counter Register on the Pi. However, the results are still not consistent, and the pen leaves large erroneous marks.
As a last resort, I am currently trying to control the motors based on the Encoder values. I do not fully understand how Encoder and EncoderOffset are related in the code, but this the code that I am having problems with:
void moveRight(int motor, int amount, int speed) {
if (BrickPi.Encoder[motor] == 0) {
BrickPiUpdateValues();
}
long start = BrickPi.Encoder[motor];
long target = start + amount;
printf("ENCODER AT START: %ld\nTARGET: %ld\n", start, target);
while (BrickPi.Encoder[motor] < target || (target != 0 && BrickPi.Encoder[motor] == 0)) {
if (BrickPi.Encoder[motor] == 0) {
BrickPiUpdateValues();
}
if (labs(target - BrickPi.Encoder[motor]) < 200) {
BrickPi.MotorSpeed[motor] = 103;
} else {
BrickPi.MotorSpeed[motor] = speed;
}
BrickPiUpdateValues();
printf("ENCODER: %ld\n", BrickPi.Encoder[motor]);
}
BrickPi.MotorSpeed[motor] = 0;
BrickPiUpdateValues();
printf("ENCODER AT END: %ld\n", BrickPi.Encoder[motor]);
BrickPi.EncoderOffset[motor] = BrickPi.Encoder[motor];
BrickPiUpdateValues();
}
The code is a work in progress and is not extremely clear. However, here are some of the issues:
-
With the above code, the robot works fine for a few calls to the function. However, sooner or later the encoder reports incorrect values (First image is a normal output, second image is incorrect)
-
Why does the encoder erroneously report 0 values (not shows in images, but happens often; that is why I try to ignore them in the code)
-
I can provide additional code if needed, but I am at least on the right track? Is there a better way to achieve this?
Thank you!