Editing Vessel Tutorial 1
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
+ | === Vessel Tutorial (Work in progress) === | ||
+ | |||
This article intends to guide you through the construction of a new Vessel DLL, by using the sample code from the OrbiterSDK. | This article intends to guide you through the construction of a new Vessel DLL, by using the sample code from the OrbiterSDK. | ||
This documents my adventure transforming the stock ShuttlePB into a reasonably accurate model of the Surveyor lunar lander. There was a model on OrbitHangar, but it had wildly inaccurate mechanics, due partly to the fact that it was based on Vinka's Spacecraft.dll. | This documents my adventure transforming the stock ShuttlePB into a reasonably accurate model of the Surveyor lunar lander. There was a model on OrbitHangar, but it had wildly inaccurate mechanics, due partly to the fact that it was based on Vinka's Spacecraft.dll. | ||
− | This tutorial assumes you have a working compiler which you know creates valid working DLLs from the OrbiterSDK sample code. I | + | This tutorial assumes you have a working compiler which you know creates valid working DLLs from the OrbiterSDK sample code. I happen to use the free Microsoft command-line compiler, together with an editor called TextPad. Setting up the free compiler and the editor is beyond the scope of this document. Check the rest of this Wiki, or the Orbiter forums. |
− | = | + | === My goal spacecraft: Surveyor === |
Surveyor was a series of unmanned lunar landers launched by NASA in 1965-1967, before the first Apollo. It is a particularly simple, yet interesting and highly successful design. | Surveyor was a series of unmanned lunar landers launched by NASA in 1965-1967, before the first Apollo. It is a particularly simple, yet interesting and highly successful design. | ||
Line 11: | Line 13: | ||
Functionally, Surveyor consists of a large solid-fueled main retro-rocket, three medium-sized throttleable liquid fueled vernier engines, and six cold-gas Reaction Control System (RCS) attitude control thrusters. | Functionally, Surveyor consists of a large solid-fueled main retro-rocket, three medium-sized throttleable liquid fueled vernier engines, and six cold-gas Reaction Control System (RCS) attitude control thrusters. | ||
− | The Surveyor flight plan in its final minutes is one exciting ride, and obviously not good for humans. There is no possiblity of abort ( | + | The Surveyor flight plan in its final minutes is one exciting ride, and obviously not good for humans. There is no possiblity of abort (Two of the seven Surveyors, 2 and 4, crashed). The maximum acceleration is 9G's. And, it is very difficult to fly manually. It's a perfect job for a machine. |
Surveyor is launched on top of an Atlas-Centaur rocket, on a direct impact trajectory to the moon. Impact will occur after about 66 hours. About 16 hours after launch, it performs a single mid-course correction maneuver, which aims it to precisely impact at the desired landing point. | Surveyor is launched on top of an Atlas-Centaur rocket, on a direct impact trajectory to the moon. Impact will occur after about 66 hours. About 16 hours after launch, it performs a single mid-course correction maneuver, which aims it to precisely impact at the desired landing point. | ||
− | Thirty minutes before impact, Surveyor maneuvers into a retrograde attitude. From here, its Altitude Marking Radar (AMR) will eventually be able to see the surface and measure the distance. The AMR locks on to the surface at about 200km, | + | Thirty minutes before impact, Surveyor maneuvers into a retrograde attitude. From here, its Altitude Marking Radar (AMR) will eventually be able to see the surface and measure the distance. The AMR locks on to the surface at about 200km, one minute before impact. Once the altitude reaches a pre-determined value called the altitude mark, it starts a short timer. |
At the altitude mark, the spacecraft is hurtling towards the ground at over 2.5km/s and will impact in less than one minute. For Surveyor 1, the altitude mark was about 110km, and the timer was about 7 seconds. Once the timer expires, the vernier engines light up, then one second later the main retro ignites for its 40 second burn. The main retro gets rid of 2.3km/s of speed, and leaves the spacecraft between 5km and 20km above the surface and travelling downwards at between zero and 230m/s. During this stage, the vernier engines are used to stabilize the spacecraft and compensate for any thrust misalignment in the main retro. The AMR is automatically jettisoned at main retro ignition, as it is no longer needed. In fact, it is stuffed into the nozzle of the main retro, and is blasted out by the retro ignition. | At the altitude mark, the spacecraft is hurtling towards the ground at over 2.5km/s and will impact in less than one minute. For Surveyor 1, the altitude mark was about 110km, and the timer was about 7 seconds. Once the timer expires, the vernier engines light up, then one second later the main retro ignites for its 40 second burn. The main retro gets rid of 2.3km/s of speed, and leaves the spacecraft between 5km and 20km above the surface and travelling downwards at between zero and 230m/s. During this stage, the vernier engines are used to stabilize the spacecraft and compensate for any thrust misalignment in the main retro. The AMR is automatically jettisoned at main retro ignition, as it is no longer needed. In fact, it is stuffed into the nozzle of the main retro, and is blasted out by the retro ignition. | ||
− | The spacecraft then drops the spent retro motor and continues down on the vernier engines. Using a | + | The spacecraft then drops the spent retro motor and continues down on the vernier engines. Using a seperate four-beam Doppler radar and a simple analog computer, it throttles the engines such that it will reach a downward velocity of about 1.3m/s about 4m above the surface. It then cuts its engines and falls the rest of the way. |
− | = Why a custom DLL = | + | === Why a custom DLL === |
Many spacecraft can and in fact have been simulated with Vinka's spacecraft.dll. This method uses a text file to describe a spacecraft, and a general purpose DLL to interpret it. | Many spacecraft can and in fact have been simulated with Vinka's spacecraft.dll. This method uses a text file to describe a spacecraft, and a general purpose DLL to interpret it. | ||
Line 29: | Line 31: | ||
I hope that this article is useful to anyone trying to write a custom vessel DLL. I find it much easier to understand a concept when 1) I need to use it and 2) There is a well-documented example of using the concept. You have to provide the need. I provide the example. | I hope that this article is useful to anyone trying to write a custom vessel DLL. I find it much easier to understand a concept when 1) I need to use it and 2) There is a well-documented example of using the concept. You have to provide the need. I provide the example. | ||
− | = The Rules = | + | === The Rules === |
− | |||
− | |||
− | |||
# Start with something you know works | # Start with something you know works | ||
− | |||
− | |||
− | |||
# Only make one change at a time | # Only make one change at a time | ||
# '''''ALWAYS''''' '''''ALWAYS''''' '''''ALWAYS''''' test each change, no matter how small | # '''''ALWAYS''''' '''''ALWAYS''''' '''''ALWAYS''''' test each change, no matter how small | ||
− | # '''''NEVER''''' '''''NEVER''''' '''''NEVER''''' proceed to the next change if the last one | + | # For historical or real vessels, get good reference material |
− | + | # '''''NEVER''''' '''''NEVER''''' '''''NEVER''''' proceed to the next change if the last one is broken | |
− | + | # There is NO rule 6 | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | = The Starting Point = | + | === The Starting Point === |
So, how do you start with something that works, if you are creating a brand new vessel? Good question. The answer is to look through the samples in Orbiter\OrbiterSDK\Samples for the ship which is closest to the one you have in mind. | So, how do you start with something that works, if you are creating a brand new vessel? Good question. The answer is to look through the samples in Orbiter\OrbiterSDK\Samples for the ship which is closest to the one you have in mind. | ||
Line 57: | Line 46: | ||
Sometimes nothing is particularly close. It was in my case. In this case, just start with the simplest possible ship, the ShuttlePB. Make a copy of this folder, then rename it to your new vessel name. If you are using Visual C++, you probably need to do some weird stuff to rename the project and all the files in it. I use the free compiler, so the project files are no good to me. If you use it also, feel free to just delete all the files except for ShuttlePB.cpp, and rename it to a new name, like Surveyor.cpp. | Sometimes nothing is particularly close. It was in my case. In this case, just start with the simplest possible ship, the ShuttlePB. Make a copy of this folder, then rename it to your new vessel name. If you are using Visual C++, you probably need to do some weird stuff to rename the project and all the files in it. I use the free compiler, so the project files are no good to me. If you use it also, feel free to just delete all the files except for ShuttlePB.cpp, and rename it to a new name, like Surveyor.cpp. | ||
− | + | Now, edit Surveyor.cpp. Do a global search-and-replace to change all references from ShuttlePB to Surveyor. Go ahead and modify the copyright message at the top, to say that this is a work derived from the original ShuttlePB. By the time we are done, almost all the old ShuttlePB code will be replaced. | |
− | + | In keeping with Rule 3, compile this file, copy the resulting DLL to the Orbiter\Modules, and use the following scenario. It should run perfectly, and look and fly exactly like the old ShuttlePB. If at any point in this tutorial, if it doesn't work | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <pre><nowiki> | |
+ | BEGIN_ENVIRONMENT | ||
+ | System Sol | ||
+ | Date MJD 51982.6685767396 | ||
+ | END_ENVIRONMENT | ||
− | + | BEGIN_FOCUS | |
+ | Ship Surveyor1 | ||
+ | END_FOCUS | ||
− | + | BEGIN_CAMERA | |
− | + | TARGET Surveyor1 | |
− | + | FOV 50.00 | |
− | + | END_CAMERA | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | BEGIN_SHIPS | |
+ | Surveyor1:Surveyor | ||
+ | STATUS Landed Moon | ||
+ | BASE Brighton Beach:1 | ||
+ | HEADING 90.00 | ||
+ | PRPLEVEL 0:1.000 | ||
+ | END | ||
+ | END_SHIPS | ||
+ | </nowiki></pre> | ||
− | = Meshes = | + | === Meshes === |
− | Creating meshes is beyond the scope of this document. Since I am in a hurry and not primarily interested in artwork, I borrowed the mesh from | + | Creating meshes is beyond the scope of this document. Since I am in a hurry and not primarily interested in artwork, I borrowed the mesh from Surveyor1-1.zip by Jim Williams, posted on OrbitHangar. |
− | If you are following along, get that add-on, and get the meshes out of it. Rename | + | If you are following along, get that add-on, and get the meshes out of it. Rename Surveyor-deployed.msh as Surveyor-Lander.msh . Copy this mesh to Orbiter\Meshes , and copy all the .dds files to Orbiter\Textures . |
− | Now, to use this mesh. In Surveyor.cpp, look for the method Surveyor::clbkSetClassCaps. Near the end, change the line (Note that it doesn't say ShuttlePB, remember that you searched-and-replaced that out) | + | Now, to use this mesh. In Surveyor.cpp, look for the method Surveyor::clbkSetClassCaps . Near the end, change the line (Note that it doesn't say ShuttlePB, remember that you searched-and-replaced that out.) |
+ | <pre><nowiki> | ||
// visual specs | // visual specs | ||
AddMesh ("Surveyor"); | AddMesh ("Surveyor"); | ||
+ | </nowiki></pre> | ||
to | to | ||
+ | <pre><nowiki> | ||
// visual specs | // visual specs | ||
AddMesh("Surveyor-Lander"); | AddMesh("Surveyor-Lander"); | ||
+ | </nowiki></pre> | ||
− | Compile and test it. Notice that the spacecraft now looks like Surveyor, but it flies like ShuttlePB. Also note that the legs are not on the surface. Go ahead and take Surveyor for a spin, and put it in orbit around the moon. It will be much easier to test the | + | Compile and test it. Notice that the spacecraft now looks like Surveyor, but it flies like ShuttlePB. Also note that the legs are not on the surface. Go ahead and take Surveyor for a spin, and put it in orbit around the moon. It will be much easier to test the verner and RCS engines in space. |
− | |||
− | |||
− | + | === Vehicle coordinate system === | |
− | + | As described in the Orbiter API guide, vessels use a left-handed coordinate system. Even though Surveyor looks nothing like and is flown nothing like an airplane, an airplane is still a useful model to describe the coordinate system. Imagine yourself sitting in the pilot's seat of an airplane. The X axis is the "left-to-right" axis. +X points out to the right wing, and -X points out to the left wing. The Y axis is the "up-to-down" axis. +Y points up, in the direction of the vertical tail fin, and -Y points down in the direction of the wheels. The Z axis is the "front-to-back" axis. +Z points to the nose, and -Z points back to the aft end of the plane. In an conventional airplane, the engines thrust towards +Z. | |
− | = Vernier Engines = | + | === Vernier Engines === |
These will be the "main" engines controllable with the NumPad+ key. Even though these vernier engines are the very reason we are using a custom DLL rather than spacecraft.dll, we are not going to do anything fancy to them yet. | These will be the "main" engines controllable with the NumPad+ key. Even though these vernier engines are the very reason we are using a custom DLL rather than spacecraft.dll, we are not going to do anything fancy to them yet. | ||
Line 130: | Line 111: | ||
Find this code in Surveyor::clbkSetClassCaps . | Find this code in Surveyor::clbkSetClassCaps . | ||
− | + | <pre><nowiki> | |
− | + | void Surveyor::clbkSetClassCaps (FILEHANDLE cfg) | |
− | + | { | |
+ | THRUSTER_HANDLE th_vernier, th_hover, th_rcs[14], th_group[4]; | ||
+ | </nowiki></pre> | ||
Change it to | Change it to | ||
− | + | <pre><nowiki> | |
− | + | void Surveyor::clbkSetClassCaps (FILEHANDLE cfg) | |
− | + | { | |
− | + | THRUSTER_HANDLE th_vernier[3], th_hover, th_rcs[14], th_group[4]; | |
+ | </nowiki></pre> | ||
since there are 3 vernier engines, and we need to keep track of them all. | since there are 3 vernier engines, and we need to keep track of them all. | ||
Line 144: | Line 128: | ||
It is best to put as many vessel parameters into constants as possible, to keep them all in one place and easy to adjust. The parameters for the verniers are Maximum thrust, Isp (Fuel efficiency), and engine location. So, add the following constants near the top of the file. | It is best to put as many vessel parameters into constants as possible, to keep them all in one place and easy to adjust. The parameters for the verniers are Maximum thrust, Isp (Fuel efficiency), and engine location. So, add the following constants near the top of the file. | ||
− | + | <pre><nowiki> | |
− | + | const double VERNIER_PROP_MASS = 70.98; | |
− | + | const double VERNIER_ISP = 3200; | |
− | + | const double VERNIER_THRUST = 463; | |
− | + | const double VERNIER_RAD = 0.86; | |
+ | const double VERNIER_STA = -0.5; | ||
+ | </nowiki></pre> | ||
Find the line in Surveyor::clbkSetClassCaps which reads as follows: | Find the line in Surveyor::clbkSetClassCaps which reads as follows: | ||
− | + | <pre><nowiki> | |
− | + | th_vernier = CreateThruster (_V(0,0,-4.35), _V(0,0,1), PB_MAXMAINTH, ph_vernier, PB_ISP); | |
− | + | CreateThrusterGroup (&th_vernier, 1, THGROUP_MAIN); | |
+ | AddExhaust (th_vernier, 8, 1, _V(0,0.3,-4.35), _V(0,0,-1)); | ||
+ | </nowiki></pre> | ||
Change them to: | Change them to: | ||
− | + | <pre><nowiki> | |
− | + | th_vernier[0] = CreateThruster(_V( 0.0*VERNIER_RAD, 1.0*VERNIER_RAD,VERNIER_STA), _V(0,0,1), VERNIER_THRUST, ph_vernier, VERNIER_ISP); | |
− | + | th_vernier[1] = CreateThruster(_V( sqrt(3.0)/2*VERNIER_RAD,-0.5*VERNIER_RAD,VERNIER_STA), _V(0,0,1), VERNIER_THRUST, ph_vernier, VERNIER_ISP); | |
− | + | th_vernier[2] = CreateThruster(_V(-sqrt(3.0)/2*VERNIER_RAD,-0.5*VERNIER_RAD,VERNIER_STA), _V(0,0,1), VERNIER_THRUST, ph_vernier, VERNIER_ISP); | |
− | + | CreateThrusterGroup(th_vernier, 3, THGROUP_MAIN); | |
− | + | for(int i=0;i<3;i++) { | |
− | + | AddExhaust(th_vernier[i], 1, 0.1); | |
+ | } | ||
+ | </nowiki></pre> | ||
This creates the three vernier engines, arranges them in an equilateral triangle, groups them into the "main" thruster group, and adds an exhaust display. The vernier engine geometry is adjustable with the constants VERNIER_RAD and VERNIER_STA. VERNIER_RAD determines how far away from the Z axis the thrusters are, and VERNIER_STA determines how far in front of the Center of Mass they are. In this case, VERNIER_STA is negative, so the vernier engines are behind the center of mass. | This creates the three vernier engines, arranges them in an equilateral triangle, groups them into the "main" thruster group, and adds an exhaust display. The vernier engine geometry is adjustable with the constants VERNIER_RAD and VERNIER_STA. VERNIER_RAD determines how far away from the Z axis the thrusters are, and VERNIER_STA determines how far in front of the Center of Mass they are. In this case, VERNIER_STA is negative, so the vernier engines are behind the center of mass. | ||
Line 188: | Line 178: | ||
Compile this and test it. Burn the main engines and look outside. You should see three engines pointing backwards (in the direction of the legs) firing. Notice that they are extremely weak and inefficient compared to the old main engine. You probably cannot launch this from the surface anymore, so it's a good thing we put it into lunar orbit first. Don't worry yet that the engines are not aligned with the mesh. | Compile this and test it. Burn the main engines and look outside. You should see three engines pointing backwards (in the direction of the legs) firing. Notice that they are extremely weak and inefficient compared to the old main engine. You probably cannot launch this from the surface anymore, so it's a good thing we put it into lunar orbit first. Don't worry yet that the engines are not aligned with the mesh. | ||
− | = Differential Thrust = | + | === Differential Thrust === |
Now for the moment you've all been waiting for, and the justification for making this a custom DLL. | Now for the moment you've all been waiting for, and the justification for making this a custom DLL. | ||
Line 202: | Line 192: | ||
So, in order to do all of this, we have to reorganize the code quite a bit. We will break the change into three independently-testable parts. First, move the following line from Surveyor::clbkSetClassCaps to the Surveyor class definition: | So, in order to do all of this, we have to reorganize the code quite a bit. We will break the change into three independently-testable parts. First, move the following line from Surveyor::clbkSetClassCaps to the Surveyor class definition: | ||
− | + | <pre><nowiki> | |
+ | THRUSTER_HANDLE th_vernier[3], th_hover, th_rcs[14], th_group[4]; | ||
+ | </nowiki></pre> | ||
This changes these variables from local variables to class fields, so they can be accessed by other class member functions. Compile and test this. It should still work identically. | This changes these variables from local variables to class fields, so they can be accessed by other class member functions. Compile and test this. It should still work identically. | ||
Line 210: | Line 202: | ||
Add the following line to the class definition: | Add the following line to the class definition: | ||
− | + | <pre><nowiki> | |
+ | void clbkPreStep(double SimT, double SimDT, double MJD); | ||
+ | </nowiki></pre> | ||
Add the following code somewhere near the bottom to give this method a stub. | Add the following code somewhere near the bottom to give this method a stub. | ||
− | + | <pre><nowiki> | |
− | + | void Surveyor::clbkPreStep(double SimT, double SimDT, double MJD) { | |
+ | } | ||
+ | </nowiki></pre> | ||
Now compile and test this. It still won't do anything different, but it is significantly different under the hood now. clbkPreStep is being called for each frame of the simulation. | Now compile and test this. It still won't do anything different, but it is significantly different under the hood now. clbkPreStep is being called for each frame of the simulation. | ||
Line 221: | Line 217: | ||
Now the actual differential thrust code. Add this to Surveyor::clbkPreStep : | Now the actual differential thrust code. Add this to Surveyor::clbkPreStep : | ||
− | + | <pre><nowiki> | |
− | + | double P,Y,R; | |
− | + | P=GetThrusterGroupLevel(THGROUP_ATT_PITCHUP)-GetThrusterGroupLevel(THGROUP_ATT_PITCHDOWN); | |
− | + | Y=GetThrusterGroupLevel(THGROUP_ATT_YAWRIGHT)-GetThrusterGroupLevel(THGROUP_ATT_YAWLEFT); | |
− | + | R=GetThrusterGroupLevel(THGROUP_ATT_BANKRIGHT)-GetThrusterGroupLevel(THGROUP_ATT_BANKLEFT); | |
+ | sprintf(oapiDebugString(),"Pitch %f Yaw %f Roll %f",P,Y,R); | ||
− | + | SetThrusterDir(th_vernier[0],_V(0.087*R,0,1)); | |
− | + | SetThrusterDir(th_vernier[1],_V(0,0,1.0+0.05*(P-Y))); | |
− | + | SetThrusterDir(th_vernier[2],_V(0,0,1.0+0.05*(P+Y))); | |
+ | </nowiki></pre> | ||
Here we are using the GetThrusterGroupLevel functions to get the level setting of each of the six RCS groups. The subtraction makes us treat a turn one way as just the negative of turning the other way. | Here we are using the GetThrusterGroupLevel functions to get the level setting of each of the six RCS groups. The subtraction makes us treat a turn one way as just the negative of turning the other way. | ||
Line 244: | Line 242: | ||
Continue to [[Vessel Tutorial 2]] | Continue to [[Vessel Tutorial 2]] | ||
− | |||
− | |||
− | |||
− |