I swear I’ve looked everywhere for this. I understand the concept, but I don’t understand the implementation. I’ve looked at a bunch of posts here, and things like the George Gillard docs, but still don’t know the implementation. For example, what values would I connect to my variables? Like inertial rotation to my desired or something?
int desired = Inertia.rotation(degrees);
int error = desired - Inertia.rotation(degrees);
With Inertia being my inertial sensor, what would I change/add to make this work? How do I figure out and make my proportional constant?
P is how much the robot tries to get to the 0-error location, so as you get closer it doesn’t try as hard. I haven’t implemented a PID yet (or a P), but from what I know, you would multiply error by P then move your motors by that new value. You can tune a PID using math (I don’t know the math). Another, easier way is to follow the steps in this post.
That function takes two parameters: angle (desired angle of the robot) and kP (constant of proportionality). Angle is the desired angle of the robot in absolute degrees (not heading) that you want the robot to face. In PID/P-loop docs, this is usually called the target.
Yes, you’ll have to tune kP for each subsystem you use a P-loop for. The higher kP is, the more responsive to error the robot will be. That might sound like a good thing, but it gets to a point where your robot will run at full power for a 0.01 degree error. I think I usually start drivetrains off at 0.05 kP, but it depends on the application.
Would I have to implement numbers to fit the angle or does it already know the desired angle?
Also @2775Josh how would I change your loop to fit a smart-drive (.spin and fwd wont work- would need some kind of turn and either left or right)
I meant does it know the intended angle based on where I code the inertial sensor to turn?
Like, if I tell the inertial sensor to turnToRotation 90 degrees, would it count 90 degrees as the angle for the “angle” variable?
No, I think you’re misunderstanding the purpose of the code Josh provided. Functions are pieces of code that can be called whereever you need them. His code is a better alternative to the built in function. I recommend brushing up on functions and the opportunities they provide if you intend to move past the built-in ones. Trying the code on an actual robot and learning from there will always be more valueable than discussing it. Best of luck!
I still get these 2 errors:
use of undeclared identifier ‘angle’ (I tried making a function for it, but it didn’t work)
member reference base type ‘void (int)’ is not a structure or union (I tried a bunch of different things and looked it up here and elsewhere but I have no clue how to fix it)
Your first error is because you have two variables called angle. It might seem like you only have one, but the first time you define angle, you’re creating a global variable (it’s outside of a function). The second time, your turn function is creating a local variable with the same name, causing an error.
When you have variables in functions (like your direction variable in your Drive function), you need to specify them every time you call the function. For example, in your code, you have to type Drive(50) instead of Drive(). The same is true for your turn function, and any function with these variables (called parameters).
You don’t need to define error outside of your function. Every time you call the function, the error will be different (because your robot will be in a different place with a different target), so it should be a local variable defined and used inside your function.
I don’t understand the purpose of your drive function, some clarification would be appreciated. Why are you using spinFor? if it’s supposed to make the robot drive straight, why is your error being calculated based off the angle and inertial variables? Also, you need to be conscious of the units of your variables. You’re assigning motor power with the error variable, which theoretically should go from -12 to 12 because you’re controlling the motor with voltage, but the possible range of error is much larger than that.
Your turn function is also flawed:
Angle is double-defined, as mentioned
There is no exit condition for the while loop, so your drive will never stop moving even if it was coded correctly (and thus the function will never end)
You can’t use Drive.spin because the .spin only applies to motors and motor groups. You need to control the left and right sides separately.
kP is defined as a parameter for the function but never used. Your error calculation is correct but you need to multiply it by kP to get the final power.