# How do you move a robot with vision data?

So this year is the first time my team has attempted vision, and although the process is crude, as of right now we are able to get the x pos, width height and area of the ball. We are stuck at the part of manipulating the data to make the robot move. We understand the concepts of getting the error and using the gyro to spin the robot to that angle, but there are no good tutorials online for java bout this. If anyone has suggestions or easy guides to follow, it would help out alot. I became the head programmer freshmen year because everyone left, and as you can probs imgain even though this is my second year programming i can only understand like half of the things people say on this site. Right now we are trying to take the angle from the ball to the center of the screen and use a pid to turn the robot, and as a crude way to move it forwards, use the area and if else statements as a way to gauge to move the robot forward. If anyone has suggestions, please share ^_^.

Our Github is : https://github.com/Anomaly816/Oops if you want to see what we have figured out this year. Sorry, its really messy, its our test robot code. All of the vision stuff is in periodic and int.

Try breaking you problem into sub-problems. If you need to rotate a robot to the angle of ball, think of this as two problems:

1. We need to have a method to turn to an angle.
2. We need to get the angle between our robot and the goal.
You can solve each of these problem individually and stick them together.

Now, as for accomplish those two problems, there are ok ways, good ways, and better ways of doing it. Which do you choose? For your situation, I highly suggest starting with the okay ways that are easy to implement. Youâ€™ll at least have something that works until you iterate a better version.
Remember, an okay way finished is 100% more effective than a better way not finished.
For example, the best way to turn to an angle might be to uses a PID, but you can probably get pretty close just by setting a power to turn until you almost get to that angle. That way, you have something working while you develop your PID.

8 Likes

Thank you !^-^ By any chance is is possible to pull data from the smart dash? Iâ€™m making the function to turn the robot rn, and it just popped into my head.

Here is a link to the Smart dashboard section in the wpilib api for Java.
https://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/smartdashboard/SmartDashboard.html
I highly suggest getting familiar with using this documentation. It is wonderful for seeing what functions do. WPILIB also has some other great documentation that you might find helpful.
https://docs.wpilib.org/en/latest/docs/software/wpilib-overview/new-for-2020.html
Edit: Feel free to keep asking questions if you have them, I just want to start by guiding you to resources to solve your problem rather than outright telling you.

1 Like

XD thank you. We finished club yesterday so i kinda just dropped everything. I just got it to rotate and Iâ€™m working on making it move forward based on the area gauge. We are taking the pos x and creating a tolerance zone based on the pixel count. And thank you for doing this ^-^. It was a big help. From this, how do I make my code more efficient? My mentor has a break down every time he looks at my screen XD.
EDIT: Iâ€™ll update github at 12 if anyone wants to see how jank this is XD. The function is called rotate and movingx if you dont wanna look though our birds next of test code.

1 Like

Just a suggestion, but if you use adrive.arcadeDrive() instead of adrive.tankDrive() it can help you break down the problem into two parts as @UnofficialForth suggested. The arcadeDrive method takes a forward velocity and a rotation speed. You can use X of the target to calculate your rotation, and Y or area to calculate the forward velocity. This way you donâ€™t have to worry about how to combine them, just do them separately.

Also, Iâ€™d suggest you take a little time to look at Command-Based Programming. It helps you structure the code and separate what the robot is doing from how the mechanisms are doing it.

2 Likes

To calculate the velocity, would I just set up a area threshold where if the read is less then ___ then it would put like +.5 to the motors and when its past the thresh hold or too close to the robot then -.25 to the motors? Thanks for the arcade drive suggestion! We tried it out tonight, but we were getting the same results as before. For some reason the robot wont rotate to the ball. It would just move towards it and go back and fourth when it reached the stop threshold. As of right now we are trying to figure out a way to calculate the exact angle to turn and then feed it to the imu to get a spot on rotation. We are using a dilation formula to get the distance and then using trig to get the exact angle we need to turn. We ended up finding out that the area of the ball can be graphed into a decaying exponential scale and we created a formula based on the max area we want and feet to get a formula that spits out the exact distance of the ball. We now just need a way to calculate the exact angle and maybe use our PID loop to correct our robots path. The code on our github abruptly turns the robot the second it detects that the ball is out of position. We tried tweeking the PID values, but for some reason we got to this point where it would either abruptly turn or it would turn really slow and isolate when it found the ball. I guess there are gonna be alot more late nights XD. Also thanks for the suggestion for command based programming. I want to see if we can switch our robot to command based once this vision stuff is worked out.

Can you possibly figure out the y value of the target? With that you can do trip to determine distance and distance error, which you can feed to a PID to get your forward velocity.

My team is fortunate enough to have a limelight, so the target axis values are taken care of for us. Then, Iâ€™m running the X- and Y-axis errors through separate WPILIB pid controllers and spitting the outputs into an arcade drive simultaneously.

Kind of a janky solution, but the robot makes a neat little s-turn to align and get in range fairly predictably. If you would like to see my code, feel free to PM me.
(Disclaimer: Iâ€™m far from an expert, but I am lazy enough to find efficient solutions )

2 Likes

You may consider it â€śjankyâ€ť, but if it gets the job done, it gets it done. Itâ€™s better to do something simple well then complex poorly.

1 Like

Is your goal to drive to a power cell (ball) that might be moving so you can intake it into your robot?

Honestly, I think youâ€™re overthinking it. You donâ€™t need to know the actual distance to the ball, and thereâ€™s no reason you need to calculate the angle and use the gyro. If you know where in the camera view the ball needs to be you can just drive to get it there. You donâ€™t need to plot a complex path or use the gyro, in fact, I wouldnâ€™t since the ball might not be there by the time you get there since the ball might be rolling.

Hereâ€™s what I would do:

1. Mount the camera so you can see the ball toward the bottom of your view and in the center when itâ€™s ready to be captured by your intake.
2. Determine the X and Y in the view for where the center of the ball needs to be for you to intake. These are now your target X and Y values.
3. When you see the ball with your camera, calculate itâ€™s center X and Y. (Since itâ€™s a sphere you donâ€™t even have to worry about distortion from your perspective.)

At this point your goal is to drive the robot so the target X and Y match the ballâ€™s center X and Y.

1. If Y is too large (the ball is too high in the view), drive forward. The higher it is, the faster you drive. This is what P in PID is for, the correction is proportional to the error.
2. If the X value is too small, turn left. If the X value is too large, turn right. Use proportional control here too, the farther the X is from the target, the faster you turn.

Personally, I would avoid using the ballâ€™s area to calculate distance because it wonâ€™t be reliable at the edges of the view when part of it is not visible.

Iâ€™m a little confused by your response (specifically back at me), I donâ€™t know what itâ€™s trying to address.

Sorry, I intended that to be a response to @Karma816, the original poster. @UnofficialForth and @lowtorola, I think youâ€™re on the right track.

Specifically my response was addressing @Karma816â€™s most recent post:

1 Like

Do you know an accurate way to calibrate the P? So far we have done it by hand and for some reason either its to strong or way too weak. We can get it to follow it, but there is this zone where the robot would detect the ball but it wouldnâ€™t turn to make it straight. Its around a 10 ish degrees on either side. Is that just because our P value is too small to make the robot move? we have it set to .000517

There is lots of advice for this on the web.
I usually start with a small value and double it until the robot oscillates around the target. Now you have a range. I then adjust it up and down until it just oscillates around the target, and then Iâ€™ll add a little bit of D to smooth it out.
One thing to keep in mind is that your PID for turning and your PID for driving forward will likely be different.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.