We have recently encountered our bot drifting slightly while in motion. This does not make much of a difference in our driving runs, but in our autonomous it causes several inconsistencies. I was wondering if anyone had an example of a program that autocorrects the bot when it starts to drift using brain inertial.
What programming langauge are you using? Blocks, Python or C++?
We use block I have attached the code that we tried.
BA straight - Copy.iqblocks (14.8 KB)
Have you tested this code?
So far it looks like it will just turn back and forth forever (bc the turn will override the drive movement). Im working on an example, of how you could solve this problem in blocks but im not done yet.
The file is not loading for me. Whenever I click on it, it just opens a blank tempelate.
Yes that is exactly what happens I don’t really understand why though
I can get you a screenshot in a couple of hours
I finished.
This code is an example of a correction pid function. A pid function is just code that corrects the motor powers using the difference within the desired(what we want the robot to stay at) and it’s actual position . How this snippet of code works is:
First, it sets the motor power and tune value ajustment value
Then, It creats a while loop that runs until the robot reaches 800 degrees of rotation on the motors
After that, it calculated the error using the difference between our desired (0) and our actual heading (the brain inertial reading).
Then it corrects the error into a -180 to 180 range for the most efficent corrections.
Finally, it applys the new corrected motor power to the left side and once the loop is finished the robot stops moving.
This is just one way how to do it, there are multiple ways to solve this problem and you should do what works best for you/you understand the best. If you have any problems with understanding this approach or trying to implement it, I’m happy to help.
Thank you so much I already left so I will try it tomorrow. Do you mind linking the file so I can see how it works
Quick explaination on it turns forever:
When you call a drive/turn function. The robot can only do 1 of those at once (unless you make your own custom function). So in this scenrio you tell the drivetrain to start moving and then the turn kicks in. This overwrites and stops the drive function from running. Causing you to turn back and forth forever since the robot will never stop “moving”. The reason why the approach that I explained works is because you are driving with a custom approach using base power and the robot just adds extra power to 1 side to correct for the error. This makes the robot turn while driving to a more straighter angle. Correcting the motion based on the angle. If you still dont’t understand I can explain futher if you want.
Oh no that makes sense thank you for the help.
I’m afraid I don’t quite understand how this works. I assume we separate the drivetrain into separate motors. However then how do we tell the drivetrain to drive forward. I assume it has something to do with the block, but I’m not positive what. Also unless I’m mistaken (which I probably am)
The drive forward is the block itself. The block spins both motors forward
In this bit of the code it calculates the speed by adding the adjustment power to the base power of 50
making it drive straight. Then it uses the Pid control to correct one of the sides to try to make it more straight (using the brain inertial).
This approach is a bit on the complex side, if you still dont understand I can try to help you find a more simplistic approach. Or continue with this one.
So if I just downloaded this exact code to the brain it should drive straight?
The line with pid should be your result if you run this (it will drive straight). You will have to tune the adjustment value (lower is less back and forth) (higher is more) (if you have too low of a value the straightening is useless)
But, it is more important that you understand the code. If you dont understand it I think it is more benefical to come up with a different appoarch. That is easier to understand.
Ok I think I’m beginning to understand, but not perfectly. Could you possibly and I know this is a lot of effort but just add a little explanation to each block, because right now it just drives in large circles however I don’t understand why.
Hold up I think I understand now. However in the code you have the drivetrain motors separated in the velocity blocks, but then in order to have the drive heading in degrees you must have a drivetrain which I cannot find out how code each motor separately when you have it set up.
Is one of the motors going reverse?
Also the adjustment value may be too high (try a lower value like .5)
That is a potential problem I saw, you may have to read the brain inertial itself
(Replace the drive heading in degrees with this)
Ok that’s what we did we will double check the motors, but it is very strange.