A Equation for a Cannon

My team was planning on making cannon during the first week. I thought I’d be cool to make it autonomously shoot. So I made a huge trig/calc function to calculate the specs. Just as I finished it, they decided to go the easy way with some roller thing, nothing fun to program.
So thats my story, and attached is the function. It’s long and it’s ugly.

It’s designed for a rotating cannon of variable velocity. It returns a pair<float , pair<float , float>> of <velocity to shoot , <angle to shoot , enemy’s angle>>. The enemy’s angle is to determine if the enemy is coming at you and blocking his trailer.

It needs a camera that is tracked to the target trailer. It also needs a rangefinder attached to the camera, preferably with a narrow beam pattern.

It’s in c++, but you could change the pairs to structs to make it c.

I’m not sure if it works, I’m not sure if it even compiles. But if anyone is interested I’ll help anyone fix it and use it.

CannonEquation.cpp (5.78 KB)


CannonEquation.cpp (5.78 KB)

Thanks a lot for posting this. I love it when people post code. However it is very hard to understand. Since it is not commented, would you be willing to give a brief overview of how it works. Our team actually is going with a cannon and this might be useful to us.
Thanks A Lot,
Julian
Team 852

I’m still not sure of this is going to work, it’s going to need good camera tracking and accurate distance judging.
But if your still interested, attached is a drawing to try to explain the equation.

At point k the camera finds a range ( s ) and angle (Q) (relative to the heading of the robot). Then it moves M and takes another range of ss. From this the velocity and heading of the enemy robot can be calculated using the law of sines and cosines (hence all the trigness). All that was the ak_pp_kk stuff. In the code angle are apoint_point_point and lines are lpoint_point.

The next part is where things get crazy

Next I made the equation for the line T_ppp and then substituted that into that simple kinematic equations to come out with an equation with the Initial velocity to shoot at.
The problem was that it was 3 pages long and the velocity was all over the place. So I had to use Newton’s Linerization to solve it (the same way graphing calculators find the interception of two lines). I had to take the derivative of this huge function and it got really big. This part is the whole while loop. For newton’s method you plug in a guess (here it’s the ss, which is’nt right, this needs to be fixed) and then use the answer as a second guess. When each number it returns becomes so close (in this case it’s randomly 5) you have a good estimate. It should take less than 10 times, likely less to become very accurate.
Then from this it gets the best angle the turret should be at and then finds the angle the enemy is coming at you and returns these and the velocity in a set of pairs.
Looking back at this it is missing some stuff like a specification of accuracy, a pitch of the angle, and others. I’m fixing the code now and making it more understandable. I might also later make a pitch varying version rather than velocity.
Also, I will be using some of this to make a robot avoiding programming that you might see posted later on if I do finish it.

cannonEquationDrawling.png


cannonEquationDrawling.png

It’s been a bit but i finally got around to making a better version.
It still does it with a varying velocity but shows what would be changed for angle varyisation.

It could also be changed to acceleration tracking rather than velocity , but that is imbedded deeply in the code

Still working on the tracking program, which might use acceleration tracking

Also you might want to stick this in a seperate namespace since it is a huge global pollutor.

I realize that the last one might have been messed up due to linux end lines vs windows. I think this one is good.

Also, it still might not compile, tell me if you can’t fix it.

CannonEquation.cpp (5.21 KB)


CannonEquation.cpp (5.21 KB)

looks long and complex. its really really hard to see what this thing is doing or how its doing it, especially with all the #defines.

and why return a pair(,pair())? that seems kindof antiintuitive… why not create a new struct{veloctiy, angle, angle}?