Jump to content

Help with my project in Programming


Wampa842

Recommended Posts

I have a school assignment to write a more-or-less useful program in Pascal (don't ask why Pascal). My idea is to make an orbital calculator for Kerbin which takes in some data, and returns whatever can be calculated from those variables and the constants of Kerbin and its SoI.

These input data are:

  • Apoapsis
  • Periapsis
  • Angle from semi-major axis (both deg and rad will do, I can cross-convert them)
  • Velocity at apoapsis (if empty, it'll be calculated)

Some outputs that I'd like to calculate:

  • Current velocity *
  • Velocity at apoapsis and periapsis *
  • Current angular velocity *
  • Eccentricity of the trajectory
  • How much faster/slower the craft is compared to Kerbin's rotation
  • Whether the orbit is kerbosynchronous or semi-synchronous, or close to that
  • Whether the craft is on an escape/re-entry/collision trajectory
  • The minimal delta-v required for an escape and a re-entry (Pe < 20km) from the periapsis and apoapsis, respectively

The results marked with an asterisk are the ones I cannot write up a formula for, and this being the largest community of rocketeers that I am part of, I thought maybe some scientists can help me out.

Cheers,

Wampa842

Link to comment
Share on other sites

<Apology>Why Pascal?</Apology>

I'm guessing it's an intro to programming or a concept course. Personally, I would just analyze some of the mods that already include all this info to obtain the formulas, but I'm a programmer and that's what I do. I'm sure someone here will give you a better answer, but I had to respond to the Pascal thing ;P

Link to comment
Share on other sites

Man I'm sorry I can't help but I just had to comment on the pascal thing. I can't even think of a place that would teach pascal. Most entry level programming is java, and everything else is c++ and c Sharp.

But yea find the equations and maybe make a simple GUI to input your values. Idk if pascal has any support for GUI.

Link to comment
Share on other sites

I'm going to use the following notation.

R = planet's radius.

μ = GM = gravitational parameter.

r = h + R = distance from center of the planet. h is altitude.

ra = ha + R = apoapsis.

rp = hp + R = periapsis.

a = (ra + rp)/2 = semi-major axis.

Using these, you can find velocity at any point along the orbit using virial. (You can derive this equation from energy and angular momentum conservation, but it takes a bit of work.)

v² = μ(2/r - 1/a)

This will work for apoapsis and periapsis velocities as well. Just substitute r = ra/p. (Note that this even works for hyperbolic trajectory if you use the convention where a < 0 for hyperbolic trajectories.)

Angular velocity follows from angular momentum conservation, which essentially tells you that Ér² is a constant. This is equivalent to Kepler's Second Law. So you know that Ér² = Éprp². And at periapsis, angular velocity is just Ép=vp/rp, because r and v are orthogonal at apsides. That gives you general formula.

É = vp rp/r²

Again, since periapsis is defined for parabolic trajectories, this will work even if your craft is on an escape trajectory.

And you should be able to compute everything else from that. Have fun.

Link to comment
Share on other sites

I'm going to use the following notation.

-snip snip snip-

And you should be able to compute everything else from that. Have fun.

That just about answers it, thanks. Some (or much, much more than 'some') research and leaning over my desk later I should be able to implement this in Pascal.

Why Pascal?

To be honest, I have no idea why. The IT school I attend teaches Programming theory using Borland Pascal and Object Pascal, claiming it is the best for learning. That may be true, but I will be knee-deep in it when I have to learn C# or C++ throughout the summer before I start university.

Link to comment
Share on other sites

Ahh, it's so funny when so called programmers laugh when they hear something about Pascal. They only know that old blue IDE. Their knowledge is so limited:(

Well, the thing is that Pascal's life didn't stop there. It's still upgraded, offers you almost the same as nowdays popular languages offer. You might want to check out Free Pascal and Lazarus.

Another thing is that Delphi is basicly Pascal. Quite good language too. Not to mention that this is developed by a large company, and they are developing something very great, called Firemonkey, which offers you to compile your code to any platform you want. If this project will be a success, today's most popular language will be the Delphi.

Link to comment
Share on other sites

To be honest, I have no idea why. The IT school I attend teaches Programming theory using Borland Pascal and Object Pascal, claiming it is the best for learning. That may be true, but I will be knee-deep in it when I have to learn C# or C++ throughout the summer before I start university.

Nah, don't worry. Transition from Borland Pascal to C or from Object Pascal/Delphi to C++ is extremely smooth. Get yourself a C++ Pocket Reference. That will let you quickly look up the syntax, and that covers almost all of the important differences. I'd start early, though. There is no reason why you can't be writing some simple C++ programs right now to be ready when you have to do this for class.

C# is a bit different, because it compiles to CLR binary, rather than machine binary, which means memory management is a lot more like Java, and that leads to slightly different approach to programming. But still, for most introductory level stuff, it's going to feel like just a syntax difference. In fact, when you start doing more advanced things with memory, C# might seem way easier, because it's a lot better at making sure you aren't creating leaks or seg-faults.

Link to comment
Share on other sites

i think they started me with c. no ++, no # just c. i learned c++ on my own. then a few years ago i discovered lua. those are my most fluent languages. did some vb too but dont remember any of it. i could learn others but i can get by with what i know. c# looks easy enough, but i dont like the idea of running not machine code (same applies to java, which i will not be learning). lua is kind of exempt because its usually used to extend a machine executable, though i do a lot of stand alone stuff just because its an easy language to use.

Link to comment
Share on other sites

Virtual machine has a lot of advantages. It's very useful to know how to code for one. Understanding garbage collection in Java and C# is also very useful for larger C++ projects. Memory management in general is an interesting subject.

Link to comment
Share on other sites

Ahh, it's so funny when so called programmers laugh when they hear something about Pascal. They only know that old blue IDE. Their knowledge is so limited:(

Call it nostalgia then :). Last time I dealt with pascal was like 17 years ago. SO... half my life ago!

Link to comment
Share on other sites

Hmm, maybe I'm the stupid here, but e.g. iOS or Android didn't exist in the 80s.

And because they didn't exist, nobody wanted to compile for them. So the statement was still true. And since GCC compilers are being kept up to date to this day, the statement stayed true.

Besides, you don't compile to the platform. You compile to an architecture. And as Nuke pointed out, ARM already existed in the 80s. As well as MIPS and x86, which are your other options on Android. iOS is strictly ARM or derivatives, if I'm not mistaken.

The linker, of course, needs to know a few things about target platform to dress up your compiled code properly. And the linkers in GCC package have been able to make executable code for these mobile platforms, as well as many, many others, for pretty much as long as these platforms have been around. People who keep GNU up to date tend to be very good at making sure the functionality is updated as soon as there is new development in architecture or operating systems. Of course, if you happen to need code for a platform it can't link for, you still have options. You can get it to dump just binary that you can dress up for a particular OS yourself, or it can give you code compiled to assembly, but not assembled or linked, so you can use native assembler and linker. Et cetera. You'd be very hard pressed to come up with a modern platform, even an obscure one, that you can't build code for using GCC tools.

GCC also gives you a choice of languages. C, C++, Objective-C, Ada, and Java are standard. A number of other languages are also available, including Pascal. It's not standard, but you can get it. Because it's part of the GCC package, it can compile and link to any platform that the other GCC compilers can handle. So yeah, you can already build iOS and Android code using GCC Pascal.

Link to comment
Share on other sites

Programming languages apart, another approach you could use is a set of coupled differential equations and a numeric calculation method (such as Euler's). For instance, at any given time, the resultant force of a body on "free" orbit should be:

Fr= Mb*a = -G*Mb*Mp/r^2

But, if there are any other forces, for instance, let say attrition (proportional to the velocity of the ship, Fa=-c*v, c being a constant), you can easily sum to the equation above.

Fr=Mb*a= -G*Mb*Mp/r^2 -c*v

thus

a=-g*Mp/r^2 - c*v/Mb

So, you know the acceleration at that instance! As the acceleration is the derivative of the velocity (hence the differential equations approach) a=dv/dt, the velocity is now v= v0 + a*dt (v0 being the speed at a prior moment) aaand.. the velocity is the derivative of the position! So x=x0+v*dt

To the programming part, you should first set you initial conditions (let's say speed and position) and basically build a loop increasing the time by a dt factor each iteration. Inside the loop you calculate the instantaneous accel, speed and position, using the last known data and so on (Euler's method).

Maybe this is not a very high-school-ish level technique, but is a damn powerful numeric solution (three body problem, anyone?)

By the way, small tip: planet orbits are bi-dimensional (at least), therefore a there are 2 components to speed (vx, vy), position (x, y) and acceleration :)

Link to comment
Share on other sites

a numeric calculation method (such as Euler's)

Euler is pretty much the worst choice you can make for an orbital mechanics problem. (There are two ways to do Euler for second order, and one of them is the absolute worst way to integrate orbital motion.)

If you want a method that's just as simple, yet gives you far, far better precision, go with Verlet.

Link to comment
Share on other sites

  • 3 months later...

But yea find the equations and maybe make a simple GUI to input your values. Idk if pascal has any support for GUI.

I once wrote an x's and o's program in Pascal. I was able to construct a basic UI using the graphics module. (the alternative would have been ascii x's and o's - oh the horror!)

Don't recall there being any mouse support but I would be surprised if there isn't.

It was a long, long time ago so its all a bit foggy. The only thing I remember for sure is that I wrote the source code with a hammer, a chisel and a large slab of granite.

Link to comment
Share on other sites

I had a similar idea for an orbital calculator in pascal. It was to have a 2d GUI and everything. I was writing it at school, but quickly gave up when I realised that the graphics unit hadn't been installed on the machines there. (I am learning pascal for my A-levels btw)

I'm tempted to give it another go.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...